Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache Ryuk's failure to prevent the double start #2935

Merged
merged 2 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class DockerClientFactory {
DockerClient dockerClient;

@VisibleForTesting
RuntimeException cachedChecksFailure;
RuntimeException cachedClientFailure;

private String activeApiVersion;
private String activeExecutionDriver;
Expand Down Expand Up @@ -139,9 +139,9 @@ public DockerClient client() {
}

// fail-fast if checks have failed previously
if (cachedChecksFailure != null) {
log.debug("There is a cached checks failure - throwing", cachedChecksFailure);
throw cachedChecksFailure;
if (cachedClientFailure != null) {
log.debug("There is a cached checks failure - throwing", cachedClientFailure);
throw cachedClientFailure;
}

final DockerClientProviderStrategy strategy = getOrInitializeStrategy();
Expand Down Expand Up @@ -170,7 +170,12 @@ public void close() {
boolean useRyuk = !Boolean.parseBoolean(System.getenv("TESTCONTAINERS_RYUK_DISABLED"));
if (useRyuk) {
log.debug("Ryuk is enabled");
ryukContainerId = ResourceReaper.start(hostIpAddress, client);
try {
ryukContainerId = ResourceReaper.start(hostIpAddress, client);
} catch (RuntimeException e) {
cachedClientFailure = e;
throw e;
}
log.info("Ryuk started - will monitor and terminate Testcontainers containers on JVM exit");
} else {
log.debug("Ryuk is disabled");
Expand Down Expand Up @@ -201,7 +206,7 @@ public void close() {
);
}
} catch (RuntimeException e) {
cachedChecksFailure = e;
cachedClientFailure = e;
throw e;
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void failedChecksFailFast() {

DockerClientFactory instance = new DockerClientFactory();
DockerClient dockerClient = instance.dockerClient;
assertThat(instance.cachedChecksFailure).isNull();
assertThat(instance.cachedClientFailure).isNull();
try {
// Remove cached client to force the initialization logic
instance.dockerClient = null;
Expand All @@ -80,12 +80,12 @@ public void failedChecksFailFast() {
assertThatThrownBy(instance::client).isInstanceOf(DockerException.class);

RuntimeException failure = new IllegalStateException("Boom!");
instance.cachedChecksFailure = failure;
instance.cachedClientFailure = failure;
// Fail fast
assertThatThrownBy(instance::client).isEqualTo(failure);
} finally {
instance.dockerClient = dockerClient;
instance.cachedChecksFailure = null;
instance.cachedClientFailure = null;
}
}
}