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();
}
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();
}
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);
}
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));
}
Aggregations