Search in sources :

Example 1 with ProtonSession

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

the class SenderFactoryImpl method newSession.

Future<ProtonSession> newSession(final ProtonConnection con, final ResourceIdentifier targetAddress) {
    final Future<ProtonSession> result = Future.future();
    ProtonSession session = con.attachments().get(targetAddress.getEndpoint(), ProtonSession.class);
    if (session != null) {
        LOG.debug("re-using existing session for sending {} data downstream", targetAddress.getEndpoint());
        result.complete(session);
    } else {
        LOG.debug("creating new session for sending {} data downstream", targetAddress.getEndpoint());
        session = con.createSession();
        con.attachments().set(targetAddress.getEndpoint(), ProtonSession.class, session);
        session.openHandler(remoteOpen -> {
            if (remoteOpen.succeeded()) {
                result.complete(remoteOpen.result());
            } else {
                result.fail(remoteOpen.cause());
            }
        });
        session.open();
    }
    return result;
}
Also used : ProtonSession(io.vertx.proton.ProtonSession)

Example 2 with ProtonSession

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

the class HonoConnectionImpl method createDefaultSession.

private ProtonSession createDefaultSession(final ProtonConnection connection) {
    if (connection == null) {
        throw new IllegalStateException("no connection to create session for");
    } else {
        log.debug("establishing AMQP session with server [{}:{}, role: {}]", connectionFactory.getHost(), connectionFactory.getPort(), connectionFactory.getServerRole());
        final ProtonSession newSession = connection.createSession();
        newSession.closeHandler(remoteClose -> {
            final StringBuilder msgBuilder = new StringBuilder("the connection's session closed unexpectedly");
            Optional.ofNullable(newSession.getRemoteCondition()).ifPresent(error -> {
                msgBuilder.append(String.format(" [condition: %s, description: %s]", error.getCondition(), error.getDescription()));
            });
            newSession.close();
            onRemoteClose(Future.failedFuture(msgBuilder.toString()));
        });
        newSession.setIncomingCapacity(clientConfigProperties.getMaxSessionWindowSize());
        newSession.open();
        return newSession;
    }
}
Also used : ProtonSession(io.vertx.proton.ProtonSession)

Example 3 with ProtonSession

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

the class HonoConnectionImplTest method testRemoteSessionCloseTriggersReconnection.

/**
 * Verifies that the client tries to reconnect to the peer if the peer
 * closes the connection's session.
 *
 * @param ctx The test context.
 */
@Test
public void testRemoteSessionCloseTriggersReconnection(final VertxTestContext ctx) {
    // GIVEN a client that is connected to a server
    final Promise<HonoConnection> connected = Promise.promise();
    @SuppressWarnings("unchecked") final DisconnectListener<HonoConnection> disconnectListener = mock(DisconnectListener.class);
    honoConnection.addDisconnectListener(disconnectListener);
    final AtomicInteger reconnectListenerInvocations = new AtomicInteger();
    honoConnection.addReconnectListener(con -> reconnectListenerInvocations.incrementAndGet());
    props.setServerRole("service-provider");
    honoConnection.connect(new ProtonClientOptions()).onComplete(connected);
    connectionFactory.setExpectedSucceedingConnectionAttempts(1);
    connected.future().onComplete(ctx.succeeding(c -> {
        ctx.verify(() -> {
            // WHEN the peer closes the session
            final ArgumentCaptor<Handler<AsyncResult<ProtonSession>>> sessionCloseHandler = VertxMockSupport.argumentCaptorHandler();
            verify(session).closeHandler(sessionCloseHandler.capture());
            sessionCloseHandler.getValue().handle(Future.succeededFuture(session));
            // THEN the client invokes the registered disconnect handler
            verify(disconnectListener).onDisconnect(honoConnection);
            // and the original connection has been closed locally
            verify(con).close();
            verify(con).disconnectHandler(null);
            // and the connection is re-established
            assertThat(connectionFactory.await()).isTrue();
            assertThat(reconnectListenerInvocations.get()).isEqualTo(1);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Context(io.vertx.core.Context) Timeout(io.vertx.junit5.Timeout) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) SaslSystemException(io.vertx.proton.sasl.SaslSystemException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Mockito.doAnswer(org.mockito.Mockito.doAnswer) DisconnectListener(org.eclipse.hono.client.DisconnectListener) AmqpError(org.apache.qpid.proton.amqp.transport.AmqpError) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Predicate(java.util.function.Predicate) ProtonQoS(io.vertx.proton.ProtonQoS) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) AdditionalAnswers(org.mockito.AdditionalAnswers) Test(org.junit.jupiter.api.Test) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) ProtonSender(io.vertx.proton.ProtonSender) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Target(org.apache.qpid.proton.amqp.messaging.Target) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) CompositeFuture(io.vertx.core.CompositeFuture) ProtonSession(io.vertx.proton.ProtonSession) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Symbol(org.apache.qpid.proton.amqp.Symbol) BiConsumer(java.util.function.BiConsumer) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) AsyncResult(io.vertx.core.AsyncResult) HonoConnection(org.eclipse.hono.client.HonoConnection) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Source(org.apache.qpid.proton.amqp.transport.Source) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HonoConnection(org.eclipse.hono.client.HonoConnection) ArgumentCaptor(org.mockito.ArgumentCaptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.jupiter.api.Test)

Example 4 with ProtonSession

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

the class AmqpClientUnitTestHelper method mockLinkConnection.

private static void mockLinkConnection(final ProtonLink<?> link) {
    final ProtonConnection connection = mock(ProtonConnection.class);
    when(connection.isDisconnected()).thenReturn(false);
    final ProtonSession session = mock(ProtonSession.class);
    when(session.getConnection()).thenReturn(connection);
    when(link.getSession()).thenReturn(session);
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonSession(io.vertx.proton.ProtonSession)

Example 5 with ProtonSession

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

the class SenderFactoryImplTest method testNewSenderIsClosedOnRemoteDetachOrClose.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void testNewSenderIsClosedOnRemoteDetachOrClose(final TestContext ctx, final BiConsumer<ProtonSender, ArgumentCaptor<Handler>> handlerCaptor) {
    // GIVEN a sender created by the factory
    final Async senderCreation = ctx.async();
    final ProtonSender sender = mock(ProtonSender.class);
    when(sender.open()).then(answer -> {
        senderCreation.complete();
        return sender;
    });
    final ProtonConnection con = mock(ProtonConnection.class);
    final ProtonSession session = mock(ProtonSession.class);
    when(session.createSender(anyString())).thenReturn(sender);
    final ResourceIdentifier address = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, Constants.DEFAULT_TENANT, null);
    final Handler<String> closeHook = mock(Handler.class);
    final SenderFactoryImpl factory = new SenderFactoryImpl();
    final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);
    factory.newSender(con, session, address, ProtonQoS.AT_LEAST_ONCE, drain -> {
    }, closeHook);
    handlerCaptor.accept(sender, captor);
    // WHEN the peer detaches from the sender
    captor.getValue().handle(Future.succeededFuture(sender));
    // THEN the sender gets closed
    verify(sender).close();
    // and the close hook is called
    verify(closeHook).handle(any());
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) ProtonConnection(io.vertx.proton.ProtonConnection) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) ProtonSession(io.vertx.proton.ProtonSession) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler)

Aggregations

ProtonSession (io.vertx.proton.ProtonSession)8 ProtonConnection (io.vertx.proton.ProtonConnection)6 ProtonSender (io.vertx.proton.ProtonSender)4 Handler (io.vertx.core.Handler)2 Vertx (io.vertx.core.Vertx)2 ProtonQoS (io.vertx.proton.ProtonQoS)2 ProtonReceiver (io.vertx.proton.ProtonReceiver)2 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 AsyncResult (io.vertx.core.AsyncResult)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 EventBus (io.vertx.core.eventbus.EventBus)1 Async (io.vertx.ext.unit.Async)1 Timeout (io.vertx.junit5.Timeout)1 VertxExtension (io.vertx.junit5.VertxExtension)1 VertxTestContext (io.vertx.junit5.VertxTestContext)1 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)1