Search in sources :

Example 21 with Async

use of io.vertx.ext.unit.Async in project vertx-camel-bridge by vert-x3.

the class InboundEndpointTest method testWithDirectEndpointWithPublish.

@Test
public void testWithDirectEndpointWithPublish(TestContext context) throws Exception {
    Async async = context.async();
    Async async2 = context.async();
    Endpoint endpoint = camel.getEndpoint("direct:foo");
    bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel).addInboundMapping(fromCamel(endpoint).toVertx("test").usePublish()));
    vertx.eventBus().consumer("test", message -> {
        context.assertEquals("hello", message.body());
        async.complete();
    });
    vertx.eventBus().consumer("test", message -> {
        context.assertEquals("hello", message.body());
        async2.complete();
    });
    camel.start();
    BridgeHelper.startBlocking(bridge);
    ProducerTemplate producer = camel.createProducerTemplate();
    producer.asyncSendBody(endpoint, "hello");
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) Endpoint(org.apache.camel.Endpoint) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 22 with Async

use of io.vertx.ext.unit.Async in project vertx-camel-bridge by vert-x3.

the class InboundEndpointTest method testWithDirectEndpointWithHeaderCopy.

@Test
public void testWithDirectEndpointWithHeaderCopy(TestContext context) throws Exception {
    Async async = context.async();
    Endpoint endpoint = camel.getEndpoint("direct:foo");
    bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel).addInboundMapping(fromCamel("direct:foo").toVertx("test")));
    vertx.eventBus().consumer("test", message -> {
        context.assertEquals("hello", message.body());
        context.assertEquals(message.headers().get("key"), "value");
        async.complete();
    });
    camel.start();
    BridgeHelper.startBlocking(bridge);
    ProducerTemplate producer = camel.createProducerTemplate();
    producer.asyncSend(endpoint, exchange -> {
        Message message = exchange.getIn();
        message.setBody("hello");
        message.setHeader("key", "value");
    });
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) Endpoint(org.apache.camel.Endpoint) Message(org.apache.camel.Message) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 23 with Async

use of io.vertx.ext.unit.Async in project hono by eclipse.

the class IntegrationTestSupport method deleteObjects.

/**
 * Deletes all temporary objects from the Device Registry which
 * have been created during the last test execution.
 *
 * @param ctx The vert.x context.
 */
public void deleteObjects(final TestContext ctx) {
    devicesToDelete.forEach((tenantId, devices) -> {
        devices.forEach(deviceId -> {
            final Async deletion = ctx.async();
            CompositeFuture.join(registry.deregisterDevice(tenantId, deviceId), registry.removeAllCredentials(tenantId, deviceId)).setHandler(ok -> deletion.complete());
            deletion.await();
        });
    });
    devicesToDelete.clear();
    tenantsToDelete.forEach(tenantId -> {
        final Async deletion = ctx.async();
        registry.removeTenant(tenantId).setHandler(ok -> deletion.complete());
        deletion.await();
    });
    tenantsToDelete.clear();
}
Also used : Async(io.vertx.ext.unit.Async)

Example 24 with Async

use of io.vertx.ext.unit.Async in project hono by eclipse.

the class ClientTestBase method testSendingMessages.

/**
 * Verifies that a number of messages uploaded to Hono's Telemetry or Event API can be successfully
 * consumed via the AMQP Messaging Network.
 *
 * @param ctx The test context.
 * @throws InterruptedException if test execution is interrupted.
 */
@Test
public void testSendingMessages(final TestContext ctx) throws InterruptedException {
    final CountDownLatch received = new CountDownLatch(IntegrationTestSupport.MSG_COUNT);
    final Async setup = ctx.async();
    final AtomicReference<String> registrationAssertion = new AtomicReference<>();
    registrationClient.register(DEVICE_ID, null).compose(ok -> registrationClient.assertRegistration(DEVICE_ID)).compose(result -> {
        registrationAssertion.set(result.getString(RegistrationConstants.FIELD_ASSERTION));
        return createConsumer(TEST_TENANT_ID, msg -> {
            LOGGER.trace("received {}", msg);
            assertMessageProperties(ctx, msg);
            assertAdditionalMessageProperties(ctx, msg);
            received.countDown();
            if (received.getCount() % 200 == 0) {
                LOGGER.info("messages received: {}", IntegrationTestSupport.MSG_COUNT - received.getCount());
            }
        });
    }).map(c -> {
        consumer = c;
        setup.complete();
        return null;
    });
    setup.await(1000);
    long start = System.currentTimeMillis();
    final AtomicInteger messagesSent = new AtomicInteger();
    while (messagesSent.get() < IntegrationTestSupport.MSG_COUNT) {
        final Async sending = ctx.async();
        sender.send(DEVICE_ID, "payload_" + messagesSent.getAndIncrement(), CONTENT_TYPE_TEXT_PLAIN, registrationAssertion.get(), creditAvailable -> sending.complete());
        if (messagesSent.get() % 200 == 0) {
            LOGGER.info("messages sent: " + messagesSent.get());
        }
        sending.await();
    }
    long timeToWait = Math.max(DEFAULT_TEST_TIMEOUT, Math.round(IntegrationTestSupport.MSG_COUNT * 1.2));
    if (!received.await(timeToWait, TimeUnit.MILLISECONDS)) {
        LOGGER.info("sent {} and received {} messages after {} milliseconds", messagesSent.get(), IntegrationTestSupport.MSG_COUNT - received.getCount(), System.currentTimeMillis() - start);
        ctx.fail("did not receive all messages sent");
    } else {
        LOGGER.info("sent {} and received {} messages after {} milliseconds", messagesSent.get(), IntegrationTestSupport.MSG_COUNT - received.getCount(), System.currentTimeMillis() - start);
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) LoggerFactory(org.slf4j.LoggerFactory) MessageConsumer(org.eclipse.hono.client.MessageConsumer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) CompositeFuture(io.vertx.core.CompositeFuture) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConnectionFactoryBuilder(org.eclipse.hono.connection.ConnectionFactoryImpl.ConnectionFactoryBuilder) MessageSender(org.eclipse.hono.client.MessageSender) After(org.junit.After) Message(org.apache.qpid.proton.message.Message) RegistrationClient(org.eclipse.hono.client.RegistrationClient) HonoClient(org.eclipse.hono.client.HonoClient) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) Before(org.junit.Before) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) Test(org.junit.Test) HonoClientImpl(org.eclipse.hono.client.impl.HonoClientImpl) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Async(io.vertx.ext.unit.Async) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 25 with Async

use of io.vertx.ext.unit.Async in project hono by eclipse.

the class ClientTestBase method deregister.

/**
 * Deregisters the test device (DEVICE_ID) and disconnects from
 * the Hono server, Hono device registry and AMQP messaging network.
 *
 * @param ctx The test context
 */
@After
public void deregister(final TestContext ctx) {
    if (registrationClient != null) {
        final Async done = ctx.async();
        LOGGER.debug("deregistering devices");
        registrationClient.deregister(DEVICE_ID).setHandler(r -> done.complete());
        done.await(2000);
    }
    disconnect(ctx);
}
Also used : Async(io.vertx.ext.unit.Async) After(org.junit.After)

Aggregations

Async (io.vertx.ext.unit.Async)156 Test (org.junit.Test)143 TestContext (io.vertx.ext.unit.TestContext)89 Handler (io.vertx.core.Handler)87 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)83 RunWith (org.junit.runner.RunWith)83 Future (io.vertx.core.Future)70 Vertx (io.vertx.core.Vertx)69 Before (org.junit.Before)69 JsonObject (io.vertx.core.json.JsonObject)65 HttpURLConnection (java.net.HttpURLConnection)55 Mockito (org.mockito.Mockito)55 Context (io.vertx.core.Context)44 Message (org.apache.qpid.proton.message.Message)44 Constants (org.eclipse.hono.util.Constants)44 Rule (org.junit.Rule)44 Timeout (org.junit.rules.Timeout)43 Assert.assertThat (org.junit.Assert.assertThat)40 Buffer (io.vertx.core.buffer.Buffer)39 AsyncResult (io.vertx.core.AsyncResult)37