use of org.alfresco.repo.rendition2.RenditionDefinition2.TIMEOUT in project hono by eclipse.
the class AmqpUploadTestBase method testAdapterRejectsBadInboundMessage.
/**
* Verifies that a message containing a payload which has the <em>empty notification</em>
* content type is rejected by the adapter.
*
* @param context The Vert.x context for running asynchronous tests.
* @throws InterruptedException if test is interrupted while running.
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 10)
public void testAdapterRejectsBadInboundMessage(final VertxTestContext context) throws InterruptedException {
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final VertxTestContext setup = new VertxTestContext();
setupProtocolAdapter(tenantId, new Tenant(), deviceId, ProtonQoS.AT_LEAST_ONCE).map(s -> {
setup.verify(() -> {
final UnsignedLong maxMessageSize = s.getRemoteMaxMessageSize();
assertWithMessage("max-message-size included in adapter's attach frame").that(maxMessageSize).isNotNull();
assertWithMessage("max-message-size").that(maxMessageSize.longValue()).isGreaterThan(0);
});
sender = s;
return s;
}).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
context.failNow(setup.causeOfFailure());
return;
}
final Message msg = ProtonHelper.message("some payload");
msg.setContentType(EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION);
msg.setAddress(getEndpointName());
sender.send(msg, delivery -> {
context.verify(() -> {
assertThat(delivery.getRemoteState()).isInstanceOf(Rejected.class);
final Rejected rejected = (Rejected) delivery.getRemoteState();
final ErrorCondition error = rejected.getError();
assertThat((Object) error.getCondition()).isEqualTo(Constants.AMQP_BAD_REQUEST);
});
context.completeNow();
});
}
use of org.alfresco.repo.rendition2.RenditionDefinition2.TIMEOUT in project hono by eclipse.
the class AmqpUploadTestBase method testAutoProvisioningViaGateway.
/**
* Verifies that an edge device is auto-provisioned if it connects via a gateway equipped with the corresponding
* authority.
*
* @param ctx The Vert.x test context.
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 10)
public void testAutoProvisioningViaGateway(final VertxTestContext ctx) {
final String tenantId = helper.getRandomTenantId();
final String gatewayId = helper.getRandomDeviceId(tenantId);
final Device gateway = new Device().setAuthorities(Collections.singleton(RegistryManagementConstants.AUTHORITY_AUTO_PROVISIONING_ENABLED));
final String username = IntegrationTestSupport.getUsername(gatewayId, tenantId);
final String edgeDeviceId = helper.getRandomDeviceId(tenantId);
final Promise<Void> provisioningNotificationReceived = Promise.promise();
helper.createAutoProvisioningMessageConsumers(ctx, provisioningNotificationReceived, tenantId, edgeDeviceId).compose(ok -> helper.registry.addDeviceForTenant(tenantId, new Tenant(), gatewayId, gateway, DEVICE_PASSWORD)).compose(ok -> connectToAdapter(username, DEVICE_PASSWORD)).compose(con -> createProducer(null, ProtonQoS.AT_LEAST_ONCE)).compose(sender -> {
final Message msg = ProtonHelper.message("apFoobar");
msg.setContentType("text/plain");
msg.setAddress(String.format("%s/%s/%s", getEndpointName(), tenantId, edgeDeviceId));
final Promise<Void> result = Promise.promise();
sender.send(msg, delivery -> {
ctx.verify(() -> assertThat(delivery.getRemoteState()).isInstanceOf(Accepted.class));
result.complete();
});
return result.future();
}).compose(ok -> provisioningNotificationReceived.future()).compose(ok -> helper.registry.getRegistrationInfo(tenantId, edgeDeviceId)).onComplete(ctx.succeeding(registrationResult -> {
ctx.verify(() -> {
final var info = registrationResult.bodyAsJsonObject();
IntegrationTestSupport.assertDeviceStatusProperties(info.getJsonObject(RegistryManagementConstants.FIELD_STATUS), true);
});
ctx.completeNow();
}));
}
use of org.alfresco.repo.rendition2.RenditionDefinition2.TIMEOUT in project hono by eclipse.
the class HonoConnectionImplTest method testCreateReceiverFailsOnDisconnectBeforeOpen.
/**
* Verifies that the attempt to create a receiver 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 testCreateReceiverFailsOnDisconnectBeforeOpen(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 Source source = mock(Source.class);
when(source.getAddress()).thenReturn("source/address");
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.isOpen()).thenReturn(Boolean.TRUE);
when(receiver.getSource()).thenReturn(source);
when(receiver.getRemoteSource()).thenReturn(source);
when(session.createReceiver(anyString())).thenReturn(receiver);
final Handler<String> remoteCloseHook = VertxMockSupport.mockHandler();
// GIVEN an established connection
honoConnection.connect().compose(c -> {
// WHEN creating a receiver link with a close hook
final Future<ProtonReceiver> result = honoConnection.createReceiver("source", ProtonQoS.AT_LEAST_ONCE, mock(ProtonMessageHandler.class), 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 org.alfresco.repo.rendition2.RenditionDefinition2.TIMEOUT in project hono by eclipse.
the class HonoConnectionImplTest method testIsConnectedWithTimeoutSucceedsAfterConcurrentReconnectSucceeded.
/**
* Verifies that {@link HonoConnectionImpl#isConnected(long)} only completes once a concurrent
* connection attempt (which eventually succeeds here) is finished.
*
* @param ctx The test execution context.
*/
@Test
public void testIsConnectedWithTimeoutSucceedsAfterConcurrentReconnectSucceeded(final VertxTestContext ctx) {
final long isConnectedTimeout = 44444L;
// let the vertx timer for the isConnectedTimeout do nothing
when(vertx.setTimer(eq(isConnectedTimeout), VertxMockSupport.anyHandler())).thenAnswer(invocation -> 0L);
final AtomicBoolean isConnectedInvocationsDone = new AtomicBoolean(false);
final AtomicReference<Future<Void>> isConnected1FutureRef = new AtomicReference<>();
final AtomicReference<Future<Void>> isConnectedTimeoutForcedFutureRef = new AtomicReference<>();
final AtomicReference<Future<Void>> isConnected2FutureRef = new AtomicReference<>();
// GIVEN a client that is configured to connect to a peer
// to which the connection can be established on the third attempt only
connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con) {
@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) {
// and GIVEN "isConnected" invocations done while the "connect" invocation is still in progress
if (isConnectedInvocationsDone.compareAndSet(false, true)) {
isConnected1FutureRef.set(honoConnection.isConnected(isConnectedTimeout));
isConnectedTimeoutForcedFutureRef.set(honoConnection.isConnected(1L));
isConnected2FutureRef.set(honoConnection.isConnected(isConnectedTimeout));
// assert "isConnected" invocations have not completed yet, apart from the one with the forced timeout
ctx.verify(() -> {
assertThat(isConnected1FutureRef.get().isComplete()).isFalse();
assertThat(isConnectedTimeoutForcedFutureRef.get().failed()).isTrue();
assertThat(isConnected2FutureRef.get().isComplete()).isFalse();
});
}
super.connect(options, username, password, containerId, closeHandler, disconnectHandler, connectionResultHandler);
}
};
connectionFactory.setExpectedFailingConnectionAttempts(2);
props.setReconnectAttempts(2);
props.setConnectTimeout(10);
honoConnection = new HonoConnectionImpl(vertx, connectionFactory, props);
// WHEN trying to connect
honoConnection.connect().compose(v -> CompositeFuture.all(isConnected1FutureRef.get(), isConnected2FutureRef.get())).onFailure(ctx::failNow);
ctx.verify(() -> {
// and the client fails twice to connect
assertThat(connectionFactory.awaitFailure()).isTrue();
// and succeeds to connect on the third attempt
assertThat(connectionFactory.await()).isTrue();
});
ctx.completeNow();
}
use of org.alfresco.repo.rendition2.RenditionDefinition2.TIMEOUT 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);
}
Aggregations