Skip to content

Commit

Permalink
Fix builds hanging if server cannot start
Browse files Browse the repository at this point in the history
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
  • Loading branch information
melix committed Jan 10, 2025
1 parent d4313fc commit 1c15623
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 1c15623

Please sign in to comment.