Skip to content

Commit 22c6403

Browse files
authored
Cache Ryuk's failure to prevent the double start (#2935)
see #2395
1 parent a4d67a6 commit 22c6403

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

core/src/main/java/org/testcontainers/DockerClientFactory.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class DockerClientFactory {
6666
DockerClient dockerClient;
6767

6868
@VisibleForTesting
69-
RuntimeException cachedChecksFailure;
69+
RuntimeException cachedClientFailure;
7070

7171
private String activeApiVersion;
7272
private String activeExecutionDriver;
@@ -139,9 +139,9 @@ public DockerClient client() {
139139
}
140140

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

147147
final DockerClientProviderStrategy strategy = getOrInitializeStrategy();
@@ -170,7 +170,12 @@ public void close() {
170170
boolean useRyuk = !Boolean.parseBoolean(System.getenv("TESTCONTAINERS_RYUK_DISABLED"));
171171
if (useRyuk) {
172172
log.debug("Ryuk is enabled");
173-
ryukContainerId = ResourceReaper.start(hostIpAddress, client);
173+
try {
174+
ryukContainerId = ResourceReaper.start(hostIpAddress, client);
175+
} catch (RuntimeException e) {
176+
cachedClientFailure = e;
177+
throw e;
178+
}
174179
log.info("Ryuk started - will monitor and terminate Testcontainers containers on JVM exit");
175180
} else {
176181
log.debug("Ryuk is disabled");
@@ -201,7 +206,7 @@ public void close() {
201206
);
202207
}
203208
} catch (RuntimeException e) {
204-
cachedChecksFailure = e;
209+
cachedClientFailure = e;
205210
throw e;
206211
}
207212
} else {

core/src/test/java/org/testcontainers/DockerClientFactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void failedChecksFailFast() {
7171

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

8282
RuntimeException failure = new IllegalStateException("Boom!");
83-
instance.cachedChecksFailure = failure;
83+
instance.cachedClientFailure = failure;
8484
// Fail fast
8585
assertThatThrownBy(instance::client).isEqualTo(failure);
8686
} finally {
8787
instance.dockerClient = dockerClient;
88-
instance.cachedChecksFailure = null;
88+
instance.cachedClientFailure = null;
8989
}
9090
}
9191
}

0 commit comments

Comments
 (0)