Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Archetype] Add await timeout and replace thenAccept to forSingle #6558

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions archetypes/helidon/src/main/archetype/common/observability.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2022 Oracle and/or its affiliates.
Copyright (c) 2022, 2023 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -153,14 +153,14 @@ curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
String get = webClient.get()
.path("/simple-greet/greet-count")
.request(String.class)
.await();
.await(Duration.ofSeconds(5));

assertThat(get, containsString("Hello World!"));

String openMetricsOutput = webClient.get()
.path("/metrics")
.request(String.class)
.await();
.await(Duration.ofSeconds(5));

assertThat("Metrics output", openMetricsOutput, containsString("application_accessctr_total"));
}]]></value>
Expand Down Expand Up @@ -270,14 +270,14 @@ curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
String get = webClient.get()
.path("/micrometer-greet/greet-count")
.request(String.class)
.await();
.await(Duration.ofSeconds(5));

assertThat(get, containsString("Hello World!"));

String openMetricsOutput = webClient.get()
.path("/micrometer")
.request(String.class)
.await();
.await(Duration.ofSeconds(5));

assertThat("Metrics output", openMetricsOutput, containsString("allRequests_total 1"));
}]]></value>
Expand Down Expand Up @@ -355,7 +355,7 @@ allRequests_total 0.0
WebClientResponse response = webClient.get()
.path("/metrics")
.request()
.await();
.await(Duration.ofSeconds(5));
assertThat(response.status().code(), is(200));
}]]></value>
</list>
Expand Down Expand Up @@ -455,7 +455,7 @@ curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
WebClientResponse response = webClient.get()
.path("health")
.request()
.await();
.await(Duration.ofSeconds(5));
assertThat(response.status().code(), is(200));
}]]></value>
</list>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -72,7 +73,7 @@ class TestCORS {

WebClientResponse r = builder.path("/simple-greet")
.submit()
.await();
.await(Duration.ofSeconds(5));

assertThat("pre-flight status", r.status().code(), is(200));
Headers preflightResponseHeaders = r.headers();
Expand Down Expand Up @@ -118,20 +119,20 @@ class TestCORS {
return builder
.path("/simple-greet")
.submit()
.await();
.await(Duration.ofSeconds(5));
}

private static String fromPayload(WebClientResponse response) {
return response
.content()
.as(String.class)
.await();
.await(Duration.ofSeconds(5));
}

private static WebClientResponse putResponse(String message, WebClientRequestBuilder builder) {
return builder
.path("/simple-greet")
.submit(message)
.await();
.await(Duration.ofSeconds(5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class Main {

// Try to start the server. If successful, print some info and arrange to
// print a message at shutdown. If unsuccessful, print the exception.
webserver.thenAccept(ws -> {
webserver.forSingle(ws -> {
{{Main-readyMessage}}
ws.whenShutdown().thenRun(() -> System.out.println("WEB server is DOWN. Good bye!"));
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MainTest {

@BeforeAll
static void startTheServer() {
webServer = Main.startServer().await();
webServer = Main.startServer().await(Duration.ofSeconds(10));

webClient = WebClient.builder()
.baseUri("http://localhost:" + webServer.port())
Expand All @@ -37,11 +37,9 @@ class MainTest {
}

@AfterAll
static void stopServer() throws ExecutionException, InterruptedException, TimeoutException {
static void stopServer() {
if (webServer != null) {
webServer.shutdown()
.toCompletableFuture()
.get(10, TimeUnit.SECONDS);
webServer.shutdown().await(10, TimeUnit.SECONDS);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2022 Oracle and/or its affiliates.
Copyright (c) 2022, 2023 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -22,6 +22,9 @@

<output>
<model>
<list key="MainTest-java-imports">
<value>java.time.Duration</value>
</list>
<list key="MainTest-other-imports">
<value>org.junit.jupiter.api.Order</value>
</list>
Expand All @@ -35,7 +38,7 @@
JsonObject jsonObject = webClient.get()
.path("/simple-greet")
.request(JsonObject.class)
.await();
.await(Duration.ofSeconds(5));
assertThat(jsonObject.getString("message"), is("Hello World!"));
}]]></value>
</list>
Expand All @@ -45,7 +48,7 @@
Message json = webClient.get()
.path("/simple-greet")
.request(Message.class)
.await();
.await(Duration.ofSeconds(5));
assertThat(json.getMessage(), is("Hello World!"));
}]]></value>
</list>
Expand Down
68 changes: 32 additions & 36 deletions archetypes/helidon/src/main/archetype/se/custom/database.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2022 Oracle and/or its affiliates.
Copyright (c) 2022, 2023 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -67,78 +67,74 @@ Instructions for H2 can be found here: https://www.h2database.com/html/cheatShee
<list key="MainTest-methods">
<value template="mustache"><![CDATA[
@Test
void testPokemonTypes() throws ExecutionException, InterruptedException {
webClient.get()
void testPokemonTypes() {
JsonArray array = webClient.get()
.path("/type")
.request(JsonArray.class)
.thenAccept(array -> {
assertThat(array.size(), is(18));
assertThat(array.get(0).asJsonObject().getInt("{{id}}"), is(1));
assertThat(array.get(0).asJsonObject().getString("{{name}}"), is("Normal"));
})
.toCompletableFuture()
.get();
.await(Duration.ofSeconds(5));

assertThat(array.size(), is(18));
assertThat(array.get(0).asJsonObject().getInt("{{id}}"), is(1));
assertThat(array.get(0).asJsonObject().getString("{{name}}"), is("Normal"));
}

@Test
void testPokemons() throws ExecutionException, InterruptedException {
void testPokemons() {
assertThat(getPokemonCount(), is(6));

webClient.get()
JsonObject pokemon = webClient.get()
.path("/pokemon/1")
.request(JsonObject.class)
.thenAccept(pokemon -> assertThat(pokemon.getString("{{name}}"), is("Bulbasaur")))
.toCompletableFuture()
.get();
.await(Duration.ofSeconds(5));

assertThat(pokemon.getString("{{name}}"), is("Bulbasaur"));

webClient.get()
pokemon = webClient.get()
.path("/pokemon/name/Charmander")
.request(JsonObject.class)
.thenAccept(pokemon -> assertThat(pokemon.getJsonNumber("{{id_type}}").intValue(), is(10)))
.toCompletableFuture()
.get();
.await(Duration.ofSeconds(5));

assertThat(pokemon.getJsonNumber("{{id_type}}").intValue(), is(10));

JsonObject json = JSON_BUILDER.createObjectBuilder()
.add("id", 100)
.add("idType", 1)
.add("name", "Test")
.build();
webClient.post()
WebClientResponse response = webClient.post()
.path("/pokemon")
.submit(json)
.thenAccept(r -> assertThat(r.status(), is(Http.Status.OK_200)))
.toCompletableFuture()
.get();
.await(Duration.ofSeconds(5));

assertThat(response.status(), is(Http.Status.OK_200));
assertThat(getPokemonCount(), is(7));

webClient.delete()
response = webClient.delete()
.path("/pokemon/100")
.request()
.thenAccept(r -> assertThat(r.status(), is(Http.Status.OK_200)))
.toCompletableFuture()
.get();
.await(Duration.ofSeconds(5));

assertThat(response.status(), is(Http.Status.OK_200));
assertThat(getPokemonCount(), is(6));
}

private int getPokemonCount() throws ExecutionException, InterruptedException {
CompletableFuture<Integer> result = new CompletableFuture<>();
webClient.get()
private int getPokemonCount() {
JsonArray array = webClient.get()
.path("/pokemon")
.request(JsonArray.class)
.thenAccept(array -> result.complete(array.size()));
return result.get();
}
]]></value>
.await(Duration.ofSeconds(5));
return array.size();
}]]></value>
</list>
<list key="MainTest-helidon-imports">
<value>io.helidon.common.http.Http</value>
<value>io.helidon.media.jsonp.JsonpSupport</value>
<value>io.helidon.media.jsonb.JsonbSupport</value>
</list>
<list key="MainTest-java-imports">
<value>java.time.Duration</value>
<value>java.util.Collections</value>
<value>java.util.concurrent.ExecutionException</value>
<value>java.util.concurrent.TimeoutException</value>
<value>jakarta.json.Json</value>
<value>jakarta.json.JsonBuilderFactory</value>
<value>jakarta.json.JsonArray</value>
Expand Down Expand Up @@ -346,7 +342,7 @@ docker run --rm --name mongo -p 27017:27017 mongo
<list key="DatabaseService-constructor" if="${server} != 'mongodb'">
<value><![CDATA[
dbClient.execute(handle -> handle.namedDml("create-table"))
.thenAccept(System.out::println)
.forSingle(System.out::println)
.exceptionally(throwable -> {
LOGGER.log(Level.WARNING, "Failed to create table, maybe it already exists?", throwable);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class FtService implements Service {
long sleep = Long.parseLong(request.path().param("millis"));

timeout.invoke(() -> sleep(sleep))
.thenAccept(response::send)
.forSingle(response::send)
.exceptionally(response::send);
}

Expand All @@ -78,17 +78,17 @@ public class FtService implements Service {
return reactiveFailure();
}
return Single.just("calls/failures: " + current + "/" + failures.get());
}).thenAccept(response::send)
}).forSingle(response::send)
.exceptionally(response::send);
}

private void fallbackHandler(ServerRequest request, ServerResponse response) {
boolean success = "true".equalsIgnoreCase(request.path().param("success"));

if (success) {
fallback.invoke(this::reactiveData).thenAccept(response::send);
fallback.invoke(this::reactiveData).forSingle(response::send);
} else {
fallback.invoke(this::reactiveFailure).thenAccept(response::send);
fallback.invoke(this::reactiveFailure).forSingle(response::send);
}
}

Expand All @@ -97,11 +97,11 @@ public class FtService implements Service {

if (success) {
breaker.invoke(this::reactiveData)
.thenAccept(response::send)
.forSingle(response::send)
.exceptionally(response::send);
} else {
breaker.invoke(this::reactiveFailure)
.thenAccept(response::send)
.forSingle(response::send)
.exceptionally(response::send);
}

Expand All @@ -111,7 +111,7 @@ public class FtService implements Service {
long sleep = Long.parseLong(request.path().param("millis"));

bulkhead.invoke(() -> sleep(sleep))
.thenAccept(response::send)
.forSingle(response::send)
.exceptionally(response::send);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package {{package}};

import java.time.Duration;

import io.helidon.common.reactive.Single;
import io.helidon.config.Config;
import io.helidon.config.ConfigValue;
Expand All @@ -10,7 +12,6 @@ import io.helidon.webclient.WebClient;

public class WebClientMain {


private WebClientMain() {
}

Expand Down Expand Up @@ -41,7 +42,7 @@ public class WebClientMain {
{{/WebClient-builder}}
.build();

performGetMethod(webClient).await();
performGetMethod(webClient).await(Duration.ofSeconds(5));
}

static Single<String> performGetMethod(WebClient webClient) {
Expand Down
Loading