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;
}
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;
}
}
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();
}));
}
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);
}
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());
}
Aggregations