@Test public void testServePendingRequests() throws Exception { int n = 10; waitFor(n); LinkedList<HttpServerRequest> requests = new LinkedList<>(); Set<HttpConnection> connections = new HashSet<>(); server.requestHandler(req -> { requests.add(req); connections.add(req.connection()); assertEquals(1, connections.size()); if (requests.size() == n) { while (requests.size() > 0) { requests.removeFirst().response().end(); } } }); startServer(); for (int i = 0;i < n;i++) { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> complete()).end(); } await(); }
@Test public void testServerConnectionHandler() throws Exception { AtomicInteger status = new AtomicInteger(); AtomicReference<HttpConnection> connRef = new AtomicReference<>(); server.connectionHandler(conn -> { assertEquals(0, status.getAndIncrement()); assertNull(connRef.getAndSet(conn)); }); server.requestHandler(req -> { assertEquals(1, status.getAndIncrement()); assertSame(connRef.get(), req.connection()); req.response().end(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { testComplete(); }); 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()); }
Set<HttpConnection> connections = Collections.synchronizedSet(new HashSet<>()); server.requestHandler(req -> { connections.add(req.connection()); switch (reqCount.getAndIncrement()) { case 0: req.connection().goAway(0); break; case 1:
@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 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(); }
@Override public HttpConnection connection() { return delegate.connection(); }
assertEquals(1, respEndHandlerCount.incrementAndGet()); }); req.connection().closeHandler(v -> { assertEquals(1, reqExceptionHandlerCount.get()); assertEquals(1, respExceptionHandlerCount.get());
@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 testDiscardConnectionWhenChannelBecomesInactive() throws Exception { AtomicInteger count = new AtomicInteger(); server.requestHandler(req -> { if (count.getAndIncrement() == 0) { Http2ServerConnection a = (Http2ServerConnection) req.connection(); SocketChannel channel = (SocketChannel) a.channel(); channel.shutdown(); } else { req.response().end(); } }); startServer(); AtomicBoolean closed = new AtomicBoolean(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> {})) .connectionHandler(conn -> conn.closeHandler(v -> closed.set(true))) .end(); AsyncTestBase.assertWaitUntil(closed::get); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { testComplete(); }).exceptionHandler(err -> { fail(); }).end(); 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(); }
AtomicLong time = new AtomicLong(); server.requestHandler(req -> { req.connection().closeHandler(v -> { complete(); });
waitFor(7); server.requestHandler(req -> { HttpConnection conn = req.connection(); req.exceptionHandler(err -> { assertFalse(Thread.holdsLock(conn));
indicatedServerName = req.connection().indicatedServerName(); assertEquals(version, req.version()); assertEquals(serverSSL, req.isSSL());
server = vertx.createHttpServer(serverOptions); server.requestHandler(req -> { req.connection().closeHandler(v -> { complete(); });
@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(); }