use of org.eclipse.hono.util.TenantObject in project hono by eclipse.
the class HttpTestBase method testUploadMessages.
/**
* Verifies that a number of messages uploaded to Hono's HTTP adapter can be successfully
* consumed via the AMQP Messaging Network.
*
* @param ctx The test context.
* @throws InterruptedException if the test fails.
*/
@Test
public void testUploadMessages(final TestContext ctx) throws InterruptedException {
final int messagesToSend = 100;
final CountDownLatch received = new CountDownLatch(messagesToSend);
final Async setup = ctx.async();
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final TenantObject tenant = TenantObject.from(tenantId, true);
helper.registry.addTenant(JsonObject.mapFrom(tenant)).compose(ok -> helper.registry.registerDevice(tenantId, deviceId)).compose(ok -> createConsumer(tenantId, msg -> {
LOGGER.trace("received {}", msg);
assertMessageProperties(ctx, msg);
assertAdditionalMessageProperties(ctx, msg);
received.countDown();
if (received.getCount() % 20 == 0) {
LOGGER.info("messages received: {}", messagesToSend - received.getCount());
}
})).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
setup.await();
final long start = System.currentTimeMillis();
final AtomicInteger messageCount = new AtomicInteger(0);
while (messageCount.get() < messagesToSend) {
final Async sending = ctx.async();
send(tenantId, deviceId, Buffer.buffer("hello " + messageCount.getAndIncrement())).setHandler(attempt -> {
if (attempt.succeeded()) {
LOGGER.debug("sent message {} [status code: 202]", messageCount.get());
} else {
LOGGER.debug("sent message {} [status code: {}]", messageCount.get(), ((ServiceInvocationException) attempt.cause()).getErrorCode());
}
sending.complete();
});
if (messageCount.get() % 20 == 0) {
LOGGER.info("messages sent: " + messageCount.get());
}
sending.await();
}
long timeToWait = Math.max(TEST_TIMEOUT - 1000, Math.round(messagesToSend * 1.2));
if (!received.await(timeToWait, TimeUnit.MILLISECONDS)) {
LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
ctx.fail("did not receive all messages sent");
} else {
LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
}
}
use of org.eclipse.hono.util.TenantObject in project hono by eclipse.
the class MqttTestBase method doTestUploadMessages.
/**
* Verifies that a number of messages uploaded to Hono's HTTP adapter can be successfully
* consumed via the AMQP Messaging Network.
*
* @param ctx The test context.
* @param useShortTopicName Whether to use standard or short topic names
* @throws InterruptedException if the test fails.
*/
public void doTestUploadMessages(final TestContext ctx, boolean useShortTopicName) throws InterruptedException {
final int messagesToSend = 200;
final CountDownLatch received = new CountDownLatch(messagesToSend);
final Async setup = ctx.async();
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final TenantObject tenant = TenantObject.from(tenantId, true);
helper.registry.addDeviceForTenant(tenant, deviceId, password).compose(ok -> createConsumer(tenantId, msg -> {
LOGGER.trace("received {}", msg);
assertMessageProperties(ctx, msg);
assertAdditionalMessageProperties(ctx, msg);
received.countDown();
if (received.getCount() % 40 == 0) {
LOGGER.info("messages received: {}", messagesToSend - received.getCount());
}
})).compose(ok -> {
final Future<MqttConnAckMessage> result = Future.future();
final MqttClientOptions options = new MqttClientOptions().setMaxInflightQueue(200).setUsername(IntegrationTestSupport.getUsername(deviceId, tenantId)).setPassword(password);
mqttClient = MqttClient.create(VERTX, options);
mqttClient.connect(IntegrationTestSupport.MQTT_PORT, IntegrationTestSupport.MQTT_HOST, result.completer());
return result;
}).setHandler(ctx.asyncAssertSuccess(ok -> setup.complete()));
setup.await();
final long start = System.currentTimeMillis();
final AtomicInteger messageCount = new AtomicInteger(0);
final AtomicReference<Async> sendResult = new AtomicReference<>();
mqttClient.publishCompletionHandler(packetId -> {
if (pendingMessages.remove(packetId)) {
sendResult.get().complete();
} else {
LOGGER.info("received PUBACK for unexpected message [id: {}]", packetId);
}
});
while (messageCount.get() < messagesToSend) {
sendResult.set(ctx.async());
send(tenantId, deviceId, Buffer.buffer("hello " + messageCount.getAndIncrement()), useShortTopicName, sendAttempt -> {
if (sendAttempt.failed()) {
LOGGER.debug("error sending message {}", messageCount.get(), sendAttempt.cause());
} else {
pendingMessages.add(sendAttempt.result());
}
});
if (messageCount.get() % 40 == 0) {
LOGGER.info("messages sent: " + messageCount.get());
}
sendResult.get().await();
}
long timeToWait = Math.max(TEST_TIMEOUT - 1000, Math.round(messagesToSend * 1.2));
if (!received.await(timeToWait, TimeUnit.MILLISECONDS)) {
LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
ctx.fail("did not receive all messages sent");
} else {
LOGGER.info("sent {} and received {} messages after {} milliseconds", messageCount, messagesToSend - received.getCount(), System.currentTimeMillis() - start);
}
}
use of org.eclipse.hono.util.TenantObject in project hono by eclipse.
the class TenantClientImplTest method testGetTenantReturnsValueFromCache.
/**
* Verifies that tenant information is taken from cache if cache is configured and the cache has this tenant
* information cached.
*
* @param ctx The vert.x test context.
*/
@Test
public void testGetTenantReturnsValueFromCache(final TestContext ctx) {
// GIVEN an adapter with a cache containing a tenant
client.setResponseCache(cache);
final JsonObject tenantJsonObject = newTenantResult("tenant");
final TenantResult<TenantObject> tenantResult = client.getResult(HttpURLConnection.HTTP_OK, tenantJsonObject.toString(), null);
when(cache.get(any(TriTuple.class))).thenReturn(tenantResult);
// WHEN getting tenant information
client.get("tenant").setHandler(ctx.asyncAssertSuccess(result -> {
// THEN the tenant information is read from the cache
ctx.assertEquals(tenantResult.getPayload(), result);
verify(sender, never()).send(any(Message.class));
}));
}
Aggregations