Search in sources :

Example 1 with ConnectTimeoutException

use of org.eclipse.hono.connection.ConnectTimeoutException in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectIgnoresSuccessfulOpenAfterTimeout.

/**
 * Verifies that a connection attempt is failed if there is a timeout opening the connection
 * and verifies that a subsequently received 'open' frame is ignored.
 *
 * @param ctx The vert.x test context.
 */
@Test
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
public void testConnectIgnoresSuccessfulOpenAfterTimeout(final VertxTestContext ctx) {
    final long connectTimeout = 200L;
    // GIVEN a factory configured to connect to a server with a mocked ProtonClient that won't actually try to connect
    props.setConnectTimeout((int) connectTimeout);
    final AtomicReference<Handler<Long>> timeoutHandlerRef = new AtomicReference<>();
    when(vertx.setTimer(eq(connectTimeout), VertxMockSupport.anyHandler())).thenAnswer(invocation -> {
        timeoutHandlerRef.set(invocation.getArgument(1));
        return 1L;
    });
    final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
    final ProtonClient protonClientMock = mock(ProtonClient.class);
    final ProtonConnection protonConnectionMock = mock(ProtonConnection.class, Mockito.RETURNS_SELF);
    doAnswer(invocation -> {
        final Handler<AsyncResult<ProtonConnection>> resultHandler = invocation.getArgument(5);
        resultHandler.handle(Future.succeededFuture(protonConnectionMock));
        return null;
    }).when(protonClientMock).connect(any(ProtonClientOptions.class), any(), anyInt(), any(), any(), VertxMockSupport.anyHandler());
    factory.setProtonClient(protonClientMock);
    // WHEN trying to connect to the server
    factory.connect(null, null, null, ctx.failing(t -> {
        // THEN the connection attempt fails with a TimeoutException and the given handler is invoked
        ctx.verify(() -> assertTrue(t instanceof ConnectTimeoutException));
        ctx.completeNow();
    }));
    final ArgumentCaptor<Handler<AsyncResult<ProtonConnection>>> openHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
    verify(protonConnectionMock).openHandler(openHandlerCaptor.capture());
    // trigger timeout
    timeoutHandlerRef.get().handle(1L);
    // call openHandler - that will be too late for the connect invocation to succeed
    openHandlerCaptor.getValue().handle(Future.succeededFuture(protonConnectionMock));
    // and the connection will be disconnected
    verify(protonConnectionMock).disconnect();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ProtonConnection(io.vertx.proton.ProtonConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) Timeout(io.vertx.junit5.Timeout) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) AsyncResult(io.vertx.core.AsyncResult) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 2 with ConnectTimeoutException

use of org.eclipse.hono.connection.ConnectTimeoutException in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectIgnoresFailedOpenAfterTimeout.

/**
 * Verifies that a connection attempt is failed if there is a timeout opening the connection
 * and verifies that a subsequently triggered failed open handler is ignored.
 *
 * @param ctx The vert.x test context.
 */
@Test
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
public void testConnectIgnoresFailedOpenAfterTimeout(final VertxTestContext ctx) {
    final long connectTimeout = 200L;
    // GIVEN a factory configured to connect to a server with a mocked ProtonClient that won't actually try to connect
    props.setConnectTimeout((int) connectTimeout);
    final AtomicReference<Handler<Long>> timeoutHandlerRef = new AtomicReference<>();
    when(vertx.setTimer(eq(connectTimeout), VertxMockSupport.anyHandler())).thenAnswer(invocation -> {
        timeoutHandlerRef.set(invocation.getArgument(1));
        return 1L;
    });
    final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
    final ProtonClient protonClientMock = mock(ProtonClient.class);
    final ProtonConnection protonConnectionMock = mock(ProtonConnection.class, Mockito.RETURNS_SELF);
    doAnswer(invocation -> {
        final Handler<AsyncResult<ProtonConnection>> resultHandler = invocation.getArgument(5);
        resultHandler.handle(Future.succeededFuture(protonConnectionMock));
        return null;
    }).when(protonClientMock).connect(any(ProtonClientOptions.class), any(), anyInt(), any(), any(), VertxMockSupport.anyHandler());
    factory.setProtonClient(protonClientMock);
    // WHEN trying to connect to the server
    factory.connect(null, null, null, ctx.failing(t -> {
        // THEN the connection attempt fails with a TimeoutException and the given handler is invoked
        ctx.verify(() -> assertTrue(t instanceof ConnectTimeoutException));
        ctx.completeNow();
    }));
    final ArgumentCaptor<Handler<AsyncResult<ProtonConnection>>> openHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
    verify(protonConnectionMock).openHandler(openHandlerCaptor.capture());
    // trigger timeout
    timeoutHandlerRef.get().handle(1L);
    // call openHandler - that will be too late for the connect invocation to succeed
    openHandlerCaptor.getValue().handle(Future.failedFuture("amqp:resource-limit-exceeded -connection disallowed by local policy"));
    // and the connection will be disconnected
    verify(protonConnectionMock).disconnect();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ProtonConnection(io.vertx.proton.ProtonConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) Timeout(io.vertx.junit5.Timeout) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) AsyncResult(io.vertx.core.AsyncResult) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 3 with ConnectTimeoutException

use of org.eclipse.hono.connection.ConnectTimeoutException in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectInvokesHandlerOnConnectTimeout.

/**
 * Verifies that the given result handler is invoked if a connection attempt times out.
 *
 * @param ctx The vert.x test context.
 */
@Test
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
public void testConnectInvokesHandlerOnConnectTimeout(final VertxTestContext ctx) {
    final long connectTimeout = 200L;
    // GIVEN a factory configured to connect to a server with a mocked ProtonClient that won't actually try to connect
    props.setConnectTimeout((int) connectTimeout);
    final AtomicReference<Handler<Long>> timeoutHandlerRef = new AtomicReference<>();
    when(vertx.setTimer(eq(connectTimeout), VertxMockSupport.anyHandler())).thenAnswer(invocation -> {
        timeoutHandlerRef.set(invocation.getArgument(1));
        return 1L;
    });
    final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
    final ProtonClient protonClientMock = mock(ProtonClient.class);
    factory.setProtonClient(protonClientMock);
    // WHEN trying to connect to the server
    factory.connect(null, null, null, ctx.failing(t -> {
        // THEN the connection attempt fails with a TimeoutException and the given handler is invoked
        ctx.verify(() -> assertTrue(t instanceof ConnectTimeoutException));
        ctx.completeNow();
    }));
    timeoutHandlerRef.get().handle(1L);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ProtonConnection(io.vertx.proton.ProtonConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) Timeout(io.vertx.junit5.Timeout) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClient(io.vertx.proton.ProtonClient) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 4 with ConnectTimeoutException

use of org.eclipse.hono.connection.ConnectTimeoutException in project hono by eclipse.

the class ConnectionFactoryImpl method connect.

@Override
public void connect(final ProtonClientOptions options, final String username, final String password, final String containerId, final Handler<AsyncResult<ProtonConnection>> closeHandler, final Handler<ProtonConnection> disconnectHandler, final Handler<AsyncResult<ProtonConnection>> connectionResultHandler) {
    Objects.requireNonNull(connectionResultHandler);
    final ProtonClientOptions clientOptions = options != null ? options : createClientOptions();
    final String effectiveUsername = username == null ? config.getUsername() : username;
    final String effectivePassword = password == null ? config.getPassword() : password;
    addOptions(clientOptions, effectiveUsername, effectivePassword);
    final String effectiveContainerId = containerId == null ? getContainerIdDefault() : containerId;
    final ProtonClient client = protonClient != null ? protonClient : ProtonClient.create(vertx);
    logger.debug("connecting to AMQP 1.0 container [{}://{}:{}, role: {}]", clientOptions.isSsl() ? PROTOCOL_AMQPS : PROTOCOL_AMQP, config.getHost(), config.getPort(), config.getServerRole());
    final AtomicBoolean connectionTimeoutReached = new AtomicBoolean(false);
    final Long connectionTimeoutTimerId = config.getConnectTimeout() > 0 ? vertx.setTimer(config.getConnectTimeout(), id -> {
        if (connectionTimeoutReached.compareAndSet(false, true)) {
            failConnectionAttempt(clientOptions, connectionResultHandler, new ConnectTimeoutException("connection attempt timed out after " + config.getConnectTimeout() + "ms"));
        }
    }) : null;
    client.connect(clientOptions, config.getHost(), config.getPort(), effectiveUsername, effectivePassword, conAttempt -> handleConnectionAttemptResult(conAttempt, effectiveContainerId, connectionTimeoutTimerId, connectionTimeoutReached, clientOptions, closeHandler, disconnectHandler, connectionResultHandler));
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) KeyCertOptions(io.vertx.core.net.KeyCertOptions) ProtonSaslExternalImpl(io.vertx.proton.sasl.impl.ProtonSaslExternalImpl) AsyncResult(io.vertx.core.AsyncResult) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) LinkedHashSet(java.util.LinkedHashSet) Strings(org.eclipse.hono.util.Strings) OpenSsl(io.netty.handler.ssl.OpenSsl) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) Set(java.util.Set) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) Future(io.vertx.core.Future) Objects(java.util.Objects) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) TrustOptions(io.vertx.core.net.TrustOptions) Handler(io.vertx.core.Handler) ProtonSaslPlainImpl(io.vertx.proton.sasl.impl.ProtonSaslPlainImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)4 Future (io.vertx.core.Future)4 Handler (io.vertx.core.Handler)4 Vertx (io.vertx.core.Vertx)4 ProtonClient (io.vertx.proton.ProtonClient)4 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)4 ProtonConnection (io.vertx.proton.ProtonConnection)4 ClientConfigProperties (org.eclipse.hono.config.ClientConfigProperties)4 ConnectTimeoutException (org.eclipse.hono.connection.ConnectTimeoutException)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)3 Promise (io.vertx.core.Promise)3 Timeout (io.vertx.junit5.Timeout)3 VertxExtension (io.vertx.junit5.VertxExtension)3 VertxTestContext (io.vertx.junit5.VertxTestContext)3 Arrays (java.util.Arrays)3 TimeUnit (java.util.concurrent.TimeUnit)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 VertxMockSupport (org.eclipse.hono.test.VertxMockSupport)3 Constants (org.eclipse.hono.util.Constants)3 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)3