@Test public void testMultipleServerClose() { this.server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)); AtomicInteger times = new AtomicInteger(); // We assume the endHandler and the close completion handler are invoked in the same context // task ThreadLocal stack = new ThreadLocal(); stack.set(true); server .websocketStream() .endHandler( v -> { assertNull(stack.get()); assertTrue(Vertx.currentContext().isEventLoopContext()); times.incrementAndGet(); }); server.close( ar1 -> { assertNull(stack.get()); assertTrue(Vertx.currentContext().isEventLoopContext()); server.close( ar2 -> { server.close( ar3 -> { assertEquals(1, times.get()); testComplete(); }); }); }); await(); }
@Test public void testWebsocketStreamCallbackAsynchronously() { this.server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)); AtomicInteger done = new AtomicInteger(); ServerWebSocketStream stream = server.websocketStream(); stream.handler(req -> {}); ThreadLocal<Object> stack = new ThreadLocal<>(); stack.set(true); stream.endHandler( v -> { assertTrue(Vertx.currentContext().isEventLoopContext()); assertNull(stack.get()); if (done.incrementAndGet() == 2) { testComplete(); } }); server.listen( ar -> { assertTrue(Vertx.currentContext().isEventLoopContext()); assertNull(stack.get()); ThreadLocal<Object> stack2 = new ThreadLocal<>(); stack2.set(true); server.close( v -> { assertTrue(Vertx.currentContext().isEventLoopContext()); assertNull(stack2.get()); if (done.incrementAndGet() == 2) { testComplete(); } }); stack2.set(null); }); await(); }
@Test public void testWebsocketPauseAndResume() { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setConnectTimeout(1000)); String path = "/some/path"; this.server = vertx.createHttpServer( new HttpServerOptions().setAcceptBacklog(1).setPort(HttpTestBase.DEFAULT_HTTP_PORT)); AtomicBoolean paused = new AtomicBoolean(); ReadStream<ServerWebSocket> stream = server.websocketStream(); stream.handler( ws -> { assertFalse(paused.get()); ws.writeMessage(Buffer.buffer("whatever")); ws.close(); }); server.listen( listenAR -> { assertTrue(listenAR.succeeded()); stream.pause(); paused.set(true); client .websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path) .exceptionHandler( err -> { assertTrue(paused.get()); assertTrue(err instanceof WebSocketHandshakeException); paused.set(false); stream.resume(); client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, ws -> { ws.handler( buffer -> { assertEquals("whatever", buffer.toString("UTF-8")); ws.closeHandler( v2 -> { testComplete(); }); }); }); }) .handler(ws -> fail()); }); await(); }
@Test public void testClosingServerClosesWebSocketStreamEndHandler() { this.server = vertx.createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)); ReadStream<ServerWebSocket> stream = server.websocketStream(); AtomicBoolean closed = new AtomicBoolean(); stream.endHandler(v -> closed.set(true)); stream.handler(ws -> {}); server.listen( ar -> { assertTrue(ar.succeeded()); assertFalse(closed.get()); server.close( v -> { assertTrue(ar.succeeded()); assertTrue(closed.get()); testComplete(); }); }); await(); }