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();
}
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);
}
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();
}
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);
}
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()));
}
});
}
Aggregations