use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class AmqpAdapterClientFactoryTest method setUp.
/**
* Sets up the fixture.
*/
@BeforeAll
public static void setUp() {
final HonoConnection connection = AmqpClientUnitTestHelper.mockHonoConnection(mock(Vertx.class));
when(connection.isConnected(anyLong())).thenReturn(Future.succeededFuture());
final ProtonSender protonSender = AmqpClientUnitTestHelper.mockProtonSender();
when(connection.createSender(any(), any(), any())).thenReturn(Future.succeededFuture(protonSender));
final ProtonReceiver receiver = AmqpClientUnitTestHelper.mockProtonReceiver();
when(connection.createReceiver(anyString(), any(), any(), any())).thenReturn(Future.succeededFuture(receiver));
factory = AmqpAdapterClientFactory.create(connection, "my-tenant");
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class AbstractHonoClient method createReceiver.
/**
* Creates a receiver link.
* <p>
* The receiver will be created with its <em>autoAccept</em> property set to {@code true}.
*
* @param ctx The vert.x context to use for establishing the link.
* @param clientConfig The configuration properties to use.
* @param con The connection to create the link for.
* @param sourceAddress The address to receive messages from.
* @param qos The quality of service to use for the link.
* @param messageHandler The handler to invoke with every message received.
* @param closeHook The handler to invoke when the link is closed by the peer (may be {@code null}).
* @return A future for the created link. The future will be completed once the link is open.
* The future will fail with a {@link ServiceInvocationException} if the link cannot be opened.
* @throws NullPointerException if any of the arguments other than close hook is {@code null}.
*/
protected static final Future<ProtonReceiver> createReceiver(final Context ctx, final ClientConfigProperties clientConfig, final ProtonConnection con, final String sourceAddress, final ProtonQoS qos, final ProtonMessageHandler messageHandler, final Handler<String> closeHook) {
Objects.requireNonNull(ctx);
Objects.requireNonNull(clientConfig);
Objects.requireNonNull(con);
Objects.requireNonNull(sourceAddress);
Objects.requireNonNull(qos);
Objects.requireNonNull(messageHandler);
final Future<ProtonReceiver> result = Future.future();
ctx.runOnContext(go -> {
final ProtonReceiver receiver = con.createReceiver(sourceAddress);
receiver.attachments().set(KEY_LINK_ESTABLISHED, Boolean.class, Boolean.FALSE);
receiver.setAutoAccept(true);
receiver.setQoS(qos);
receiver.setPrefetch(clientConfig.getInitialCredits());
receiver.handler((delivery, message) -> {
messageHandler.handle(delivery, message);
if (LOG.isTraceEnabled()) {
int remainingCredits = receiver.getCredit() - receiver.getQueued();
LOG.trace("handling message [remotely settled: {}, queued messages: {}, remaining credit: {}]", delivery.remotelySettled(), receiver.getQueued(), remainingCredits);
}
});
receiver.openHandler(recvOpen -> {
if (recvOpen.succeeded()) {
LOG.debug("receiver open [source: {}]", sourceAddress);
receiver.attachments().set(KEY_LINK_ESTABLISHED, Boolean.class, Boolean.TRUE);
result.complete(recvOpen.result());
} else {
final ErrorCondition error = receiver.getRemoteCondition();
if (error == null) {
LOG.debug("opening receiver [{}] failed", sourceAddress, recvOpen.cause());
result.fail(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND, "cannot open receiver", recvOpen.cause()));
} else {
LOG.debug("opening receiver [{}] failed: {} - {}", sourceAddress, error.getCondition(), error.getDescription());
result.fail(StatusCodeMapper.from(error));
}
}
});
receiver.detachHandler(remoteDetached -> onRemoteDetach(receiver, con.getRemoteContainer(), false, closeHook));
receiver.closeHandler(remoteClosed -> onRemoteDetach(receiver, con.getRemoteContainer(), true, closeHook));
receiver.open();
});
return result;
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class AbstractHonoClientTest method testCreateReceiverFails.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void testCreateReceiverFails(final Supplier<ErrorCondition> errorSupplier, final Predicate<Throwable> failureAssertion) {
final Record attachments = new RecordImpl();
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.getRemoteCondition()).thenReturn(errorSupplier.get());
when(receiver.attachments()).thenReturn(attachments);
final ProtonConnection con = mock(ProtonConnection.class);
when(con.createReceiver(anyString())).thenReturn(receiver);
final Future<ProtonReceiver> result = AbstractHonoClient.createReceiver(context, props, con, "source", ProtonQoS.AT_LEAST_ONCE, (delivery, msg) -> {
}, null);
final ArgumentCaptor<Handler> openHandler = ArgumentCaptor.forClass(Handler.class);
verify(receiver).openHandler(openHandler.capture());
openHandler.getValue().handle(Future.failedFuture(new IllegalStateException()));
assertTrue(result.failed());
assertTrue(failureAssertion.test(result.cause()));
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class EventConsumerImplTest method testHandlerCallsCloseHook.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void testHandlerCallsCloseHook(final TestContext ctx, final BiConsumer<ProtonReceiver, ArgumentCaptor<Handler>> handlerCaptor) {
// GIVEN an open event consumer
final Async consumerCreation = ctx.async();
final BiConsumer<ProtonDelivery, Message> eventConsumer = mock(BiConsumer.class);
final RecordImpl attachments = new RecordImpl();
final Source source = mock(Source.class);
when(source.getAddress()).thenReturn("source/address");
final ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.isOpen()).thenReturn(Boolean.TRUE);
when(receiver.getSource()).thenReturn(source);
when(receiver.attachments()).thenReturn(attachments);
when(receiver.open()).then(answer -> {
attachments.set(EventConsumerImpl.KEY_LINK_ESTABLISHED, Boolean.class, Boolean.TRUE);
consumerCreation.complete();
return receiver;
});
final ProtonConnection con = mock(ProtonConnection.class);
when(con.createReceiver(anyString())).thenReturn(receiver);
final Handler<String> closeHook = mock(Handler.class);
final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);
EventConsumerImpl.create(vertx.getOrCreateContext(), new ClientConfigProperties(), con, "source/address", eventConsumer, ok -> {
}, closeHook);
consumerCreation.await();
handlerCaptor.accept(receiver, captor);
// WHEN the receiver link is closed
captor.getValue().handle(Future.succeededFuture(receiver));
// THEN the close hook is called
verify(closeHook).handle(any());
// and the receiver link is closed
verify(receiver).close();
}
use of io.vertx.proton.ProtonReceiver in project hono by eclipse.
the class EventEndpointTest method testOnLinkAttachDisconnectsClientsUsingWrongQos.
/**
* Verifies that the endpoint rejects a client's attempt to create a link using <em>AT_MOST_ONCE</em>
* delivery mode.
*/
@Test
public void testOnLinkAttachDisconnectsClientsUsingWrongQos() {
ProtonConnection con = mock(ProtonConnection.class);
ProtonReceiver receiver = mock(ProtonReceiver.class);
when(receiver.getRemoteQoS()).thenReturn(ProtonQoS.AT_MOST_ONCE);
ResourceIdentifier targetAddress = ResourceIdentifier.from("event", "tenant", null);
endpoint.onLinkAttach(con, receiver, targetAddress);
ArgumentCaptor<ErrorCondition> errorCondition = ArgumentCaptor.forClass(ErrorCondition.class);
verify(receiver).setCondition(errorCondition.capture());
assertThat(errorCondition.getValue(), is(ErrorConditions.ERROR_UNSUPPORTED_DELIVERY_MODE));
verify(receiver).close();
}
Aggregations