Search in sources :

Example 6 with MessageConsumer

use of io.vertx.core.eventbus.MessageConsumer in project vert.x by eclipse.

the class MetricsTest method testBroadcastMessage.

private void testBroadcastMessage(Vertx from, Vertx[] to, boolean publish, SentMessage... expected) {
    FakeEventBusMetrics eventBusMetrics = FakeMetricsBase.getMetrics(from.eventBus());
    AtomicInteger broadcastCount = new AtomicInteger();
    AtomicInteger receiveCount = new AtomicInteger();
    for (Vertx vertx : to) {
        MessageConsumer<Object> consumer = vertx.eventBus().consumer(ADDRESS1);
        consumer.completionHandler(onSuccess(v -> {
            if (broadcastCount.incrementAndGet() == to.length) {
                String msg = TestUtils.randomAlphaString(10);
                if (publish) {
                    from.eventBus().publish(ADDRESS1, msg);
                } else {
                    from.eventBus().send(ADDRESS1, msg);
                }
            }
        }));
        consumer.handler(msg -> {
            if (receiveCount.incrementAndGet() == to.length) {
                testComplete();
            }
        });
    }
    waitUntil(() -> eventBusMetrics.getSentMessages().size() == expected.length);
    assertEquals(new HashSet<>(Arrays.asList(expected)), new HashSet<>(eventBusMetrics.getSentMessages()));
    await();
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) java.util(java.util) io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventBus(io.vertx.core.eventbus.EventBus) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) Is.is(org.hamcrest.core.Is.is) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) NetClient(io.vertx.core.net.NetClient) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) io.vertx.test.fakemetrics(io.vertx.test.fakemetrics) EventLoopGroup(io.netty.channel.EventLoopGroup) VertxInternal(io.vertx.core.impl.VertxInternal) Trust(io.vertx.test.tls.Trust) Test(org.junit.Test) EventLoop(io.netty.channel.EventLoop) Collectors(java.util.stream.Collectors) io.vertx.core.http(io.vertx.core.http) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocket(io.vertx.core.net.NetSocket) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 7 with MessageConsumer

use of io.vertx.core.eventbus.MessageConsumer in project vert.x by eclipse.

the class MetricsTest method testReply.

private void testReply(Vertx from, Vertx to, boolean expectedLocal, boolean expectedRemote) throws Exception {
    FakeEventBusMetrics fromMetrics = FakeMetricsBase.getMetrics(from.eventBus());
    FakeEventBusMetrics toMetrics = FakeMetricsBase.getMetrics(to.eventBus());
    MessageConsumer<Object> consumer = to.eventBus().consumer(ADDRESS1);
    CountDownLatch latch = new CountDownLatch(1);
    consumer.completionHandler(onSuccess(v -> {
        String msg = TestUtils.randomAlphaString(10);
        from.eventBus().request(ADDRESS1, msg, reply -> {
            latch.countDown();
        });
    }));
    consumer.handler(msg -> {
        toMetrics.getReceivedMessages().clear();
        toMetrics.getSentMessages().clear();
        msg.reply(TestUtils.randomAlphaString(10));
    });
    awaitLatch(latch);
    assertWaitUntil(() -> fromMetrics.getReceivedMessages().size() > 0);
    ReceivedMessage receivedMessage = fromMetrics.getReceivedMessages().get(0);
    assertEquals(false, receivedMessage.publish);
    assertEquals(expectedLocal, receivedMessage.local);
    assertEquals(1, receivedMessage.handlers);
    assertWaitUntil(() -> toMetrics.getSentMessages().size() > 0);
    SentMessage sentMessage = toMetrics.getSentMessages().get(0);
    assertEquals(false, sentMessage.publish);
    assertEquals(expectedLocal, sentMessage.local);
    assertEquals(expectedRemote, sentMessage.remote);
    assertEquals(sentMessage.address, receivedMessage.address);
}
Also used : DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) java.util(java.util) io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) ContextInternal(io.vertx.core.impl.ContextInternal) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventBus(io.vertx.core.eventbus.EventBus) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) Is.is(org.hamcrest.core.Is.is) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) NetClient(io.vertx.core.net.NetClient) JdkSSLEngineOptions(io.vertx.core.net.JdkSSLEngineOptions) io.vertx.test.fakemetrics(io.vertx.test.fakemetrics) EventLoopGroup(io.netty.channel.EventLoopGroup) VertxInternal(io.vertx.core.impl.VertxInternal) Trust(io.vertx.test.tls.Trust) Test(org.junit.Test) EventLoop(io.netty.channel.EventLoop) Collectors(java.util.stream.Collectors) io.vertx.core.http(io.vertx.core.http) NetClientOptions(io.vertx.core.net.NetClientOptions) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) NetSocket(io.vertx.core.net.NetSocket) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with MessageConsumer

use of io.vertx.core.eventbus.MessageConsumer in project vert.x by eclipse.

the class MetricsContextTest method testMessageHandler.

private void testMessageHandler(BiConsumer<Vertx, Handler<Void>> runOnContext) {
    AtomicReference<Thread> scheduleThread = new AtomicReference<>();
    AtomicReference<Thread> deliveredThread = new AtomicReference<>();
    AtomicBoolean registeredCalled = new AtomicBoolean();
    AtomicBoolean unregisteredCalled = new AtomicBoolean();
    AtomicBoolean messageDelivered = new AtomicBoolean();
    VertxMetricsFactory factory = (options) -> new DummyVertxMetrics() {

        @Override
        public EventBusMetrics createEventBusMetrics() {
            return new DummyEventBusMetrics() {

                @Override
                public Void handlerRegistered(String address, String repliedAddress) {
                    registeredCalled.set(true);
                    return null;
                }

                @Override
                public void handlerUnregistered(Void handler) {
                    unregisteredCalled.set(true);
                }

                @Override
                public void scheduleMessage(Void handler, boolean local) {
                    scheduleThread.set(Thread.currentThread());
                }

                @Override
                public void messageDelivered(Void handler, boolean local) {
                    deliveredThread.set(Thread.currentThread());
                }
            };
        }
    };
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    EventBus eb = vertx.eventBus();
    Thread t = new Thread(() -> {
        eb.send("the_address", "the_msg");
    });
    runOnContext.accept(vertx, v -> {
        MessageConsumer<Object> consumer = eb.consumer("the_address");
        consumer.handler(msg -> {
            Thread consumerThread = Thread.currentThread();
            executeInVanillaThread(() -> {
                vertx.getOrCreateContext().runOnContext(v2 -> {
                    consumer.unregister(onSuccess(v3 -> {
                        assertTrue(registeredCalled.get());
                        assertSame(t, scheduleThread.get());
                        assertSame(consumerThread, deliveredThread.get());
                        assertWaitUntil(() -> unregisteredCalled.get());
                        testComplete();
                    }));
                });
            });
        }).completionHandler(onSuccess(v2 -> {
            t.start();
        }));
    });
    await();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) HttpResponse(io.vertx.core.spi.observability.HttpResponse) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) HttpRequest(io.vertx.core.spi.observability.HttpRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) EventBusOptions(io.vertx.core.eventbus.EventBusOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) EventBus(io.vertx.core.eventbus.EventBus) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 9 with MessageConsumer

use of io.vertx.core.eventbus.MessageConsumer in project vert.x by eclipse.

the class EventBusTestBase method testSendWhileUnsubscribing.

@Test
public void testSendWhileUnsubscribing() throws Exception {
    startNodes(2);
    AtomicBoolean unregistered = new AtomicBoolean();
    Verticle sender = new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            getVertx().runOnContext(v -> sendMsg());
        }

        private void sendMsg() {
            if (!unregistered.get()) {
                getVertx().eventBus().send("whatever", "marseille");
                burnCpu();
                getVertx().runOnContext(v -> sendMsg());
            } else {
                getVertx().eventBus().send("whatever", "marseille", ar -> {
                    Throwable cause = ar.cause();
                    assertThat(cause, instanceOf(ReplyException.class));
                    ReplyException replyException = (ReplyException) cause;
                    assertEquals(ReplyFailure.NO_HANDLERS, replyException.failureType());
                    testComplete();
                });
            }
        }
    };
    Verticle receiver = new AbstractVerticle() {

        boolean unregisterCalled;

        @Override
        public void start(Future<Void> startFuture) throws Exception {
            EventBus eventBus = getVertx().eventBus();
            MessageConsumer<String> consumer = eventBus.consumer("whatever");
            consumer.handler(m -> {
                if (!unregisterCalled) {
                    consumer.unregister(v -> unregistered.set(true));
                    unregisterCalled = true;
                }
                m.reply("ok");
            }).completionHandler(startFuture);
        }
    };
    CountDownLatch deployLatch = new CountDownLatch(1);
    vertices[0].exceptionHandler(this::fail).deployVerticle(receiver, onSuccess(receiverId -> {
        vertices[1].exceptionHandler(this::fail).deployVerticle(sender, onSuccess(senderId -> {
            deployLatch.countDown();
        }));
    }));
    awaitLatch(deployLatch);
    await();
    CountDownLatch closeLatch = new CountDownLatch(2);
    vertices[0].close(v -> closeLatch.countDown());
    vertices[1].close(v -> closeLatch.countDown());
    awaitLatch(closeLatch);
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) Verticle(io.vertx.core.Verticle) Context(io.vertx.core.Context) Future(io.vertx.core.Future) Consumer(java.util.function.Consumer) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) MessageCodec(io.vertx.core.eventbus.MessageCodec) Buffer(io.vertx.core.buffer.Buffer) DeploymentOptions(io.vertx.core.DeploymentOptions) AbstractVerticle(io.vertx.core.AbstractVerticle) CharsetUtil(io.netty.util.CharsetUtil) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) JsonObject(io.vertx.core.json.JsonObject) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) ReplyException(io.vertx.core.eventbus.ReplyException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Verticle(io.vertx.core.Verticle) AbstractVerticle(io.vertx.core.AbstractVerticle) Future(io.vertx.core.Future) EventBus(io.vertx.core.eventbus.EventBus) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractVerticle(io.vertx.core.AbstractVerticle) ReplyException(io.vertx.core.eventbus.ReplyException) Test(org.junit.Test)

Example 10 with MessageConsumer

use of io.vertx.core.eventbus.MessageConsumer in project vertx-openshift-it by cescoffier.

the class DistributedPeerToPeer method setup.

void setup(Handler<AsyncResult<Void>> handler) {
    EventBus eventBus = vertx.eventBus();
    MessageConsumer<String> consumer = createMessageConsumer(eventBus, address);
    consumer.handler(msg -> {
        eventBus.send(msg.body(), clusterManager.getNodeID());
    }).completionHandler(ar -> {
        if (ar.succeeded()) {
            handler.handle(Future.succeededFuture());
        } else {
            handler.handle(Future.failedFuture(ar.cause()));
        }
    });
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) ClusterManager(io.vertx.core.spi.cluster.ClusterManager) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) RoutingContext(io.vertx.ext.web.RoutingContext) UUID(java.util.UUID) Future(io.vertx.core.Future) JsonArray(io.vertx.core.json.JsonArray) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) EventBus(io.vertx.core.eventbus.EventBus)

Aggregations

MessageConsumer (io.vertx.core.eventbus.MessageConsumer)13 Buffer (io.vertx.core.buffer.Buffer)9 EventBus (io.vertx.core.eventbus.EventBus)9 VertxInternal (io.vertx.core.impl.VertxInternal)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Test (org.junit.Test)7 io.vertx.core (io.vertx.core)6 Future (io.vertx.core.Future)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Vertx (io.vertx.core.Vertx)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Consumer (java.util.function.Consumer)5 AsyncResult (io.vertx.core.AsyncResult)4 Handler (io.vertx.core.Handler)4 DatagramSocket (io.vertx.core.datagram.DatagramSocket)4 DeliveryOptions (io.vertx.core.eventbus.DeliveryOptions)4 ReplyFailure (io.vertx.core.eventbus.ReplyFailure)4 io.vertx.core.http (io.vertx.core.http)4 HttpHeaders (io.vertx.core.http.HttpHeaders)4