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