Search in sources :

Example 26 with ProtonReceiver

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");
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ProtonSender(io.vertx.proton.ProtonSender) HonoConnection(org.eclipse.hono.client.HonoConnection) Vertx(io.vertx.core.Vertx) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 27 with ProtonReceiver

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;
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) ClientErrorException(org.eclipse.hono.client.ClientErrorException)

Example 28 with ProtonReceiver

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()));
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ProtonConnection(io.vertx.proton.ProtonConnection) Handler(io.vertx.core.Handler) Record(org.apache.qpid.proton.engine.Record) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl)

Example 29 with ProtonReceiver

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();
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Handler(io.vertx.core.Handler) RecordImpl(org.apache.qpid.proton.engine.impl.RecordImpl) Source(org.apache.qpid.proton.amqp.transport.Source) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties)

Example 30 with ProtonReceiver

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();
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ProtonConnection(io.vertx.proton.ProtonConnection) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Test(org.junit.Test)

Aggregations

ProtonReceiver (io.vertx.proton.ProtonReceiver)45 ProtonConnection (io.vertx.proton.ProtonConnection)19 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)19 Vertx (io.vertx.core.Vertx)17 ProtonQoS (io.vertx.proton.ProtonQoS)16 Handler (io.vertx.core.Handler)13 ProtonSender (io.vertx.proton.ProtonSender)13 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)13 Message (org.apache.qpid.proton.message.Message)12 Future (io.vertx.core.Future)11 BeforeEach (org.junit.jupiter.api.BeforeEach)11 ProtonDelivery (io.vertx.proton.ProtonDelivery)10 HttpURLConnection (java.net.HttpURLConnection)10 ClientErrorException (org.eclipse.hono.client.ClientErrorException)10 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)10 Test (org.junit.jupiter.api.Test)10 AsyncResult (io.vertx.core.AsyncResult)9 Promise (io.vertx.core.Promise)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9