diff --git a/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/Main.java b/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/Main.java index e31fb326c67..6549bb3ca66 100644 --- a/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/Main.java +++ b/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/Main.java @@ -1,20 +1,34 @@ +/* + * Copyright (c) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.helidon.examples.webserver.threads; - import io.helidon.logging.common.LogConfig; import io.helidon.config.Config; +import io.helidon.webclient.api.WebClient; import io.helidon.webserver.WebServer; import io.helidon.webserver.http.HttpRouting; - - - /** * The application main class. */ public class Main { + static WebServer webserver; + static WebClient webclient; /** * Cannot be instantiated. @@ -22,7 +36,6 @@ public class Main { private Main() { } - /** * Application main entry point. * @param args command line arguments. @@ -36,16 +49,17 @@ public static void main(String[] args) { Config config = Config.create(); Config.global(config); - - WebServer server = WebServer.builder() + webserver = WebServer.builder() .config(config.get("server")) .routing(Main::routing) .build() .start(); + webclient = WebClient.builder() + .baseUri("http://localhost:" + webserver.port() + "/thread") + .build(); - System.out.println("WEB server is up! http://localhost:" + server.port() + "/thread"); - + System.out.println("WEB server is up! http://localhost:" + webserver.port() + "/thread"); } diff --git a/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/ThreadService.java b/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/ThreadService.java index 7f07dffb5da..0323c0e2b26 100644 --- a/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/ThreadService.java +++ b/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/ThreadService.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.helidon.examples.webserver.threads; import java.util.ArrayList; @@ -28,10 +44,6 @@ class ThreadService implements HttpService { // Executor of virtual threads. private static ExecutorService virtualExecutorService; - WebClient client = WebClient.builder() - .baseUri("http://localhost:8080/thread") - .build(); - /** * The config value for the key {@code greeting}. */ @@ -100,7 +112,7 @@ private void computeHandler(ServerRequest request, ServerResponse response) { } /** - * Sleep for a specified number of secons. + * Sleep for a specified number of seconds. * The optional path parameter controls the number of seconds to sleep. Defaults to 1 * * @param request server request @@ -149,13 +161,14 @@ private void fanOutHandler(ServerRequest request, ServerResponse response) { } /** - * Simulate a remote client call be calling the sleep endpoint on ourself. + * Simulate a remote client call be calling this server's sleep endpoint * * @param seconds number of seconds the endpoint should sleep. - * @return + * @return string response from client */ private String callRemote(int seconds) { LOGGER.log(Level.INFO, Thread.currentThread() + ": Calling remote sleep for " + seconds + "s"); + WebClient client = Main.webclient; try (HttpClientResponse response = client.get("/sleep/" + seconds).request()) { if (response.status().equals(Status.OK_200)) { return response.as(String.class); @@ -168,7 +181,7 @@ private String callRemote(int seconds) { /** * Sleep current thread * @param seconds number of seconds to sleep - * @return + * @return number of seconds requested to sleep */ private int sleep(int seconds) { try { @@ -182,6 +195,7 @@ private int sleep(int seconds) { /** * Perform a CPU intensive computation * @param iterations: number of times to perform computation + * @return result of computation */ private double compute(int iterations) { LOGGER.log(Level.INFO, Thread.currentThread() + ": Computing with " + iterations + " iterations"); diff --git a/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/package-info.java b/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/package-info.java index 17d1693e0ef..f7dfc7e8a4e 100644 --- a/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/package-info.java +++ b/examples/webserver/threads/src/main/java/io/helidon/examples/webserver/threads/package-info.java @@ -1 +1,17 @@ +/* + * Copyright (c) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.helidon.examples.webserver.threads; diff --git a/examples/webserver/threads/src/main/resources/application.yaml b/examples/webserver/threads/src/main/resources/application.yaml index fbda3d443a1..05ee0f283e5 100644 --- a/examples/webserver/threads/src/main/resources/application.yaml +++ b/examples/webserver/threads/src/main/resources/application.yaml @@ -1,14 +1,29 @@ +# +# Copyright (c) 2024 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + server: port: 8080 host: 0.0.0.0 app: - greeting: "Hello" - application-platform-executor: - thread-name-prefix: "application-platform-executor-" - core-pool-size: 1 - max-pool-size: 2 - queue-capacity: 10 - application-virtual-executor: - thread-name-prefix: "application-virtual-executor-" - virtual-threads: true + application-platform-executor: + thread-name-prefix: "application-platform-executor-" + core-pool-size: 1 + max-pool-size: 2 + queue-capacity: 10 + application-virtual-executor: + thread-name-prefix: "application-virtual-executor-" + virtual-threads: true diff --git a/examples/webserver/threads/src/main/resources/logging.properties b/examples/webserver/threads/src/main/resources/logging.properties index 0f674e20e0a..5fddb7f3d50 100644 --- a/examples/webserver/threads/src/main/resources/logging.properties +++ b/examples/webserver/threads/src/main/resources/logging.properties @@ -1,3 +1,19 @@ +# +# Copyright (c) 2024 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + handlers=io.helidon.logging.jul.HelidonConsoleHandler java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS.%1$tL %5$s%6$s%n # Global logging level. Can be overridden by specific loggers diff --git a/examples/webserver/threads/src/test/java/io/helidon/examples/webserver/threads/MainTest.java b/examples/webserver/threads/src/test/java/io/helidon/examples/webserver/threads/MainTest.java index 274eeb2557a..bbe78c10f9a 100644 --- a/examples/webserver/threads/src/test/java/io/helidon/examples/webserver/threads/MainTest.java +++ b/examples/webserver/threads/src/test/java/io/helidon/examples/webserver/threads/MainTest.java @@ -1,9 +1,57 @@ +/* + * Copyright (c) 2024 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package io.helidon.examples.webserver.threads; -import io.helidon.webserver.testing.junit5.RoutingTest; +import io.helidon.http.Status; +import io.helidon.webclient.api.HttpClientResponse; +import io.helidon.webclient.api.WebClient; +import io.helidon.webserver.http.HttpRouting; +import io.helidon.webserver.testing.junit5.ServerTest; +import io.helidon.webserver.testing.junit5.SetUpRoute; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; -@RoutingTest +@ServerTest class MainTest { - MainTest() { + private final WebClient client; + + protected MainTest(WebClient client) { + this.client = client; + Main.webclient = this.client; // Needed for ThreadService to make calls + } + + @SetUpRoute + static void routing(HttpRouting.Builder builder) { + Main.routing(builder); + } + + @Test + void testFanOut() { + try (HttpClientResponse response = client.get("/thread/fanout/2").request()) { + assertThat(response.status(), is(Status.OK_200)); + } + } + + @Test + void testCompute() { + try (HttpClientResponse response = client.get("/thread/compute").request()) { + assertThat(response.status(), is(Status.OK_200)); + } } } \ No newline at end of file