@Test public void testSharedServersRoundRobinWithOtherServerRunningOnDifferentPort() throws Exception { // Have a server running on a different port to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(8081)); theServer.requestHandler(req -> { fail("Should not process request"); }).listen(onSuccess(s -> latch.countDown())); awaitLatch(latch); testSharedServersRoundRobin(); }
private void testHttpClientResponseThrowsExceptionInHandler( String chunk, BiConsumer<HttpClientResponse, CountDownLatch> handler) throws Exception { server.requestHandler(req -> { HttpServerResponse resp = req.response(); if (chunk != null) { resp.end(chunk); } else { resp.end(); } }); startServer(); int num = 50; CountDownLatch latch = new CountDownLatch(num); for (int i = 0;i < num;i++) { client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", onSuccess(resp -> { handler.accept(resp, latch); })); } awaitLatch(latch); }
@Test public void testClosingVertxCloseSharedServers() throws Exception { int numServers = 2; Vertx vertx = Vertx.vertx(); List<HttpServerImpl> servers = new ArrayList<>(); for (int i = 0;i < numServers;i++) { HttpServer server = vertx.createHttpServer(createBaseServerOptions()).requestHandler(req -> { }); startServer(server); servers.add((HttpServerImpl) server); } CountDownLatch latch = new CountDownLatch(1); vertx.close(onSuccess(v -> { latch.countDown(); })); awaitLatch(latch); servers.forEach(server -> { assertTrue(server.isClosed()); }); }
@Test public void testSharedServersRoundRobinButFirstStartAndStopServer() throws Exception { // Start and stop a server on the same port/host before hand to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)); theServer.requestHandler(req -> { fail("Should not process request"); }).listen(onSuccess(s -> latch.countDown())); awaitLatch(latch); CountDownLatch closeLatch = new CountDownLatch(1); theServer.close(ar -> { assertTrue(ar.succeeded()); closeLatch.countDown(); }); assertTrue(closeLatch.await(10, TimeUnit.SECONDS)); testSharedServersRoundRobin(); }
latch1.countDown(); })); awaitLatch(latch1); HttpClient client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false).setDefaultPort(8080)); latch2.countDown(); })); awaitLatch(latch2); latch3.countDown(); })); awaitLatch(latch3); latch2.countDown(); })); awaitLatch(latch2);
@Test public void testClientConnectionExceptionHandler() throws Exception { server.requestHandler(req -> { NetSocket so = req.netSocket(); so.write(Buffer.buffer(TestUtils.randomAlphaString(40) + "\r\n")); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { }); req.connectionHandler(conn -> { conn.exceptionHandler(err -> { testComplete(); }); }); req.sendHead(); await(); }
server.close(onSuccess(v -> firstCloseLatch.countDown())); awaitLatch(firstCloseLatch); awaitLatch(startServerLatch); awaitLatch(reqLatch); awaitLatch(serverCloseLatch);
@Test public void testServerExceptionHandler() throws Exception { server.exceptionHandler(err -> { assertTrue(err instanceof TooLongFrameException); testComplete(); }); server.requestHandler(req -> { fail(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { }); req.putHeader("the_header", TestUtils.randomAlphaString(10000)); req.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); }
listenLatch.countDown(); })); awaitLatch(listenLatch); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setPipelining(true).setKeepAlive(true)); }); })).exceptionHandler(this::fail).end(); awaitLatch(doneLatch); assertEquals(Arrays.asList("/second", "/third"), responses); awaitLatch(respLatch); server.close(); assertEquals(1, connCount.get());
awaitLatch(latch); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", onSuccess(resp -> { assertSame(resp.request().connection(), connections.get(1));
listenLatch.countDown(); })); awaitLatch(listenLatch);
@Test public void testServerConnectionExceptionHandler() throws Exception { server.connectionHandler(conn -> { conn.exceptionHandler(err -> { assertTrue(err instanceof TooLongFrameException); testComplete(); }); }); server.requestHandler(req -> { req.response().end(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp1 -> { HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp2 -> { }); req.putHeader("the_header", TestUtils.randomAlphaString(10000)); req.sendHead(); }); await(); }
@Test public void testDontReuseConnectionWhenResponseEndsDuringAnOngoingRequest() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setPipelining(true).setKeepAlive(true)); server.requestHandler(req -> { req.response().end(); }); CountDownLatch serverLatch = new CountDownLatch(1); server.listen(ar -> { assertTrue(ar.succeeded()); serverLatch.countDown(); }); awaitLatch(serverLatch); HttpClientRequest req1 = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { resp.endHandler(v1 -> { // End request after the response ended vertx.setTimer(100, v2 -> { resp.request().end(); }); }); })); // Send head to the server and trigger the request handler req1.sendHead(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { testComplete(); }).end(); await(); }
latch.countDown(); })); awaitLatch(latch); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(keepAlive).setPipelining(pipelined));
@Test public void testHttpClientRequestShouldExceptionHandlerWhenTheClosedHandlerIsCalled() throws Exception { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { vertx.setTimer(1000, id -> { req.response().close(); }); }); startServer(); HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", resp -> { }).setChunked(true); CountDownLatch latch = new CountDownLatch(1); req.sendHead(version -> latch.countDown()); awaitLatch(latch); CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), req); req.exceptionHandler(err -> { Throwable failure = sender.close(); if (failure != null) { fail(failure); } else { testComplete(); } }); sender.send(); await(); }
testComplete(); }).setChunked(true).write(chunk1); awaitLatch(latch); req.end(chunk2); await();
awaitLatch(serverLatch);
listenLatch.countDown(); })); awaitLatch(listenLatch); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setPipelining(false).setKeepAlive(true));