use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class HonoClientImplTest method testGetOrCreateTelemetrySenderFailsIfInvokedConcurrently.
/**
* Verifies that a concurrent request to create a sender fails the given future for tracking the attempt.
*
* @param ctx The helper to use for running async tests.
*/
@Test
public void testGetOrCreateTelemetrySenderFailsIfInvokedConcurrently(final TestContext ctx) {
// GIVEN a client that already tries to create a telemetry sender for "tenant"
final Async connected = ctx.async();
client.connect(new ProtonClientOptions()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
connected.await();
client.getOrCreateSender("telemetry/tenant", () -> Future.future());
// WHEN an additional, concurrent attempt is made to create a telemetry sender for "tenant"
client.getOrCreateSender("telemetry/tenant", () -> {
ctx.fail("should not create concurrent client");
return Future.succeededFuture(mock(MessageSender.class));
}).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the concurrent attempt fails without any attempt being made to create another sender
ctx.assertTrue(ServerErrorException.class.isInstance(t));
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class RegistrationClientImplTest method testGetRegistrationInfoReturnsValueFromCache.
/**
* Verifies that registration information is taken from cache.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetRegistrationInfoReturnsValueFromCache(final TestContext ctx) {
// GIVEN an adapter with a cache containing a registration assertion
// response for "device"
client.setResponseCache(cache);
final JsonObject registrationAssertion = newRegistrationAssertionResult();
final RegistrationResult regResult = RegistrationResult.from(HttpURLConnection.HTTP_OK, registrationAssertion);
when(cache.get(eq(TriTuple.of("assert", "device", "gateway")))).thenReturn(regResult);
// WHEN getting registration information
client.assertRegistration("device", "gateway").setHandler(ctx.asyncAssertSuccess(result -> {
// THEN the registration information is read from the cache
ctx.assertEquals(registrationAssertion, result);
verify(sender, never()).send(any(Message.class));
}));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class RegistrationClientImplTest method testAssertRegistrationAddsResponseToCacheOnCacheMiss.
/**
* Verifies that on a cache miss the adapter retrieves registration information
* from the Device Registration service and puts it to the cache.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testAssertRegistrationAddsResponseToCacheOnCacheMiss(final TestContext ctx) {
// GIVEN an adapter with an empty cache
client.setResponseCache(cache);
// WHEN getting registration information
final Async assertion = ctx.async();
client.assertRegistration("device").setHandler(ctx.asyncAssertSuccess(result -> assertion.complete()));
final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
verify(sender).send(messageCaptor.capture(), any(Handler.class));
final JsonObject registrationAssertion = newRegistrationAssertionResult();
final Message response = ProtonHelper.message(registrationAssertion.encode());
MessageHelper.addProperty(response, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
MessageHelper.addCacheDirective(response, CacheDirective.maxAgeDirective(60));
response.setCorrelationId(messageCaptor.getValue().getMessageId());
final ProtonDelivery delivery = mock(ProtonDelivery.class);
client.handleResponse(delivery, response);
// THEN the registration information has been added to the cache
verify(cache).put(eq(TriTuple.of("assert", "device", null)), any(RegistrationResult.class), any(Duration.class));
}
use of io.vertx.ext.unit.TestContext in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectInvokesHandlerOnfailureToConnect.
/**
* Verifies that the given result handler is invoked if a connection attempt fails.
*
* @param ctx The vert.x test context.
*/
@Test
public void testConnectInvokesHandlerOnfailureToConnect(final TestContext ctx) {
// GIVEN a factory configured to connect to a non-existing server
ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
// WHEN trying to connect to the server
final Async handlerInvocation = ctx.async();
ProtonClientOptions options = new ProtonClientOptions().setConnectTimeout(100);
factory.connect(options, null, null, ctx.asyncAssertFailure(t -> {
handlerInvocation.complete();
}));
// THEN the connection attempt fails and the given handler is invoked
handlerInvocation.await(2000);
}
use of io.vertx.ext.unit.TestContext in project vertx-tcp-eventbus-bridge by vert-x3.
the class TcpEventBusBridgeTest method testSendPing.
@Test
public void testSendPing(TestContext context) {
NetClient client = vertx.createNetClient();
final Async async = context.async();
eventHandler = event -> {
if (event.type() == BridgeEventType.SOCKET_PING) {
async.complete();
}
};
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
socket.handler(buff -> {
});
FrameHelper.sendFrame("register", "echo", null, socket);
FrameHelper.sendFrame("ping", socket);
}));
}
Aggregations