use of org.eclipse.hono.util.QoS in project hono by eclipse.
the class KafkaRecordHelperTest method testGetQoS.
/**
* Verifies that {@link KafkaRecordHelper#getQoS(List)} returns the QoS.
*/
@Test
public void testGetQoS() {
final QoS qos = QoS.AT_MOST_ONCE;
headers.add(KafkaRecordHelper.createKafkaHeader("qos", qos.ordinal()));
assertThat(KafkaRecordHelper.getQoS(headers)).isEqualTo(Optional.of(qos));
}
use of org.eclipse.hono.util.QoS in project hono by eclipse.
the class CoapTestBase method testUploadMessages.
/**
* Uploads messages to the CoAP endpoint.
*
* @param ctx The test context to run on.
* @param tenantId The tenant that the device belongs to.
* @param warmUp A sender of messages used to warm up the adapter before running the test itself or {@code null} if
* no warm up should be performed.
* @param messageConsumer Consumer that is invoked when a message was received.
* @param requestSender The test device that will publish the data.
* @param numberOfMessages The number of messages that are uploaded.
* @param expectedQos The expected QoS level, may be {@code null} leading to expecting the default for event or telemetry.
* @throws InterruptedException if the test is interrupted before it has finished.
*/
protected void testUploadMessages(final VertxTestContext ctx, final String tenantId, final Supplier<Future<Void>> warmUp, final Consumer<DownstreamMessage<? extends MessageContext>> messageConsumer, final Function<Integer, Future<OptionSet>> requestSender, final int numberOfMessages, final QoS expectedQos) throws InterruptedException {
final CountDownLatch received = new CountDownLatch(numberOfMessages);
final VertxTestContext setup = new VertxTestContext();
createConsumer(tenantId, msg -> {
ctx.verify(() -> {
logger.trace("received {}", msg);
DownstreamMessageAssertions.assertTelemetryMessageProperties(msg, tenantId);
assertThat(msg.getQos()).isEqualTo(getExpectedQoS(expectedQos));
assertAdditionalMessageProperties(msg);
if (messageConsumer != null) {
messageConsumer.accept(msg);
}
});
received.countDown();
if (received.getCount() % 20 == 0) {
logger.info("messages received: {}", numberOfMessages - received.getCount());
}
}).compose(ok -> Optional.ofNullable(warmUp).map(w -> w.get()).orElseGet(() -> Future.succeededFuture())).onComplete(setup.succeedingThenComplete());
ctx.verify(() -> assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue());
final long start = System.currentTimeMillis();
final AtomicInteger messageCount = new AtomicInteger(0);
while (messageCount.get() < numberOfMessages && !ctx.failed()) {
final CountDownLatch sending = new CountDownLatch(1);
requestSender.apply(messageCount.getAndIncrement()).compose(this::assertCoapResponse).onComplete(attempt -> {
if (attempt.succeeded()) {
logger.debug("sent message {}", messageCount.get());
} else {
logger.info("failed to send message {}: {}", messageCount.get(), attempt.cause().getMessage());
ctx.failNow(attempt.cause());
}
sending.countDown();
});
if (messageCount.get() % 20 == 0) {
logger.info("messages sent: {}", messageCount.get());
}
sending.await();
}
if (ctx.failed()) {
return;
}
final long timeToWait = Math.max(TEST_TIMEOUT_MILLIS - 1000, Math.round(numberOfMessages * 20));
if (received.await(timeToWait, TimeUnit.MILLISECONDS)) {
logger.info("sent {} and received {} messages after {} milliseconds", messageCount, numberOfMessages - received.getCount(), System.currentTimeMillis() - start);
ctx.completeNow();
} else {
logger.info("sent {} and received {} messages after {} milliseconds", messageCount, numberOfMessages - received.getCount(), System.currentTimeMillis() - start);
ctx.failNow(new AssertionError("did not receive all messages sent"));
}
}
use of org.eclipse.hono.util.QoS in project hono by eclipse.
the class HttpTestBase method testUploadMessages.
/**
* Uploads messages to the HTTP endpoint.
*
* @param ctx The test context to run on.
* @param tenantId The tenant that the device belongs to.
* @param messageConsumer Consumer that is invoked when a message was received.
* @param requestSender The test device that will publish the data.
* @param numberOfMessages The number of messages that are uploaded.
* @param expectedQos The expected QoS level, may be {@code null} leading to expecting the default for event or telemetry.
* @throws InterruptedException if the test is interrupted before it has finished.
*/
protected void testUploadMessages(final VertxTestContext ctx, final String tenantId, final Function<DownstreamMessage<? extends MessageContext>, Future<Void>> messageConsumer, final Function<Integer, Future<HttpResponse<Buffer>>> requestSender, final int numberOfMessages, final QoS expectedQos) throws InterruptedException {
final VertxTestContext messageSending = new VertxTestContext();
final Checkpoint messageSent = messageSending.checkpoint(numberOfMessages);
final Checkpoint messageReceived = messageSending.laxCheckpoint(numberOfMessages);
final AtomicInteger receivedMessageCount = new AtomicInteger(0);
final VertxTestContext setup = new VertxTestContext();
createConsumer(tenantId, msg -> {
logger.trace("received {}", msg);
ctx.verify(() -> {
DownstreamMessageAssertions.assertTelemetryMessageProperties(msg, tenantId);
assertThat(msg.getQos()).isEqualTo(getExpectedQoS(expectedQos));
assertAdditionalMessageProperties(msg);
});
Optional.ofNullable(messageConsumer).map(consumer -> consumer.apply(msg)).orElseGet(() -> Future.succeededFuture()).onComplete(attempt -> {
if (attempt.succeeded()) {
receivedMessageCount.incrementAndGet();
messageReceived.flag();
} else {
logger.error("failed to process message from device", attempt.cause());
messageSending.failNow(attempt.cause());
}
});
if (receivedMessageCount.get() % 20 == 0) {
logger.info("messages received: {}", receivedMessageCount.get());
}
}).onComplete(setup.succeedingThenComplete());
assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
if (setup.failed()) {
ctx.failNow(setup.causeOfFailure());
return;
}
final long start = System.currentTimeMillis();
int messageCount = 0;
while (messageCount < numberOfMessages && !messageSending.failed()) {
messageCount++;
final int currentMessage = messageCount;
final CountDownLatch sending = new CountDownLatch(1);
requestSender.apply(currentMessage).compose(this::assertHttpResponse).onComplete(attempt -> {
try {
if (attempt.succeeded()) {
logger.debug("sent message {}", currentMessage);
messageSent.flag();
} else {
logger.info("failed to send message {}: {}", currentMessage, attempt.cause().getMessage());
messageSending.failNow(attempt.cause());
}
} finally {
sending.countDown();
}
});
sending.await();
if (currentMessage % 20 == 0) {
logger.info("messages sent: " + currentMessage);
}
}
final long timeToWait = Math.max(TEST_TIMEOUT_MILLIS - 50 - (System.currentTimeMillis() - testStartTimeMillis), 1);
assertThat(messageSending.awaitCompletion(timeToWait, TimeUnit.MILLISECONDS)).isTrue();
if (messageSending.failed()) {
logger.error("test execution failed", messageSending.causeOfFailure());
ctx.failNow(messageSending.causeOfFailure());
} else {
logger.info("successfully sent {} and received {} messages after {} milliseconds", messageCount, receivedMessageCount.get(), System.currentTimeMillis() - start);
ctx.completeNow();
}
}
use of org.eclipse.hono.util.QoS in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadQoS1MessageSendsPubAckOnSuccess.
private void testUploadQoS1MessageSendsPubAckOnSuccess(final Promise<Void> outcome, final EndpointType type, final BiConsumer<AbstractVertxBasedMqttProtocolAdapter<?>, MqttContext> upload) {
// WHEN a device publishes a message using QoS 1
final MqttEndpoint endpoint = mockEndpoint();
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);
when(messageFromDevice.topicName()).thenReturn(String.format("%s/my-tenant/4712", type.getCanonicalName()));
final MqttContext context = newMqttContext(messageFromDevice, endpoint, span);
upload.accept(adapter, context);
// THEN the device does not receive a PUBACK
verify(endpoint, never()).publishAcknowledge(anyInt());
// and the message has not been reported as forwarded
verify(metrics, never()).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), any(MetricsTags.QoS.class), anyInt(), any());
// until the message has been settled and accepted
outcome.complete();
verify(endpoint).publishAcknowledge(5555555);
verify(metrics).reportTelemetry(eq(type), eq("my-tenant"), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), any());
}
use of org.eclipse.hono.util.QoS in project hono by eclipse.
the class KafkaBasedTelemetrySenderTest method testThatSendTelemetryThrowsOnMissingMandatoryParameter.
/**
* Verifies that
* {@link KafkaBasedTelemetrySender#sendTelemetry(TenantObject, RegistrationAssertion, QoS, String, Buffer, Map, io.opentracing.SpanContext)}
* throws an NPE if a mandatory parameter is {@code null}.
*/
@Test
public void testThatSendTelemetryThrowsOnMissingMandatoryParameter() {
final QoS qos = QoS.AT_LEAST_ONCE;
final MockProducer<String, Buffer> mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
final CachingKafkaProducerFactory<String, Buffer> factory = CachingKafkaProducerFactory.testFactory(vertxMock, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
final Tracer tracer = NoopTracerFactory.create();
final KafkaBasedTelemetrySender sender = new KafkaBasedTelemetrySender(vertxMock, factory, kafkaProducerConfig, true, tracer);
assertThrows(NullPointerException.class, () -> sender.sendTelemetry(null, device, qos, "the-content-type", null, null, null));
assertThrows(NullPointerException.class, () -> sender.sendTelemetry(tenant, null, qos, "the-content-type", null, null, null));
assertThrows(NullPointerException.class, () -> sender.sendTelemetry(tenant, device, null, "the-content-type", null, null, null));
}
Aggregations