use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsIfReceiverIsNotOpen.
/**
* Verifies that a response handler is immediately failed with a
* {@link ServerErrorException} when the receiver link is not open (yet).
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateAndSendRequestFailsIfReceiverIsNotOpen(final TestContext ctx) {
// GIVEN a client whose sender and receiver are not open
when(receiver.isOpen()).thenReturn(Boolean.FALSE);
// WHEN sending a request
Async requestFailure = ctx.async();
client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
ctx.assertTrue(ServerErrorException.class.isInstance(t));
requestFailure.complete();
}));
// THEN the request fails immediately
requestFailure.await();
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class AbstractRequestResponseClientTest method testCreateAndSendRequestFailsIfSenderIsNotOpen.
/**
* Verifies that a response handler is immediately failed with a
* {@link ServerErrorException} when the sender link is not open (yet).
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateAndSendRequestFailsIfSenderIsNotOpen(final TestContext ctx) {
// GIVEN a client whose sender and receiver are not open
when(sender.isOpen()).thenReturn(Boolean.FALSE);
// WHEN sending a request
Async requestFailure = ctx.async();
client.createAndSendRequest("get", null, ctx.asyncAssertFailure(t -> {
ctx.assertTrue(ServerErrorException.class.isInstance(t));
requestFailure.complete();
}));
// THEN the request fails immediately
requestFailure.await();
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class EventConsumerImplTest method testCreateRegistersBiConsumerAsMessageHandler.
/**
* Verifies that the message delivery for a received event is forwarded to the
* registered event consumer.
*
* @param ctx The test context.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testCreateRegistersBiConsumerAsMessageHandler(final TestContext ctx) {
// GIVEN an event consumer that releases all messages
final Async consumerCreation = ctx.async();
final BiConsumer<ProtonDelivery, Message> eventConsumer = (delivery, message) -> {
ProtonHelper.released(delivery, true);
};
final RecordImpl attachments = new RecordImpl();
final Source source = mock(Source.class);
when(source.toString()).thenReturn("event/tenant");
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.getSource()).thenReturn(source);
when(receiver.attachments()).thenReturn(attachments);
when(receiver.getRemoteQoS()).thenReturn(ProtonQoS.AT_LEAST_ONCE);
when(receiver.open()).then(answer -> {
consumerCreation.complete();
return receiver;
});
final ProtonConnection con = mock(ProtonConnection.class);
when(con.createReceiver(anyString())).thenReturn(receiver);
when(receiver.openHandler(any(Handler.class))).thenAnswer(invocation -> {
final Handler handler = invocation.getArgument(0);
handler.handle(Future.succeededFuture(receiver));
return receiver;
});
final ArgumentCaptor<ProtonMessageHandler> messageHandler = ArgumentCaptor.forClass(ProtonMessageHandler.class);
EventConsumerImpl.create(vertx.getOrCreateContext(), new ClientConfigProperties(), con, "tenant", eventConsumer, open -> {
}, remoteDetach -> {
});
consumerCreation.await();
verify(receiver).handler(messageHandler.capture());
// WHEN an event is received
final ProtonDelivery delivery = mock(ProtonDelivery.class);
final Message msg = mock(Message.class);
messageHandler.getValue().handle(delivery, msg);
// THEN the message is released and settled
verify(delivery).disposition(any(Released.class), eq(Boolean.TRUE));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class HonoClientImplTest method testClientDoesNotTriggerReconnectionAfterShutdown.
/**
* Verifies that the client does not try to re-connect to a server instance if the client was shutdown.
*
* @param ctx The test context.
*/
@Test
public void testClientDoesNotTriggerReconnectionAfterShutdown(final TestContext ctx) {
// GIVEN a client that tries to connect to a server but does not succeed
final Async connectionHandlerInvocation = ctx.async();
connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con, 0, Integer.MAX_VALUE);
client = new HonoClientImpl(vertx, connectionFactory, props);
client.connect(new ProtonClientOptions().setReconnectAttempts(1)).setHandler(ctx.asyncAssertFailure(cause -> connectionHandlerInvocation.complete()));
// WHEN client gets shutdown
client.shutdown();
// THEN reconnect gets stopped, i.e. connection fails
connectionHandlerInvocation.await();
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class HonoClientImplTest method testGetOrCreateRequestResponseClientFailsIfInvokedConcurrently.
/**
* Verifies that a concurrent request to create a request-response client fails the given
* future for tracking the attempt.
*
* @param ctx The helper to use for running async tests.
*/
@Test
public void testGetOrCreateRequestResponseClientFailsIfInvokedConcurrently(final TestContext ctx) {
// GIVEN a client that already tries to create a registration client for "tenant"
final Async connected = ctx.async();
client.connect(new ProtonClientOptions()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
connected.await();
client.getOrCreateRequestResponseClient("registration/tenant", () -> Future.future(), result -> {
});
// WHEN an additional, concurrent attempt is made to create a client for "tenant"
client.getOrCreateRequestResponseClient("registration/tenant", () -> {
ctx.fail("should not create concurrent client");
return Future.succeededFuture(mock(RegistrationClient.class));
}, ctx.<RequestResponseClient>asyncAssertFailure(t -> {
// THEN the concurrent attempt fails without any attempt being made to create another client
ctx.assertTrue(ServerErrorException.class.isInstance(t));
}));
}
Aggregations