@Test public void testServerConnectionClose() throws Exception { // Test server connection close + client close handler server.requestHandler(req -> { req.connection().close(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> { })) .connectionHandler(conn -> { conn.closeHandler(v -> { testComplete(); }); }).sendHead(); await(); }
@Test public void testHttpClientMetricsQueueClose() throws Exception { server = vertx.createHttpServer(); List<Runnable> requests = Collections.synchronizedList(new ArrayList<>()); server.requestHandler(req -> { requests.add(() -> { vertx.runOnContext(v -> { req.connection().close(); }); }); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(8080, "localhost", onSuccess(s -> { listenLatch.countDown(); })); awaitLatch(listenLatch); client = vertx.createHttpClient(); FakeHttpClientMetrics metrics = FakeHttpClientMetrics.getMetrics(client); for (int i = 0;i < 5;i++) { client.getNow(8080, "localhost", "/somepath", resp -> { }); } assertWaitUntil(() -> requests.size() == 5); EndpointMetric endpoint = metrics.endpoint("localhost:8080"); assertEquals(5, endpoint.connectionCount.get()); ArrayList<Runnable> copy = new ArrayList<>(requests); requests.clear(); copy.forEach(Runnable::run); assertWaitUntil(() -> metrics.endpoints().isEmpty()); assertEquals(0, endpoint.connectionCount.get()); }
closed.countDown(); }); conn.close(); }); }));
@Test public void testConnectionCloseDuringShouldCallHandleExceptionOnlyOnce() throws Exception { server.requestHandler(req -> { vertx.setTimer(500, id -> { req.connection().close(); }); }); AtomicInteger count = new AtomicInteger(); startServer(); HttpClientRequest post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onFailure(res -> {})); post.setChunked(true); post.write(TestUtils.randomBuffer(10000)); CountDownLatch latch = new CountDownLatch(1); post.exceptionHandler(x-> { count.incrementAndGet(); vertx.setTimer(10, id -> { latch.countDown(); }); }); // then stall until timeout and the exception handler will be called. awaitLatch(latch); assertEquals(count.get(), 1); }
@Test public void testClientConnectionClose() throws Exception { // Test client connection close + server close handler CountDownLatch latch = new CountDownLatch(1); server.requestHandler(req -> { AtomicInteger len = new AtomicInteger(); req.handler(buff -> { if (len.addAndGet(buff.length()) == 1024) { latch.countDown(); } }); req.connection().closeHandler(v -> { testComplete(); }); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> {})); req.setChunked(true); req.write(TestUtils.randomBuffer(1024)); awaitLatch(latch); req.connection().close(); await(); }
@Test public void testClientResponseExceptionHandlerCalledWhenConnectionClosed() throws Exception { AtomicReference<HttpConnection> conn = new AtomicReference<>(); server.requestHandler(req -> { conn.set(req.connection()); req.response().setChunked(true).write("chunk"); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { resp.handler(buff -> { conn.get().close(); }); resp.exceptionHandler(err -> { testComplete(); }); })); await(); }
protected void testCloseHandlerNotCalledWhenConnectionClosedAfterEnd(int expected) throws Exception { AtomicInteger closeCount = new AtomicInteger(); AtomicInteger endCount = new AtomicInteger(); server.requestHandler(req -> { req.response().closeHandler(v -> { closeCount.incrementAndGet(); }); req.response().endHandler(v -> { endCount.incrementAndGet(); }); req.connection().closeHandler(v -> { assertEquals(expected, closeCount.get()); assertEquals(1, endCount.get()); testComplete(); }); req.response().end("some-data"); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { resp.endHandler(v -> { resp.request().connection().close(); }); })); await(); }
resp.endHandler(v -> { assertOnIOContext(ctx); resp.request().connection().close(); }); }));
@Test public void testPoolNotExpiring() throws Exception { AtomicLong now = new AtomicLong(); server.requestHandler(req -> { req.response().end(); now.set(System.currentTimeMillis()); vertx.setTimer(2000, id -> { req.connection().close(); }); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPoolCleanerPeriod(0).setKeepAliveTimeout(100)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.endHandler(v1 -> { resp.request().connection().closeHandler(v2 -> { long time = System.currentTimeMillis() - now.get(); assertTrue("Was expecting " + time + " to be > 2000", time >= 2000); testComplete(); }); }); })); await(); }
@Test public void testClientRequestExceptionHandlerCalledWhenConnectionClosed() throws Exception { server.requestHandler(req -> { req.handler(buff -> { req.connection().close(); }); }); startServer(); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> {})).setChunked(true); req.exceptionHandler(err -> { testComplete(); }); req.write("chunk"); await(); }
req.connection().close(); }); });
@Test public void testCloseHandlerWhenConnectionEnds() throws Exception { server.requestHandler(req -> { req.response().closeHandler(v -> { testComplete(); }); req.response().setChunked(true).write("some-data"); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { resp.handler(v -> { resp.request().connection().close(); }); })); await(); }
complete(); resp.request().connection().close(); } else if (err instanceof DecompressionException) { complete();
@Test public void testHttpServerRequestShouldExceptionHandlerWhenTheClosedHandlerIsCalled() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { HttpServerResponse resp = req.response(); resp.setChunked(true); CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), resp); sender.send(); resp.closeHandler(v -> { Throwable failure = sender.close(); if (failure != null) { fail(failure); } else { testComplete(); } }); }); server.listen(onSuccess(s -> { client.getNow(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(resp -> { vertx.setTimer(1000, id -> { resp.request().connection().close(); }); })); })); await(); }
complete(); }); conn.close(); })); req.exceptionHandler(err ->{
@Test public void testCloseHandlerWhenConnectionClose() throws Exception { server.requestHandler(req -> { HttpServerResponse resp = req.response(); resp.setChunked(true).write("some-data"); resp.closeHandler(v -> { checkHttpServerResponse(resp); testComplete(); }); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { resp.handler(v -> { resp.request().connection().close(); }); })); await(); }
@Test public void testClientResponseExceptionHandlerCalledWhenConnectionClosed() throws Exception { AtomicReference<HttpConnection> conn = new AtomicReference<>(); server.requestHandler(req -> { conn.set(req.connection()); req.response().setChunked(true).write("chunk"); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { resp.handler(buff -> { conn.get().close(); }); resp.exceptionHandler(err -> { testComplete(); }); }); await(); }
@Test public void testClientRequestExceptionHandlerCalledWhenConnectionClosed() throws Exception { server.requestHandler(req -> { req.handler(buff -> { req.connection().close(); }); }); startServer(); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { resp.handler(chunk -> { resp.request().connection().close(); }); }).setChunked(true); req.exceptionHandler(err -> { testComplete(); }); req.write("chunk"); await(); }
@Test public void testCloseHandlerWhenConnectionEnds() throws Exception { server.requestHandler(req -> { req.response().closeHandler(v -> { testComplete(); }); req.response().setChunked(true).write("some-data"); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { resp.handler(v -> { resp.request().connection().close(); }); }); await(); }
@Test public void testCloseHandlerWhenConnectionClose() throws Exception { server.requestHandler(req -> { HttpServerResponse resp = req.response(); resp.setChunked(true).write("some-data"); resp.closeHandler(v -> { checkHttpServerResponse(resp); testComplete(); }); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { resp.handler(v -> { resp.request().connection().close(); }); }); await(); }