From aa6c664110583521cdd6585fb56779f13e01fb01 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Wed, 17 May 2023 10:25:57 +0200 Subject: [PATCH] Experiment with HttpClient HTTP request/response API method design that provides non racy methods when called outside event-loop. --- .../java/io/vertx/core/http/HttpClient.java | 17 +++++++++++++++++ src/test/java/io/vertx/core/http/HttpTest.java | 7 ++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/vertx/core/http/HttpClient.java b/src/main/java/io/vertx/core/http/HttpClient.java index a16c1cc0e23..0583f31e1f0 100644 --- a/src/main/java/io/vertx/core/http/HttpClient.java +++ b/src/main/java/io/vertx/core/http/HttpClient.java @@ -15,6 +15,7 @@ import io.vertx.core.Future; import java.util.concurrent.TimeUnit; +import java.util.function.Function; /** * The API to interacts with an HTTP server. @@ -41,6 +42,10 @@ default Future request() { */ Future request(RequestOptions options); + default Future request(RequestOptions options, Function> handler) { + return request(options).compose(handler); + } + /** * Create an HTTP request to send to the server at the {@code host} and {@code port}. * @@ -54,6 +59,10 @@ default Future request(HttpMethod method, int port, String ho return request(new RequestOptions().setMethod(method).setPort(port).setHost(host).setURI(requestURI)); } + default Future request(HttpMethod method, int port, String host, String requestURI, Function> handler) { + return request(method, port, host, requestURI).compose(handler); + } + /** * Create an HTTP request to send to the server at the {@code host} and default port. * @@ -66,6 +75,10 @@ default Future request(HttpMethod method, String host, String return request(new RequestOptions().setMethod(method).setHost(host).setURI(requestURI)); } + default Future request(HttpMethod method, String host, String requestURI , Function> handler) { + return request(method, host, requestURI).compose(handler); + } + /** * Create an HTTP request to send to the server at the default host and port. * @@ -77,6 +90,10 @@ default Future request(HttpMethod method, String requestURI) return request(new RequestOptions().setMethod(method).setURI(requestURI)); } + default Future request(HttpMethod method, String requestURI, Function> handler) { + return request(method, requestURI).compose(handler); + } + /** * Shutdown with a 30 seconds timeout ({@code shutdown(30, TimeUnit.SECONDS)}). * diff --git a/src/test/java/io/vertx/core/http/HttpTest.java b/src/test/java/io/vertx/core/http/HttpTest.java index bad20197e50..5310b16844e 100644 --- a/src/test/java/io/vertx/core/http/HttpTest.java +++ b/src/test/java/io/vertx/core/http/HttpTest.java @@ -6604,10 +6604,11 @@ public void shouldThrowISEIfSendingResponseFromHeadersEndHandler() throws Except } ); startServer(testAddress); - client.request(requestOptions) - .compose(req -> req.send() + client.request(requestOptions, req -> req + .send() .andThen(onSuccess(resp -> assertEquals(200, resp.statusCode()))) - .compose(HttpClientResponse::end)) + .compose(HttpClientResponse::end) + ) .onComplete(onSuccess(nothing -> complete())); await(); }