From 1c15623b117efa8b4f2c5e0512016201d7b91da2 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 ++++++++++++++----- 1 file changed, 16 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));