Skip to content

Commit 2a2efde

Browse files
authored
[Archetype] Add await timeout and replace thenAccept to forSingle (helidon-io#6558)
* add timeout and forSingle Signed-off-by: tvallin <thibault.vallin@oracle.com> * fix tests Signed-off-by: tvallin <thibault.vallin@oracle.com> --------- Signed-off-by: tvallin <thibault.vallin@oracle.com>
1 parent 0587466 commit 2a2efde

16 files changed

Lines changed: 121 additions & 115 deletions

File tree

archetypes/helidon/src/main/archetype/common/observability.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
153153
String get = webClient.get()
154154
.path("/simple-greet/greet-count")
155155
.request(String.class)
156-
.await();
156+
.await(Duration.ofSeconds(5));
157157
158158
assertThat(get, containsString("Hello World!"));
159159
160160
String openMetricsOutput = webClient.get()
161161
.path("/metrics")
162162
.request(String.class)
163-
.await();
163+
.await(Duration.ofSeconds(5));
164164
165165
assertThat("Metrics output", openMetricsOutput, containsString("application_accessctr_total"));
166166
}]]></value>
@@ -270,14 +270,14 @@ curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
270270
String get = webClient.get()
271271
.path("/micrometer-greet/greet-count")
272272
.request(String.class)
273-
.await();
273+
.await(Duration.ofSeconds(5));
274274
275275
assertThat(get, containsString("Hello World!"));
276276
277277
String openMetricsOutput = webClient.get()
278278
.path("/micrometer")
279279
.request(String.class)
280-
.await();
280+
.await(Duration.ofSeconds(5));
281281
282282
assertThat("Metrics output", openMetricsOutput, containsString("allRequests_total 1"));
283283
}]]></value>
@@ -355,7 +355,7 @@ allRequests_total 0.0
355355
WebClientResponse response = webClient.get()
356356
.path("/metrics")
357357
.request()
358-
.await();
358+
.await(Duration.ofSeconds(5));
359359
assertThat(response.status().code(), is(200));
360360
}]]></value>
361361
</list>
@@ -456,7 +456,7 @@ curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
456456
WebClientResponse response = webClient.get()
457457
.path("health")
458458
.request()
459-
.await();
459+
.await(Duration.ofSeconds(5));
460460
assertThat(response.status().code(), is(200));
461461
}]]></value>
462462
</list>

archetypes/helidon/src/main/archetype/mp/custom/files/src/test/java/__pkg__/TestCORS.java.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.junit.jupiter.api.AfterAll;
1111
import org.junit.jupiter.api.BeforeAll;
1212
import org.junit.jupiter.api.Test;
1313

14+
import java.time.Duration;
1415
import java.util.List;
1516
import java.util.Optional;
1617

@@ -72,7 +73,7 @@ class TestCORS {
7273
7374
WebClientResponse r = builder.path("/simple-greet")
7475
.submit()
75-
.await();
76+
.await(Duration.ofSeconds(5));
7677
7778
assertThat("pre-flight status", r.status().code(), is(200));
7879
Headers preflightResponseHeaders = r.headers();
@@ -118,20 +119,20 @@ class TestCORS {
118119
return builder
119120
.path("/simple-greet")
120121
.submit()
121-
.await();
122+
.await(Duration.ofSeconds(5));
122123
}
123124

124125
private static String fromPayload(WebClientResponse response) {
125126
return response
126127
.content()
127128
.as(String.class)
128-
.await();
129+
.await(Duration.ofSeconds(5));
129130
}
130131

131132
private static WebClientResponse putResponse(String message, WebClientRequestBuilder builder) {
132133
return builder
133134
.path("/simple-greet")
134135
.submit(message)
135-
.await();
136+
.await(Duration.ofSeconds(5));
136137
}
137138
}

archetypes/helidon/src/main/archetype/se/common/files/src/main/java/__pkg__/Main.java.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public final class Main {
5858

5959
// Try to start the server. If successful, print some info and arrange to
6060
// print a message at shutdown. If unsuccessful, print the exception.
61-
webserver.thenAccept(ws -> {
61+
webserver.forSingle(ws -> {
6262
{{Main-readyMessage}}
6363
ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
6464
})

archetypes/helidon/src/main/archetype/se/common/files/src/test/java/__pkg__/MainTest.java.mustache

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class MainTest {
2828

2929
@BeforeAll
3030
static void startTheServer() {
31-
webServer = Main.startServer().await();
31+
webServer = Main.startServer().await(Duration.ofSeconds(10));
3232
3333
webClient = WebClient.builder()
3434
.baseUri("http://localhost:" + webServer.port())
@@ -39,11 +39,9 @@ class MainTest {
3939
}
4040

4141
@AfterAll
42-
static void stopServer() throws ExecutionException, InterruptedException, TimeoutException {
42+
static void stopServer() {
4343
if (webServer != null) {
44-
webServer.shutdown()
45-
.toCompletableFuture()
46-
.get(10, TimeUnit.SECONDS);
44+
webServer.shutdown().await(10, TimeUnit.SECONDS);
4745
}
4846
}
4947

archetypes/helidon/src/main/archetype/se/custom/custom-tests.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright (c) 2022 Oracle and/or its affiliates.
4+
Copyright (c) 2022, 2023 Oracle and/or its affiliates.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -22,6 +22,9 @@
2222

2323
<output>
2424
<model>
25+
<list key="MainTest-java-imports">
26+
<value>java.time.Duration</value>
27+
</list>
2528
<list key="MainTest-other-imports">
2629
<value>org.junit.jupiter.api.Order</value>
2730
</list>
@@ -35,7 +38,7 @@
3538
JsonObject jsonObject = webClient.get()
3639
.path("/simple-greet")
3740
.request(JsonObject.class)
38-
.await();
41+
.await(Duration.ofSeconds(5));
3942
assertThat(jsonObject.getString("message"), is("Hello World!"));
4043
}]]></value>
4144
</list>
@@ -45,7 +48,7 @@
4548
Message json = webClient.get()
4649
.path("/simple-greet")
4750
.request(Message.class)
48-
.await();
51+
.await(Duration.ofSeconds(5));
4952
assertThat(json.getMessage(), is("Hello World!"));
5053
}]]></value>
5154
</list>

archetypes/helidon/src/main/archetype/se/custom/database.xml

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright (c) 2022 Oracle and/or its affiliates.
4+
Copyright (c) 2022, 2023 Oracle and/or its affiliates.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -67,78 +67,74 @@ Instructions for H2 can be found here: https://www.h2database.com/html/cheatShee
6767
<list key="MainTest-methods">
6868
<value template="mustache"><![CDATA[
6969
@Test
70-
void testPokemonTypes() throws ExecutionException, InterruptedException {
71-
webClient.get()
70+
void testPokemonTypes() {
71+
JsonArray array = webClient.get()
7272
.path("/type")
7373
.request(JsonArray.class)
74-
.thenAccept(array -> {
75-
assertThat(array.size(), is(18));
76-
assertThat(array.get(0).asJsonObject().getInt("{{id}}"), is(1));
77-
assertThat(array.get(0).asJsonObject().getString("{{name}}"), is("Normal"));
78-
})
79-
.toCompletableFuture()
80-
.get();
74+
.await(Duration.ofSeconds(5));
75+
76+
assertThat(array.size(), is(18));
77+
assertThat(array.get(0).asJsonObject().getInt("{{id}}"), is(1));
78+
assertThat(array.get(0).asJsonObject().getString("{{name}}"), is("Normal"));
8179
}
8280
8381
@Test
84-
void testPokemons() throws ExecutionException, InterruptedException {
82+
void testPokemons() {
8583
assertThat(getPokemonCount(), is(6));
86-
87-
webClient.get()
84+
JsonObject pokemon = webClient.get()
8885
.path("/pokemon/1")
8986
.request(JsonObject.class)
90-
.thenAccept(pokemon -> assertThat(pokemon.getString("{{name}}"), is("Bulbasaur")))
91-
.toCompletableFuture()
92-
.get();
87+
.await(Duration.ofSeconds(5));
88+
89+
assertThat(pokemon.getString("{{name}}"), is("Bulbasaur"));
9390
94-
webClient.get()
91+
pokemon = webClient.get()
9592
.path("/pokemon/name/Charmander")
9693
.request(JsonObject.class)
97-
.thenAccept(pokemon -> assertThat(pokemon.getJsonNumber("{{id_type}}").intValue(), is(10)))
98-
.toCompletableFuture()
99-
.get();
94+
.await(Duration.ofSeconds(5));
95+
96+
assertThat(pokemon.getJsonNumber("{{id_type}}").intValue(), is(10));
10097
10198
JsonObject json = JSON_BUILDER.createObjectBuilder()
10299
.add("id", 100)
103100
.add("idType", 1)
104101
.add("name", "Test")
105102
.build();
106-
webClient.post()
103+
WebClientResponse response = webClient.post()
107104
.path("/pokemon")
108105
.submit(json)
109-
.thenAccept(r -> assertThat(r.status(), is(Http.Status.OK_200)))
110-
.toCompletableFuture()
111-
.get();
106+
.await(Duration.ofSeconds(5));
107+
108+
assertThat(response.status(), is(Http.Status.OK_200));
112109
assertThat(getPokemonCount(), is(7));
113110
114-
webClient.delete()
111+
response = webClient.delete()
115112
.path("/pokemon/100")
116113
.request()
117-
.thenAccept(r -> assertThat(r.status(), is(Http.Status.OK_200)))
118-
.toCompletableFuture()
119-
.get();
114+
.await(Duration.ofSeconds(5));
120115
116+
assertThat(response.status(), is(Http.Status.OK_200));
121117
assertThat(getPokemonCount(), is(6));
122118
}
123119
124-
private int getPokemonCount() throws ExecutionException, InterruptedException {
125-
CompletableFuture<Integer> result = new CompletableFuture<>();
126-
webClient.get()
120+
private int getPokemonCount() {
121+
JsonArray array = webClient.get()
127122
.path("/pokemon")
128123
.request(JsonArray.class)
129-
.thenAccept(array -> result.complete(array.size()));
130-
return result.get();
131-
}
132-
]]></value>
124+
.await(Duration.ofSeconds(5));
125+
return array.size();
126+
}]]></value>
133127
</list>
134128
<list key="MainTest-helidon-imports">
135129
<value>io.helidon.common.http.Http</value>
136130
<value>io.helidon.media.jsonp.JsonpSupport</value>
137131
<value>io.helidon.media.jsonb.JsonbSupport</value>
138132
</list>
139133
<list key="MainTest-java-imports">
134+
<value>java.time.Duration</value>
140135
<value>java.util.Collections</value>
141136
<value>java.util.concurrent.ExecutionException</value>
137+
<value>java.util.concurrent.TimeoutException</value>
142138
<value>jakarta.json.Json</value>
143139
<value>jakarta.json.JsonBuilderFactory</value>
144140
<value>jakarta.json.JsonArray</value>
@@ -346,7 +342,7 @@ docker run --rm --name mongo -p 27017:27017 mongo
346342
<list key="DatabaseService-constructor" if="${server} != 'mongodb'">
347343
<value><![CDATA[
348344
dbClient.execute(handle -> handle.namedDml("create-table"))
349-
.thenAccept(System.out::println)
345+
.forSingle(System.out::println)
350346
.exceptionally(throwable -> {
351347
LOGGER.log(Level.WARNING, "Failed to create table, maybe it already exists?", throwable);
352348
return null;

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/FtService.java.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class FtService implements Service {
6161
long sleep = Long.parseLong(request.path().param("millis"));
6262
6363
timeout.invoke(() -> sleep(sleep))
64-
.thenAccept(response::send)
64+
.forSingle(response::send)
6565
.exceptionally(response::send);
6666
}
6767

@@ -78,17 +78,17 @@ public class FtService implements Service {
7878
return reactiveFailure();
7979
}
8080
return Single.just("calls/failures: " + current + "/" + failures.get());
81-
}).thenAccept(response::send)
81+
}).forSingle(response::send)
8282
.exceptionally(response::send);
8383
}
8484

8585
private void fallbackHandler(ServerRequest request, ServerResponse response) {
8686
boolean success = "true".equalsIgnoreCase(request.path().param("success"));
8787
8888
if (success) {
89-
fallback.invoke(this::reactiveData).thenAccept(response::send);
89+
fallback.invoke(this::reactiveData).forSingle(response::send);
9090
} else {
91-
fallback.invoke(this::reactiveFailure).thenAccept(response::send);
91+
fallback.invoke(this::reactiveFailure).forSingle(response::send);
9292
}
9393
}
9494

@@ -97,11 +97,11 @@ public class FtService implements Service {
9797
9898
if (success) {
9999
breaker.invoke(this::reactiveData)
100-
.thenAccept(response::send)
100+
.forSingle(response::send)
101101
.exceptionally(response::send);
102102
} else {
103103
breaker.invoke(this::reactiveFailure)
104-
.thenAccept(response::send)
104+
.forSingle(response::send)
105105
.exceptionally(response::send);
106106
}
107107

@@ -111,7 +111,7 @@ public class FtService implements Service {
111111
long sleep = Long.parseLong(request.path().param("millis"));
112112
113113
bulkhead.invoke(() -> sleep(sleep))
114-
.thenAccept(response::send)
114+
.forSingle(response::send)
115115
.exceptionally(response::send);
116116
}
117117

archetypes/helidon/src/main/archetype/se/custom/files/src/main/java/__pkg__/WebClientMain.java.mustache

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package {{package}};
22

3+
import java.time.Duration;
4+
35
import io.helidon.common.reactive.Single;
46
import io.helidon.config.Config;
57
import io.helidon.config.ConfigValue;
@@ -10,7 +12,6 @@ import io.helidon.webclient.WebClient;
1012

1113
public class WebClientMain {
1214
13-
1415
private WebClientMain() {
1516
}
1617

@@ -41,7 +42,7 @@ public class WebClientMain {
4142
{{/WebClient-builder}}
4243
.build();
4344

44-
performGetMethod(webClient).await();
45+
performGetMethod(webClient).await(Duration.ofSeconds(5));
4546
}
4647

4748
static Single<String> performGetMethod(WebClient webClient) {

0 commit comments

Comments
 (0)