jeudi 22 février 2018

Mockserver: callback once request is received

I'm using Mockserver to test an HTTP service that produces side-effects asynchronously. Let's say for example, I'm testing ServiceA that exposes an HTTP API method at /api/status/{id}. Separately from returning the status result to the caller of the API, ServiceA kicks off an async analysis that calls ServiceB if some conditions are met. Let's say this takes from a few milliseconds to a few seconds for this async process to complete.

Currently, my mockserver code looks something like this:

Long waitTimeoutSeconds = 5L;
Long start = System.currentTimeMillis();
HttpRequest[] asyncRequestsRetrieved = null;

while (start + (waitTimeoutSeconds * 1000L) < System.currentTimeMillis()) {
    asyncRequestsRetrieved = mockServer.retrieveRecordedRequests(HttpRequest.request().withPath("/service/b/path"));
    if (asyncRequestsRetrieved != null && asyncRequestsRetrieved.size > 0) {
        break;
    }
    Thread.sleep(50L);
}

assertNotNull(asyncRequestsRetrieved);
assertTrue(asyncRequestsRetrieved!!.size > 0);

Does Mockserver expose a pattern to take an action (say, notify a listener) when a request is received at a given endpoint, or is polling my only option?

Aucun commentaire:

Enregistrer un commentaire