@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void sendPingMessageExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendPingFrame("PING".getBytes()).get(10, TimeUnit.SECONDS); } }
@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void streamBytesExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendBinaryFrame("STREAM".getBytes(), true, 0).get(1, TimeUnit.SECONDS); } }
@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void sendByteMessageExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendBinaryFrame("BYTES".getBytes()).get(10, TimeUnit.SECONDS); } }
@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void sendPongMessageExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendPongFrame("PONG".getBytes()).get(1, TimeUnit.SECONDS); } }
@Test(expectedExceptions = ExecutionException.class) public void streamTextExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendTextFrame("STREAM", true, 0).get(1, TimeUnit.SECONDS); } }
@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void sendTextMessageExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendTextFrame("TEXT").get(10, TimeUnit.SECONDS); } }
@Test(timeOut = 60000) public void onCloseWithCode() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new Listener(latch, text)).build()).get(); websocket.sendCloseFrame(); latch.await(); assertTrue(text.get().startsWith("1000"), "Expected a 1000 code but got " + text.get()); } }
@Test(timeOut = 60000) public void onClose() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() { @Override public void onOpen(WebSocket websocket) { } @Override public void onClose(WebSocket websocket, int code, String reason) { text.set("OnClose"); latch.countDown(); } @Override public void onError(Throwable t) { t.printStackTrace(); latch.countDown(); } }).build()).get(); websocket.sendCloseFrame(); latch.await(); assertEquals(text.get(), "OnClose"); } }
@Test(timeOut = 60000) public void testRedirectToWSResource() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().setFollowRedirect(true))) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); WebSocket websocket = c.prepareGet(getRedirectURL()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() { @Override public void onOpen(WebSocket websocket) { text.set("OnOpen"); latch.countDown(); } @Override public void onClose(WebSocket websocket, int code, String reason) { } @Override public void onError(Throwable t) { t.printStackTrace(); latch.countDown(); } }).build()).get(); latch.await(); assertEquals(text.get(), "OnOpen"); websocket.sendCloseFrame(); } }
@Override protected void doStop() throws Exception { if (websocket != null && websocket.isOpen()) { if (LOG.isDebugEnabled()) { LOG.debug("Disconnecting from {}", getHttpUri().toASCIIString()); } websocket.removeWebSocketListener(listener); websocket.sendCloseFrame(); websocket = null; } super.doStop(); }
public void close() { logger.info("Slack RTM closing..."); stop = true; if (webSocket != null && webSocket.isOpen()) { webSocket.sendCloseFrame(); } if (asyncHttpClient != null && !asyncHttpClient.isClosed()) { try { asyncHttpClient.close(); } catch (IOException e) { // ignore } } logger.info("Slack RTM closed."); }