Skip to content

Commit a6eb35a

Browse files
committed
Add javadoc examples of using .join() with .async()
In test code we frequently will use .join() to wait for the async callback to execute before proceeding with asserts etc
1 parent f0c7b41 commit a6eb35a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

client/src/main/java/io/avaje/http/client/HttpAsyncResponse.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,55 @@
88

99
/**
1010
* Async processing of the request with responses as CompletableFuture.
11+
*
12+
* <h4>Testing and .join()</h4>
13+
* <p>
14+
* Note that when testing with async requests we frequently use {@code .join()}
15+
* on the {@code CompletableFuture} such that the main thread waits for the async
16+
* processing to complete. After that various asserts can run knowing that the
17+
* async callback code has been executed.
18+
*
19+
* <h4>Example using .join() for testing purposes</h4>
20+
* <pre>{@code
21+
*
22+
* clientContext.request()
23+
* ...
24+
* .POST().async()
25+
* .bean(HelloDto.class)
26+
* .whenComplete((helloDto, throwable) -> {
27+
* ...
28+
* }).join(); // wait for async processing to complete
29+
*
30+
* // can assert now ...
31+
* assertThat(...)
32+
*
33+
* }</pre>
34+
*
35+
* <h4>Example async().bean()</h4>
36+
* <p>
37+
* In this example POST async that will return a bean converted from json response.
38+
* <pre>{@code
39+
*
40+
* clientContext.request()
41+
* ...
42+
* .POST().async()
43+
* .bean(HelloDto.class)
44+
* .whenComplete((helloDto, throwable) -> {
45+
*
46+
* if (throwable != null) {
47+
* HttpException httpException = (HttpException) throwable.getCause();
48+
* int statusCode = httpException.statusCode();
49+
*
50+
* // maybe convert json error response body to a bean (using Jackson/Gson)
51+
* MyErrorBean errorResponse = httpException.bean(MyErrorBean.class);
52+
* ..
53+
*
54+
* } else {
55+
* // process helloDto
56+
* ...
57+
* }
58+
* });
59+
* }</pre>
1160
*/
1261
public interface HttpAsyncResponse {
1362

client/src/main/java/io/avaje/http/client/HttpClientResponse.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ public interface HttpClientResponse {
1414

1515
/**
1616
* Send the request async using CompletableFuture.
17+
*
18+
* <h4>Example async().bean()</h4>
19+
* <p>
20+
* In this example POST async that will return a bean converted from json response.
21+
* <pre>{@code
22+
*
23+
* clientContext.request()
24+
* ...
25+
* .POST().async()
26+
* .bean(HelloDto.class)
27+
* .whenComplete((helloDto, throwable) -> {
28+
*
29+
* if (throwable != null) {
30+
* HttpException httpException = (HttpException) throwable.getCause();
31+
* int statusCode = httpException.statusCode();
32+
*
33+
* // maybe convert json error response body to a bean (using Jackson/Gson)
34+
* MyErrorBean errorResponse = httpException.bean(MyErrorBean.class);
35+
* ..
36+
*
37+
* } else {
38+
* // process helloDto
39+
* ...
40+
* }
41+
* });
42+
* }</pre>
1743
*/
1844
HttpAsyncResponse async();
1945

0 commit comments

Comments
 (0)