use of io.vertx.junit5.Timeout 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 io.vertx.junit5.Timeout 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 io.vertx.junit5.Timeout in project hono by eclipse.
the class HonoProtonHelperTest method testExecuteOnContextRunsOnGivenContext.
/**
* Verifies that code is scheduled to be executed on a given Context
* other than the current Context.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
public void testExecuteOnContextRunsOnGivenContext(final VertxTestContext ctx) {
final Context mockContext = mock(Context.class);
doAnswer(invocation -> {
final Handler<Void> codeToRun = invocation.getArgument(0);
codeToRun.handle(null);
return null;
}).when(mockContext).runOnContext(any(Handler.class));
HonoProtonHelper.executeOnContext(mockContext, result -> result.complete("done")).onComplete(ctx.succeeding(s -> {
ctx.verify(() -> {
verify(mockContext).runOnContext(any(Handler.class));
assertThat(s).isEqualTo("done");
});
ctx.completeNow();
}));
}
use of io.vertx.junit5.Timeout in project hono by eclipse.
the class HonoConnectionImplTest method testCreateSenderFailsOnDisconnectBeforeOpen.
/**
* Verifies that the attempt to create a sender fails with a
* {@code ServerErrorException} if the connection gets disconnected
* before the remote peer has sent its attach frame. It is verified
* that this is done before the link establishment timeout.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateSenderFailsOnDisconnectBeforeOpen(final VertxTestContext ctx) {
// choose a distinct value here
final long linkEstablishmentTimeout = 444L;
props.setLinkEstablishmentTimeout(linkEstablishmentTimeout);
// don't run linkEstablishmentTimeout timer handler
when(vertx.setTimer(eq(linkEstablishmentTimeout), VertxMockSupport.anyHandler())).thenAnswer(invocation -> 0L);
final ProtonSender sender = mock(ProtonSender.class);
when(sender.isOpen()).thenReturn(Boolean.TRUE);
when(session.createSender(anyString())).thenReturn(sender);
final Target target = new Target();
target.setAddress("someAddress");
when(sender.getRemoteTarget()).thenReturn(target);
when(sender.getCredit()).thenReturn(0);
// mock handlers
final Handler<String> remoteCloseHook = VertxMockSupport.mockHandler();
// GIVEN an established connection
honoConnection.connect().compose(c -> {
// WHEN creating a sender link with a close hook
final Future<ProtonSender> result = honoConnection.createSender("target", ProtonQoS.AT_LEAST_ONCE, remoteCloseHook);
// THEN the result is not completed at first
ctx.verify(() -> assertThat(result.isComplete()).isFalse());
// WHEN the downstream connection fails
connectionFactory.getDisconnectHandler().handle(con);
return result;
}).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertThat(((ServerErrorException) t).getErrorCode()).isEqualTo(HttpURLConnection.HTTP_UNAVAILABLE));
ctx.completeNow();
}));
}
use of io.vertx.junit5.Timeout in project hono by eclipse.
the class RequestResponseClientTest method testCreateAndSendRequestSetsNoTimerIfRequestTimeoutZero.
/**
* Verifies that the client configured with a zero request timeout sets no timer for
* canceling the request in case of no received response.
*
* @param ctx The vert.x test context.
*/
@Test
public void testCreateAndSendRequestSetsNoTimerIfRequestTimeoutZero(final VertxTestContext ctx) {
// WHEN configuring the client with a zero request timeout and sending a request message
final JsonObject payload = new JsonObject().put("key", "value");
client.map(c -> {
c.setRequestTimeout(0);
return c;
}).onComplete(ctx.succeeding(c -> {
c.createAndSendRequest("get", null, payload.toBuffer(), "application/json", SimpleRequestResponseResult::from, span);
}));
// THEN the message is sent
final Message request = verifySenderSend();
assertThat(request).isNotNull();
assertThat(request.getBody()).isNotNull();
assertThat(request.getBody()).isInstanceOf(Data.class);
final Buffer body = MessageHelper.getPayload(request);
assertThat(body.getBytes()).isEqualTo(payload.toBuffer().getBytes());
// and no timer has been set to time out the request
verify(vertx, never()).setTimer(anyLong(), VertxMockSupport.anyHandler());
ctx.completeNow();
}
Aggregations