}
@Test(timeout = 1000)
public void shouldHaveManyListenersForTheSameMessageType() throws Exception {
final Semaphore lock = new Semaphore(0);
DefaultMessagingDelegate engine = new DefaultMessagingDelegate();
engine.start();
final AtomicInteger counter = new AtomicInteger();
final MessageListener<Message<String>> listenerA = new MessageListener<Message<String>>() {
@Override
public void onMessage(Message<String> message) {
counter.incrementAndGet();
lock.release();
}
};
final MessageListener<Message<String>> listenerB = new MessageListener<Message<String>>() {
@Override
public void onMessage(Message<String> message) {
counter.incrementAndGet();
lock.release();
}
};
final String type = "foo";
final String expectedBody = "HELLO WORLD!";
Message<String> message = new TestMessage<String>(type, expectedBody);
engine.addMessageListener(type, listenerA);
engine.addMessageListener(type, listenerB);
final int totalMessages = 1000;
for (int i = 0; i < totalMessages; i++) {
engine.send(message);
}
lock.tryAcquire(totalMessages * 2, 3, TimeUnit.SECONDS);
assertEquals(totalMessages * 2, counter.get());
engine.stop();
}