diff --git a/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/CachingWebClientTest.java b/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/CachingWebClientTest.java index 14689ff1e8..16029ff5d4 100644 --- a/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/CachingWebClientTest.java +++ b/vertx-web-client/src/test/java/io/vertx/ext/web/client/tests/CachingWebClientTest.java @@ -5,9 +5,7 @@ import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.*; -import io.vertx.junit5.Checkpoint; import io.vertx.junit5.VertxTest; -import io.vertx.junit5.VertxTestContext; import io.vertx.ext.web.client.*; import io.vertx.ext.web.client.impl.cache.CacheKey; import io.vertx.ext.web.client.impl.cache.CachedHttpResponse; @@ -19,8 +17,8 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import org.junit.jupiter.api.BeforeEach; @@ -78,13 +76,13 @@ private void startMockServer(Consumer reqHandler) { .await(); } - private void startMockServer(VertxTestContext testContext, String cacheControl) { + private void startMockServer(String cacheControl) { startMockServer(req -> { req.response().headers().set("Cache-Control", cacheControl); }); } - private String executeRequestBlocking(VertxTestContext testContext, WebClient client, Consumer> reqConsumer) { + private String executeRequestBlocking(WebClient client, Consumer> reqConsumer) { HttpRequest request = client.get("localhost", "/"); reqConsumer.accept(request); @@ -99,24 +97,24 @@ private String executeRequestBlocking(VertxTestContext testContext, WebClient cl return body; } - private String executeGetBlocking(VertxTestContext testContext, Consumer> reqConsumer) { - return executeRequestBlocking(testContext, defaultClient, reqConsumer); + private String executeGetBlocking(Consumer> reqConsumer) { + return executeRequestBlocking(defaultClient, reqConsumer); } - private String executeGetBlocking(VertxTestContext testContext) { - return executeRequestBlocking(testContext, defaultClient, req -> {}); + private String executeGetBlocking() { + return executeRequestBlocking(defaultClient, req -> {}); } - private String executeGetBlocking(VertxTestContext testContext, String uri) { - return executeGetBlocking(testContext, req -> req.uri(uri)); + private String executeGetBlocking(String uri) { + return executeGetBlocking(req -> req.uri(uri)); } - private String executeGetBlocking(VertxTestContext testContext, WebClient client) { - return executeRequestBlocking(testContext, client, req -> {}); + private String executeGetBlocking(WebClient client) { + return executeRequestBlocking(client, req -> {}); } - private String executeGetBlocking(VertxTestContext testContext, WebClient client, Consumer> reqConsumer) { - return executeRequestBlocking(testContext, client, reqConsumer); + private String executeGetBlocking(WebClient client, Consumer> reqConsumer) { + return executeRequestBlocking(client, reqConsumer); } private void assertCacheUse(HttpMethod method, WebClient client, boolean shouldCacheBeUsed) { @@ -152,51 +150,51 @@ private void assertNotCached(WebClient client) { assertCacheUse(HttpMethod.GET, client, false); } - private void assertNotCached(VertxTestContext testContext) { + private void assertNotCached() { assertNotCached(defaultClient); } // Non-GET methods that we shouldn't cache @Test - public void testPOSTNotCached(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=600"); + public void testPOSTNotCached() throws Exception { + startMockServer("public, max-age=600"); assertCacheUse(HttpMethod.POST, defaultClient, false); - testContext.completeNow(); + } @Test - public void testPUTNotCached(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=600"); + public void testPUTNotCached() throws Exception { + startMockServer("public, max-age=600"); assertCacheUse(HttpMethod.PUT, defaultClient, false); - testContext.completeNow(); + } @Test - public void testPATCHNotCached(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=600"); + public void testPATCHNotCached() throws Exception { + startMockServer("public, max-age=600"); assertCacheUse(HttpMethod.PATCH, defaultClient, false); - testContext.completeNow(); + } @Test - public void testDELETENotCached(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=600"); + public void testDELETENotCached() throws Exception { + startMockServer("public, max-age=600"); assertCacheUse(HttpMethod.DELETE, defaultClient, false); - testContext.completeNow(); + } // Cache-Control: no-store || no-cache @Test - public void testNoStore(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "no-store"); - assertNotCached(testContext); - testContext.completeNow(); + public void testNoStore() throws Exception { + startMockServer("no-store"); + assertNotCached(); + } @Test - public void testNoCache(VertxTestContext testContext) throws Exception { + public void testNoCache() throws Exception { final AtomicBoolean replyWith304 = new AtomicBoolean(false); startMockServer(req -> { @@ -210,12 +208,12 @@ public void testNoCache(VertxTestContext testContext) throws Exception { } }); - String body1 = executeGetBlocking(testContext); // Initial request - String body2 = executeGetBlocking(testContext); // Another request, reply with new value + String body1 = executeGetBlocking(); // Initial request + String body2 = executeGetBlocking(); // Another request, reply with new value replyWith304.compareAndSet(false, true); - String body3 = executeGetBlocking(testContext); // Another request, server says cache is valid + String body3 = executeGetBlocking(); // Another request, server says cache is valid replyWith304.compareAndSet(true, false); - String body4 = executeGetBlocking(testContext); // Another request, reply with new value + String body4 = executeGetBlocking(); // Another request, reply with new value assertNotEquals(body1, body2); assertNotEquals(body1, body3); @@ -223,70 +221,70 @@ public void testNoCache(VertxTestContext testContext) throws Exception { assertEquals(body2, body3); assertNotEquals(body2, body4); assertNotEquals(body3, body4); - testContext.completeNow(); + } // Cache-Control: public @Test - public void testPublicWithMaxAge(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=600"); + public void testPublicWithMaxAge() throws Exception { + startMockServer("public, max-age=600"); assertCached(); - testContext.completeNow(); + } @Test - public void testPublicWithMaxAgeMultiHeader(VertxTestContext testContext) throws Exception { + public void testPublicWithMaxAgeMultiHeader() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public"); req.response().headers().add("Cache-Control", "max-age=600"); }); assertCached(); - testContext.completeNow(); + } @Test - public void testPublicWithoutMaxAge(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public"); + public void testPublicWithoutMaxAge() throws Exception { + startMockServer("public"); assertCached(); - testContext.completeNow(); + } @Test - public void testPublicMaxAgeZero(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public,max-age=0"); - assertNotCached(testContext); - testContext.completeNow(); + public void testPublicMaxAgeZero() throws Exception { + startMockServer("public,max-age=0"); + assertNotCached(); + } @Test - public void testPublicSharedMaxAge(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, s-maxage=600"); + public void testPublicSharedMaxAge() throws Exception { + startMockServer("public, s-maxage=600"); assertCached(); - testContext.completeNow(); + } @Test - public void testPublicSharedMaxAgeZero(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, s-maxage=0"); - assertNotCached(testContext); - testContext.completeNow(); + public void testPublicSharedMaxAgeZero() throws Exception { + startMockServer("public, s-maxage=0"); + assertNotCached(); + } @Test - public void testPublicWithExpiresNow(VertxTestContext testContext) throws Exception { + public void testPublicWithExpiresNow() throws Exception { startMockServer(req -> { req.response().headers().set("Cache-Control", "public"); req.response().headers().set("Expires", DateFormatter.format(new Date())); }); - assertNotCached(testContext); - testContext.completeNow(); + assertNotCached(); + } @Test - public void testPublicWithExpiresPast(VertxTestContext testContext) throws Exception { + public void testPublicWithExpiresPast() throws Exception { String expires = DateFormatter.format(new Date( System.currentTimeMillis() - Duration.ofMinutes(5).toMillis() )); @@ -295,12 +293,12 @@ public void testPublicWithExpiresPast(VertxTestContext testContext) throws Excep req.response().headers().set("Expires", expires); }); - assertNotCached(testContext); - testContext.completeNow(); + assertNotCached(); + } @Test - public void testPublicWithExpiresFuture(VertxTestContext testContext) throws Exception { + public void testPublicWithExpiresFuture() throws Exception { String expires = DateFormatter.format(new Date( System.currentTimeMillis() + Duration.ofMinutes(5).toMillis() )); @@ -310,11 +308,11 @@ public void testPublicWithExpiresFuture(VertxTestContext testContext) throws Exc }); assertCached(); - testContext.completeNow(); + } @Test - public void testPublicWithMaxAgeFutureAndExpiresPast(VertxTestContext testContext) throws Exception { + public void testPublicWithMaxAgeFutureAndExpiresPast() throws Exception { String expires = DateFormatter.format(new Date( System.currentTimeMillis() - Duration.ofMinutes(5).toMillis() )); @@ -324,16 +322,15 @@ public void testPublicWithMaxAgeFutureAndExpiresPast(VertxTestContext testContex }); assertCached(); - testContext.completeNow(); + } @Test - public void testPublicWithMaxAgeFutureAndExpiresFuture(VertxTestContext testContext) throws Exception { + public void testPublicWithMaxAgeFutureAndExpiresFuture() throws Exception { String expires = DateFormatter.format(new Date( System.currentTimeMillis() + Duration.ofMinutes(5).toMillis() )); - Checkpoint waiter = testContext.checkpoint(); AtomicBoolean req1Completed = new AtomicBoolean(false); startMockServer(req -> { @@ -354,8 +351,7 @@ public void testPublicWithMaxAgeFutureAndExpiresFuture(VertxTestContext testCont .await().bodyAsString(); // HTTP cache only has 1 second resolution, so this must be 1+ seconds past than the max-age - vertx.setTimer(2000, l -> waiter.flag()); - waiter.await(); + Thread.sleep(2000); String body3 = defaultClient .get("localhost", "/") @@ -368,11 +364,11 @@ public void testPublicWithMaxAgeFutureAndExpiresFuture(VertxTestContext testCont assertNotNull(body3); assertEquals(body1, body2); assertNotEquals(body1, body3); - testContext.completeNow(); + } @Test - public void testPublicWithMaxAgeZeroAndExpiresFuture(VertxTestContext testContext) throws Exception { + public void testPublicWithMaxAgeZeroAndExpiresFuture() throws Exception { String expires = DateFormatter.format(new Date( System.currentTimeMillis() + Duration.ofMinutes(5).toMillis() )); @@ -381,81 +377,77 @@ public void testPublicWithMaxAgeZeroAndExpiresFuture(VertxTestContext testContex req.response().headers().set("Expires", expires); }); - assertNotCached(testContext); - testContext.completeNow(); + assertNotCached(); + } @Test - public void testPublicWithMaxAgeZeroAndExpiresZero(VertxTestContext testContext) throws Exception { + public void testPublicWithMaxAgeZeroAndExpiresZero() throws Exception { String expires = DateFormatter.format(new Date()); startMockServer(req -> { req.response().headers().set("Cache-Control", "public, max-age=0"); req.response().headers().set("Expires", expires); }); - assertNotCached(testContext); - testContext.completeNow(); + assertNotCached(); + } @Test - public void testPublicAndPrivate(VertxTestContext testContext) throws Exception { + public void testPublicAndPrivate() throws Exception { // This is a silly case because it is invalid, but it validates that we err on the side of not // caching responses. - startMockServer(testContext, "public, private, max-age=300"); - assertNotCached(testContext); - testContext.completeNow(); + startMockServer("public, private, max-age=300"); + assertNotCached(); + } @Test - public void testUpdateStaleResponse(VertxTestContext testContext) throws Exception { - Checkpoint waiter = testContext.checkpoint(); - - startMockServer(testContext, "public, max-age=1"); + public void testUpdateStaleResponse() throws Exception { + startMockServer("public, max-age=1"); - String body1 = executeGetBlocking(testContext); + String body1 = executeGetBlocking(); - vertx.setTimer(2000, l -> waiter.flag()); - waiter.await(); + Thread.sleep(2000); - String body2 = executeGetBlocking(testContext); - String body3 = executeGetBlocking(testContext); + String body2 = executeGetBlocking(); + String body3 = executeGetBlocking(); assertNotEquals(body1, body2); assertEquals(body2, body3); - testContext.completeNow(); + } @Test - public void testCacheHitWontAllocateRequest(VertxTestContext testContext) throws Exception { + public void testCacheHitWontAllocateRequest() throws Exception { - Checkpoint busyLatch = testContext.checkpoint(5); + CountDownLatch busyLatch = new CountDownLatch(5); server.requestHandler(req -> { switch (req.path()) { case "/cached": req.response().putHeader(HttpHeaders.CACHE_CONTROL, "public, max-age=1").end(UUID.randomUUID().toString()); break; case "/blocked": - busyLatch.flag(); + busyLatch.countDown(); break; } }); server.listen().await(); - String expected = executeGetBlocking(testContext, "/cached"); + String expected = executeGetBlocking("/cached"); for (int i = 0; i < PoolOptions.DEFAULT_MAX_POOL_SIZE; i++) { HttpRequest request = defaultClient.get("localhost", "/blocked"); request.send(); } busyLatch.await(); - assertEquals(executeGetBlocking(testContext, "/cached"), expected); + assertEquals(executeGetBlocking("/cached"), expected); + } @Test - public void test304NotModifiedResponse(VertxTestContext testContext) throws Exception { + public void test304NotModifiedResponse() throws Exception { AtomicBoolean primerDone = new AtomicBoolean(); - Checkpoint primer = testContext.checkpoint(); - Checkpoint waiter = testContext.checkpoint(); startMockServer(req -> { HttpServerResponse resp = req.response(); @@ -469,27 +461,26 @@ public void test304NotModifiedResponse(VertxTestContext testContext) throws Exce .setStatusCode(200) .putHeader("etag", "etag_value") .end(UUID.randomUUID().toString()) - .onComplete(v -> { primerDone.set(true); primer.flag(); }); + .onComplete(v -> primerDone.set(true)); } }); - String body1 = executeGetBlocking(testContext); - primer.await(); + String body1 = executeGetBlocking(); + Thread.sleep(100); - vertx.setTimer(2000L, l -> waiter.flag()); - waiter.await(); + Thread.sleep(2000); - String body2 = executeGetBlocking(testContext); + String body2 = executeGetBlocking(); assertEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testStaleWhileRevalidate(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=1, stale-while-revalidate=2"); + public void testStaleWhileRevalidate() throws Exception { + startMockServer("public, max-age=1, stale-while-revalidate=2"); - String body1 = executeGetBlocking(testContext); + String body1 = executeGetBlocking(); String key = defaultCacheStore.db.keySet().iterator().next(); assertEquals(defaultCacheStore.db.get(key).getBody().toString(), body1); @@ -497,84 +488,75 @@ public void testStaleWhileRevalidate(VertxTestContext testContext) throws Except // Wait > max-age but < stale-while-revalidate Thread.sleep(2000); - String body2 = executeGetBlocking(testContext); + String body2 = executeGetBlocking(); // Wait > max-age + stale-while-revalidate but account for already waited Thread.sleep(2000); assertNotEquals(defaultCacheStore.db.get(key).getBody().toString(), body1); - testContext.completeNow(); + } @Test - public void testStaleWhileRevalidateExpired(VertxTestContext testContext) throws Exception { - Checkpoint waiter1 = testContext.checkpoint(); - Checkpoint waiter2 = testContext.checkpoint(); - - startMockServer(testContext, "public, max-age=1, stale-while-revalidate=1"); + public void testStaleWhileRevalidateExpired() throws Exception { + startMockServer("public, max-age=1, stale-while-revalidate=1"); - String body1 = executeGetBlocking(testContext); + String body1 = executeGetBlocking(); // max-age 1 + stale-while-revalidate 1 + leeway 1 => 3s - vertx.setTimer(3000, l -> waiter1.flag()); - waiter1.await(); + Thread.sleep(3000); - String body2 = executeGetBlocking(testContext); + String body2 = executeGetBlocking(); // max-age 1 + stale-while-revalidate 1 + leeway 1 => 3s - vertx.setTimer(3000, l -> waiter2.flag()); - waiter2.await(); + Thread.sleep(3000); - String body3 = executeGetBlocking(testContext); + String body3 = executeGetBlocking(); assertNotEquals(body1, body2); assertNotEquals(body1, body3); assertNotEquals(body2, body3); - testContext.completeNow(); + } @Test - public void testStaleIfError(VertxTestContext testContext) throws Exception { - AtomicBoolean waiterDone = new AtomicBoolean(); - Checkpoint waiter = testContext.checkpoint(); + public void testStaleIfError() throws Exception { + AtomicBoolean stale = new AtomicBoolean(); startMockServer(req -> { req.response().headers().set(HttpHeaders.CACHE_CONTROL, "public, max-age=1, stale-if-error=2"); - if (waiterDone.get()) { + if (stale.get()) { req.response().setStatusCode(503); req.response().end(); } }); - String body1 = executeGetBlocking(testContext); - vertx.setTimer(2000L, l -> { waiterDone.set(true); waiter.flag(); }); - waiter.await(); - String body2 = executeGetBlocking(testContext); + String body1 = executeGetBlocking(); + Thread.sleep(2000); + stale.set(true); + String body2 = executeGetBlocking(); assertEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testStaleIfErrorExpired(VertxTestContext testContext) throws Exception { - AtomicBoolean waiter1Done = new AtomicBoolean(); - Checkpoint waiter1 = testContext.checkpoint(); - Checkpoint waiter2 = testContext.checkpoint(); + public void testStaleIfErrorExpired() throws Exception { + AtomicBoolean stale = new AtomicBoolean(); startMockServer(req -> { req.response().headers().set(HttpHeaders.CACHE_CONTROL, "public, max-age=1, stale-if-error=2"); - if (waiter1Done.get()) { + if (stale.get()) { req.response().setStatusCode(503); req.response().end(); } }); - String body1 = executeGetBlocking(testContext); - vertx.setTimer(2000L, l -> { waiter1Done.set(true); waiter1.flag(); }); - waiter1.await(); + String body1 = executeGetBlocking(); + Thread.sleep(2000); + stale.set(true); - String body2 = executeGetBlocking(testContext); - vertx.setTimer(3000L, l -> waiter2.flag()); - waiter2.await(); + String body2 = executeGetBlocking(); + Thread.sleep(3000); HttpResponse response = defaultClient .get("localhost", "/") @@ -584,107 +566,108 @@ public void testStaleIfErrorExpired(VertxTestContext testContext) throws Excepti assertEquals(body1, body2); assertNull(response.bodyAsString()); assertEquals(503, response.statusCode()); + } @Test - public void testMatchingPaths(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=300"); + public void testMatchingPaths() throws Exception { + startMockServer("public, max-age=300"); - String body1 = executeGetBlocking(testContext, "/path/to/resource"); - String body2 = executeGetBlocking(testContext, "/path/to/resource"); + String body1 = executeGetBlocking("/path/to/resource"); + String body2 = executeGetBlocking("/path/to/resource"); assertEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testDifferentPaths(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=300"); + public void testDifferentPaths() throws Exception { + startMockServer("public, max-age=300"); - String body1 = executeGetBlocking(testContext, "/path/to/resource"); - String body2 = executeGetBlocking(testContext, "/other/path"); + String body1 = executeGetBlocking("/path/to/resource"); + String body2 = executeGetBlocking("/other/path"); assertNotEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testWithMatchingQueryParams(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=300"); + public void testWithMatchingQueryParams() throws Exception { + startMockServer("public, max-age=300"); - String body1 = executeGetBlocking(testContext, req -> { + String body1 = executeGetBlocking(req -> { req.setQueryParam("q", "search"); }); - String body2 = executeGetBlocking(testContext, req -> { + String body2 = executeGetBlocking(req -> { req.setQueryParam("q", "search"); }); assertEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testWithDifferentQueryParams(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=300"); + public void testWithDifferentQueryParams() throws Exception { + startMockServer("public, max-age=300"); - String body1 = executeGetBlocking(testContext, req -> { + String body1 = executeGetBlocking(req -> { req.setQueryParam("q", "search"); }); - String body2 = executeGetBlocking(testContext, req -> { + String body2 = executeGetBlocking(req -> { req.setQueryParam("q", "other"); }); assertNotEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testWithDifferentQueryParamOrdering(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "public, max-age=300"); + public void testWithDifferentQueryParamOrdering() throws Exception { + startMockServer("public, max-age=300"); - String body1 = executeGetBlocking(testContext, req -> { + String body1 = executeGetBlocking(req -> { req .setQueryParam("q", "search") .setQueryParam("param", "value"); }); - String body2 = executeGetBlocking(testContext, req -> { + String body2 = executeGetBlocking(req -> { req .setQueryParam("param", "value") .setQueryParam("q", "search"); }); assertEquals(body1, body2); - testContext.completeNow(); + } // Cache-Control: private with client NOT enabled private caching @Test - public void testPrivate(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "private"); - assertNotCached(testContext); - testContext.completeNow(); + public void testPrivate() throws Exception { + startMockServer("private"); + assertNotCached(); + } @Test - public void testPrivateMaxAge(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "private, max-age=300"); - assertNotCached(testContext); - testContext.completeNow(); + public void testPrivateMaxAge() throws Exception { + startMockServer("private, max-age=300"); + assertNotCached(); + } @Test - public void testPrivateMaxAgeZero(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "private, max-age=0"); - assertNotCached(testContext); - testContext.completeNow(); + public void testPrivateMaxAgeZero() throws Exception { + startMockServer("private, max-age=0"); + assertNotCached(); + } @Test - public void testPrivateExpires(VertxTestContext testContext) throws Exception { + public void testPrivateExpires() throws Exception { String expires = DateFormatter.format(new Date( System.currentTimeMillis() + Duration.ofMinutes(5).toMillis() )); @@ -693,195 +676,192 @@ public void testPrivateExpires(VertxTestContext testContext) throws Exception { req.response().headers().add("Expires", expires); }); - assertNotCached(testContext); - testContext.completeNow(); + assertNotCached(); + } // Cache-Control: private with client enabled private caching @Test - public void testPrivateEnabled(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "private"); + public void testPrivateEnabled() throws Exception { + startMockServer("private"); assertCached(sessionClient); - testContext.completeNow(); + } @Test - public void testPrivateEnabledMaxAge(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "private, max-age=300"); + public void testPrivateEnabledMaxAge() throws Exception { + startMockServer("private, max-age=300"); assertCached(sessionClient); - testContext.completeNow(); + } @Test - public void testPrivateEnabledMaxAgeZero(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "private, max-age=0"); + public void testPrivateEnabledMaxAgeZero() throws Exception { + startMockServer("private, max-age=0"); assertNotCached(sessionClient); - testContext.completeNow(); + } @Test - public void testPrivateSharedMaxAgeAndMaxAgeZero(VertxTestContext testContext) throws Exception { - startMockServer(testContext, "private, s-maxage=300, max-age=0"); + public void testPrivateSharedMaxAgeAndMaxAgeZero() throws Exception { + startMockServer("private, s-maxage=300, max-age=0"); assertNotCached(sessionClient); - testContext.completeNow(); + } @Test - public void testPrivateSharedMaxAgeAndMaxAge(VertxTestContext testContext) throws Exception { - Checkpoint waiter = testContext.checkpoint(); - - startMockServer(testContext, "private, s-maxage=300, max-age=1"); + public void testPrivateSharedMaxAgeAndMaxAge() throws Exception { + startMockServer("private, s-maxage=300, max-age=1"); - String body1 = executeGetBlocking(testContext, sessionClient); - String body2 = executeGetBlocking(testContext, sessionClient); + String body1 = executeGetBlocking(sessionClient); + String body2 = executeGetBlocking(sessionClient); // Wait for the max-age time to pass, but not long enough for s-maxage // HTTP cache only has 1 second resolution, so this must be 1+ seconds past than the max-age - vertx.setTimer(2000, l -> waiter.flag()); - waiter.await(); + Thread.sleep(2000); - String body3 = executeGetBlocking(testContext, sessionClient); + String body3 = executeGetBlocking(sessionClient); assertEquals(body1, body2); assertNotEquals(body2, body3); - testContext.completeNow(); + } // Cache-Control: public; Vary: User-Agent @Test - public void testPublicVaryMaxAgeZero(VertxTestContext testContext) throws Exception { + public void testPublicVaryMaxAgeZero() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public, max-age=0"); req.response().headers().add("Vary", "User-Agent"); }); assertNotCached(varyClient); - testContext.completeNow(); + } @Test - public void testVaryUserAgentTwoDesktops(VertxTestContext testContext) throws Exception { + public void testVaryUserAgentTwoDesktops() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public, max-age=300"); req.response().headers().add("Vary", "User-Agent"); }); // Chrome Desktop - String body1 = executeGetBlocking(testContext, varyClient, req -> { + String body1 = executeGetBlocking(varyClient,req -> { req.putHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); }); // Firefox Desktop - String body2 = executeGetBlocking(testContext, varyClient, req -> { + String body2 = executeGetBlocking(varyClient,req -> { req.putHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0"); }); // Desktop user agents are normalized so two desktop clients should hit the same cache assertEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testVaryUserAgentDesktopVsMobile(VertxTestContext testContext) throws Exception { + public void testVaryUserAgentDesktopVsMobile() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public, max-age=300"); req.response().headers().add("Vary", "User-Agent"); }); // Chrome Desktop - String body1 = executeGetBlocking(testContext, varyClient, req -> { + String body1 = executeGetBlocking(varyClient,req -> { req.putHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); }); // iPhone Mobile - String body2 = executeGetBlocking(testContext, varyClient, req -> { + String body2 = executeGetBlocking(varyClient,req -> { req.putHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1"); }); // Desktop and Mobile may receive different content and should not share a cache assertNotEquals(body1, body2); - testContext.completeNow(); + } // Cache-Control: public; Vary: Content-Encoding @Test - public void testVaryEncodingTransformedToIdentityAlwaysSoWeIgnoreIt(VertxTestContext testContext) throws Exception { + public void testVaryEncodingTransformedToIdentityAlwaysSoWeIgnoreIt() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public, max-age=300"); req.response().headers().add("Content-Encoding", "gzip"); req.response().headers().add("Vary", "Accept-Encoding"); }); - String body1 = executeGetBlocking(testContext, varyClient, req -> { + String body1 = executeGetBlocking(varyClient,req -> { req.putHeader("Accept-Encoding", "gzip,deflate"); }); - String body2 = executeGetBlocking(testContext, varyClient, req -> { + String body2 = executeGetBlocking(varyClient,req -> { req.putHeader("Accept-Encoding", "br"); }); assertEquals(body1, body2); - testContext.completeNow(); + } @Test - public void testVaryCustomHeader(VertxTestContext testContext) throws Exception { + public void testVaryCustomHeader() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public, max-age=300"); req.response().headers().add("Vary", "X-Custom-Header"); }); - String body1 = executeGetBlocking(testContext, varyClient, req -> { + String body1 = executeGetBlocking(varyClient,req -> { req.putHeader("X-Custom-Header", "0x00000000"); }); - String body2 = executeGetBlocking(testContext, varyClient, req -> { + String body2 = executeGetBlocking(varyClient,req -> { req.putHeader("X-Custom-Header", "0xDEADBEEF"); }); - String body3 = executeGetBlocking(testContext, varyClient, req -> { + String body3 = executeGetBlocking(varyClient,req -> { req.putHeader("X-Custom-Header", "0x00000000"); }); assertNotEquals(body1, body2); assertNotEquals(body2, body3); assertEquals(body1, body3); - testContext.completeNow(); + } @Test - public void testVaryUserAgentAndCustomHeader(VertxTestContext testContext) throws Exception { + public void testVaryUserAgentAndCustomHeader() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public, max-age=300"); req.response().headers().add("Vary", "User-Agent, X-Custom-Header"); }); // 1. Chrome desktop, custom header 0 - String body1 = executeGetBlocking(testContext, varyClient, req -> { + String body1 = executeGetBlocking(varyClient,req -> { req .putHeader("X-Custom-Header", "0x00000000") .putHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); }); // 2. Chrome desktop, custom header deadbeef, should not be cached - String body2 = executeGetBlocking(testContext, varyClient, req -> { + String body2 = executeGetBlocking(varyClient,req -> { req .putHeader("X-Custom-Header", "0xDEADBEEF") .putHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); }); // 3. Chrome desktop, custom header 0, should be cached from req1 - String body3 = executeGetBlocking(testContext, varyClient, req -> { + String body3 = executeGetBlocking(varyClient,req -> { req .putHeader("X-Custom-Header", "0x00000000") .putHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"); }); // 4. iPhone mobile, custom header 0, should not be cached - String body4 = executeGetBlocking(testContext, varyClient, req -> { + String body4 = executeGetBlocking(varyClient,req -> { req .putHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1") .putHeader("X-Custom-Header", "0x00000000"); @@ -893,37 +873,34 @@ public void testVaryUserAgentAndCustomHeader(VertxTestContext testContext) throw assertNotEquals(body2, body3); assertNotEquals(body2, body4); assertNotEquals(body3, body4); - testContext.completeNow(); + } @Test - public void testVaryWithStaleResponse(VertxTestContext testContext) throws Exception { - Checkpoint waiter = testContext.checkpoint(); - + public void testVaryWithStaleResponse() throws Exception { startMockServer(req -> { req.response().headers().add("Cache-Control", "public, max-age=2"); req.response().headers().add("Vary", "User-Agent"); }); - String body1 = executeGetBlocking(testContext, varyClient, req -> { + String body1 = executeGetBlocking(varyClient,req -> { req.putHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1"); }); - String body2 = executeGetBlocking(testContext, varyClient, req -> { + String body2 = executeGetBlocking(varyClient,req -> { req.putHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1"); }); - vertx.setTimer(3000, l -> waiter.flag()); - waiter.await(); + Thread.sleep(3000); - String body3 = executeGetBlocking(testContext, varyClient, req -> { + String body3 = executeGetBlocking(varyClient,req -> { req.putHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Mobile/15E148 Safari/604.1"); }); assertEquals(body1, body2); assertNotEquals(body1, body3); assertNotEquals(body2, body3); - testContext.completeNow(); + } static class TestCacheStore implements CacheStore { diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/RouterTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/RouterTest.java index c91657d728..11ab9b3003 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/RouterTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/RouterTest.java @@ -17,6 +17,7 @@ package io.vertx.ext.web.tests; import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.net.NetSocket; import io.vertx.ext.web.client.HttpResponse; import io.vertx.core.Handler; import io.vertx.core.MultiMap; @@ -1660,36 +1661,37 @@ public void testEndHandler() throws Exception { @Test public void testExceptionHandler() throws Exception { AtomicInteger cnt = new AtomicInteger(); - client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").onComplete(TestUtils.onSuccess(req -> { - router.route().handler(rc -> { - rc.addEndHandler(done -> { - if (done.failed()) { - cnt.incrementAndGet(); - } - }); - rc.next(); - }); - router.route().handler(rc -> { - rc.addEndHandler(done -> { - if (done.failed()) { - cnt.incrementAndGet(); - } - }); - rc.next(); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + if (done.failed()) { + cnt.incrementAndGet(); + } }); - router.route().handler(rc -> { - rc.addEndHandler(done -> { - if (done.failed()) { - cnt.incrementAndGet(); - } - }); - rc.next(); + rc.next(); + }); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + if (done.failed()) { + cnt.incrementAndGet(); + } }); - router.route().handler(rc -> { - req.connection().close(); + rc.next(); + }); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + if (done.failed()) { + cnt.incrementAndGet(); + } }); - req.end(); - })); + rc.next(); + }); + HttpClientRequest request = client + .request(HttpMethod.GET, server.actualPort(), "localhost", "/path") + .await(); + router.route().handler(rc -> { + request.connection().close(); + }); + request.end(); assertWaitUntil(() -> cnt.get() == 3); } @@ -1697,30 +1699,29 @@ public void testExceptionHandler() throws Exception { @Test public void testCloseHandler() throws Exception { AtomicInteger cnt = new AtomicInteger(); - client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").onComplete(TestUtils.onSuccess(req -> { - router.route().handler(rc -> { - rc.addEndHandler(done -> { - cnt.incrementAndGet(); - }); - rc.next(); - }); - router.route().handler(rc -> { - rc.addEndHandler(done -> { - cnt.incrementAndGet(); - }); - rc.next(); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + cnt.incrementAndGet(); }); - router.route().handler(rc -> { - rc.addEndHandler(done -> { - cnt.incrementAndGet(); - }); - rc.next(); + rc.next(); + }); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + cnt.incrementAndGet(); }); - router.route().handler(rc -> { - req.connection().close(); + rc.next(); + }); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + cnt.incrementAndGet(); }); - req.end(); - })); + rc.next(); + }); + HttpClientRequest req = client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").await(); + router.route().handler(rc -> { + req.connection().close(); + }); + req.end(); assertWaitUntil(() -> cnt.get() == 3); } @@ -1730,30 +1731,29 @@ public void testEndHandlerCalledOnce() throws Exception { AtomicInteger endCnt = new AtomicInteger(); AtomicInteger excCnt = new AtomicInteger(); AtomicInteger closeCnt = new AtomicInteger(); - client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").onComplete(TestUtils.onSuccess(req -> { - router.route().handler(rc -> { - rc.addEndHandler(done -> { - excCnt.incrementAndGet(); - }); - rc.next(); - }); - router.route().handler(rc -> { - rc.addEndHandler(done -> { - endCnt.incrementAndGet(); - }); - rc.next(); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + excCnt.incrementAndGet(); }); - router.route().handler(rc -> { - rc.addEndHandler(done -> { - closeCnt.incrementAndGet(); - }); - rc.next(); + rc.next(); + }); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + endCnt.incrementAndGet(); }); - router.route().handler(rc -> { - req.connection().close(); + rc.next(); + }); + router.route().handler(rc -> { + rc.addEndHandler(done -> { + closeCnt.incrementAndGet(); }); - req.end(); - })); + rc.next(); + }); + HttpClientRequest req = client.request(HttpMethod.GET, server.actualPort(), "localhost", "/path").await(); + router.route().handler(rc -> { + req.connection().close(); + }); + req.end(); assertWaitUntil(() -> endCnt.get() == 1); assertWaitUntil(() -> excCnt.get() == 1); assertWaitUntil(() -> closeCnt.get() == 1); @@ -2669,22 +2669,21 @@ private void testMissingHostHeader(VertxTestContext testContext, String httpVers Checkpoint done = testContext.checkpoint(); router.route().handler(rc -> rc.response().end()); NetClient nc = vertx.createNetClient(); - nc.connect(SocketAddress.inetSocketAddress(8080, "localhost")).onComplete(TestUtils.onSuccess(so -> { - so.write("GET / " + httpVersion + "\r\n\r\n"); - Buffer response = Buffer.buffer(); - so.handler(chunk -> { - response.appendBuffer(chunk); - String s = response.toString(); - int idx = s.indexOf("\r\n"); - if (idx >= 0) { - so.handler(null); - String[] line = s.substring(0, idx).split("\\s+"); - assertTrue(line.length >= 3); - assertEquals("" + expectedStatusCode, line[1]); - done.flag(); - } - }); - })); + NetSocket so = nc.connect(SocketAddress.inetSocketAddress(8080, "localhost")).await(); + Buffer response = Buffer.buffer(); + so.handler(chunk -> { + response.appendBuffer(chunk); + String s = response.toString(); + int idx = s.indexOf("\r\n"); + if (idx >= 0) { + so.handler(null); + String[] line = s.substring(0, idx).split("\\s+"); + assertTrue(line.length >= 3); + assertEquals("" + expectedStatusCode, line[1]); + done.flag(); + } + }); + so.write("GET / " + httpVersion + "\r\n\r\n"); } @Test diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/BodyHandlerTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/BodyHandlerTest.java index 91c31ace51..22624adad0 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/BodyHandlerTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/BodyHandlerTest.java @@ -20,6 +20,7 @@ import io.vertx.core.MultiMap; import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; +import io.vertx.core.http.HttpClientRequest; import io.vertx.core.http.HttpHeaders; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.RequestOptions; @@ -30,7 +31,6 @@ import io.vertx.ext.web.handler.BodyHandler; import io.vertx.ext.web.handler.PlatformHandler; import io.vertx.ext.web.tests.WebTestBase; -import io.vertx.junit5.Checkpoint; import io.vertx.junit5.VertxTestContext; import io.vertx.test.core.TestUtils; import org.junit.jupiter.api.AfterAll; @@ -406,7 +406,7 @@ private void sendFileUploadRequest(Buffer fileData, } @Test - public void testRoutingContextFailedBeforeFileIsFullyUploaded(VertxTestContext testContext) { + public void testRoutingContextFailedBeforeFileIsFullyUploaded() { String uploadsDirectory = new File(tempUploads, "failUpload").getPath(); new File(uploadsDirectory).mkdirs(); router.clear(); @@ -434,34 +434,31 @@ public void testRoutingContextFailedBeforeFileIsFullyUploaded(VertxTestContext t .setHost("localhost") .setPort(8080) .setURI("/upload"); - Checkpoint responseLatch = testContext.checkpoint(); - client.request(requestOptions).onComplete(TestUtils.onSuccess(req -> { - req.response().onComplete(TestUtils.onSuccess(resp -> { - assertEquals(503, resp.statusCode()); - responseLatch.flag(); - })); - String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO"; - Buffer buffer = TestUtils.randomBuffer(2048); - req.headers().set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); - req.headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length())); - req.setChunked(true); - req.write("--" + boundary + "\r\n" + - "Content-Disposition: form-data; name=\"somename\"; filename=\"somefile.dat\"\r\n" + - "Content-Type: application/octet-stream\r\n" + - "Content-Transfer-Encoding: binary\r\n" + - "\r\n"); - req.write(buffer.getBuffer(0, 1024)); - vertx.setPeriodic(50, id -> { - if (stop.get()) { - vertx.cancelTimer(id); - req.write(buffer.getBuffer(0, 1024)); - String footer = "\r\n--" + boundary + "--\r\n"; - req.end(footer); - } - }); - })); - - responseLatch.await(); + HttpClientRequest req = client.request(requestOptions).await(); + String boundary = "dLV9Wyq26L_-JQxk6ferf-RT153LhOO"; + Buffer buffer = TestUtils.randomBuffer(2048); + req.headers().set(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); + req.headers().set(HttpHeaders.CONTENT_LENGTH, String.valueOf(buffer.length())); + req.setChunked(true); + req.write("--" + boundary + "\r\n" + + "Content-Disposition: form-data; name=\"somename\"; filename=\"somefile.dat\"\r\n" + + "Content-Type: application/octet-stream\r\n" + + "Content-Transfer-Encoding: binary\r\n" + + "\r\n"); + req.write(buffer.getBuffer(0, 1024)); + vertx.setPeriodic(50, id -> { + if (stop.get()) { + vertx.cancelTimer(id); + req.write(buffer.getBuffer(0, 1024)); + String footer = "\r\n--" + boundary + "--\r\n"; + req.end(footer); + } + }); + int resp = req + .response() + .map(r -> r.statusCode()) + .await(); + assertEquals(503, resp); assertWaitUntil(() -> vertx.fileSystem().readDirBlocking(uploadsDirectory).isEmpty()); } diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/SlowClusterEventbusBridgeTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/SlowClusterEventbusBridgeTest.java index 5d5f511935..65a2bc6a33 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/SlowClusterEventbusBridgeTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/SlowClusterEventbusBridgeTest.java @@ -145,7 +145,7 @@ public void testRegistration(VertxTestContext testContext) throws Exception { } @Test - public void testNoOrphanClusteredSubscription(VertxTestContext testContext) throws Exception { + public void testNoOrphanClusteredSubscription() throws Exception { String addr = "someaddress"; String websocketURI = "/eventbus/websocket"; @@ -161,16 +161,14 @@ public void testNoOrphanClusteredSubscription(VertxTestContext testContext) thro .connect(websocketURI) .compose(v -> bridgeClient.register(addr)) .compose(v -> bridgeClient.unregister(addr)) - .onComplete(TestUtils.onSuccess(v -> { - Promise> promise = Promise.promise(); - node1.setTimer(1500, l -> { - node1.clusterManager().getRegistrations(addr, promise); - promise.future().onComplete(TestUtils.onSuccess(registrationInfos -> { - assertTrue(registrationInfos == null || registrationInfos.isEmpty()); - testContext.completeNow(); - })); - }); - })); + .await(); + + Thread.sleep(1500); + + Promise> promise = Promise.promise(); + node1.clusterManager().getRegistrations(addr, promise); + List registrationInfos = promise.future().await(); + assertTrue(registrationInfos == null || registrationInfos.isEmpty()); } private static class SlowClusterManager extends WrappedClusterManager { diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/StaticHandlerTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/StaticHandlerTest.java index 8bab4d0bb9..bcf03cf821 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/StaticHandlerTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/StaticHandlerTest.java @@ -349,12 +349,12 @@ public void testHttp2Push(VertxTestContext testContext) throws Exception { .compose(req -> req.pushHandler(push -> { assertNotNull(push); - push.response().onComplete(TestUtils.onSuccess(resp -> { - resp.body().onComplete(TestUtils.onSuccess(body -> { + push.response() + .compose(HttpClientResponse::body) + .onComplete(TestUtils.onSuccess(body -> { assertTrue(body.length() > 0); pushReceived.flag(); })); - })); }).send() .expecting(HttpResponseExpectation.SC_OK) .expecting(resp -> resp.version() == HttpVersion.HTTP_2) diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/WebAuthn4JHandlerTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/WebAuthn4JHandlerTest.java index f14dde0bdd..3aa5baf87c 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/WebAuthn4JHandlerTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/WebAuthn4JHandlerTest.java @@ -411,26 +411,25 @@ protected void testRequestBuffer(HttpMethod method, String path, Consumer responseBodyBufferAction) throws Exception { RequestOptions requestOptions = new RequestOptions().setMethod(method).setPort(8080).setURI(path).setHost("localhost"); Promise promise = Promise.promise(); - client.request(requestOptions).onComplete(TestUtils.onSuccess(req -> { - req.response().onComplete(TestUtils.onSuccess(resp -> { - assertEquals(statusCode, resp.statusCode()); - assertEquals(statusMessage, resp.statusMessage()); - if (responseAction != null) { - responseAction.accept(resp); - } - if (responseBodyBufferAction == null) { - promise.complete(); - } else { - resp.bodyHandler(buff -> { - responseBodyBufferAction.accept(buff); - promise.complete(); - }); - } - })); + client.request(requestOptions).compose(req -> { if (requestAction != null) { requestAction.accept(req); } - req.end(); + return req.send(); + }).onComplete(TestUtils.onSuccess(resp -> { + assertEquals(statusCode, resp.statusCode()); + assertEquals(statusMessage, resp.statusMessage()); + if (responseAction != null) { + responseAction.accept(resp); + } + if (responseBodyBufferAction == null) { + promise.complete(); + } else { + resp.bodyHandler(buff -> { + responseBodyBufferAction.accept(buff); + promise.complete(); + }); + } })); promise.future().await(); } diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSHandlerTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSHandlerTest.java index 37734da820..7db484d175 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSHandlerTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSHandlerTest.java @@ -397,14 +397,11 @@ public void testWebContext(VertxTestContext testContext) { wsClient.connect(new WebSocketConnectOptions() .setPort(8080) - .setURI("/webcontext/websocket")).onComplete(TestUtils.onSuccess( - ws -> { - wsClient.connect(new WebSocketConnectOptions() - .setPort(8080) - .setURI("/webcontextuser/websocket")).onComplete(TestUtils.onSuccess(wsuser -> done.flag()) - ); - } - )); + .setURI("/webcontext/websocket")) + .compose(ws -> wsClient.connect(new WebSocketConnectOptions() + .setPort(8080) + .setURI("/webcontextuser/websocket"))) + .onComplete(TestUtils.onSuccess(wsuser -> done.flag())); } @Test diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSSessionTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSSessionTest.java index 00e5470536..a30eb2d863 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSSessionTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSSessionTest.java @@ -18,6 +18,7 @@ import io.netty.util.internal.PlatformDependent; import io.vertx.core.Context; import io.vertx.core.Future; +import io.vertx.core.http.HttpClientRequest; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.HttpMethod; import io.vertx.junit5.Checkpoint; @@ -59,8 +60,9 @@ public void testNoDeadlockWhenWritingFromAnotherThreadWithSseTransport(VertxTest }; }; startServers(); - client.request(HttpMethod.GET, "/test/400/8ne8e94a/eventsource").onComplete(TestUtils.onSuccess(req -> { - req.send().onComplete(TestUtils.onSuccess(resp -> { + client.request(HttpMethod.GET, "/test/400/8ne8e94a/eventsource") + .compose(HttpClientRequest::send) + .onComplete(TestUtils.onSuccess(resp -> { AtomicInteger count = new AtomicInteger(); resp.handler(msg -> { if (count.incrementAndGet() == 400) { @@ -68,7 +70,6 @@ public void testNoDeadlockWhenWritingFromAnotherThreadWithSseTransport(VertxTest } }); })); - })); } @Test diff --git a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSWriteTest.java b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSWriteTest.java index a755300793..abf4f3bce2 100644 --- a/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSWriteTest.java +++ b/vertx-web/src/test/java/io/vertx/ext/web/tests/handler/sockjs/SockJSWriteTest.java @@ -16,6 +16,7 @@ package io.vertx.ext.web.tests.handler.sockjs; import io.vertx.core.buffer.Buffer; +import io.vertx.core.http.HttpClientRequest; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.WebSocketBase; import io.vertx.junit5.Checkpoint; @@ -115,13 +116,14 @@ public void testEventSource(VertxTestContext testContext) throws Exception { }; startServers(); client.request(HttpMethod.GET, "/test/400/8ne8e94a/eventsource") - .onComplete(TestUtils.onSuccess(req -> req.send().onComplete(TestUtils.onSuccess(resp -> { + .compose(HttpClientRequest::send) + .onComplete(TestUtils.onSuccess(resp -> { resp.handler(buffer -> { if (buffer.toString().equals("data: a[\"" + expected + "\"]\r\n\r\n")) { cp.flag(); } }); - })))); + })); } @Test @@ -136,9 +138,10 @@ public void testEventSourceFailure(VertxTestContext testContext) throws Exceptio }; startServers(); client.request(HttpMethod.GET, "/test/400/8ne8e94a/eventsource") - .onComplete(TestUtils.onSuccess(req -> req.send().onComplete(TestUtils.onSuccess(resp -> { - req.connection().close(); - })))); + .compose(HttpClientRequest::send) + .onComplete(TestUtils.onSuccess(resp -> { + resp.request().connection().close(); + })); } @Test @@ -152,14 +155,15 @@ public void testXHRStreaming(VertxTestContext testContext) throws Exception { }; startServers(); client.request(HttpMethod.POST, "/test/400/8ne8e94a/xhr_streaming") - .onComplete(TestUtils.onSuccess(req -> req.send(Buffer.buffer()).onComplete(TestUtils.onSuccess(resp -> { + .compose(req -> req.send(Buffer.buffer())) + .onComplete(TestUtils.onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.handler(buffer -> { if (buffer.toString().equals("a[\"" + expected + "\"]\n")) { cp.flag(); } }); - })))); + })); } @Test @@ -174,9 +178,10 @@ public void testXHRStreamingFailure(VertxTestContext testContext) throws Excepti }; startServers(); client.request(HttpMethod.POST, "/test/400/8ne8e94a/xhr_streaming") - .onComplete(TestUtils.onSuccess(req -> req.send().onComplete(TestUtils.onSuccess(resp -> { - req.connection().close(); - })))); + .compose(HttpClientRequest::send) + .onComplete(TestUtils.onSuccess(resp -> { + resp.request().connection().close(); + })); } @Test @@ -192,7 +197,8 @@ public void testXHRPolling(VertxTestContext testContext) throws Exception { Runnable[] task = new Runnable[1]; task[0] = () -> client.request(HttpMethod.POST, "/test/400/8ne8e94a/xhr") - .onComplete(TestUtils.onSuccess(req -> req.send(Buffer.buffer()).onComplete(TestUtils.onSuccess(resp -> { + .compose(req -> req.send(Buffer.buffer())) + .onComplete(TestUtils.onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.handler(buffer -> { if (buffer.toString().equals("a[\"" + expected + "\"]\n")) { @@ -201,7 +207,7 @@ public void testXHRPolling(VertxTestContext testContext) throws Exception { task[0].run(); } }); - })))); + })); task[0].run(); }