Search in sources :

Example 21 with ProtonClientOptions

use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.

the class HonoClientImplTest method testGetOrCreateRequestResponseClientFailsOnConnectionFailure.

/**
 * Verifies that a request to create a request-response client is failed immediately when the
 * underlying connection to the server fails.
 *
 * @param ctx The Vertx test context.
 */
@Test
public void testGetOrCreateRequestResponseClientFailsOnConnectionFailure(final TestContext ctx) {
    // GIVEN a client that tries to create a registration client for "tenant"
    final Async connected = ctx.async();
    client.connect(new ProtonClientOptions()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
    connected.await();
    final Async creationFailure = ctx.async();
    final Async supplierInvocation = ctx.async();
    client.getOrCreateRequestResponseClient("registration/tenant", () -> {
        ctx.assertFalse(creationFailure.isCompleted());
        supplierInvocation.complete();
        return Future.future();
    }, ctx.asyncAssertFailure(cause -> creationFailure.complete()));
    // WHEN the underlying connection fails
    supplierInvocation.await();
    connectionFactory.getDisconnectHandler().handle(con);
    // THEN all creation requests are failed
    creationFailure.await();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RequestResponseClient(org.eclipse.hono.client.RequestResponseClient) TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) InetAddress(java.net.InetAddress) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) RegistrationClient(org.eclipse.hono.client.RegistrationClient) AsyncResult(io.vertx.core.AsyncResult) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Vertx(io.vertx.core.Vertx) Assert.assertTrue(org.junit.Assert.assertTrue) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Test(org.junit.Test)

Example 22 with ProtonClientOptions

use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.

the class HonoClientImplTest method testConnectTriesToReconnectOnFailedConnectAttempt.

/**
 * Verifies that the client repeatedly tries to connect until a connection is established.
 *
 * @param ctx The test context.
 */
@Test
public void testConnectTriesToReconnectOnFailedConnectAttempt(final TestContext ctx) {
    // 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, 1, 2);
    client = new HonoClientImpl(vertx, connectionFactory, props);
    // WHEN trying to connect
    final ProtonClientOptions options = new ProtonClientOptions().setReconnectInterval(50).setReconnectAttempts(3);
    final Async disconnectHandlerInvocation = ctx.async();
    client.connect(options, failedCon -> disconnectHandlerInvocation.complete()).setHandler(ctx.asyncAssertSuccess());
    // THEN the client succeeds to connect on the third attempt
    assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
    // and sets the disconnect handler provided as a parameter to the connect method
    connectionFactory.getDisconnectHandler().handle(con);
    disconnectHandlerInvocation.await();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RequestResponseClient(org.eclipse.hono.client.RequestResponseClient) TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) InetAddress(java.net.InetAddress) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) RegistrationClient(org.eclipse.hono.client.RegistrationClient) AsyncResult(io.vertx.core.AsyncResult) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Vertx(io.vertx.core.Vertx) Assert.assertTrue(org.junit.Assert.assertTrue) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Handler(io.vertx.core.Handler) Async(io.vertx.ext.unit.Async) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Test(org.junit.Test)

Example 23 with ProtonClientOptions

use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.

the class HonoClientImplTest method testConnectFailsAfterMaxConnectionAttempts.

/**
 * Verifies that the client tries to connect a limited
 * number of times only.
 *
 * @param ctx The vert.x test client.
 */
@Test
public void testConnectFailsAfterMaxConnectionAttempts(final TestContext ctx) {
    // GIVEN a client that is configured to connect
    // to a peer that is not listening
    props.setHost(InetAddress.getLoopbackAddress().getHostAddress());
    props.setPort(45000);
    client = new HonoClientImpl(vertx, props);
    final ProtonClientOptions options = new ProtonClientOptions().setConnectTimeout(50).setReconnectAttempts(3).setReconnectInterval(50);
    // WHEN the client tries to connect
    client.connect(options).setHandler(ctx.asyncAssertFailure(t -> {
        // THEN the connection attempt fails
        ctx.assertEquals(HttpURLConnection.HTTP_UNAVAILABLE, ((ServerErrorException) t).getErrorCode());
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RequestResponseClient(org.eclipse.hono.client.RequestResponseClient) TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) InetAddress(java.net.InetAddress) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) RegistrationClient(org.eclipse.hono.client.RegistrationClient) AsyncResult(io.vertx.core.AsyncResult) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) AfterClass(org.junit.AfterClass) Vertx(io.vertx.core.Vertx) Assert.assertTrue(org.junit.Assert.assertTrue) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) CountDownLatch(java.util.concurrent.CountDownLatch) Rule(org.junit.Rule) Handler(io.vertx.core.Handler) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Test(org.junit.Test)

Example 24 with ProtonClientOptions

use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectEnablesSslIfTrustStoreIsConfigured.

/**
 * Verifies that the factory uses TLS when connecting to the peer if a trust store
 * is configured but TLS has not been enabled explicitly.
 */
@SuppressWarnings("unchecked")
@Test
public void testConnectEnablesSslIfTrustStoreIsConfigured() {
    // GIVEN a factory configured to use a specific trust store
    final ClientConfigProperties config = new ClientConfigProperties();
    config.setHost("remote.host");
    config.setTrustStorePath("/tmp/trusted-ca.p12");
    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());
}
Also used : Handler(io.vertx.core.Handler) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Example 25 with ProtonClientOptions

use of io.vertx.proton.ProtonClientOptions in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectDoesNotUseSaslPlainForEmptyUsernameAndPassword.

/**
 * Verifies that the factory does not enable SASL_PLAIN if the username and password are empty
 * strings.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testConnectDoesNotUseSaslPlainForEmptyUsernameAndPassword(final TestContext ctx) {
    // GIVEN a factory configured to connect to a server
    final ProtonClientOptions options = new ProtonClientOptions();
    final ProtonClient client = mock(ProtonClient.class);
    final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
    factory.setProtonClient(client);
    // WHEN connecting to the server using empty strings for username and password
    factory.connect(options, "", "", null, null, c -> {
    });
    // THEN the factory does not enable the SASL_PLAIN mechanism when establishing
    // the connection
    ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
    verify(client).connect(optionsCaptor.capture(), anyString(), anyInt(), eq(""), eq(""), any(Handler.class));
    assertFalse(optionsCaptor.getValue().getEnabledSaslMechanisms().contains("PLAIN"));
}
Also used : Handler(io.vertx.core.Handler) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Aggregations

ProtonClientOptions (io.vertx.proton.ProtonClientOptions)27 Test (org.junit.Test)20 Vertx (io.vertx.core.Vertx)19 ClientConfigProperties (org.eclipse.hono.config.ClientConfigProperties)19 Handler (io.vertx.core.Handler)17 Future (io.vertx.core.Future)16 TestContext (io.vertx.ext.unit.TestContext)16 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)15 Constants (org.eclipse.hono.util.Constants)15 RunWith (org.junit.runner.RunWith)15 TimeUnit (java.util.concurrent.TimeUnit)14 MessageSender (org.eclipse.hono.client.MessageSender)14 BeforeClass (org.junit.BeforeClass)14 ProtonConnection (io.vertx.proton.ProtonConnection)13 HttpURLConnection (java.net.HttpURLConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 AsyncResult (io.vertx.core.AsyncResult)12 Async (io.vertx.ext.unit.Async)12 ClientErrorException (org.eclipse.hono.client.ClientErrorException)12 RegistrationClient (org.eclipse.hono.client.RegistrationClient)12