use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCancelRequestFailsResponseHandler.
/**
* Verifies that the client cancels and fails a request for which no response
* has been received after a certain amount of time. The request is then
* failed with a {@link ServerErrorException}.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testCancelRequestFailsResponseHandler(final TestContext ctx) {
// GIVEN a request-response client which times out requests after 200 ms
client.setRequestTimeout(200);
// WHEN no response is received for a request sent to the peer
doAnswer(invocation -> {
// do not wait 200ms before running the timeout task but instead
// run it immediately
Handler<Long> task = invocation.getArgument(1);
task.handle(1L);
return null;
}).when(vertx).setTimer(anyLong(), any(Handler.class));
final Async requestFailure = ctx.async();
client.createAndSendRequest("request", null, (JsonObject) null, ctx.asyncAssertFailure(t -> {
ctx.assertTrue(ServerErrorException.class.isInstance(t));
requestFailure.complete();
}));
// THEN the request handler is failed
requestFailure.await();
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestAddsResponseToCache.
/**
* Verifies that the adapter puts the response from the service to the cache
* using the default cache timeout if the response does not contain a
* <em>no-cache</em> cache directive.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testCreateAndSendRequestAddsResponseToCache(final TestContext ctx) {
// GIVEN an adapter with an empty cache
client.setResponseCache(cache);
// WHEN sending a request
client.createAndSendRequest("get", (JsonObject) null, ctx.asyncAssertSuccess(result -> {
// THEN the response has been put to the cache
verify(cache).put(eq("cacheKey"), any(SimpleRequestResponseResult.class), eq(Duration.ofSeconds(RequestResponseClientConfigProperties.DEFAULT_RESPONSE_CACHE_TIMEOUT)));
}), "cacheKey");
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
verify(sender).send(messageCaptor.capture(), any(Handler.class));
final Message response = ProtonHelper.message("result");
MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
response.setCorrelationId(messageCaptor.getValue().getMessageId());
final ProtonDelivery delivery = mock(ProtonDelivery.class);
client.handleResponse(delivery, response);
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractRequestResponseClientTest method testHandleResponseInvokesHandlerForMatchingCorrelationId.
/**
* Verifies that the client passes a response message to the handler registered for the request that
* the response correlates with.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testHandleResponseInvokesHandlerForMatchingCorrelationId(final TestContext ctx) {
// GIVEN a request message that has been sent to a peer
final Async responseReceived = ctx.async();
client.createAndSendRequest("request", null, (JsonObject) null, ctx.asyncAssertSuccess(s -> {
ctx.assertEquals(200, s.getStatus());
ctx.assertEquals("payload", s.getPayload());
responseReceived.complete();
}));
// WHEN a response is received for the request
final Message response = ProtonHelper.message("payload");
response.setCorrelationId(MESSAGE_ID);
MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, 200);
final ProtonDelivery delivery = mock(ProtonDelivery.class);
client.handleResponse(delivery, response);
// THEN the response is passed to the handler registered with the request
responseReceived.await();
verify(vertx, never()).setTimer(anyLong(), any(Handler.class));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestAddsResponseToCacheWithMaxAge.
/**
* Verifies that the adapter puts the response from the service to the cache
* using the max age indicated by a response's <em>max-age</em> cache directive.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testCreateAndSendRequestAddsResponseToCacheWithMaxAge(final TestContext ctx) {
// GIVEN an adapter with an empty cache
client.setResponseCache(cache);
// WHEN sending a request
client.createAndSendRequest("get", (JsonObject) null, ctx.asyncAssertSuccess(result -> {
// THEN the response has been put to the cache
verify(cache).put(eq("cacheKey"), any(SimpleRequestResponseResult.class), eq(Duration.ofSeconds(35)));
}), "cacheKey");
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
verify(sender).send(messageCaptor.capture(), any(Handler.class));
final Message response = ProtonHelper.message("result");
MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(35));
response.setCorrelationId(messageCaptor.getValue().getMessageId());
final ProtonDelivery delivery = mock(ProtonDelivery.class);
client.handleResponse(delivery, response);
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestDoesNotAddResponseToCache.
/**
* Verifies that the adapter does not put the response from the service to the cache
* if the response contains a <em>no-cache</em> cache directive.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testCreateAndSendRequestDoesNotAddResponseToCache(final TestContext ctx) {
// GIVEN an adapter with an empty cache
client.setResponseCache(cache);
// WHEN sending a request
client.createAndSendRequest("get", (JsonObject) null, ctx.asyncAssertSuccess(result -> {
// THEN the response is not put to the cache
verify(cache, never()).put(eq("cacheKey"), any(SimpleRequestResponseResult.class), any(Duration.class));
}), "cacheKey");
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
verify(sender).send(messageCaptor.capture(), any(Handler.class));
final Message response = ProtonHelper.message("result");
MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
MessageHelper.addCacheDirective(response, CacheDirective.noCacheDirective());
response.setCorrelationId(messageCaptor.getValue().getMessageId());
final ProtonDelivery delivery = mock(ProtonDelivery.class);
client.handleResponse(delivery, response);
}
Aggregations