From df7b14968ae1ba54ffba2cf905a2e98d569bf827 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 7 Aug 2024 10:38:19 +0200 Subject: [PATCH] Small optimization. Added test case. Signed-off-by: Simone Bordet --- .../jetty/client/transport/HttpReceiver.java | 9 +++-- .../HttpClientTransportOverHTTP2Test.java | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpReceiver.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpReceiver.java index 54a243308047..93859d695d7c 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpReceiver.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpReceiver.java @@ -366,7 +366,7 @@ protected void responseSuccess(HttpExchange exchange, Runnable afterSuccessTask) if (!exchange.responseComplete(null)) return; - invoker.run(() -> + Runnable successTask = () -> { if (LOG.isDebugEnabled()) LOG.debug("Executing responseSuccess on {}", this); @@ -387,7 +387,12 @@ protected void responseSuccess(HttpExchange exchange, Runnable afterSuccessTask) // Mark atomically the response as terminated, with // respect to concurrency between request and response. terminateResponse(exchange); - }, afterSuccessTask); + }; + + if (afterSuccessTask == null) + invoker.run(successTask); + else + invoker.run(successTask, afterSuccessTask); } /** diff --git a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java index 86ae453cee64..0173430314a3 100644 --- a/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java +++ b/jetty-core/jetty-http2/jetty-http2-tests/src/test/java/org/eclipse/jetty/http2/tests/HttpClientTransportOverHTTP2Test.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; @@ -33,6 +34,8 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.IntStream; +import org.eclipse.jetty.client.AsyncRequestContent; +import org.eclipse.jetty.client.CompletableResponseListener; import org.eclipse.jetty.client.Connection; import org.eclipse.jetty.client.ContentResponse; import org.eclipse.jetty.client.Destination; @@ -793,6 +796,37 @@ public void onComplete(Result result) assertThat(onContentSourceErrorRef.get(), is(nullValue())); } + @Test + public void testRequestContentResponseContent() throws Exception + { + start(new Handler.Abstract() + { + @Override + public boolean handle(Request request, org.eclipse.jetty.server.Response response, Callback callback) + { + Content.copy(request, response, callback); + return true; + } + }); + + AsyncRequestContent content = new AsyncRequestContent(); + var request = httpClient.newRequest("localhost", connector.getLocalPort()) + .method(HttpMethod.POST) + .body(content); + CompletableFuture completable = new CompletableResponseListener(request).send(); + + for (int i = 0; i < 16; ++i) + { + content.write(false, ByteBuffer.allocate(512), Callback.NOOP); + Thread.sleep(10); + } + content.close(); + + ContentResponse response = completable.get(15, TimeUnit.SECONDS); + + assertEquals(HttpStatus.OK_200, response.getStatus()); + } + @Test @Tag("external") public void testExternalServer() throws Exception