Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do consume messages on the correct thread after resume #5456

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected void dispatch(Message<T> msg, ContextInternal context, Handler<Message
if (handler == null) {
throw new NullPointerException();
}
context.dispatch(msg, handler);
context.emit(msg, handler);
}

private void deliver(Handler<Message<T>> theHandler, Message<T> message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.vertx.tests.eventbus;

import io.vertx.core.*;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.test.core.VertxTestBase;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class MessageConsumerTest extends VertxTestBase {


@Test
public void testMessageConsumptionStayOnWorkerThreadAfterResume() throws Exception {
TestVerticle verticle = new TestVerticle(2);
Future<String> deployVerticle = vertx.deployVerticle(verticle, new DeploymentOptions().setThreadingModel(ThreadingModel.WORKER));

CountDownLatch startLatch = new CountDownLatch(1);
deployVerticle.onComplete(onSuccess(cf -> startLatch.countDown()));
awaitLatch(startLatch);

vertx.eventBus().send("testAddress", "message1");
vertx.eventBus().send("testAddress", "message2");

awaitLatch(verticle.msgLatch);

assertEquals(2, verticle.messageArrivedOnWorkerThread.size());
assertTrue("message1 should be processed on worker thread", verticle.messageArrivedOnWorkerThread.get("message1"));
assertTrue("message2 should be processed on worker thread", verticle.messageArrivedOnWorkerThread.get("message2"));
}


private static class TestVerticle extends AbstractVerticle {

private final CountDownLatch msgLatch;

private final Map<String, Boolean> messageArrivedOnWorkerThread = new HashMap<>();

private TestVerticle(Integer numberOfExpectedMessages) {
this.msgLatch = new CountDownLatch(numberOfExpectedMessages);
}

@Override
public void start() {
MessageConsumer<String> consumer = vertx.eventBus().localConsumer("testAddress");
handleMessages(consumer);
}

private void handleMessages(MessageConsumer<String> consumer) {
consumer.handler(msg -> {
consumer.pause();
messageArrivedOnWorkerThread.putIfAbsent(msg.body(), Context.isOnWorkerThread());
msgLatch.countDown();
vertx.setTimer(20, id -> {
consumer.resume();
});
});
}
}
}