use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class HonoClientImplTest method testGetOrCreateTelemetrySenderFailsIfInvokedConcurrently.
/**
* Verifies that a concurrent request to create a sender fails the given future for tracking the attempt.
*
* @param ctx The helper to use for running async tests.
*/
@Test
public void testGetOrCreateTelemetrySenderFailsIfInvokedConcurrently(final TestContext ctx) {
// GIVEN a client that already tries to create a telemetry sender for "tenant"
final Async connected = ctx.async();
client.connect(new ProtonClientOptions()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
connected.await();
client.getOrCreateSender("telemetry/tenant", () -> Future.future());
// WHEN an additional, concurrent attempt is made to create a telemetry sender for "tenant"
client.getOrCreateSender("telemetry/tenant", () -> {
ctx.fail("should not create concurrent client");
return Future.succeededFuture(mock(MessageSender.class));
}).setHandler(ctx.asyncAssertFailure(t -> {
// THEN the concurrent attempt fails without any attempt being made to create another sender
ctx.assertTrue(ServerErrorException.class.isInstance(t));
}));
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class ConnectionFactoryImpl method connect.
@Override
public void connect(final ProtonClientOptions options, final String username, final String password, final Handler<AsyncResult<ProtonConnection>> closeHandler, final Handler<ProtonConnection> disconnectHandler, final Handler<AsyncResult<ProtonConnection>> connectionResultHandler) {
if (vertx == null) {
throw new IllegalStateException("Vert.x instance must be set");
} else if (config == null) {
throw new IllegalStateException("Client configuration must be set");
}
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 ProtonClient client = protonClient != null ? protonClient : ProtonClient.create(vertx);
logger.debug("connecting to AMQP 1.0 container [{}://{}:{}]", clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
client.connect(clientOptions, config.getHost(), config.getPort(), effectiveUsername, effectivePassword, conAttempt -> handleConnectionAttemptResult(conAttempt, clientOptions, closeHandler, disconnectHandler, connectionResultHandler));
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class ConnectionFactoryImpl method handleConnectionAttemptResult.
private void handleConnectionAttemptResult(final AsyncResult<ProtonConnection> conAttempt, final ProtonClientOptions clientOptions, final Handler<AsyncResult<ProtonConnection>> closeHandler, final Handler<ProtonConnection> disconnectHandler, final Handler<AsyncResult<ProtonConnection>> connectionResultHandler) {
if (conAttempt.failed()) {
logger.debug("can't connect to AMQP 1.0 container [{}://{}:{}]: {}", clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(), conAttempt.cause().getMessage());
connectionResultHandler.handle(Future.failedFuture(conAttempt.cause()));
} else {
// at this point the SASL exchange has completed successfully
logger.debug("connected to AMQP 1.0 container [{}://{}:{}], opening connection ...", clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
ProtonConnection downstreamConnection = conAttempt.result();
downstreamConnection.setContainer(String.format("%s-%s", config.getName(), UUID.randomUUID())).setHostname(config.getAmqpHostname()).openHandler(openCon -> {
if (openCon.succeeded()) {
logger.debug("connection to container [{}] at [{}://{}:{}] open", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort());
downstreamConnection.disconnectHandler(disconnectHandler);
downstreamConnection.closeHandler(closeHandler);
connectionResultHandler.handle(Future.succeededFuture(downstreamConnection));
} else {
final ErrorCondition error = downstreamConnection.getRemoteCondition();
if (error == null) {
logger.warn("can't open connection to container [{}] at [{}://{}:{}]", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(), openCon.cause());
} else {
logger.warn("can't open connection to container [{}] at [{}://{}:{}]: {} -{}", downstreamConnection.getRemoteContainer(), clientOptions.isSsl() ? "amqps" : "amqp", config.getHost(), config.getPort(), error.getCondition(), error.getDescription());
}
connectionResultHandler.handle(Future.failedFuture(openCon.cause()));
}
}).open();
}
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectInvokesHandlerOnfailureToConnect.
/**
* Verifies that the given result handler is invoked if a connection attempt fails.
*
* @param ctx The vert.x test context.
*/
@Test
public void testConnectInvokesHandlerOnfailureToConnect(final TestContext ctx) {
// GIVEN a factory configured to connect to a non-existing server
ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
// WHEN trying to connect to the server
final Async handlerInvocation = ctx.async();
ProtonClientOptions options = new ProtonClientOptions().setConnectTimeout(100);
factory.connect(options, null, null, ctx.asyncAssertFailure(t -> {
handlerInvocation.complete();
}));
// THEN the connection attempt fails and the given handler is invoked
handlerInvocation.await(2000);
}
use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectEnablesSslIfExplicitlyConfigured.
/**
* Verifies that the factory uses TLS when connecting to the peer if no trust store
* is configured but TLS has been enabled explicitly.
*/
@SuppressWarnings("unchecked")
@Test
public void testConnectEnablesSslIfExplicitlyConfigured() {
// GIVEN a factory configured to connect to a server using TLS
final ClientConfigProperties config = new ClientConfigProperties();
config.setHost("remote.host");
config.setTlsEnabled(true);
final ProtonClient client = mock(ProtonClient.class);
final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, config);
factory.setProtonClient(client);
// WHEN connecting to the server
factory.connect(null, null, null, c -> {
});
// THEN the factory uses TLS when establishing the connection
final ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
verify(client).connect(optionsCaptor.capture(), eq("remote.host"), anyInt(), any(), any(), any(Handler.class));
assertTrue(optionsCaptor.getValue().isSsl());
}
Aggregations