From b293661b0e5ab25a3e51deec67dabe7724ae44ce Mon Sep 17 00:00:00 2001 From: Daniel Kec Date: Wed, 4 Dec 2024 16:36:07 +0100 Subject: [PATCH] Run tests on random ports --- .../examples/dbclient/jdbc/JdbcExampleMain.java | 10 +++++----- .../dbclient/jdbc/AbstractPokemonServiceTest.java | 6 +++++- .../examples/dbclient/mongo/MongoDbExampleMain.java | 10 +++++----- .../io/helidon/examples/dbclient/mongo/MainIT.java | 6 +++++- .../io/helidon/examples/dbclient/pokemons/Main.java | 10 +++++----- .../dbclient/pokemons/AbstractPokemonServiceTest.java | 6 +++++- .../examples/metrics/filtering/se/MainTest.java | 2 ++ .../helidon/examples/se/httpstatuscount/MainTest.java | 2 ++ .../java/io/helidon/examples/metrics/kpi/MainTest.java | 2 ++ .../helidon/examples/webserver/opentracing/Main.java | 10 +++++----- .../examples/webserver/opentracing/MainTest.java | 6 +++++- 11 files changed, 46 insertions(+), 24 deletions(-) diff --git a/examples/dbclient/jdbc/src/main/java/io/helidon/examples/dbclient/jdbc/JdbcExampleMain.java b/examples/dbclient/jdbc/src/main/java/io/helidon/examples/dbclient/jdbc/JdbcExampleMain.java index 1f9caabb0..a066d866b 100644 --- a/examples/dbclient/jdbc/src/main/java/io/helidon/examples/dbclient/jdbc/JdbcExampleMain.java +++ b/examples/dbclient/jdbc/src/main/java/io/helidon/examples/dbclient/jdbc/JdbcExampleMain.java @@ -46,12 +46,14 @@ public static void main(String[] args) { LogConfig.configureRuntime(); // Prepare routing for the server - WebServer server = setupServer(WebServer.builder()); + WebServer server = setupServer(WebServer.builder()) + .build() + .start(); System.out.println("WEB server is up! http://localhost:" + server.port() + "/"); } - static WebServer setupServer(WebServerConfig.Builder builder) { + static WebServerConfig.Builder setupServer(WebServerConfig.Builder builder) { // By default, this will pick up application.yaml from the classpath Config config = Config.global(); @@ -68,8 +70,6 @@ static WebServer setupServer(WebServerConfig.Builder builder) { return builder .config(config.get("server")) .addFeature(observe) - .routing(routing -> routing.register("/db", new PokemonService(dbClient))) - .build() - .start(); + .routing(routing -> routing.register("/db", new PokemonService(dbClient))); } } diff --git a/examples/dbclient/jdbc/src/test/java/io/helidon/examples/dbclient/jdbc/AbstractPokemonServiceTest.java b/examples/dbclient/jdbc/src/test/java/io/helidon/examples/dbclient/jdbc/AbstractPokemonServiceTest.java index 384e7daa1..e0eb966df 100644 --- a/examples/dbclient/jdbc/src/test/java/io/helidon/examples/dbclient/jdbc/AbstractPokemonServiceTest.java +++ b/examples/dbclient/jdbc/src/test/java/io/helidon/examples/dbclient/jdbc/AbstractPokemonServiceTest.java @@ -41,7 +41,11 @@ abstract class AbstractPokemonServiceTest { private static WebClient client; static void beforeAll() { - server = JdbcExampleMain.setupServer(WebServer.builder()); + server = JdbcExampleMain.setupServer(WebServer.builder()) + .port(-1) + .build() + .start(); + client = WebClient.create(config -> config.baseUri("http://localhost:" + server.port()) .addMediaSupport(JsonpSupport.create())); } diff --git a/examples/dbclient/mongodb/src/main/java/io/helidon/examples/dbclient/mongo/MongoDbExampleMain.java b/examples/dbclient/mongodb/src/main/java/io/helidon/examples/dbclient/mongo/MongoDbExampleMain.java index de853eb9c..643204aa7 100644 --- a/examples/dbclient/mongodb/src/main/java/io/helidon/examples/dbclient/mongo/MongoDbExampleMain.java +++ b/examples/dbclient/mongodb/src/main/java/io/helidon/examples/dbclient/mongo/MongoDbExampleMain.java @@ -56,20 +56,20 @@ static WebServer startServer() { // load logging configuration LogConfig.configureRuntime(); - WebServer server = setupServer(WebServer.builder()); + WebServer server = setupServer(WebServer.builder()) + .build() + .start(); System.out.println("WEB server is up! http://localhost:" + server.port() + "/"); return server; } - static WebServer setupServer(WebServerConfig.Builder builder) { + static WebServerConfig.Builder setupServer(WebServerConfig.Builder builder) { // By default, this will pick up application.yaml from the classpath Config config = Config.create(); return builder.routing(routing -> routing(routing, config)) - .config(config.get("server")) - .build() - .start(); + .config(config.get("server")); } /** diff --git a/examples/dbclient/mongodb/src/test/java/io/helidon/examples/dbclient/mongo/MainIT.java b/examples/dbclient/mongodb/src/test/java/io/helidon/examples/dbclient/mongo/MainIT.java index 18b89b947..8f88bef49 100644 --- a/examples/dbclient/mongodb/src/test/java/io/helidon/examples/dbclient/mongo/MainIT.java +++ b/examples/dbclient/mongodb/src/test/java/io/helidon/examples/dbclient/mongo/MainIT.java @@ -58,7 +58,11 @@ public class MainIT { static void beforeAll() { String url = String.format("mongodb://127.0.0.1:%s/pokemon", container.getMappedPort(27017)); System.setProperty(CONNECTION_URL_KEY, url); - server = MongoDbExampleMain.setupServer(WebServer.builder()); + server = MongoDbExampleMain.setupServer(WebServer.builder()) + .port(-1) + .build() + .start(); + client = WebClient.create(config -> config.baseUri("http://localhost:" + server.port()) .addMediaSupport(JsonbSupport.create(Config.create())) .addMediaSupport(JsonpSupport.create())); diff --git a/examples/dbclient/pokemons/src/main/java/io/helidon/examples/dbclient/pokemons/Main.java b/examples/dbclient/pokemons/src/main/java/io/helidon/examples/dbclient/pokemons/Main.java index d3e83ca89..bf56292d7 100644 --- a/examples/dbclient/pokemons/src/main/java/io/helidon/examples/dbclient/pokemons/Main.java +++ b/examples/dbclient/pokemons/src/main/java/io/helidon/examples/dbclient/pokemons/Main.java @@ -74,12 +74,14 @@ private static void startServer() { Config config = mongo ? Config.create(ConfigSources.classpath(MONGO_CFG)) : Config.create(); Config.global(config); - WebServer server = setupServer(WebServer.builder()); + WebServer server = setupServer(WebServer.builder()) + .build() + .start(); System.out.println("WEB server is up! http://localhost:" + server.port() + "/"); } - static WebServer setupServer(WebServerConfig.Builder builder) { + static WebServerConfig.Builder setupServer(WebServerConfig.Builder builder) { Config config = Config.global(); // Client services are added through a service loader - see mongoDB example for explicit services @@ -94,9 +96,7 @@ static WebServer setupServer(WebServerConfig.Builder builder) { .build(); return builder.config(config.get("server")) .addFeature(observe) - .routing(Main::routing) - .build() - .start(); + .routing(Main::routing); } /** diff --git a/examples/dbclient/pokemons/src/test/java/io/helidon/examples/dbclient/pokemons/AbstractPokemonServiceTest.java b/examples/dbclient/pokemons/src/test/java/io/helidon/examples/dbclient/pokemons/AbstractPokemonServiceTest.java index 6f51f44de..80fbba538 100644 --- a/examples/dbclient/pokemons/src/test/java/io/helidon/examples/dbclient/pokemons/AbstractPokemonServiceTest.java +++ b/examples/dbclient/pokemons/src/test/java/io/helidon/examples/dbclient/pokemons/AbstractPokemonServiceTest.java @@ -42,7 +42,11 @@ abstract class AbstractPokemonServiceTest { private static WebClient client; static void beforeAll() { - server = Main.setupServer(WebServer.builder()); + server = Main.setupServer(WebServer.builder()) + .port(-1) + .build() + .start(); + client = WebClient.create(config -> config.baseUri("http://localhost:" + server.port()) .addMediaSupport(JsonpSupport.create())); } diff --git a/examples/metrics/filtering/se/src/test/java/io/helidon/examples/metrics/filtering/se/MainTest.java b/examples/metrics/filtering/se/src/test/java/io/helidon/examples/metrics/filtering/se/MainTest.java index 4a55a46de..baecb5e7a 100644 --- a/examples/metrics/filtering/se/src/test/java/io/helidon/examples/metrics/filtering/se/MainTest.java +++ b/examples/metrics/filtering/se/src/test/java/io/helidon/examples/metrics/filtering/se/MainTest.java @@ -51,6 +51,8 @@ public MainTest(Http1Client client) { @SetUpServer public static void setup(WebServerConfig.Builder server) { Main.setup(server); + // random port + server.port(-1); } @Test diff --git a/examples/metrics/http-status-count-se/src/test/java/io/helidon/examples/se/httpstatuscount/MainTest.java b/examples/metrics/http-status-count-se/src/test/java/io/helidon/examples/se/httpstatuscount/MainTest.java index 4c1a87659..657de6e2c 100644 --- a/examples/metrics/http-status-count-se/src/test/java/io/helidon/examples/se/httpstatuscount/MainTest.java +++ b/examples/metrics/http-status-count-se/src/test/java/io/helidon/examples/se/httpstatuscount/MainTest.java @@ -52,6 +52,8 @@ public MainTest(Http1Client client) { @SetUpServer public static void setup(WebServerConfig.Builder server) { Main.setup(server); + // random port + server.port(-1); } @Test diff --git a/examples/metrics/kpi/src/test/java/io/helidon/examples/metrics/kpi/MainTest.java b/examples/metrics/kpi/src/test/java/io/helidon/examples/metrics/kpi/MainTest.java index e40b82f88..caf5a5707 100644 --- a/examples/metrics/kpi/src/test/java/io/helidon/examples/metrics/kpi/MainTest.java +++ b/examples/metrics/kpi/src/test/java/io/helidon/examples/metrics/kpi/MainTest.java @@ -52,6 +52,8 @@ public MainTest(Http1Client client) { @SetUpServer public static void setup(WebServerConfig.Builder server) { Main.setup(server); + // random port + server.port(-1); } @Test diff --git a/examples/webserver/opentracing/src/main/java/io/helidon/examples/webserver/opentracing/Main.java b/examples/webserver/opentracing/src/main/java/io/helidon/examples/webserver/opentracing/Main.java index 3282b3b76..e59b5dd39 100644 --- a/examples/webserver/opentracing/src/main/java/io/helidon/examples/webserver/opentracing/Main.java +++ b/examples/webserver/opentracing/src/main/java/io/helidon/examples/webserver/opentracing/Main.java @@ -49,12 +49,14 @@ public static void main(String[] args) { // configure logging in order to not have the standard JVM defaults LogConfig.configureRuntime(); - WebServer server = setupServer(WebServerConfig.builder(), 9411); + WebServer server = setupServer(WebServerConfig.builder(), 9411) + .build() + .start(); System.out.println("Started at http://localhost:" + server.port()); } - static WebServer setupServer(WebServerConfig.Builder builder, int port) { + static WebServerConfig.Builder setupServer(WebServerConfig.Builder builder, int port) { Config config = Config.builder() .sources(ConfigSources.create(Map.of("host", "localhost", "port", "8080"))) @@ -72,8 +74,6 @@ static WebServer setupServer(WebServerConfig.Builder builder, int port) { .build()) .routing(routing -> routing .get("/test", (req, res) -> res.send("Hello World!")) - .post("/hello", (req, res) -> res.send("Hello: " + req.content().as(String.class)))) - .build() - .start(); + .post("/hello", (req, res) -> res.send("Hello: " + req.content().as(String.class)))); } } diff --git a/examples/webserver/opentracing/src/test/java/io/helidon/examples/webserver/opentracing/MainTest.java b/examples/webserver/opentracing/src/test/java/io/helidon/examples/webserver/opentracing/MainTest.java index 9079fd48c..424655fdf 100644 --- a/examples/webserver/opentracing/src/test/java/io/helidon/examples/webserver/opentracing/MainTest.java +++ b/examples/webserver/opentracing/src/test/java/io/helidon/examples/webserver/opentracing/MainTest.java @@ -51,7 +51,11 @@ public class MainTest { @BeforeAll static void checkContainer() { - server = Main.setupServer(WebServer.builder(), container.getMappedPort(9411)); + server = Main.setupServer(WebServer.builder(), container.getMappedPort(9411)) + .port(-1) + .build() + .start(); + client = WebClient.create(config -> config.baseUri("http://localhost:" + server.port()) .addMediaSupport(JsonpSupport.create())); zipkinClient = Http1Client.create(config -> config