}
@Test
public void testLocalSessions() throws Exception
{
LocalSession session0 = _bayeux.newLocalSession("s0");
Assert.assertEquals("L:s0_", session0.toString());
session0.handshake();
Assert.assertNotEquals("L:s0_", session0.toString());
Assert.assertTrue(session0.toString().startsWith("L:s0_"));
final LocalSession session1 = _bayeux.newLocalSession("s1");
session1.handshake();
final LocalSession session2 = _bayeux.newLocalSession("s2");
session2.handshake();
final Queue<String> events = new ConcurrentLinkedQueue<>();
ClientSessionChannel.MessageListener listener = new ClientSessionChannel.MessageListener()
{
public void onMessage(ClientSessionChannel channel, Message message)
{
events.add(channel.getSession().getId());
events.add(message.getData().toString());
}
};
session0.getChannel("/foo/bar").subscribe(listener);
session0.getChannel("/foo/bar").subscribe(listener);
session1.getChannel("/foo/bar").subscribe(listener);
session2.getChannel("/foo/bar").subscribe(listener);
Assert.assertEquals(3,_bayeux.getChannel("/foo/bar").getSubscribers().size());
session0.getChannel("/foo/bar").unsubscribe(listener);
Assert.assertEquals(3,_bayeux.getChannel("/foo/bar").getSubscribers().size());
session0.getChannel("/foo/bar").unsubscribe(listener);
Assert.assertEquals(2,_bayeux.getChannel("/foo/bar").getSubscribers().size());
ClientSessionChannel foobar0=session0.getChannel("/foo/bar");
foobar0.subscribe(listener);
foobar0.subscribe(listener);
ClientSessionChannel foostar0=session0.getChannel("/foo/*");
foostar0.subscribe(listener);
Assert.assertEquals(3,_bayeux.getChannel("/foo/bar").getSubscribers().size());
Assert.assertEquals(session0,foobar0.getSession());
Assert.assertEquals("/foo/bar",foobar0.getId());
Assert.assertEquals(false,foobar0.isDeepWild());
Assert.assertEquals(false,foobar0.isWild());
Assert.assertEquals(false,foobar0.isMeta());
Assert.assertEquals(false,foobar0.isService());
foobar0.publish("hello");
Assert.assertEquals(session0.getId(),events.poll());
Assert.assertEquals("hello",events.poll());
Assert.assertEquals(session0.getId(),events.poll());
Assert.assertEquals("hello",events.poll());
Assert.assertEquals(session0.getId(),events.poll());
Assert.assertEquals("hello",events.poll());
Assert.assertEquals(session1.getId(),events.poll());
Assert.assertEquals("hello",events.poll());
Assert.assertEquals(session2.getId(),events.poll());
Assert.assertEquals("hello",events.poll());
foostar0.unsubscribe(listener);
session1.batch(new Runnable()
{
public void run()
{
ClientSessionChannel foobar1=session1.getChannel("/foo/bar");
foobar1.publish("part1");
Assert.assertEquals(null,events.poll());
foobar1.publish("part2");
}
});
Assert.assertEquals(session1.getId(),events.poll());
Assert.assertEquals("part1",events.poll());
Assert.assertEquals(session2.getId(),events.poll());
Assert.assertEquals("part1",events.poll());
Assert.assertEquals(session0.getId(),events.poll());
Assert.assertEquals("part1",events.poll());
Assert.assertEquals(session0.getId(),events.poll());
Assert.assertEquals("part1",events.poll());
Assert.assertEquals(session1.getId(),events.poll());
Assert.assertEquals("part2",events.poll());
Assert.assertEquals(session2.getId(),events.poll());
Assert.assertEquals("part2",events.poll());
Assert.assertEquals(session0.getId(),events.poll());
Assert.assertEquals("part2",events.poll());
Assert.assertEquals(session0.getId(),events.poll());
Assert.assertEquals("part2",events.poll());
foobar0.unsubscribe();
Assert.assertEquals(2,_bayeux.getChannel("/foo/bar").getSubscribers().size());
Assert.assertTrue(session0.isConnected());
Assert.assertTrue(session1.isConnected());
Assert.assertTrue(session2.isConnected());
ServerSession ss0=session0.getServerSession();
ServerSession ss1=session1.getServerSession();
ServerSession ss2=session2.getServerSession();
Assert.assertTrue(ss0.isConnected());
Assert.assertTrue(ss1.isConnected());
Assert.assertTrue(ss2.isConnected());
session0.disconnect();
Assert.assertFalse(session0.isConnected());
Assert.assertFalse(ss0.isConnected());
session1.getServerSession().disconnect();
Assert.assertFalse(session1.isConnected());
Assert.assertFalse(ss1.isConnected());
session2.getServerSession().disconnect();
Assert.assertFalse(session2.isConnected());
Assert.assertFalse(ss2.isConnected());
}