Search in sources :

Example 16 with ProtonClientOptions

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

the class HonoClientImplTest method testGetOrCreateSenderFailsOnConnectionFailure.

/**
 * Verifies that a request to create a message sender is failed immediately when the
 * underlying connection to the server fails.
 *
 * @param ctx The Vertx test context.
 */
@Test
public void testGetOrCreateSenderFailsOnConnectionFailure(final TestContext ctx) {
    // GIVEN a client that 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();
    final Async disconnected = ctx.async();
    final Async supplierInvocation = ctx.async();
    client.getOrCreateSender("telemetry/tenant", () -> {
        ctx.assertFalse(disconnected.isCompleted());
        supplierInvocation.complete();
        return Future.future();
    }).setHandler(ctx.asyncAssertFailure(cause -> {
        disconnected.complete();
    }));
    // WHEN the underlying connection fails
    supplierInvocation.await();
    connectionFactory.getDisconnectHandler().handle(con);
    // THEN all creation requests are failed
    disconnected.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 17 with ProtonClientOptions

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

the class HonoClientImplTest method testCreateConsumerFailsOnConnectionFailure.

/**
 * Verifies that a request to create a consumer is failed immediately when the
 * underlying connection to the server fails.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCreateConsumerFailsOnConnectionFailure(final TestContext ctx) {
    // GIVEN a connected 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();
    final Async creationFailure = ctx.async();
    final Async supplierInvocation = ctx.async();
    client.createConsumer("tenant", () -> {
        supplierInvocation.complete();
        return Future.future();
    }).setHandler(ctx.asyncAssertFailure(cause -> {
        creationFailure.complete();
    }));
    // wait until the consumer supplier has been invoked
    // so that we can be sure that the disconnect handler for
    // for the creation request has been registered
    supplierInvocation.await();
    // WHEN the underlying connection fails
    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 18 with ProtonClientOptions

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

the class HonoClientImplTest method testDownstreamDisconnectTriggersReconnect.

/**
 * Verifies that the client tries to re-establish a lost connection to a server.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDownstreamDisconnectTriggersReconnect(final TestContext ctx) {
    // GIVEN an client that is connected to a peer to which the
    // connection can be established on the third attempt only
    connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con, 1, 2);
    final ProtonClientOptions options = new ProtonClientOptions().setReconnectInterval(50).setReconnectAttempts(3);
    client = new HonoClientImpl(vertx, connectionFactory, props);
    client.connect(options).setHandler(ctx.asyncAssertSuccess());
    assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
    connectionFactory.setExpectedFailingConnectionAttempts(2);
    connectionFactory.setExpectedSucceedingConnectionAttempts(1);
    // WHEN the downstream connection fails
    connectionFactory.getDisconnectHandler().handle(con);
    // THEN the adapter reconnects to the downstream container
    assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
    connectionFactory.setExpectedFailingConnectionAttempts(2);
    connectionFactory.setExpectedSucceedingConnectionAttempts(1);
    // and when the downstream connection fails again
    connectionFactory.getDisconnectHandler().handle(con);
    // THEN the adapter reconnects to the downstream container again
    assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
}
Also used : ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Test(org.junit.Test)

Example 19 with ProtonClientOptions

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

the class HonoClientImplTest method testOnRemoteCloseTriggersReconnection.

/**
 * Verifies that the client tries to re-connect to a server instance if the
 * connection is closed by the peer.
 *
 * @param ctx The test context.
 */
@Test
public void testOnRemoteCloseTriggersReconnection(final TestContext ctx) {
    // GIVEN a client that is connected to a server
    final Async connected = ctx.async();
    final Async disconnectHandlerInvocation = ctx.async();
    client.connect(new ProtonClientOptions().setReconnectAttempts(1), failedCon -> disconnectHandlerInvocation.complete()).setHandler(ctx.asyncAssertSuccess(ok -> connected.complete()));
    connected.await();
    // WHEN the peer closes the connection
    connectionFactory.getCloseHandler().handle(Future.failedFuture("shutting down for maintenance"));
    // THEN the client invokes the disconnect handler provided in the original connect method call
    disconnectHandlerInvocation.await();
    // and the original connection has been closed locally
    verify(con).close();
    verify(con).disconnectHandler(null);
}
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 20 with ProtonClientOptions

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

the class HonoClientImplTest method testDownstreamDisconnectClearsSenderCreationLocks.

/**
 * Verifies that all sender creation locks are cleared when the connection to the server fails.
 *
 * @param ctx The Vertx test context.
 */
@Test
public void testDownstreamDisconnectClearsSenderCreationLocks(final TestContext ctx) {
    // expect the connection factory to be invoked twice
    // first on initial connection
    // second on reconnect
    connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con);
    // GIVEN a client trying to create a sender to a peer
    final ProtonClientOptions options = new ProtonClientOptions().setReconnectInterval(50).setReconnectAttempts(3);
    client = new HonoClientImpl(vertx, connectionFactory, props);
    client.connect(options).setHandler(ctx.asyncAssertSuccess());
    assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
    connectionFactory.setExpectedSucceedingConnectionAttempts(1);
    // WHEN the downstream connection fails just when the client wants to open a sender link
    final Async senderCreationFailure = ctx.async();
    client.getOrCreateSender("telemetry/tenant", () -> {
        connectionFactory.getDisconnectHandler().handle(con);
        return Future.future();
    }).setHandler(ctx.asyncAssertFailure(cause -> senderCreationFailure.complete()));
    // THEN the sender creation fails,
    senderCreationFailure.await();
    // the connection is re-established
    assertTrue(connectionFactory.await(1, TimeUnit.SECONDS));
    // and the next attempt to create a sender succeeds
    client.getOrCreateSender("telemetry/tenant", () -> Future.succeededFuture(mock(MessageSender.class))).setHandler(ctx.asyncAssertSuccess());
}
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) MessageSender(org.eclipse.hono.client.MessageSender) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) 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