protected void testPubSub(FanoutService service) throws Exception {
System.out.println(MessageFormat.format("\n\n========================================\nPUBSUB TEST {0}\n========================================\n\n", service.toString()));
service.startSynchronously();
final Map<String, String> announcementsA = new ConcurrentHashMap<String, String>();
FanoutClient clientA = new FanoutClient("localhost", fanoutPort);
clientA.addListener(new FanoutAdapter() {
@Override
public void announcement(String channel, String message) {
announcementsA.put(channel, message);
}
});
clientA.startSynchronously();
final Map<String, String> announcementsB = new ConcurrentHashMap<String, String>();
FanoutClient clientB = new FanoutClient("localhost", fanoutPort);
clientB.addListener(new FanoutAdapter() {
@Override
public void announcement(String channel, String message) {
announcementsB.put(channel, message);
}
});
clientB.startSynchronously();
// subscribe clients A and B to the channels
clientA.subscribe("a");
clientA.subscribe("b");
clientA.subscribe("c");
clientB.subscribe("a");
clientB.subscribe("b");
clientB.subscribe("c");
// give async messages a chance to be delivered
Thread.sleep(1000);
clientA.announce("a", "apple");
clientA.announce("b", "banana");
clientA.announce("c", "cantelope");
clientB.announce("a", "avocado");
clientB.announce("b", "beet");
clientB.announce("c", "carrot");
// give async messages a chance to be delivered
Thread.sleep(2000);
// confirm that client B received client A's announcements
assertEquals("apple", announcementsB.get("a"));
assertEquals("banana", announcementsB.get("b"));
assertEquals("cantelope", announcementsB.get("c"));
// confirm that client A received client B's announcements
assertEquals("avocado", announcementsA.get("a"));
assertEquals("beet", announcementsA.get("b"));
assertEquals("carrot", announcementsA.get("c"));
clientA.stop();
clientB.stop();
service.stop();
}