Skip to content

Commit

Permalink
Run tests on random ports
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkec committed Dec 4, 2024
1 parent 04e44a2 commit b293661
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand All @@ -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))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b293661

Please sign in to comment.