Search in sources :

Example 16 with HonoConnection

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();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) SaslSystemException(io.vertx.proton.sasl.SaslSystemException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Mockito.doAnswer(org.mockito.Mockito.doAnswer) DisconnectListener(org.eclipse.hono.client.DisconnectListener) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Predicate(java.util.function.Predicate) ProtonQoS(io.vertx.proton.ProtonQoS) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) AdditionalAnswers(org.mockito.AdditionalAnswers) Test(org.junit.jupiter.api.Test) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) ProtonSender(io.vertx.proton.ProtonSender) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Target(org.apache.qpid.proton.amqp.messaging.Target) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) CompositeFuture(io.vertx.core.CompositeFuture) ProtonSession(io.vertx.proton.ProtonSession) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Symbol(org.apache.qpid.proton.amqp.Symbol) BiConsumer(java.util.function.BiConsumer) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) AsyncResult(io.vertx.core.AsyncResult) HonoConnection(org.eclipse.hono.client.HonoConnection) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Source(org.apache.qpid.proton.amqp.transport.Source) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerErrorException(org.eclipse.hono.client.ServerErrorException) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.jupiter.api.Test)

Example 17 with HonoConnection

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();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) SaslSystemException(io.vertx.proton.sasl.SaslSystemException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Mockito.doAnswer(org.mockito.Mockito.doAnswer) DisconnectListener(org.eclipse.hono.client.DisconnectListener) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Predicate(java.util.function.Predicate) ProtonQoS(io.vertx.proton.ProtonQoS) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) AdditionalAnswers(org.mockito.AdditionalAnswers) Test(org.junit.jupiter.api.Test) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) ProtonSender(io.vertx.proton.ProtonSender) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Target(org.apache.qpid.proton.amqp.messaging.Target) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) CompositeFuture(io.vertx.core.CompositeFuture) ProtonSession(io.vertx.proton.ProtonSession) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Symbol(org.apache.qpid.proton.amqp.Symbol) BiConsumer(java.util.function.BiConsumer) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) AsyncResult(io.vertx.core.AsyncResult) HonoConnection(org.eclipse.hono.client.HonoConnection) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Source(org.apache.qpid.proton.amqp.transport.Source) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HonoConnection(org.eclipse.hono.client.HonoConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Test(org.junit.jupiter.api.Test)

Example 18 with HonoConnection

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)));
    }
}
Also used : Context(io.vertx.core.Context) HttpURLConnection(java.net.HttpURLConnection) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Context(io.vertx.core.Context) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) AsyncResult(io.vertx.core.AsyncResult) HonoConnection(org.eclipse.hono.client.HonoConnection) Handler(io.vertx.core.Handler) Collections(java.util.Collections)

Example 19 with HonoConnection

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);
        }
    });
}
Also used : HonoConnection(org.eclipse.hono.client.HonoConnection)

Aggregations

HonoConnection (org.eclipse.hono.client.HonoConnection)19 Vertx (io.vertx.core.Vertx)14 ProtonReceiver (io.vertx.proton.ProtonReceiver)13 Context (io.vertx.core.Context)12 Future (io.vertx.core.Future)12 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)12 ProtonQoS (io.vertx.proton.ProtonQoS)12 BeforeEach (org.junit.jupiter.api.BeforeEach)12 AsyncResult (io.vertx.core.AsyncResult)11 Handler (io.vertx.core.Handler)11 Promise (io.vertx.core.Promise)11 ProtonSender (io.vertx.proton.ProtonSender)11 HttpURLConnection (java.net.HttpURLConnection)11 AtomicReference (java.util.concurrent.atomic.AtomicReference)11 ServerErrorException (org.eclipse.hono.client.ServerErrorException)11 ClientConfigProperties (org.eclipse.hono.config.ClientConfigProperties)11 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)10 ProtonConnection (io.vertx.proton.ProtonConnection)10 ProtonSession (io.vertx.proton.ProtonSession)10 SaslSystemException (io.vertx.proton.sasl.SaslSystemException)10