/** * In a situation where the upgrade/connection is successfull, and there is no activity for a * while, the idle timeout triggers on the client side and automatically initiates a close * handshake. */ @Test public void testIdleDetectedByClient() throws Exception { TrackingSocket wsocket = new TrackingSocket(); WebSocketClient client = factory.newWebSocketClient(wsocket); URI wsUri = server.getWsUri(); Future<UpgradeResponse> future = client.connect(wsUri); ServerConnection ssocket = server.accept(); ssocket.upgrade(); // Validate that connect occurred future.get(500, TimeUnit.MILLISECONDS); wsocket.waitForConnected(500, TimeUnit.MILLISECONDS); // Wait for inactivity idle timeout. long start = System.currentTimeMillis(); wsocket.waitForClose(10, TimeUnit.SECONDS); long end = System.currentTimeMillis(); long dur = (end - start); // Make sure idle timeout takes less than 5 total seconds Assert.assertThat("Idle Timeout", dur, lessThanOrEqualTo(5000L)); // Client should see a close event, with status NO_CLOSE wsocket.assertCloseCode(StatusCode.NORMAL); }
@Test @Slow public void testServerSlowToRead() throws Exception { TrackingSocket tsocket = new TrackingSocket(); client.setMasker(new ZeroMasker()); client.getPolicy().setIdleTimeout(60000); URI wsUri = server.getWsUri(); Future<Session> future = client.connect(tsocket, wsUri); ServerConnection sconnection = server.accept(); sconnection.setSoTimeout(60000); sconnection.upgrade(); // Confirm connected future.get(500, TimeUnit.MILLISECONDS); tsocket.waitForConnected(500, TimeUnit.MILLISECONDS); int messageCount = 10; // TODO: increase to 1000 // Setup slow server read thread ServerReadThread reader = new ServerReadThread(sconnection); reader.setExpectedMessageCount(messageCount); reader.setSlowness(100); // slow it down reader.start(); // Have client write as quickly as it can. ClientWriteThread writer = new ClientWriteThread(tsocket.getConnection()); writer.setMessageCount(messageCount); writer.setMessage("Hello"); writer.setSlowness(-1); // disable slowness writer.start(); writer.join(); // Verify receive reader.waitForExpectedMessageCount(10, TimeUnit.SECONDS); Assert.assertThat("Frame Receive Count", reader.getFrameCount(), is(messageCount)); // Close tsocket.getConnection().close(StatusCode.NORMAL, "Done"); Assert.assertTrue("Client Socket Closed", tsocket.closeLatch.await(10, TimeUnit.SECONDS)); tsocket.assertCloseCode(StatusCode.NORMAL); reader.cancel(); // stop reading }
@Test @Slow public void testServerSlowToSend() throws Exception { // final Exchanger<String> exchanger = new Exchanger<String>(); TrackingSocket tsocket = new TrackingSocket(); // tsocket.messageExchanger = exchanger; client.setMasker(new ZeroMasker()); client.getPolicy().setIdleTimeout(60000); URI wsUri = server.getWsUri(); Future<Session> future = client.connect(tsocket, wsUri); ServerConnection sconnection = server.accept(); sconnection.setSoTimeout(60000); sconnection.upgrade(); // Confirm connected future.get(500, TimeUnit.MILLISECONDS); tsocket.waitForConnected(500, TimeUnit.MILLISECONDS); // Have server write slowly. int messageCount = 1000; ServerWriteThread writer = new ServerWriteThread(sconnection); writer.setMessageCount(messageCount); writer.setMessage("Hello"); // writer.setExchanger(exchanger); writer.setSlowness(10); writer.start(); writer.join(); // Verify receive Assert.assertThat("Message Receive Count", tsocket.messageQueue.size(), is(messageCount)); // Close sconnection.close(StatusCode.NORMAL); Assert.assertTrue("Client Socket Closed", tsocket.closeLatch.await(10, TimeUnit.SECONDS)); tsocket.assertCloseCode(StatusCode.NORMAL); }