@Test public void testReloadWithSubscriptionAndPublish() throws Exception { defineClass(Latch.class); evaluateApplication(); Latch latch = get("latch"); Assert.assertTrue(latch.await(5000)); // Calling reload() results in the state being saved. evaluateScript("cometd.reload();"); // Reload the page destroyPage(); initPage(); initExtension(); defineClass(Latch.class); evaluateApplication(); latch = get("latch"); Assert.assertTrue(latch.await(5000)); // Check that handshake was faked evaluateScript("window.assert(extHandshake === null, 'extHandshake');"); evaluateScript("window.assert(rcvHandshake !== null, 'rcvHandshake');"); // Check that subscription went out evaluateScript("window.assert(extSubscribe !== null, 'extSubscribe');"); evaluateScript("window.assert(rcvSubscribe === null, 'rcvSubscribe');"); // Check that publish went out evaluateScript("window.assert(extPublish !== null, 'extPublish');"); evaluateScript("cometd.disconnect(true);"); }
@Test 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 state being saved. 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);"); }
@Test public void testReloadWithHandshakeCallback() throws Exception { defineClass(Latch.class); evaluateScript("var readyLatch = new Latch(1);"); Latch readyLatch = get("readyLatch"); evaluateScript( "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});"); evaluateScript( "cometd.handshake(function(message)" + "{" + " if (message.successful) {" + " readyLatch.countDown();" + " }" + "});"); Assert.assertTrue(readyLatch.await(5000)); // Wait that the long poll is established before reloading Thread.sleep(metaConnectPeriod / 2); // Reload the page evaluateScript("cometd.reload();"); destroyPage(); initPage(); initExtension(); defineClass(Latch.class); evaluateScript("var readyLatch = new Latch(1);"); readyLatch = get("readyLatch"); evaluateScript( "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});"); evaluateScript( "cometd.handshake(function(message)" + "{" + " if (message.successful) {" + " readyLatch.countDown();" + " }" + "});"); Assert.assertTrue(readyLatch.await(5000)); disconnect(); }
@Test public void testReloadWithConfiguration() throws Exception { defineClass(Latch.class); evaluateScript("var readyLatch = new Latch(1);"); Latch readyLatch = get("readyLatch"); String attributeName = "reload.test"; evaluateScript( "" + "cometd.unregisterExtension('reload');" + "cometd.registerExtension('reload', new cometdModule.ReloadExtension({" + " name: '" + attributeName + "'" + "}));" + "" + "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();"); String reloadState = evaluateScript("window.sessionStorage.getItem('" + attributeName + "');"); Assert.assertNotNull(reloadState); evaluateScript("cometd.disconnect(true)"); }
@Test public void testReloadedHandshakeContainsExtension() throws Exception { bayeuxServer.addExtension( new BayeuxServer.Extension.Adapter() { @Override public boolean sendMeta(ServerSession to, ServerMessage.Mutable message) { if (Channel.META_HANDSHAKE.equals(message.getChannel())) { message.getExt(true).put("foo", true); } return true; } }); 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: ' + JSON.stringify(ext));" + "window.assert(ext.foo === true, 'ext.foo must be true: ' + JSON.stringify(ext));"); evaluateScript("cometd.disconnect(true)"); }
@Test public void testReloadAcrossServerRestart() throws Exception { defineClass(Latch.class); evaluateScript( "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});"); evaluateScript("var readyLatch = new Latch(1);"); Latch readyLatch = get("readyLatch"); evaluateScript("var stopLatch = new Latch(1);"); Latch stopLatch = get("stopLatch"); evaluateScript( "" + "cometd.addListener('/meta/connect', function(message) " + "{ " + " if (message.successful) " + " readyLatch.countDown();" + " else" + " stopLatch.countDown();" + "});"); evaluateScript("cometd.handshake();"); Assert.assertTrue(readyLatch.await(5000)); // Get the clientId String clientId = evaluateScript("cometd.getClientId();"); // Stop the server int port = connector.getLocalPort(); server.stop(); Assert.assertTrue(stopLatch.await(5000)); // Disconnect evaluateScript("cometd.disconnect();"); // Restart the server connector.setPort(port); server.start(); // Reload the page evaluateScript("cometd.reload();"); destroyPage(); initPage(); initExtension(); defineClass(Latch.class); evaluateScript( "cometd.configure({url: '" + cometdURL + "', logLevel: '" + getLogLevel() + "'});"); evaluateScript("var readyLatch = new Latch(1);"); readyLatch = get("readyLatch"); evaluateScript( "" + "var failures = 0;" + "cometd.addListener('/meta/connect', function(message) " + "{ " + " if (message.successful) " + " readyLatch.countDown();" + " else" + " ++failures;" + "});"); evaluateScript("cometd.handshake();"); Assert.assertTrue(readyLatch.await(5000)); // Must not have failed with a 402::Unknown Client error Assert.assertEquals(0, ((Number) get("failures")).intValue()); disconnect(); }
private void testReloadWithTransport(String url, String transportName) throws Exception { defineClass(Latch.class); evaluateScript("cometd.configure({url: '" + url + "', logLevel: '" + getLogLevel() + "'});"); evaluateScript("cometd.unregisterTransports();"); evaluateScript( "cometd.registerTransport('" + transportName + "', originalTransports['" + transportName + "']);"); 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();"); // Calling reload() results in the state being saved. evaluateScript("cometd.reload();"); // Reload the page destroyPage(); initPage(); initExtension(); defineClass(Latch.class); evaluateScript("cometd.configure({url: '" + url + "', logLevel: '" + getLogLevel() + "'});"); // Leave the default transports so that we can test if the previous transport is the one used on // reload evaluateScript( "cometd.registerTransport('" + transportName + "', originalTransports['" + transportName + "']);"); evaluateScript("var readyLatch = new Latch(1);"); readyLatch = get("readyLatch"); evaluateScript( "" + "cometd.addListener('/meta/connect', function(message) " + "{" + " if (message.successful) readyLatch.countDown(); " + "});"); evaluateScript("cometd.handshake();"); Assert.assertTrue(readyLatch.await(5000)); String newClientId = evaluateScript("cometd.getClientId();"); Assert.assertEquals(clientId, newClientId); String transportType = evaluateScript("cometd.getTransport().getType();"); Assert.assertEquals(transportName, transportType); evaluateScript("cometd.disconnect();"); Thread.sleep(1000); // Be sure the sessionStorage item has been removed on disconnect. Boolean reloadState = evaluateScript("window.sessionStorage.getItem('org.cometd.reload') == null;"); Assert.assertTrue(reloadState); }