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:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user