Avoid async dispatch if completed in AsyncServerResponse

This commit checks whether the CompletableFuture<ServerResponse> passed
to AsyncServerResponse.async has been completed, and if so returns a
CompletedAsyncServerResponse that simply delegates to the completed
response, instead of the DefaultAsyncServerResponse that uses async
dispatch.

Closes gh-32223
This commit is contained in:
Arjen Poutsma
2024-02-22 11:20:17 +01:00
parent 3ddc512108
commit 89d746ddf8
5 changed files with 147 additions and 35 deletions

View File

@@ -28,7 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat;
class DefaultAsyncServerResponseTests {
@Test
void block() {
void blockCompleted() {
ServerResponse wrappee = ServerResponse.ok().build();
CompletableFuture<ServerResponse> future = CompletableFuture.completedFuture(wrappee);
AsyncServerResponse response = AsyncServerResponse.create(future);
@@ -36,4 +36,21 @@ class DefaultAsyncServerResponseTests {
assertThat(response.block()).isSameAs(wrappee);
}
@Test
void blockNotCompleted() {
ServerResponse wrappee = ServerResponse.ok().build();
CompletableFuture<ServerResponse> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(500);
return wrappee;
}
catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
});
AsyncServerResponse response = AsyncServerResponse.create(future);
assertThat(response.block()).isSameAs(wrappee);
}
}