@Test public void testRequestTimeoutCanceledWhenRequestHasAnOtherError() { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(exception::set)) .setTimeout(800) .end(); vertx.setTimer(1500, id -> { assertNotNull("Expected an exception to be set", exception.get()); assertFalse("Expected to not end with timeout exception, but did: " + exception.get(), exception.get() instanceof TimeoutException); testComplete(); }); await(); }
@Test public void testRequestTimesoutWhenIndicatedPeriodExpiresWithoutAResponseFromRemoteServer() { server.requestHandler(noOpHandler()); // No response handler so timeout triggers AtomicBoolean failed = new AtomicBoolean(); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(t -> { // Catch the first, the second is going to be a connection closed exception when the // server is shutdown on testComplete if (failed.compareAndSet(false, true)) { assertTrue("Expected to end with timeout exception but ended with other exception: " + t, t instanceof TimeoutException); testComplete(); } })); req.setTimeout(1000); req.end(); })); await(); }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(n)); for (int i = 0;i < n;i++) { AtomicBoolean responseReceived = new AtomicBoolean(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { try { Thread.sleep(150); } catch (InterruptedException e) { fail(e); } responseReceived.set(true); // Complete later, if some timeout tasks have been queued, this will be executed after vertx.runOnContext(v -> complete()); }).exceptionHandler(err -> { fail("Was not expecting to get a timeout after the response is received"); }).setTimeout(500).end(); } } }, new DeploymentOptions().setWorker(true));
@Test public void testRequestTimeoutCanceledWhenRequestEndsNormally() { server.requestHandler(req -> req.response().end()); server.listen(onSuccess(s -> { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); req.exceptionHandler(exception::set); req.setTimeout(500); req.end(); vertx.setTimer(1000, id -> { assertNull("Did not expect any exception", exception.get()); testComplete(); }); })); await(); }
@Test public void testFollowRedirectPropagatesTimeout() throws Exception { AtomicInteger redirections = new AtomicInteger(); server.requestHandler(req -> { switch (redirections.getAndIncrement()) { case 0: String scheme = createBaseServerOptions().isSsl() ? "https" : "http"; req.response().setStatusCode(307).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/whatever").end(); break; } }); startServer(); AtomicBoolean done = new AtomicBoolean(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(t -> { if (done.compareAndSet(false, true)) { assertEquals(2, redirections.get()); testComplete(); } })).setFollowRedirects(true) .setTimeout(500).end(); await(); }
@Test public void testHttpClientRequestTimeoutResetsTheConnection() throws Exception { waitFor(3); server.requestHandler(req -> { AtomicBoolean errored = new AtomicBoolean(); req.exceptionHandler(err -> { if (errored.compareAndSet(false, true)) { complete(); } }); }); startServer(); HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> { complete(); })); AtomicBoolean errored = new AtomicBoolean(); req.exceptionHandler(err -> { if (errored.compareAndSet(false, true)) { complete(); } }); CountDownLatch latch = new CountDownLatch(1); req.setChunked(true).sendHead(version -> latch.countDown()); awaitLatch(latch); req.setTimeout(100); await(); }
assertEquals(2, connections.size()); testComplete(); }).setTimeout(5000).exceptionHandler(this::fail).end();
@Test public void testResponseDataTimeout() { Buffer expected = TestUtils.randomBuffer(1000); server.requestHandler(req -> { req.response().setChunked(true).write(expected); }); server.listen(onSuccess(s -> { Buffer received = Buffer.buffer(); HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.request().setTimeout(500); resp.handler(received::appendBuffer); })); AtomicInteger count = new AtomicInteger(); req.exceptionHandler(t -> { if (count.getAndIncrement() == 0) { assertTrue(t instanceof TimeoutException); assertEquals(expected, received); testComplete(); } }); req.sendHead(); })); await(); }
@Test public void testRequestTimeoutExtendedWhenResponseChunksReceived() { long timeout = 2000; int numChunks = 100; AtomicInteger count = new AtomicInteger(0); long interval = timeout * 2 / numChunks; server.requestHandler(req -> { req.response().setChunked(true); vertx.setPeriodic(interval, timerID -> { req.response().write("foo"); if (count.incrementAndGet() == numChunks) { req.response().end(); vertx.cancelTimer(timerID); } }); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.endHandler(v -> testComplete()); })); req.exceptionHandler(t -> fail("Should not be called")); req.setTimeout(timeout); req.end(); })); await(); }
@Test public void testSendingGoAwayDiscardsTheConnection() throws Exception { AtomicInteger reqCount = new AtomicInteger(); server.requestHandler(req -> { switch (reqCount.getAndIncrement()) { case 0: req.response().setChunked(true).write("some-data"); break; case 1: req.response().end(); break; default: fail(); } }); startServer(); client.getNow(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> { resp.request().connection().goAway(0); client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp2 -> { testComplete(); }).setTimeout(5000).exceptionHandler(this::fail).end(); })); await(); }
assertTrue(t instanceof TimeoutException); })); req.setTimeout(500); req.end(); })); req.exceptionHandler(t -> fail("Should not throw exception")); req.setTimeout(3000); req.end(); }));
latch.countDown(); })); req.setTimeout(responseDelay / 2);
@Test public void testInitialMaxConcurrentStreamZero() throws Exception { server.close(); server = vertx.createHttpServer(createBaseServerOptions().setInitialSettings(new Http2Settings().setMaxConcurrentStreams(0))); server.requestHandler(req -> { req.response().end(); }); server.connectionHandler(conn -> { vertx.setTimer(500, id -> { conn.updateSettings(new Http2Settings().setMaxConcurrentStreams(10)); }); }); startServer(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { testComplete(); }).connectionHandler(conn -> { assertEquals(10, conn.remoteSettings().getMaxConcurrentStreams()); }).setTimeout(10000).exceptionHandler(this::fail).end(); await(); }
@Test public void testClientRequestArguments() throws Exception { HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); assertNullPointerException(() -> req.putHeader((String) null, "someValue")); assertNullPointerException(() -> req.putHeader((CharSequence) null, "someValue")); assertNullPointerException(() -> req.putHeader("someKey", (Iterable<String>) null)); assertNullPointerException(() -> req.write((Buffer) null)); assertNullPointerException(() -> req.write((String) null)); assertNullPointerException(() -> req.write(null, "UTF-8")); assertNullPointerException(() -> req.write("someString", null)); assertNullPointerException(() -> req.end((Buffer) null)); assertNullPointerException(() -> req.end((String) null)); assertNullPointerException(() -> req.end(null, "UTF-8")); assertNullPointerException(() -> req.end("someString", null)); assertIllegalArgumentException(() -> req.setTimeout(0)); }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(n)); for (int i = 0;i < n;i++) { AtomicBoolean responseReceived = new AtomicBoolean(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { try { Thread.sleep(150); } catch (InterruptedException e) { fail(e); } responseReceived.set(true); // Complete later, if some timeout tasks have been queued, this will be executed after vertx.runOnContext(v -> complete()); }).exceptionHandler(err -> { fail("Was not expecting to get a timeout after the response is received"); }).setTimeout(500).end(); } } }, new DeploymentOptions().setWorker(true));
@Test public void testRequestTimeoutCanceledWhenRequestEndsNormally() { server.requestHandler(req -> req.response().end()); server.listen(onSuccess(s -> { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); req.exceptionHandler(exception::set); req.setTimeout(500); req.end(); vertx.setTimer(1000, id -> { assertNull("Did not expect any exception", exception.get()); testComplete(); }); })); await(); }
@Test public void testRequestTimeoutCanceledWhenRequestHasAnOtherError() { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { fail("End should not be called because the request should fail to connect"); }); req.exceptionHandler(exception::set); req.setTimeout(800); req.end(); vertx.setTimer(1500, id -> { assertNotNull("Expected an exception to be set", exception.get()); assertFalse("Expected to not end with timeout exception, but did: " + exception.get(), exception.get() instanceof TimeoutException); testComplete(); }); await(); }
@Test public void testRequestTimesoutWhenIndicatedPeriodExpiresWithoutAResponseFromRemoteServer() { server.requestHandler(noOpHandler()); // No response handler so timeout triggers AtomicBoolean failed = new AtomicBoolean(); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { fail("End should not be called because the request should timeout"); }); req.exceptionHandler(t -> { // Catch the first, the second is going to be a connection closed exception when the // server is shutdown on testComplete if (failed.compareAndSet(false, true)) { assertTrue("Expected to end with timeout exception but ended with other exception: " + t, t instanceof TimeoutException); testComplete(); } }); req.setTimeout(1000); req.end(); })); await(); }
@Test public void testInitialMaxConcurrentStreamZero() throws Exception { server.close(); server = vertx.createHttpServer(createBaseServerOptions().setInitialSettings(new Http2Settings().setMaxConcurrentStreams(0))); server.requestHandler(req -> { req.response().end(); }); server.connectionHandler(conn -> { vertx.setTimer(500, id -> { conn.updateSettings(new Http2Settings().setMaxConcurrentStreams(10)); }); }); startServer(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { testComplete(); }).connectionHandler(conn -> { assertEquals(10, conn.remoteSettings().getMaxConcurrentStreams()); }).setTimeout(10000).exceptionHandler(this::fail).end(); await(); }
@Test public void testClientRequestArguments() throws Exception { HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); assertNullPointerException(() -> req.putHeader((String) null, "someValue")); assertNullPointerException(() -> req.putHeader((CharSequence) null, "someValue")); assertNullPointerException(() -> req.putHeader("someKey", (Iterable<String>) null)); assertNullPointerException(() -> req.write((Buffer) null)); assertNullPointerException(() -> req.write((String) null)); assertNullPointerException(() -> req.write(null, "UTF-8")); assertNullPointerException(() -> req.write("someString", null)); assertNullPointerException(() -> req.end((Buffer) null)); assertNullPointerException(() -> req.end((String) null)); assertNullPointerException(() -> req.end(null, "UTF-8")); assertNullPointerException(() -> req.end("someString", null)); assertIllegalArgumentException(() -> req.setTimeout(0)); }