new SetiService(seti1);
new SetiService(seti2);
new SetiService(seti3);
BayeuxClient client1A = startClient(oort1, null);
Assert.assertTrue(client1A.waitFor(5000, BayeuxClient.State.CONNECTED));
BayeuxClient client1B = startClient(oort1, null);
Assert.assertTrue(client1B.waitFor(5000, BayeuxClient.State.CONNECTED));
BayeuxClient client1C = startClient(oort2, null);
Assert.assertTrue(client1C.waitFor(5000, BayeuxClient.State.CONNECTED));
BayeuxClient client3 = startClient(oort3, null);
Assert.assertTrue(client3.waitFor(5000, BayeuxClient.State.CONNECTED));
LatchListener publishLatch = new LatchListener();
Map<String, Object> login1A = new HashMap<>();
login1A.put("user", "user1");
ClientSessionChannel loginChannel1A = client1A.getChannel("/service/login");
loginChannel1A.addListener(publishLatch);
loginChannel1A.publish(login1A);
Assert.assertTrue(publishLatch.await(5, TimeUnit.SECONDS));
// Login the same user to the same server with a different client
publishLatch.reset(1);
Map<String, Object> login1B = new HashMap<>();
login1B.put("user", "user1");
ClientSessionChannel loginChannel1B = client1B.getChannel("/service/login");
loginChannel1B.addListener(publishLatch);
loginChannel1B.publish(login1B);
Assert.assertTrue(publishLatch.await(5, TimeUnit.SECONDS));
// Login the same user to another server with a different client
publishLatch.reset(1);
Map<String, Object> login1C = new HashMap<>();
login1C.put("user", "user1");
ClientSessionChannel loginChannel1C = client1C.getChannel("/service/login");
loginChannel1C.addListener(publishLatch);
loginChannel1C.publish(login1C);
Assert.assertTrue(publishLatch.await(5, TimeUnit.SECONDS));
publishLatch.reset(1);
Map<String, Object> login2 = new HashMap<>();
login2.put("user", "user2");
ClientSessionChannel loginChannel2 = client3.getChannel("/service/login");
loginChannel2.addListener(publishLatch);
loginChannel2.publish(login2);
Assert.assertTrue(publishLatch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(presenceLatch.await(5, TimeUnit.SECONDS));
// Send a message from client3: client1A, client1B and client1C must receive it
String channel = "/service/forward";
final LatchListener messageLatch = new LatchListener(3);
final AtomicInteger counter = new AtomicInteger();
client1A.getChannel(channel).addListener(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
counter.incrementAndGet();
messageLatch.countDown();
}
});
client1B.getChannel(channel).addListener(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
counter.incrementAndGet();
messageLatch.countDown();
}
});
client1C.getChannel(channel).addListener(new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
counter.incrementAndGet();
messageLatch.countDown();
}
});
Map<String, Object> data = new HashMap<>();
data.put("peer", "user1");
client3.getChannel(channel).publish(data);
Assert.assertTrue(messageLatch.await(5, TimeUnit.SECONDS));
// Wait a bit more to collect other messages that may be delivered wrongly
Thread.sleep(1000);
// Be sure exactly 3 have been delivered
Assert.assertEquals(3, counter.get());
// Disassociate client1A
publishLatch.reset(1);
Map<String, Object> logout = new HashMap<>();
logout.put("user", "user1");
ClientSessionChannel logoutChannel1A = client1A.getChannel("/service/logout");
logoutChannel1A.addListener(publishLatch);
logoutChannel1A.publish(logout);
Assert.assertTrue(publishLatch.await(5, TimeUnit.SECONDS));
// Send again the message from client3, now only client1B and client1C must get it
counter.set(0);
messageLatch.reset(2);
client3.getChannel(channel).publish(data);
Assert.assertTrue(messageLatch.await(5, TimeUnit.SECONDS));
// Wait a bit more to collect other messages that may be delivered wrongly
Thread.sleep(1000);