"outgoing: function(message) { listener.outgoing(message); return message;}" +
"});");
Listener listener = get("listener");
evaluateScript("var readyLatch = new Latch(1);");
Latch readyLatch = get("readyLatch");
evaluateScript("cometd.addListener('/meta/connect', function(message) { readyLatch.countDown(); });");
evaluateScript("cometd.handshake();");
Assert.assertTrue(readyLatch.await(5000));
// Wait for the long poll to be established
// Cannot rely on latches for this, since we need to intercept the connect2
Thread.sleep(1000);
Assert.assertEquals(3, listener.getOutgoingMessageCount()); // handshake, connect1, connect2
Assert.assertEquals(2, listener.getIncomingMessageCount()); // handshake, connect1
listener.reset();
evaluateScript("var subscribeLatch = new Latch(1);");
Latch subscribeLatch = get("subscribeLatch");
evaluateScript("cometd.addListener('/meta/subscribe', subscribeLatch, 'countDown');");
evaluateScript("var messageLatch = new Latch(1);");
Latch messageLatch = get("messageLatch");
evaluateScript("var subscription = cometd.subscribe('/echo', messageLatch, 'countDown');");
Assert.assertTrue(subscribeLatch.await(5000));
Assert.assertEquals(1, listener.getOutgoingMessageCount()); // subscribe
Assert.assertEquals(1, listener.getIncomingMessageCount()); // subscribe
listener.reset();
evaluateScript("var publishLatch = new Latch(1);");
Latch publishLatch = get("publishLatch");
evaluateScript("cometd.addListener('/meta/publish', publishLatch, 'countDown');");
evaluateScript("cometd.publish('/echo', 'test');");
Assert.assertTrue(publishLatch.await(5000));
Assert.assertTrue(messageLatch.await(5000));
Assert.assertEquals(1, listener.getOutgoingMessageCount()); // publish
Assert.assertEquals(2, listener.getIncomingMessageCount()); // publish, message
listener.reset();
evaluateScript("var unsubscribeLatch = new Latch(1);");
Latch unsubscribeLatch = get("unsubscribeLatch");
evaluateScript("cometd.addListener('/meta/unsubscribe', unsubscribeLatch, 'countDown');");
evaluateScript("cometd.unsubscribe(subscription);");
Assert.assertTrue(unsubscribeLatch.await(5000));
Assert.assertEquals(1, listener.getOutgoingMessageCount()); // unsubscribe
Assert.assertEquals(1, listener.getIncomingMessageCount()); // unsubscribe
readyLatch.reset(1);
listener.reset();