Search in sources :

Example 6 with ProtonDelivery

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

the class ForwardingEventDownstreamAdapterTest method testProcessMessageReleasesMessageIfNoCreditIsAvailable.

/**
 * Verifies that an event is released if no downstream credit is available.
 *
 * @param ctx The test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testProcessMessageReleasesMessageIfNoCreditIsAvailable(final TestContext ctx) {
    final UpstreamReceiver client = newClient();
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    when(delivery.remotelySettled()).thenReturn(Boolean.FALSE);
    // GIVEN an adapter with a connection to a downstream container
    ProtonSender sender = newMockSender(false);
    when(sender.sendQueueFull()).thenReturn(true);
    ForwardingEventDownstreamAdapter adapter = new ForwardingEventDownstreamAdapter(vertx, newMockSenderFactory(sender));
    adapter.setMetrics(mock(MessagingMetrics.class));
    adapter.setDownstreamConnectionFactory(newMockConnectionFactory(false));
    adapter.start(Future.future());
    adapter.addSender(client, sender);
    // WHEN processing an event
    Message msg = ProtonHelper.message(EVENT_MSG_CONTENT);
    MessageHelper.addDeviceId(msg, DEVICE_ID);
    adapter.processMessage(client, delivery, msg);
    // THEN the message is released
    verify(delivery).disposition(any(Released.class), eq(Boolean.TRUE));
    // and not delivered to the downstream container
    verify(sender, never()).send(any(Message.class), any(Handler.class));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) Released(org.apache.qpid.proton.amqp.messaging.Released) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) Handler(io.vertx.core.Handler) UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver) MessagingMetrics(org.eclipse.hono.messaging.MessagingMetrics) Test(org.junit.Test)

Example 7 with ProtonDelivery

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

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadTelemetryDoesNotWaitForAcceptedOutcome.

/**
 * Verifies that the adapter does not wait for a telemetry message being settled and accepted
 * by a downstream peer before responding with a 202 status to the device.
 */
@Test
public void testUploadTelemetryDoesNotWaitForAcceptedOutcome() {
    // GIVEN an adapter with a downstream telemetry consumer attached
    final Future<ProtonDelivery> outcome = Future.succeededFuture(mock(ProtonDelivery.class));
    givenATelemetrySenderForOutcome(outcome);
    HttpServer server = getHttpServer(false);
    AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null);
    // WHEN a device publishes a telemetry message
    final Buffer payload = Buffer.buffer("some payload");
    final HttpServerResponse response = mock(HttpServerResponse.class);
    final RoutingContext ctx = newRoutingContext(payload, response);
    adapter.uploadTelemetryMessage(ctx, "tenant", "device", payload, "application/text");
    // THEN the device receives a 202 response immediately
    verify(response).setStatusCode(202);
    verify(response).end();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RoutingContext(io.vertx.ext.web.RoutingContext) ProtonDelivery(io.vertx.proton.ProtonDelivery) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpServer(io.vertx.core.http.HttpServer) Test(org.junit.Test)

Example 8 with ProtonDelivery

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

the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadQoS1MessageSendsPubAckOnSuccess.

private void testUploadQoS1MessageSendsPubAckOnSuccess(final Future<ProtonDelivery> outcome, final BiConsumer<AbstractVertxBasedMqttProtocolAdapter<?>, MqttContext> upload) {
    // GIVEN an adapter with a downstream event consumer
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    // WHEN a device publishes an event
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
    final Buffer payload = Buffer.buffer("some payload");
    final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
    when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
    when(messageFromDevice.messageId()).thenReturn(5555555);
    when(messageFromDevice.payload()).thenReturn(payload);
    final MqttContext context = new MqttContext(messageFromDevice, endpoint);
    upload.accept(adapter, context);
    // THEN the device does not receive a PUBACK
    verify(endpoint, never()).publishAcknowledge(anyInt());
    // until the event has been settled and accepted
    outcome.complete(mock(ProtonDelivery.class));
    verify(endpoint).publishAcknowledge(5555555);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ProtonDelivery(io.vertx.proton.ProtonDelivery) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttServer(io.vertx.mqtt.MqttServer)

Example 9 with ProtonDelivery

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

the class AbstractVertxBasedMqttProtocolAdapterTest method testOnUnauthenticatedMessageDoesNotSendPubAckOnFailure.

/**
 * Verifies that the adapter does not send a PUBACK package to the device if
 * an event message has not been accepted by the peer.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testOnUnauthenticatedMessageDoesNotSendPubAckOnFailure(final TestContext ctx) {
    // GIVEN an adapter with a downstream event consumer
    final Future<ProtonDelivery> outcome = Future.future();
    givenAnEventSenderForOutcome(outcome);
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);
    // WHEN a device publishes an event
    final Buffer payload = Buffer.buffer("some payload");
    final MqttEndpoint endpoint = mock(MqttEndpoint.class);
    when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
    final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
    when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
    when(messageFromDevice.messageId()).thenReturn(5555555);
    final MqttContext context = new MqttContext(messageFromDevice, endpoint);
    adapter.uploadEventMessage(context, "my-tenant", "4712", payload).setHandler(ctx.asyncAssertFailure());
    // and the peer rejects the message
    outcome.fail(new ClientErrorException(HttpURLConnection.HTTP_BAD_REQUEST));
    // THEN the device has not received a PUBACK
    verify(endpoint, never()).publishAcknowledge(anyInt());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) ProtonDelivery(io.vertx.proton.ProtonDelivery) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) MqttServer(io.vertx.mqtt.MqttServer) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Test(org.junit.Test)

Example 10 with ProtonDelivery

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

the class AbstractSender method send.

@Override
public final Future<ProtonDelivery> send(final Message rawMessage) {
    Objects.requireNonNull(rawMessage);
    final Future<ProtonDelivery> result = Future.future();
    context.runOnContext(send -> {
        if (sender.sendQueueFull()) {
            result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "no credit available"));
        } else {
            sendMessage(rawMessage).setHandler(result.completer());
        }
    });
    return result;
}
Also used : ProtonDelivery(io.vertx.proton.ProtonDelivery) ServerErrorException(org.eclipse.hono.client.ServerErrorException)

Aggregations

ProtonDelivery (io.vertx.proton.ProtonDelivery)38 Test (org.junit.Test)31 Message (org.apache.qpid.proton.message.Message)29 Handler (io.vertx.core.Handler)21 ProtonSender (io.vertx.proton.ProtonSender)15 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)15 ProtonReceiver (io.vertx.proton.ProtonReceiver)13 Async (io.vertx.ext.unit.Async)12 Vertx (io.vertx.core.Vertx)11 ProtonHelper (io.vertx.proton.ProtonHelper)11 JsonObject (io.vertx.core.json.JsonObject)10 TestContext (io.vertx.ext.unit.TestContext)10 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)10 Before (org.junit.Before)10 Rule (org.junit.Rule)10 RunWith (org.junit.runner.RunWith)10 ArgumentCaptor (org.mockito.ArgumentCaptor)10 Mockito (org.mockito.Mockito)10 Context (io.vertx.core.Context)9 HttpURLConnection (java.net.HttpURLConnection)9