Skip to content

Commit bac4dfe

Browse files
Make outer maximum startup timeout in DockerComposeContainer configurable (#5588)
1 parent 1419cf2 commit bac4dfe

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public class DockerComposeContainer<SELF extends DockerComposeContainer<SELF>>
103103

104104
private final Map<String, WaitAllStrategy> waitStrategyMap = new ConcurrentHashMap<>();
105105

106+
private Duration startupTimeout = Duration.ofMinutes(30);
107+
106108
private final SocatContainer ambassadorContainer = new SocatContainer();
107109

108110
private final Map<String, List<Consumer<OutputFrame>>> logConsumers = new ConcurrentHashMap<>();
@@ -433,14 +435,14 @@ private String getServiceInstanceName(String serviceName) {
433435
/*
434436
* can have multiple wait strategies for a single container, e.g. if waiting on several ports
435437
* if no wait strategy is defined, the WaitAllStrategy will return immediately.
436-
* The WaitAllStrategy uses an long timeout, because timeouts should be handled by the inner strategies.
438+
* The WaitAllStrategy uses the startup timeout for everything as a global maximum, but we expect timeouts to be handled by the inner strategies.
437439
*/
438440
private void addWaitStrategy(String serviceInstanceName, @NonNull WaitStrategy waitStrategy) {
439441
final WaitAllStrategy waitAllStrategy = waitStrategyMap.computeIfAbsent(
440442
serviceInstanceName,
441443
__ -> {
442444
return new WaitAllStrategy(WaitAllStrategy.Mode.WITH_MAXIMUM_OUTER_TIMEOUT)
443-
.withStartupTimeout(Duration.ofMinutes(30));
445+
.withStartupTimeout(startupTimeout);
444446
}
445447
);
446448
waitAllStrategy.withStrategy(waitStrategy);
@@ -598,6 +600,16 @@ public SELF withRemoveImages(RemoveImages removeImages) {
598600
return self();
599601
}
600602

603+
/**
604+
* Set the maximum startup timeout all the waits set are bounded to.
605+
*
606+
* @return this instance. for chaining
607+
*/
608+
public SELF withStartupTimeout(Duration startupTimeout) {
609+
this.startupTimeout = startupTimeout;
610+
return self();
611+
}
612+
601613
public Optional<ContainerState> getContainerByServiceName(String serviceName) {
602614
return Optional.ofNullable(serviceInstanceMap.get(serviceName));
603615
}

core/src/test/java/org/testcontainers/containers/DockerComposeContainerWithServicesTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.testcontainers.containers;
22

33
import org.junit.Test;
4+
import org.testcontainers.containers.wait.strategy.Wait;
45

56
import java.io.File;
7+
import java.time.Duration;
68
import java.util.List;
79
import java.util.stream.Collectors;
810
import java.util.stream.Stream;
911

12+
import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows;
1013
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
1114

1215
public class DockerComposeContainerWithServicesTest {
@@ -87,6 +90,21 @@ public void testScaleInComposeFileIsRespected() {
8790
}
8891
}
8992

93+
@Test
94+
public void testStartupTimeoutSetsTheHighestTimeout() {
95+
assertThrows("We expect a timeout from the startup timeout", org.rnorth.ducttape.TimeoutException.class,
96+
() -> {
97+
try (
98+
DockerComposeContainer<?> compose = new DockerComposeContainer<>(SIMPLE_COMPOSE_FILE)
99+
.withServices("redis")
100+
.withStartupTimeout(Duration.ofMillis(1))
101+
.withExposedService("redis", 80, Wait.forListeningPort().withStartupTimeout(Duration.ofMinutes(1)));
102+
) {
103+
compose.start();
104+
}
105+
});
106+
}
107+
90108
private void verifyStartedContainers(final DockerComposeContainer<?> compose, final String... names) {
91109
final List<String> containerNames = compose
92110
.listChildContainers()

0 commit comments

Comments
 (0)