Search in sources :

Example 16 with MessageSender

use of org.eclipse.hono.client.MessageSender in project hono by eclipse.

the class AbstractStandaloneApiTest method testMessageUploadSucceedsForRegisteredDevice.

/**
 * Verifies that proper messages are forwarded downstream.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testMessageUploadSucceedsForRegisteredDevice(final TestContext ctx) {
    int count = 30;
    final Async messagesReceived = ctx.async(count);
    downstreamAdapter.setMessageConsumer(msg -> {
        messagesReceived.countDown();
        LOG.debug("received message [id: {}]", msg.getMessageId());
    });
    String registrationAssertion = getAssertion(Constants.DEFAULT_TENANT, DEVICE_1);
    LOG.debug("got registration assertion for device [{}]: {}", DEVICE_1, registrationAssertion);
    final AtomicReference<MessageSender> senderRef = new AtomicReference<>();
    final Async senderCreation = ctx.async();
    getSender(Constants.DEFAULT_TENANT).setHandler(ctx.asyncAssertSuccess(sender -> {
        senderRef.set(sender);
        senderCreation.complete();
    }));
    senderCreation.await();
    IntStream.range(0, count).forEach(i -> {
        Async waitForCredit = ctx.async();
        LOG.trace("sending message {}", i);
        senderRef.get().send(DEVICE_1, "payload" + i, "text/plain; charset=utf-8", registrationAssertion, done -> waitForCredit.complete());
        waitForCredit.await();
    });
}
Also used : IntStream(java.util.stream.IntStream) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) Future(io.vertx.core.Future) RegistrationAssertionHelper(org.eclipse.hono.service.registration.RegistrationAssertionHelper) TimeUnit(java.util.concurrent.TimeUnit) RegistrationAssertionHelperImpl(org.eclipse.hono.service.registration.RegistrationAssertionHelperImpl) Rule(org.junit.Rule) MessageSender(org.eclipse.hono.client.MessageSender) Timeout(org.junit.rules.Timeout) HonoClient(org.eclipse.hono.client.HonoClient) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) MessageSender(org.eclipse.hono.client.MessageSender) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 17 with MessageSender

use of org.eclipse.hono.client.MessageSender in project hono by eclipse.

the class TelemetrySenderImplTest method testSendMessageFailsOnLackOfCredit.

/**
 * Verifies that the sender fails if no credit is available.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testSendMessageFailsOnLackOfCredit(final TestContext ctx) {
    // GIVEN a sender that has no credit
    when(sender.sendQueueFull()).thenReturn(Boolean.TRUE);
    MessageSender messageSender = new TelemetrySenderImpl(config, sender, "tenant", "telemetry/tenant", context);
    // WHEN trying to send a message
    final Future<ProtonDelivery> result = messageSender.send("device", "some payload", "application/text", "token");
    // THEN the message is not sent
    assertFalse(result.succeeded());
    verify(sender, never()).send(any(Message.class), any(Handler.class));
}
Also used : ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) MessageSender(org.eclipse.hono.client.MessageSender) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

MessageSender (org.eclipse.hono.client.MessageSender)17 Message (org.apache.qpid.proton.message.Message)16 Test (org.junit.Test)11 Handler (io.vertx.core.Handler)8 ProtonDelivery (io.vertx.proton.ProtonDelivery)8 Future (io.vertx.core.Future)7 Vertx (io.vertx.core.Vertx)6 TestContext (io.vertx.ext.unit.TestContext)5 TimeUnit (java.util.concurrent.TimeUnit)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ClientErrorException (org.eclipse.hono.client.ClientErrorException)5 HonoClient (org.eclipse.hono.client.HonoClient)5 Constants (org.eclipse.hono.util.Constants)5 Buffer (io.vertx.core.buffer.Buffer)4 JsonObject (io.vertx.core.json.JsonObject)4 Async (io.vertx.ext.unit.Async)4 MqttPublishMessage (io.vertx.mqtt.messages.MqttPublishMessage)4 HttpURLConnection (java.net.HttpURLConnection)4 RegistrationClient (org.eclipse.hono.client.RegistrationClient)4 TenantObject (org.eclipse.hono.util.TenantObject)4