Skip to content

Commit cf56839

Browse files
author
Haydon Perrin
committed
test-javalin-jsonb: Added async request handling tests.
1 parent e7101f5 commit cf56839

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

tests/test-javalin-jsonb/src/main/java/org/example/myapp/web/HelloController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.time.LocalDate;
66
import java.util.ArrayList;
77
import java.util.List;
8+
import java.util.concurrent.CompletableFuture;
9+
import java.util.concurrent.Executors;
810

911
import javax.validation.Valid;
1012

@@ -142,6 +144,21 @@ List<HelloDto> getAll() {
142144
return myService.findAll();
143145
}
144146

147+
@Get("/async")
148+
CompletableFuture<List<HelloDto>> getAllAsync() {
149+
return CompletableFuture.supplyAsync(() -> {
150+
// Simulate a delay as if an actual IO operation is being executed.
151+
// This also helps ensure that we aren't just getting lucky with timings.
152+
try {
153+
Thread.sleep(10L);
154+
} catch (InterruptedException e) {
155+
throw new RuntimeException(e);
156+
}
157+
158+
return myService.findAll();
159+
}, Executors.newSingleThreadExecutor()); // Example of how to use a custom executor.
160+
}
161+
145162
// @Hidden
146163
@Delete(":id")
147164
void deleteById(int id) {

tests/test-javalin-jsonb/src/test/java/org/example/myapp/HelloControllerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ void hello2() {
6262
assertThat(helloDtos).hasSize(2);
6363
}
6464

65+
@Test
66+
void helloAsyncRequestHandling() {
67+
TypeRef<List<HelloDto>> listDto = new TypeRef<List<HelloDto>>() { };
68+
final List<HelloDto> beans = given()
69+
.get(baseUrl + "/hello/async")
70+
.then()
71+
.statusCode(200)
72+
.extract()
73+
.as(listDto);
74+
75+
assertThat(beans).hasSize(2);
76+
77+
final List<HelloDto> helloDtos = client.request()
78+
.path("hello/async")
79+
.GET().list(HelloDto.class);
80+
81+
assertThat(helloDtos).hasSize(2);
82+
}
83+
6584
@Test
6685
void getWithPathParamAndQueryParam() {
6786

0 commit comments

Comments
 (0)