From 8e97a3de341661f1ea99c8d0af303f92e4d98020 Mon Sep 17 00:00:00 2001 From: Joe Di Pol Date: Tue, 13 Apr 2021 12:07:12 -0700 Subject: [PATCH 1/2] Replace webserver startup poling loop with await() in tests --- .../java/io/helidon/examples/cors/Main.java | 17 ++++++-------- .../io/helidon/examples/cors/MainTest.java | 8 ++----- .../io/helidon/service/employee/Main.java | 12 +++++----- .../io/helidon/service/employee/MainTest.java | 15 +++---------- .../examples/integrations/neo4j/se/Main.java | 15 +++++-------- .../examples/quickstart/se/MainTest.java | 12 +--------- .../io/helidon/examples/openapi/Main.java | 16 ++++++-------- .../io/helidon/examples/openapi/MainTest.java | 14 ++---------- .../examples/mtls/ServerBuilderMain.java | 13 ++++++----- .../examples/mtls/ServerConfigMain.java | 14 +++++++----- .../examples/mtls/MutualTlsExampleTest.java | 22 +++++-------------- 11 files changed, 54 insertions(+), 104 deletions(-) diff --git a/examples/cors/src/main/java/io/helidon/examples/cors/Main.java b/examples/cors/src/main/java/io/helidon/examples/cors/Main.java index 09001db6c72..09662358067 100644 --- a/examples/cors/src/main/java/io/helidon/examples/cors/Main.java +++ b/examples/cors/src/main/java/io/helidon/examples/cors/Main.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 2021 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. @@ -20,6 +20,7 @@ import java.util.logging.Logger; import io.helidon.common.LogConfig; +import io.helidon.common.reactive.Single; import io.helidon.config.Config; import io.helidon.health.HealthSupport; import io.helidon.health.checks.HealthChecks; @@ -55,7 +56,7 @@ public static void main(final String[] args) throws IOException { * @return the created {@link WebServer} instance * @throws IOException if there are problems reading logging properties */ - static WebServer startServer() throws IOException { + static Single startServer() throws IOException { // load logging configuration LogConfig.configureRuntime(); @@ -64,15 +65,13 @@ static WebServer startServer() throws IOException { Config config = Config.create(); // Get webserver config from the "server" section of application.yaml - WebServer server = WebServer.builder(createRouting(config)) + Single server = WebServer.builder(createRouting(config)) .config(config.get("server")) .addMediaSupport(JsonpSupport.create()) - .build(); + .build() + .start(); - // Try to start the server. If successful, print some info and arrange to - // print a message at shutdown. If unsuccessful, print the exception. - server.start() - .thenAccept(ws -> { + server.thenAccept(ws -> { System.out.println( "WEB server is up! http://localhost:" + ws.port() + "/greet"); ws.whenShutdown().thenRun(() @@ -84,8 +83,6 @@ static WebServer startServer() throws IOException { return null; }); - // Server threads are not daemon. No need to block. Just react. - return server; } diff --git a/examples/cors/src/test/java/io/helidon/examples/cors/MainTest.java b/examples/cors/src/test/java/io/helidon/examples/cors/MainTest.java index ef4150980d8..b572be15794 100644 --- a/examples/cors/src/test/java/io/helidon/examples/cors/MainTest.java +++ b/examples/cors/src/test/java/io/helidon/examples/cors/MainTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 2021 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. @@ -55,11 +55,7 @@ public class MainTest { public static void start() throws Exception { // the port is only available if the server started already! // so we need to wait - webServer = Main.startServer() - .start() - .toCompletableFuture() - .get(); - + webServer = Main.startServer().await(); webClient = WebClient.builder() .baseUri("http://localhost:" + webServer.port()) diff --git a/examples/employee-app/src/main/java/io/helidon/service/employee/Main.java b/examples/employee-app/src/main/java/io/helidon/service/employee/Main.java index 6c8f21fca08..be19a7071b2 100644 --- a/examples/employee-app/src/main/java/io/helidon/service/employee/Main.java +++ b/examples/employee-app/src/main/java/io/helidon/service/employee/Main.java @@ -17,6 +17,7 @@ package io.helidon.service.employee; import io.helidon.common.LogConfig; +import io.helidon.common.reactive.Single; import io.helidon.config.Config; import io.helidon.health.HealthSupport; import io.helidon.health.checks.HealthChecks; @@ -49,7 +50,7 @@ public static void main(final String[] args) { * Start the server. * @return the created {@link WebServer} instance */ - static WebServer startServer() { + static Single startServer() { // load logging configuration LogConfig.configureRuntime(); @@ -58,14 +59,13 @@ static WebServer startServer() { Config config = Config.create(); // Get webserver config from the "server" section of application.yaml and JSON support registration - WebServer server = WebServer.builder(createRouting(config)) + Single server = WebServer.builder(createRouting(config)) .config(config.get("server")) .addMediaSupport(JsonbSupport.create()) - .build(); + .build() + .start(); - // Try to start the server. If successful, print some info and arrange to - // print a message at shutdown. If unsuccessful, print the exception. - server.start().thenAccept(ws -> { + server.thenAccept(ws -> { System.out.println("WEB server is up!"); System.out.println("Web client at: http://localhost:" + ws.port() + "/public/index.html"); diff --git a/examples/employee-app/src/test/java/io/helidon/service/employee/MainTest.java b/examples/employee-app/src/test/java/io/helidon/service/employee/MainTest.java index 5e7a41cb3f9..855369d460e 100644 --- a/examples/employee-app/src/test/java/io/helidon/service/employee/MainTest.java +++ b/examples/employee-app/src/test/java/io/helidon/service/employee/MainTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020 Oracle and/or its affiliates. + * Copyright (c) 2019, 2021 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. @@ -20,6 +20,7 @@ import io.helidon.common.http.Http; import io.helidon.common.http.MediaType; +import io.helidon.common.reactive.Single; import io.helidon.webclient.WebClient; import io.helidon.webserver.WebServer; @@ -35,17 +36,7 @@ public class MainTest { @BeforeAll public static void startTheServer() throws Exception { - webServer = Main.startServer(); - - long timeout = 2000; // 2 seconds should be enough to start the server - long now = System.currentTimeMillis(); - - while (!webServer.isRunning()) { - Thread.sleep(100); - if ((System.currentTimeMillis() - now) > timeout) { - Assertions.fail("Failed to start webserver"); - } - } + webServer = Main.startServer().await(); webClient = WebClient.builder() .baseUri("http://localhost:" + webServer.port()) diff --git a/examples/integrations/neo4j/neo4j-se/src/main/java/io/helidon/examples/integrations/neo4j/se/Main.java b/examples/integrations/neo4j/neo4j-se/src/main/java/io/helidon/examples/integrations/neo4j/se/Main.java index 16bdaacb6c0..9627fce12d9 100644 --- a/examples/integrations/neo4j/neo4j-se/src/main/java/io/helidon/examples/integrations/neo4j/se/Main.java +++ b/examples/integrations/neo4j/neo4j-se/src/main/java/io/helidon/examples/integrations/neo4j/se/Main.java @@ -21,6 +21,7 @@ import java.util.logging.LogManager; import io.helidon.common.LogConfig; +import io.helidon.common.reactive.Single; import io.helidon.config.Config; import io.helidon.examples.integrations.neo4j.se.domain.MovieRepository; import io.helidon.health.HealthSupport; @@ -61,23 +62,21 @@ public static void main(final String[] args) throws IOException { * @return the created WebServer instance * @throws IOException if there are problems reading logging properties */ - public static WebServer startServer() throws IOException { + public static Single startServer() throws IOException { // load logging configuration LogConfig.configureRuntime(); // By default this will pick up application.yaml from the classpath Config config = Config.create(); - WebServer server = WebServer.builder(createRouting(config)) + Single server = WebServer.builder(createRouting(config)) .config(config.get("server")) .addMediaSupport(JsonpSupport.create()) .addMediaSupport(JsonbSupport.create()) - .build(); + .build() + .start(); - // Try to start the server. If successful, print some info and arrange to - // print a message at shutdown. If unsuccessful, print the exception. - server.start() - .thenAccept(ws -> { + server.thenAccept(ws -> { System.out.println( "WEB server is up! http://localhost:" + ws.port() + "/api/movies"); ws.whenShutdown().thenRun(() @@ -89,8 +88,6 @@ public static WebServer startServer() throws IOException { return null; }); - // Server threads are not daemon. No need to block. Just react. - return server; } diff --git a/examples/integrations/neo4j/neo4j-se/src/test/java/io/helidon/examples/quickstart/se/MainTest.java b/examples/integrations/neo4j/neo4j-se/src/test/java/io/helidon/examples/quickstart/se/MainTest.java index 7b7889c7bc0..6b7b0b1d8ce 100644 --- a/examples/integrations/neo4j/neo4j-se/src/test/java/io/helidon/examples/quickstart/se/MainTest.java +++ b/examples/integrations/neo4j/neo4j-se/src/test/java/io/helidon/examples/quickstart/se/MainTest.java @@ -54,17 +54,7 @@ private static void startTheServer() throws Exception { System.setProperty("neo4j.uri", embeddedDatabaseServer.boltURI().toString()); - webServer = Main.startServer(); - - long timeout = 2000; // 2 seconds should be enough to start the server - long now = System.currentTimeMillis(); - - while (!webServer.isRunning()) { - Thread.sleep(100); - if ((System.currentTimeMillis() - now) > timeout) { - Assertions.fail("Failed to start webserver"); - } - } + webServer = Main.startServer().await(); webClient = WebClient.builder() .baseUri("http://localhost:" + webServer.port()) diff --git a/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java b/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java index 6572d51100e..3750747a868 100644 --- a/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java +++ b/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2020 Oracle and/or its affiliates. + * Copyright (c) 2019, 2021 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. @@ -17,6 +17,7 @@ package io.helidon.examples.openapi; import io.helidon.common.LogConfig; +import io.helidon.common.reactive.Single; import io.helidon.config.Config; import io.helidon.health.HealthSupport; import io.helidon.health.checks.HealthChecks; @@ -49,7 +50,7 @@ public static void main(final String[] args) { * Start the server. * @return the created {@link WebServer} instance */ - static WebServer startServer() { + static Single startServer() { // load logging configuration LogConfig.configureRuntime(); @@ -58,15 +59,15 @@ static WebServer startServer() { Config config = Config.create(); // Get webserver config from the "server" section of application.yaml and register JSON support - WebServer server = WebServer.builder(createRouting(config)) + Single server = WebServer.builder(createRouting(config)) .config(config.get("server")) .addMediaSupport(JsonpSupport.create()) - .build(); + .build() + .start(); // Try to start the server. If successful, print some info and arrange to // print a message at shutdown. If unsuccessful, print the exception. - server.start() - .thenAccept(ws -> { + server.thenAccept(ws -> { System.out.println( "WEB server is up! http://localhost:" + ws.port() + "/greet"); ws.whenShutdown().thenRun(() @@ -77,9 +78,6 @@ static WebServer startServer() { t.printStackTrace(System.err); return null; }); - - // Server threads are not daemon. No need to block. Just react. - return server; } diff --git a/examples/openapi/src/test/java/io/helidon/examples/openapi/MainTest.java b/examples/openapi/src/test/java/io/helidon/examples/openapi/MainTest.java index dae767f53ac..26d6ff88431 100644 --- a/examples/openapi/src/test/java/io/helidon/examples/openapi/MainTest.java +++ b/examples/openapi/src/test/java/io/helidon/examples/openapi/MainTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020 Oracle and/or its affiliates. + * Copyright (c) 2018, 2021 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. @@ -52,17 +52,7 @@ public class MainTest { @BeforeAll public static void startTheServer() throws Exception { - webServer = Main.startServer(); - - long timeout = 2000; // 2 seconds should be enough to start the server - long now = System.currentTimeMillis(); - - while (!webServer.isRunning()) { - Thread.sleep(100); - if ((System.currentTimeMillis() - now) > timeout) { - Assertions.fail("Failed to start webserver"); - } - } + webServer = Main.startServer().await(); webClient = WebClient.builder() .baseUri("http://localhost:" + webServer.port()) diff --git a/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerBuilderMain.java b/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerBuilderMain.java index 8a5489f1014..c225c057c50 100644 --- a/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerBuilderMain.java +++ b/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerBuilderMain.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 2021 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. @@ -18,6 +18,7 @@ import io.helidon.common.configurable.Resource; import io.helidon.common.http.Http; import io.helidon.common.pki.KeyConfig; +import io.helidon.common.reactive.Single; import io.helidon.webserver.ClientAuthentication; import io.helidon.webserver.Routing; import io.helidon.webserver.SocketConfiguration; @@ -49,20 +50,20 @@ public static void main(String[] args) { startServer(8080, 443); } - static WebServer startServer(int unsecured, int secured) { + static Single startServer(int unsecured, int secured) { SocketConfiguration socketConf = SocketConfiguration.builder() .name("secured") .port(secured) .tls(tlsConfig()) .build(); - WebServer webServer = WebServer.builder() + Single webServer = WebServer.builder() .port(unsecured) .routing(createPlainRouting()) .addSocket(socketConf, createMtlsRouting()) - .build(); + .build() + .start(); - webServer.start() - .thenAccept(ws -> { + webServer.thenAccept(ws -> { System.out.println("WebServer is up!"); System.out.println("Unsecured: http://localhost:" + ws.port() + "/"); System.out.println("Secured: https://localhost:" + ws.port("secured") + "/"); diff --git a/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerConfigMain.java b/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerConfigMain.java index ff71cd24da1..da62e94413e 100644 --- a/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerConfigMain.java +++ b/examples/webserver/mutual-tls/src/main/java/io/helidon/webserver/examples/mtls/ServerConfigMain.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 2021 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. @@ -16,6 +16,7 @@ package io.helidon.webserver.examples.mtls; import io.helidon.common.http.Http; +import io.helidon.common.reactive.Single; import io.helidon.config.Config; import io.helidon.webserver.Routing; import io.helidon.webserver.WebServer; @@ -45,13 +46,14 @@ public static void main(String[] args) { startServer(config.get("server")); } - static WebServer startServer(Config config) { - WebServer webServer = WebServer.builder(createPlainRouting()) + static Single startServer(Config config) { + Single webServer = WebServer.builder(createPlainRouting()) .config(config) .addNamedRouting("secured", createMtlsRouting()) - .build(); - webServer.start() - .thenAccept(ws -> { + .build() + .start(); + + webServer.thenAccept(ws -> { System.out.println("WebServer is up!"); System.out.println("Unsecured: http://localhost:" + ws.port() + "/"); System.out.println("Secured: https://localhost:" + ws.port("secured") + "/"); diff --git a/examples/webserver/mutual-tls/src/test/java/io/helidon/webserver/examples/mtls/MutualTlsExampleTest.java b/examples/webserver/mutual-tls/src/test/java/io/helidon/webserver/examples/mtls/MutualTlsExampleTest.java index 728f92a5f39..38edc26620d 100644 --- a/examples/webserver/mutual-tls/src/test/java/io/helidon/webserver/examples/mtls/MutualTlsExampleTest.java +++ b/examples/webserver/mutual-tls/src/test/java/io/helidon/webserver/examples/mtls/MutualTlsExampleTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. + * Copyright (c) 2020, 2021 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. @@ -19,6 +19,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import io.helidon.common.reactive.Single; import io.helidon.config.Config; import io.helidon.config.ConfigSources; import io.helidon.webclient.WebClient; @@ -50,7 +51,7 @@ public void killServer() throws InterruptedException, ExecutionException, Timeou @Test public void testConfigAccessSuccessful() throws InterruptedException { Config config = Config.just(() -> ConfigSources.classpath("application-test.yaml").build()); - waitForServerToStart(ServerConfigMain.startServer(config.get("server"))); + webServer = ServerConfigMain.startServer(config.get("server")).await(); WebClient webClient = WebClient.create(config.get("client")); assertThat(ClientConfigMain.callUnsecured(webClient, webServer.port()), is("Hello world unsecured!")); @@ -59,23 +60,10 @@ public void testConfigAccessSuccessful() throws InterruptedException { @Test public void testBuilderAccessSuccessful() throws InterruptedException { - waitForServerToStart(ServerBuilderMain.startServer(-1, -1)); + webServer = ServerBuilderMain.startServer(-1, -1).await(); WebClient webClient = ClientBuilderMain.createWebClient(); assertThat(ClientBuilderMain.callUnsecured(webClient, webServer.port()), is("Hello world unsecured!")); assertThat(ClientBuilderMain.callSecured(webClient, webServer.port("secured")), is("Hello Helidon-client!")); } - - private void waitForServerToStart(WebServer webServer) throws InterruptedException { - this.webServer = webServer; - long timeout = 2000; // 2 seconds should be enough to start the server - long now = System.currentTimeMillis(); - - while (!webServer.isRunning()) { - Thread.sleep(100); - if ((System.currentTimeMillis() - now) > timeout) { - Assertions.fail("Failed to start webserver"); - } - } - } -} +} \ No newline at end of file From 9be38e08fd4a9f142a86d05da35d55dc9b8707a8 Mon Sep 17 00:00:00 2001 From: Joe Di Pol Date: Tue, 13 Apr 2021 12:11:51 -0700 Subject: [PATCH 2/2] Remove comment --- .../openapi/src/main/java/io/helidon/examples/openapi/Main.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java b/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java index 3750747a868..8a51f10ec33 100644 --- a/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java +++ b/examples/openapi/src/main/java/io/helidon/examples/openapi/Main.java @@ -65,8 +65,6 @@ static Single startServer() { .build() .start(); - // Try to start the server. If successful, print some info and arrange to - // print a message at shutdown. If unsuccessful, print the exception. server.thenAccept(ws -> { System.out.println( "WEB server is up! http://localhost:" + ws.port() + "/greet");