Skip to content

Commit

Permalink
Small optimization.
Browse files Browse the repository at this point in the history
Added test case.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Aug 7, 2024
1 parent a0268a3 commit df7b149
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<ContentResponse> 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
Expand Down

0 comments on commit df7b149

Please sign in to comment.