Search in sources :

Example 1 with ConnectionFactory

use of org.eclipse.hono.connection.ConnectionFactory in project hono by eclipse.

the class HonoClientImplTest method testClientDoesNotTriggerReconnectionAfterShutdown.

/**
 * Verifies that the client does not try to re-connect to a server instance if the client was shutdown.
 *
 * @param ctx The test context.
 */
@Test
public void testClientDoesNotTriggerReconnectionAfterShutdown(final TestContext ctx) {
    // GIVEN a client that tries to connect to a server but does not succeed
    final Async connectionHandlerInvocation = ctx.async();
    connectionFactory = new DisconnectHandlerProvidingConnectionFactory(con, 0, Integer.MAX_VALUE);
    client = new HonoClientImpl(vertx, connectionFactory, props);
    client.connect(new ProtonClientOptions().setReconnectAttempts(1)).setHandler(ctx.asyncAssertFailure(cause -> connectionHandlerInvocation.complete()));
    // WHEN client gets shutdown
    client.shutdown();
    // THEN reconnect gets stopped, i.e. connection fails
    connectionHandlerInvocation.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 2 with ConnectionFactory

use of org.eclipse.hono.connection.ConnectionFactory in project hono by eclipse.

the class StandaloneAuthServerTest method prepareServer.

/**
 * Sets up the server.
 *
 * @param ctx The vertx test context.
 */
@BeforeClass
public static void prepareServer(final TestContext ctx) {
    AuthTokenHelper tokenHelper = AuthTokenHelperImpl.forSharedSecret(SIGNING_SECRET, 5);
    ServiceConfigProperties props = new ServiceConfigProperties();
    props.setInsecurePortEnabled(true);
    props.setInsecurePort(0);
    server = new SimpleAuthenticationServer();
    server.setConfig(props);
    server.setSaslAuthenticatorFactory(new HonoSaslAuthenticatorFactory(vertx, tokenHelper));
    server.addEndpoint(new AuthenticationEndpoint(vertx));
    AuthenticationServerConfigProperties serviceProps = new AuthenticationServerConfigProperties();
    serviceProps.getSigning().setTokenExpiration(5);
    serviceProps.getSigning().setSharedSecret(SIGNING_SECRET);
    serviceProps.setPermissionsPath(new ClassPathResource("authentication-service-test-permissions.json"));
    FileBasedAuthenticationService authServiceImpl = new FileBasedAuthenticationService();
    authServiceImpl.setConfig(serviceProps);
    authServiceImpl.setTokenFactory(tokenHelper);
    Async startup = ctx.async();
    Future<String> serverTracker = Future.future();
    serverTracker.setHandler(ctx.asyncAssertSuccess(s -> startup.complete()));
    Future<String> serviceTracker = Future.future();
    vertx.deployVerticle(authServiceImpl, serviceTracker.completer());
    serviceTracker.compose(s -> {
        vertx.deployVerticle(server, ctx.asyncAssertSuccess(d -> serverTracker.complete(d)));
    }, serverTracker);
    startup.await(2000);
    AuthenticationServerClientConfigProperties clientProps = new AuthenticationServerClientConfigProperties();
    clientProps.setHost("127.0.0.1");
    clientProps.setName("test-client");
    clientProps.setPort(server.getInsecurePort());
    clientProps.getValidation().setSharedSecret(SIGNING_SECRET);
    ConnectionFactory clientFactory = new ConnectionFactoryImpl(vertx, clientProps);
    client = new AuthenticationServerClient(vertx, clientFactory);
}
Also used : HonoSaslAuthenticatorFactory(org.eclipse.hono.service.auth.HonoSaslAuthenticatorFactory) AuthTokenHelper(org.eclipse.hono.service.auth.AuthTokenHelper) TestContext(io.vertx.ext.unit.TestContext) ConnectionFactoryImpl(org.eclipse.hono.connection.ConnectionFactoryImpl) Async(io.vertx.ext.unit.Async) AuthTokenHelperImpl(org.eclipse.hono.service.auth.AuthTokenHelperImpl) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) ClassPathResource(org.springframework.core.io.ClassPathResource) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Future(io.vertx.core.Future) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) AuthenticationServerClientConfigProperties(org.eclipse.hono.service.auth.delegating.AuthenticationServerClientConfigProperties) HonoSaslAuthenticatorFactory(org.eclipse.hono.service.auth.HonoSaslAuthenticatorFactory) AuthenticationServerClient(org.eclipse.hono.service.auth.delegating.AuthenticationServerClient) AuthTokenHelper(org.eclipse.hono.service.auth.AuthTokenHelper) AuthenticationServerClient(org.eclipse.hono.service.auth.delegating.AuthenticationServerClient) ClassPathResource(org.springframework.core.io.ClassPathResource) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) Async(io.vertx.ext.unit.Async) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) AuthenticationServerClientConfigProperties(org.eclipse.hono.service.auth.delegating.AuthenticationServerClientConfigProperties) ConnectionFactoryImpl(org.eclipse.hono.connection.ConnectionFactoryImpl) BeforeClass(org.junit.BeforeClass)

Example 3 with ConnectionFactory

use of org.eclipse.hono.connection.ConnectionFactory 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)

Example 4 with ConnectionFactory

use of org.eclipse.hono.connection.ConnectionFactory 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)

Aggregations

Future (io.vertx.core.Future)4 Vertx (io.vertx.core.Vertx)4 Async (io.vertx.ext.unit.Async)4 TestContext (io.vertx.ext.unit.TestContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 ConnectionFactory (org.eclipse.hono.connection.ConnectionFactory)4 BeforeClass (org.junit.BeforeClass)4 Test (org.junit.Test)4 RunWith (org.junit.runner.RunWith)4 AsyncResult (io.vertx.core.AsyncResult)3 Handler (io.vertx.core.Handler)3 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)3 ProtonConnection (io.vertx.proton.ProtonConnection)3 HttpURLConnection (java.net.HttpURLConnection)3 InetAddress (java.net.InetAddress)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeUnit (java.util.concurrent.TimeUnit)3 ClientErrorException (org.eclipse.hono.client.ClientErrorException)3 MessageSender (org.eclipse.hono.client.MessageSender)3 RegistrationClient (org.eclipse.hono.client.RegistrationClient)3