use of org.eclipse.hono.client.HonoConnection in project hono by eclipse.
the class HonoConnectionImplTest 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 VertxTestContext ctx) {
// GIVEN a client that tries to connect to a server but does not succeed
final AtomicInteger connectAttempts = new AtomicInteger(0);
final ConnectionFactory factory = mock(ConnectionFactory.class);
when(factory.getHost()).thenReturn("server");
when(factory.getPort()).thenReturn(5672);
doAnswer(invocation -> {
final Handler<AsyncResult<ProtonConnection>> resultHandler = invocation.getArgument(6);
if (connectAttempts.incrementAndGet() == 3) {
// WHEN client gets shutdown
honoConnection.shutdown();
}
resultHandler.handle(Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE)));
return null;
}).when(factory).connect(any(), any(), any(), anyString(), VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler(), VertxMockSupport.anyHandler());
honoConnection = new HonoConnectionImpl(vertx, factory, props);
honoConnection.connect().onComplete(ctx.failing(cause -> {
// THEN three attempts have been made to connect
ctx.verify(() -> assertThat(connectAttempts.get()).isEqualTo(3));
ctx.completeNow();
}));
}
use of org.eclipse.hono.client.HonoConnection in project hono by eclipse.
the class HonoConnectionImplTest 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 VertxTestContext ctx) {
// GIVEN a client that is connected to a server
final Promise<HonoConnection> connected = Promise.promise();
@SuppressWarnings("unchecked") final DisconnectListener<HonoConnection> disconnectListener = mock(DisconnectListener.class);
honoConnection.addDisconnectListener(disconnectListener);
final AtomicInteger reconnectListenerInvocations = new AtomicInteger();
honoConnection.addReconnectListener(con -> reconnectListenerInvocations.incrementAndGet());
honoConnection.connect(new ProtonClientOptions()).onComplete(connected);
connectionFactory.setExpectedSucceedingConnectionAttempts(1);
connected.future().onComplete(ctx.succeeding(c -> {
// WHEN the peer closes the connection
connectionFactory.getCloseHandler().handle(Future.failedFuture("shutting down for maintenance"));
ctx.verify(() -> {
// THEN the client invokes the registered disconnect handler
verify(disconnectListener).onDisconnect(honoConnection);
// and the original connection has been closed locally
verify(con).close();
verify(con).disconnectHandler(null);
// and the connection is re-established
assertThat(connectionFactory.await()).isTrue();
assertThat(reconnectListenerInvocations.get()).isEqualTo(1);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.client.HonoConnection in project hono by eclipse.
the class DeferredConnectionCheckHandler method setConnectionAttemptFinished.
/**
* Marks an ongoing connection attempt as finished, providing the connection result.
* <p>
* This causes any accumulated connection checks to be completed.
*
* @param connectionResult The result of the connection attempt.
*/
public void setConnectionAttemptFinished(final AsyncResult<HonoConnection> connectionResult) {
final List<ExpiringConnectionCheckPromise> promises = connectionCheckPromises.getAndSet(null);
if (promises != null && !promises.isEmpty()) {
LOG.trace("completing {} accumulated connection checks", promises.size());
final Context ctx = vertx.getOrCreateContext();
promises.forEach(promise -> ctx.runOnContext(v -> promise.tryCompleteAndCancelTimer(connectionResult)));
}
}
use of org.eclipse.hono.client.HonoConnection in project hono by eclipse.
the class AmqpExampleDevice method connect.
private void connect() {
final HonoConnection connection = HonoConnection.newConnection(VERTX, config);
connection.connect().onComplete(ar -> {
if (ar.succeeded()) {
startDevice(ar.result());
} else {
System.out.println("Connection failed: " + ar.cause());
System.exit(1);
}
});
}
Aggregations