use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class HonoClientImplTest method testGetOrCreateSenderFailsOnConnectionFailure.
/**
* Verifies that a request to create a message sender is failed immediately when the
* underlying connection to the server fails.
*
* @param ctx The Vertx test context.
*/
@Test
public void testGetOrCreateSenderFailsOnConnectionFailure(final TestContext ctx) {
// GIVEN a client that 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();
final Async disconnected = ctx.async();
final Async supplierInvocation = ctx.async();
client.getOrCreateSender("telemetry/tenant", () -> {
ctx.assertFalse(disconnected.isCompleted());
supplierInvocation.complete();
return Future.future();
}).setHandler(ctx.asyncAssertFailure(cause -> {
disconnected.complete();
}));
// WHEN the underlying connection fails
supplierInvocation.await();
connectionFactory.getDisconnectHandler().handle(con);
// THEN all creation requests are failed
disconnected.await();
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class HonoClientImplTest method testCreateConsumerFailsOnConnectionFailure.
/**
* Verifies that a request to create a consumer is failed immediately when the
* underlying connection to the server fails.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateConsumerFailsOnConnectionFailure(final TestContext ctx) {
// GIVEN a connected 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();
final Async creationFailure = ctx.async();
final Async supplierInvocation = ctx.async();
client.createConsumer("tenant", () -> {
supplierInvocation.complete();
return Future.future();
}).setHandler(ctx.asyncAssertFailure(cause -> {
creationFailure.complete();
}));
// wait until the consumer supplier has been invoked
// so that we can be sure that the disconnect handler for
// for the creation request has been registered
supplierInvocation.await();
// WHEN the underlying connection fails
connectionFactory.getDisconnectHandler().handle(con);
// THEN all creation requests are failed
creationFailure.await();
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class HonoClientImplTest method testDownstreamDisconnectTriggersReconnect.
/**
* Verifies that the client tries to re-establish a lost connection to a server.
*
* @param ctx The vert.x test context.
*/
@Test
public void testDownstreamDisconnectTriggersReconnect(final TestContext ctx) {
// GIVEN an client that is connected to a peer to which the
// connection can be established on the third attempt only
connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con, 1, 2);
final ProtonClientOptions options = new ProtonClientOptions().setReconnectInterval(50).setReconnectAttempts(3);
client = new HonoClientImpl(vertx, connectionFactory, props);
client.connect(options).setHandler(ctx.asyncAssertSuccess());
assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
connectionFactory.setExpectedFailingConnectionAttempts(2);
connectionFactory.setExpectedSucceedingConnectionAttempts(1);
// WHEN the downstream connection fails
connectionFactory.getDisconnectHandler().handle(con);
// THEN the adapter reconnects to the downstream container
assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
connectionFactory.setExpectedFailingConnectionAttempts(2);
connectionFactory.setExpectedSucceedingConnectionAttempts(1);
// and when the downstream connection fails again
connectionFactory.getDisconnectHandler().handle(con);
// THEN the adapter reconnects to the downstream container again
assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class HonoClientImplTest method testOnRemoteCloseTriggersReconnection.
/**
* Verifies that the client tries to re-connect to a server instance if the
* connection is closed by the peer.
*
* @param ctx The test context.
*/
@Test
public void testOnRemoteCloseTriggersReconnection(final TestContext ctx) {
// GIVEN a client that is connected to a server
final Async connected = ctx.async();
final Async disconnectHandlerInvocation = ctx.async();
client.connect(new ProtonClientOptions().setReconnectAttempts(1), failedCon -> disconnectHandlerInvocation.complete()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
connected.await();
// WHEN the peer closes the connection
connectionFactory.getCloseHandler().handle(Future.failedFuture("shutting down for maintenance"));
// THEN the client invokes the disconnect handler provided in the original connect method call
disconnectHandlerInvocation.await();
// and the original connection has been closed locally
verify(con).close();
verify(con).disconnectHandler(null);
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class HonoClientImplTest method testDownstreamDisconnectClearsSenderCreationLocks.
/**
* Verifies that all sender creation locks are cleared when the connection to the server fails.
*
* @param ctx The Vertx test context.
*/
@Test
public void testDownstreamDisconnectClearsSenderCreationLocks(final TestContext ctx) {
// expect the connection factory to be invoked twice
// first on initial connection
// second on reconnect
connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con);
// GIVEN a client trying to create a sender to a peer
final ProtonClientOptions options = new ProtonClientOptions().setReconnectInterval(50).setReconnectAttempts(3);
client = new HonoClientImpl(vertx, connectionFactory, props);
client.connect(options).setHandler(ctx.asyncAssertSuccess());
assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
connectionFactory.setExpectedSucceedingConnectionAttempts(1);
// WHEN the downstream connection fails just when the client wants to open a sender link
final Async senderCreationFailure = ctx.async();
client.getOrCreateSender("telemetry/tenant", () -> {
connectionFactory.getDisconnectHandler().handle(con);
return Future.future();
}).setHandler(ctx.asyncAssertFailure(cause -> senderCreationFailure.complete()));
// THEN the sender creation fails,
senderCreationFailure.await();
// the connection is re-established
assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
// and the next attempt to create a sender succeeds
client.getOrCreateSender("telemetry/tenant", () -> Future.succeededFuture(mock(MessageSender.class))).setHandler(ctx.asyncAssertSuccess());
}
Aggregations