String randomChannelName = NotificationListenerTest.class.getName() + new Random().nextInt();
final AtomicInteger messagesReceivedBy1 = new AtomicInteger(0);
final AtomicInteger messagesReceivedBy2 = new AtomicInteger(0);
final NotificationListener notificationListener1 = new NotificationListener("notify.syncany.org", 8080, new NotificationListenerListener() {
@Override
public void pushNotificationReceived(String channel, String message) {
logger.log(Level.INFO, "Client 1: pushNotificationReceived(channel = "+channel+", message = "+message+")");
messagesReceivedBy1.addAndGet(1);
}
});
final NotificationListener notificationListener2 = new NotificationListener("notify.syncany.org", 8080, new NotificationListenerListener() {
@Override
public void pushNotificationReceived(String channel, String message) {
logger.log(Level.INFO, "Client 2: pushNotificationReceived(channel = "+channel+", message = "+message+")");
messagesReceivedBy2.addAndGet(1);
}
});
// Start
notificationListener1.start();
notificationListener2.start();
notificationListener1.subscribe(randomChannelName);
notificationListener2.subscribe(randomChannelName);
Thread.sleep(1500); // Let them settle
notificationListener1.announce(randomChannelName, "Message from 1");
notificationListener2.announce(randomChannelName, "Message from 2");
for (int i = 0; i < 100; i++) {
Thread.sleep(100); // Wait for messages
if (messagesReceivedBy1.get() == 2 && messagesReceivedBy2.get() == 2) {
break;
}
}
assertEquals("Different amount of messages received by 1", 2, messagesReceivedBy1.get());
assertEquals("Different amount of messages received by 2", 2, messagesReceivedBy2.get());
notificationListener1.stop();
notificationListener2.stop();
}