Package org.cometd.javascript

Examples of org.cometd.javascript.Latch


    @Test
    public void testOutgoingExtensionExceptionCallback() throws Exception
    {
        defineClass(Latch.class);
        evaluateScript("var latch = new Latch(1);");
        Latch latch = (Latch)get("latch");
        evaluateScript("var connectLatch = new Latch(1);");
        Latch connectLatch = get("connectLatch");
        evaluateScript("" +
                "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});" +
                "cometd.addListener('/meta/connect', function(message) { connectLatch.countDown(); });" +
                "cometd.registerExtension('testext', {" +
                "   outgoing: function(message) { throw 'test'; }" +
                "});" +
                "" +
                "cometd.onExtensionException = function(exception, extensionName, outgoing, message) " +
                "{" +
                "   if (exception === 'test' && extensionName === 'testext' && outgoing === true)" +
                "   {" +
                "       this.unregisterExtension(extensionName);" +
                "       latch.countDown();" +
                "   }" +
                "};" +
                "" +
                "cometd.handshake();");
        Assert.assertTrue(latch.await(5000));

        Assert.assertTrue(connectLatch.await(5000));

        evaluateScript("cometd.disconnect(true);");
    }
View Full Code Here


    @Test
    public void testIncomingExtensionExceptionCallback() throws Exception
    {
        defineClass(Latch.class);
        evaluateScript("var latch = new Latch(1);");
        Latch latch = (Latch)get("latch");
        evaluateScript("var connectLatch = new Latch(1);");
        Latch connectLatch = get("connectLatch");
        evaluateScript("" +
                "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});" +
                "cometd.addListener('/meta/connect', function(message) { connectLatch.countDown(); });" +
                "cometd.registerExtension('testext', {" +
                "   incoming: function(message) { throw 'test'; }" +
                "});" +
                "" +
                "cometd.onExtensionException = function(exception, extensionName, outgoing, message) " +
                "{" +
                "   if (exception === 'test' && extensionName === 'testext' && outgoing === false)" +
                "   {" +
                "       this.unregisterExtension(extensionName);" +
                "       latch.countDown();" +
                "   }" +
                "};" +
                "" +
                "cometd.handshake();");
        Assert.assertTrue(latch.await(5000));

        Assert.assertTrue(connectLatch.await(5000));

        evaluateScript("cometd.disconnect(true);");
    }
View Full Code Here

    public void testDojoHitch() throws Exception
    {
        defineClass(Latch.class);
        evaluateScript("cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});");
        evaluateScript("var handshakeLatch = new Latch(1);");
        Latch handshakeLatch = get("handshakeLatch");
        evaluateScript("cometd.addListener('/meta/handshake', handshakeLatch, 'countDown');");
        evaluateScript("cometd.handshake();");
        Assert.assertTrue(handshakeLatch.await(5000));

        evaluateScript("var latch1 = new Latch(1);");
        Latch latch1 = get("latch1");
        evaluateScript("cometd.subscribe('/test', dojo.hitch(latch1, 'countDown'), {});");
        evaluateScript("var latch2 = new Latch(1);");
        Latch latch2 = get("latch2");
        evaluateScript("cometd.subscribe('/test', dojo.hitch(latch2, 'countDown'));");
        evaluateScript("cometd.publish('/test', {});");
        Assert.assertTrue(latch1.await(5000));
        Assert.assertTrue(latch2.await(5000));

        evaluateScript("cometd.disconnect(true);");
    }
View Full Code Here

    {
        defineClass(Latch.class);

        evaluateScript("var handshakeLatch = new Latch(1);");
        evaluateScript("var handshakeLatch2 = new Latch(1);");
        Latch handshakeLatch = get("handshakeLatch");
        Latch handshakeLatch2 = get("handshakeLatch2");

        evaluateScript("" +
                "var cometd2 = new dojox.CometD('dojo');" +
                "var jsonpTransport = cometd2.unregisterTransport('long-polling');" +
                "" +
                "/* Check that the other cometd object has not been influenced */" +
                "window.assert(cometd.findTransport('long-polling') != null);" +
                "" +
                "cometd.addListener('/meta/handshake', handshakeLatch, 'countDown');" +
                "cometd2.addListener('/meta/handshake', handshakeLatch2, 'countDown');" +
                "" +
                "cometd.init({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});" +
                "");
        Assert.assertTrue(handshakeLatch.await(5000));
        Assert.assertFalse(handshakeLatch2.await(1000));

        String cometdURL2 = cometdURL.replace("localhost", "127.0.0.1");
        evaluateScript("" +
                "cometd2.init({url: '" + cometdURL2 + "', logLevel: '" + getLogLevel() + "'});" +
                "");
        Assert.assertTrue(handshakeLatch2.await(5000));

        String channelName = "/test";

        evaluateScript("var subscribeLatch = new Latch(1);");
        evaluateScript("var subscribeLatch2 = new Latch(1);");
        Latch subscribeLatch = get("subscribeLatch");
        Latch subscribeLatch2 = get("subscribeLatch2");
        evaluateScript("var publishLatch = new Latch(2);");
        evaluateScript("var publishLatch2 = new Latch(2);");
        Latch publishLatch = get("publishLatch");
        Latch publishLatch2 = get("publishLatch2");
        evaluateScript("" +
                "cometd.addListener('/meta/subscribe', subscribeLatch, 'countDown');" +
                "cometd2.addListener('/meta/subscribe', subscribeLatch2, 'countDown');" +
                "cometd.subscribe('" + channelName + "', publishLatch, 'countDown');" +
                "cometd2.subscribe('" + channelName + "', publishLatch2, 'countDown');" +
                "");
        Assert.assertTrue(subscribeLatch.await(5000));
        Assert.assertTrue(subscribeLatch2.await(5000));

        evaluateScript("" +
                "cometd.publish('" + channelName + "', {});" +
                "cometd2.publish('" + channelName + "', {});" +
                "");
        Assert.assertTrue(publishLatch.await(5000));
        Assert.assertTrue(publishLatch2.await(5000));

        evaluateScript("" +
                "cometd.disconnect(true);" +
                "cometd2.disconnect(true);" +
                "");
View Full Code Here

                "outgoing: function(message) { ++outCount; return message;}" +
                "});");
        evaluateScript("cometd.registerExtension('testempty', {});");

        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
        Thread.sleep(1000);

        Number inCount = get("inCount");
        Number outCount = get("outCount");
        Assert.assertEquals(2, inCount.intValue()); // handshake, connect1
        Assert.assertEquals(3, outCount.intValue()); // handshake, connect1, connect2

        Boolean unregistered = evaluateScript("cometd.unregisterExtension('testin');");
        Assert.assertTrue(unregistered);
        unregistered = evaluateScript("cometd.unregisterExtension('testout');");
        Assert.assertTrue(unregistered);

        evaluateScript("var publishLatch = new Latch(1);");
        Latch publishLatch = get("publishLatch");
        evaluateScript("cometd.addListener('/meta/publish', function(message) { publishLatch.countDown(); });");
        evaluateScript("cometd.publish('/echo', 'ping');");
        Assert.assertTrue(publishLatch.await(5000));

        inCount = get("inCount");
        outCount = get("outCount");
        Assert.assertEquals(2, inCount.intValue());
        Assert.assertEquals(3, outCount.intValue());
View Full Code Here

                "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();
View Full Code Here

        evaluateScript("cometd.addListener('/meta/handshake', function(message) " +
                       "{" +
                       "    if (message.ext1 === 1 && message.ext2 === 1) ok = true;" +
                       "});");
        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));

        Assert.assertTrue((Boolean)get("ok"));

        evaluateScript("cometd.disconnect(true);");
        // Wait for the connect to return
        Thread.sleep(1000);

        evaluateScript("cometd.unregisterExtension('ext1');");
        evaluateScript("cometd.unregisterExtension('ext2');");
        evaluateScript("ok = false;");
        // Set incoming extension order to be forward
        evaluateScript("cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "', reverseIncomingExtensions: false});");

        evaluateScript("cometd.registerExtension('ext1', {" +
                       "incoming: function(message) " +
                       "{" +
                       "    if (message.ext2 !== 1) message.ext1 = 1;" +
                       "    return message;" +
                       "} " +
                       "});");
        evaluateScript("cometd.registerExtension('ext2', {" +
                       "incoming: function(message) " +
                       "{" +
                       "    if (message.ext1 === 1) message.ext2 = 1;" +
                       "    return message;" +
                       "} " +
                       "});");
        readyLatch.reset(1);
        evaluateScript("cometd.handshake();");
        Assert.assertTrue(readyLatch.await(5000));

        Assert.assertTrue((Boolean)get("ok"));

        evaluateScript("cometd.disconnect(true);");
    }
View Full Code Here

    @Test
    public void testReloadWithConfiguration() throws Exception
    {
        defineClass(Latch.class);
        evaluateScript("var readyLatch = new Latch(1);");
        Latch readyLatch = get("readyLatch");
        String cookieName = "reload.test";
        evaluateScript("" +
                "cometd.unregisterExtension('reload');" +
                "cometd.registerExtension('reload', new org.cometd.ReloadExtension({" +
                "    cookieName: '" + cookieName + "'," +
                "    cookiePath: '/test'," +
                "    cookieMaxAge: 4" +
                "}));" +
                "" +
                "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});" +
                "cometd.addListener('/meta/connect', function(message) " +
                "{ " +
                "   if (message.successful) readyLatch.countDown(); " +
                "});");
        evaluateScript("cometd.handshake();");
        Assert.assertTrue(readyLatch.await(5000));

        // Wait that the long poll is established before reloading
        Thread.sleep(metaConnectPeriod / 2);

        evaluateScript("cometd.reload();");
View Full Code Here

    {
        bayeuxServer.addExtension(new AcknowledgedMessagesExtension());

        defineClass(Latch.class);
        evaluateScript("var readyLatch = new Latch(1);");
        Latch readyLatch = get("readyLatch");
        evaluateScript("" +
                "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});" +
                "cometd.addListener('/meta/connect', function(message) " +
                "{ " +
                "   if (message.successful) readyLatch.countDown(); " +
                "});");
        evaluateScript("cometd.handshake();");
        Assert.assertTrue(readyLatch.await(5000));

        // Wait that the long poll is established before reloading
        Thread.sleep(metaConnectPeriod / 2);

        evaluateScript("cometd.reload();");

        // Reload the page
        destroyPage();
        initPage();
        initExtension();

        defineClass(Latch.class);
        evaluateScript("var readyLatch = new Latch(1);");
        readyLatch = get("readyLatch");
        evaluateScript("" +
                "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});" +
                "var ext = undefined;" +
                "cometd.addListener('/meta/handshake', function(message)" +
                "{" +
                "   if (message.successful)" +
                "      ext = message.ext;" +
                "});" +
                "cometd.addListener('/meta/connect', function(message) " +
                "{ " +
                "   if (message.successful) readyLatch.countDown(); " +
                "});");
        evaluateScript("cometd.handshake();");
        Assert.assertTrue(readyLatch.await(5000));

        evaluateScript("" +
                "window.assert(ext !== undefined, 'ext must be present');" +
                "window.assert(ext.reload === true, 'ext.reload must be true');" +
                "window.assert(ext.ack === true, 'ext.ack must be true');");
View Full Code Here

    public void testReloadDoesNotExpire() throws Exception
    {
        defineClass(Latch.class);
        evaluateScript("cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});");
        evaluateScript("var readyLatch = new Latch(1);");
        Latch readyLatch = get("readyLatch");
        evaluateScript("" +
                "cometd.addListener('/meta/connect', function(message) " +
                "{ " +
                "   if (message.successful) readyLatch.countDown(); " +
                "});");
        evaluateScript("cometd.handshake();");
        Assert.assertTrue(readyLatch.await(5000));

        // Get the clientId
        String clientId = evaluateScript("cometd.getClientId();");

        // Wait that the long poll is established before reloading
        Thread.sleep(metaConnectPeriod / 2);

        // Calling reload() results in the cookie being written
        evaluateScript("cometd.reload();");

        // Reload the page
        destroyPage();
        initPage();
        initExtension();

        defineClass(Latch.class);
        evaluateScript("cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});");
        evaluateScript("var readyLatch = new Latch(1);");
        readyLatch = get("readyLatch");
        evaluateScript("var expireLatch = new Latch(1);");
        Latch expireLatch = get("expireLatch");
        evaluateScript("" +
                "cometd.addListener('/meta/connect', function(message) " +
                "{" +
                "   if (message.successful) " +
                "       readyLatch.countDown();" +
                "   else" +
                "       expireLatch.countDown();" +
                "});");
        evaluateScript("cometd.handshake();");
        Assert.assertTrue(readyLatch.await(5000));

        String newClientId = evaluateScript("cometd.getClientId();");
        Assert.assertEquals(clientId, newClientId);

        // Make sure that reloading will not expire the client on the server
        Assert.assertFalse(expireLatch.await(expirationPeriod + metaConnectPeriod));

        evaluateScript("cometd.disconnect(true);");
    }
View Full Code Here

TOP

Related Classes of org.cometd.javascript.Latch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.