From 5acdbf39cda0275eea2a66361493f7ebb1be1d95 Mon Sep 17 00:00:00 2001 From: Cedric Champeau Date: Fri, 10 Jan 2025 10:53:46 +0100 Subject: [PATCH] Fix builds hanging if server cannot start If the server factory fails to start the test resources service, then we'd wait indefinitely for the server port file to appear, which causes builds to hang. Closes #785 --- .../testresources/buildtools/ServerUtils.java | 22 ++++++++++++++----- .../server/TestResourcesService.java | 4 ++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/test-resources-build-tools/src/main/java/io/micronaut/testresources/buildtools/ServerUtils.java b/test-resources-build-tools/src/main/java/io/micronaut/testresources/buildtools/ServerUtils.java index 1f7827f6c..3b166d44b 100644 --- a/test-resources-build-tools/src/main/java/io/micronaut/testresources/buildtools/ServerUtils.java +++ b/test-resources-build-tools/src/main/java/io/micronaut/testresources/buildtools/ServerUtils.java @@ -81,8 +81,10 @@ public class ServerUtils { private static final String FLAT_JAR = "flat.jar"; // See io.micronaut.testresources.testcontainers.DockerSupport.TIMEOUT - private static final String DOCKER_CHECK_TIMEOUT_SECONDS_ENV = "TEST_RESOURCES_DOCKER_CHECK_TIMEOUT_SECONDS"; - private static final String DOCKER_CHECK_TIMEOUT_SECONDS_PROPERTY = "docker.check.timeout.seconds"; + private static final String DOCKER_CHECK_TIMEOUT_SECONDS_ENV = + "TEST_RESOURCES_DOCKER_CHECK_TIMEOUT_SECONDS"; + private static final String DOCKER_CHECK_TIMEOUT_SECONDS_PROPERTY = + "docker.check.timeout.seconds"; /** * Writes the server settings in an output directory. @@ -335,16 +337,24 @@ private static void startAndWait(ServerFactory serverFactory, waitForServerToBeAvailable(serverFactory, explicitPort, portFilePath); } - private static void waitForServerToBeAvailable(ServerFactory serverFactory, Integer explicitPort, - Path portFilePath) { + private static void waitForServerToBeAvailable(ServerFactory serverFactory, + Integer explicitPort, + Path portFilePath) { Integer port = explicitPort; if (explicitPort == null) { - while (!Files.exists(portFilePath)) { + int retries = 4; + long dur = STARTUP_TIME_WAIT_MS; + while (--retries > 0 && !Files.exists(portFilePath)) { try { - serverFactory.waitFor(Duration.of(STARTUP_TIME_WAIT_MS, ChronoUnit.MILLIS)); + serverFactory.waitFor(Duration.of(dur, ChronoUnit.MILLIS)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); + break; } + dur *= 2; + } + if (!Files.exists(portFilePath)) { + throw new IllegalStateException("Port file not created. Server probably failed to start."); } try { port = Integer.parseInt(Files.readString(portFilePath)); diff --git a/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesService.java b/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesService.java index a15230256..8912157ac 100644 --- a/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesService.java +++ b/test-resources-server/src/main/java/io/micronaut/testresources/server/TestResourcesService.java @@ -39,6 +39,10 @@ public class TestResourcesService { private static final Logger LOGGER = LoggerFactory.getLogger(TestResourcesService.class); public static void main(String[] args) { + if (true) { + System.out.println("This is a test"); + System.exit(-1); + } long sd = System.nanoTime(); ApplicationContext context = Micronaut.run(TestResourcesService.class, args); Arrays.stream(args)