@Test public void testMultipleRecursiveCallsAndPipelining() throws Exception { int sendRequests = 100; AtomicInteger receivedRequests = new AtomicInteger(); server.requestHandler(x -> { x.response().end("hello"); }) .listen(8080, r -> { if (r.succeeded()) { HttpClient client = vertx.createHttpClient(new HttpClientOptions() .setKeepAlive(true) .setPipelining(true) .setDefaultPort(8080) ); IntStream.range(0, 5).forEach(i -> recursiveCall(client, receivedRequests, sendRequests)); } }); await(); }
int n = 5; client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setPipeliningLimit(n).setMaxPoolSize(1)); CompletableFuture<Void> closeFuture = new CompletableFuture<>(); AtomicBoolean first = new AtomicBoolean(true);
@Test public void testDumpManyRequestsOnQueue() throws Exception { int sendRequests = 10000; AtomicInteger receivedRequests = new AtomicInteger(); vertx.createHttpServer(createBaseServerOptions()).requestHandler(r-> { r.response().end(); if (receivedRequests.incrementAndGet() == sendRequests) { testComplete(); } }).listen(onSuccess(s -> { HttpClientOptions ops = createBaseClientOptions() .setDefaultPort(DEFAULT_HTTP_PORT) .setPipelining(true) .setKeepAlive(true); HttpClient client = vertx.createHttpClient(ops); IntStream.range(0, sendRequests).forEach(x -> client.getNow("/", r -> {})); })); await(); }
@Test public void testPoolingNoKeepAliveAndPipelining() { try { vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false).setPipelining(true)); fail(); } catch (IllegalStateException ignore) { } }
@Test public void testClientContextWithKeepAlive() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(false).setMaxPoolSize(1)); testClientContext(); }
@Test public void testClientContextWithPipelining() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1)); testClientContext(); }
@Test public void testPipelinedWithPendingResponse() throws Exception { int numReq = 10; waitFor(numReq); AtomicInteger inflight = new AtomicInteger(); AtomicInteger count = new AtomicInteger(); server.requestHandler(req -> { int val = count.getAndIncrement(); assertEquals(0, inflight.getAndIncrement()); vertx.setTimer(100, v -> { inflight.decrementAndGet(); req.response().end("" + val); }); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); for (int i = 0;i < numReq;i++) { String expected = "" + i; client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { resp.bodyHandler(body -> { assertEquals(expected, body.toString()); complete(); }); })).end(TestUtils.randomAlphaString(1024)); } await(); }
int maxPoolSize = 10; client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(keepAlive).setPipelining(pipelining).setMaxPoolSize(maxPoolSize));
startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); for (int i = 0;i < numReq;i++) { String expected = "" + i;
@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(); }
@Test public void testHandleInvalid204Response() throws Exception { int numReq = 3; waitFor(numReq); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setKeepAlive(true).setMaxPoolSize(1)); server.requestHandler(r -> { // Generate an invalid response for the pipe-lined r.response().setChunked(true).setStatusCode(204).end(); }).listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v1 -> { for (int i = 0;i < numReq;i++) { AtomicInteger count = new AtomicInteger(); HttpClientRequest post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(r -> { r.endHandler(v2 -> { complete(); }); })).exceptionHandler(err -> { if (count.incrementAndGet() == 1) { complete(); } }); post.end(); } })); await(); }
@Test public void testPerPeerPooling() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setMaxPoolSize(1) .setKeepAlive(true) .setPipelining(false)); testPerXXXPooling((i, handler) -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", handler).setHost("host" + i + ":8080"), HttpServerRequest::host); }
startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { }).end(TestUtils.randomAlphaString(1024));
@Test public void testMaxWaitQueueSizeIsRespected() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setDefaultHost(DEFAULT_HTTP_HOST).setDefaultPort(DEFAULT_HTTP_PORT) .setPipelining(false).setMaxWaitQueueSize(0).setMaxPoolSize(2)); waitFor(3); Set<String> expected = new HashSet<>(Arrays.asList("/1", "/2")); server.requestHandler(req -> { assertTrue(expected.contains(req.path())); complete(); }); startServer(); HttpClientRequest req1 = client.get("/1", onFailure(err -> { })); HttpClientRequest req2 = client.get("/2", onFailure(resp -> { })); HttpClientRequest req3 = client.get("/3", onFailure(t -> { assertTrue("Incorrect exception: " + t.getClass().getName(), t instanceof ConnectionPoolTooBusyException); complete(); })); req1.end(); req2.end(); req3.end(); await(); }
@Repeat(times = 10) @Test public void testCloseServerConnectionWithPendingMessages() throws Exception { int n = 5; server.requestHandler(req -> { vertx.setTimer(100, id -> { req.response().close(); }); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(n).setPipelining(true)); AtomicBoolean completed = new AtomicBoolean(); for (int i = 0; i < n * 2; i++) { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(resp -> { })) .connectionHandler(conn -> { conn.closeHandler(v -> { if (completed.compareAndSet(false, true)) { testComplete(); } }); }).end(); } await(); }
@Test public void testPerHostPooling() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setMaxPoolSize(1) .setKeepAlive(true) .setPipelining(false)); testPerXXXPooling((i, handler) -> client.get(DEFAULT_HTTP_PORT, "host" + i, "/somepath", handler).setHost("host:8080") .putHeader("key", "host" + i), req -> req.getHeader("key")); }
@Test public void testSendFilePipelined() throws Exception { int n = 4; waitFor(n); File sent = TestUtils.tmpFile(".dat", 16 * 1024); server.requestHandler( req -> { req.response().sendFile(sent.getAbsolutePath()); }); startServer(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setPipelining(true).setMaxPoolSize(1)); for (int i = 0;i < n;i++) { client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.exceptionHandler(this::fail); resp.bodyHandler(body -> { complete(); }); })); } await(); } }
@Test public void testBeginPipelinedRequestByResponseSentBeforeRequestCompletion() throws Exception { server.requestHandler(req -> { if (req.method() == HttpMethod.POST) { req.pause(); vertx.setTimer(100, id1 -> { req.response().end(); vertx.setTimer(100, id2 -> { req.resume(); }); }); } else if (req.method() == HttpMethod.GET) { req.response().end(); } }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { }).end(TestUtils.randomAlphaString(1024)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { testComplete(); }); await(); }
@Test public void testPerPeerPoolingWithProxy() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setMaxPoolSize(1) .setKeepAlive(true) .setPipelining(false).setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost(DEFAULT_HTTP_HOST) .setPort(DEFAULT_HTTP_PORT))); testPerXXXPooling((i, handler) -> client.get(80, "host" + i, "/somepath", handler), HttpServerRequest::host); }
@Test public void testBeginPipelinedRequestByResponseSentOnRequestCompletion() throws Exception { server.requestHandler(req -> { if (req.method() == HttpMethod.POST) { req.pause(); vertx.setTimer(100, id -> { req.resume(); }); } req.endHandler(v -> { req.response().end(); }); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setPipelining(true).setMaxPoolSize(1).setKeepAlive(true)); client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { }).end(TestUtils.randomAlphaString(1024)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { testComplete(); }); await(); }