From 90da9ab146b84f284097ad08a5c3773a02786bca Mon Sep 17 00:00:00 2001 From: Aymen Jamaaoui <5993228+aymenjamaaoui@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:28:25 +0100 Subject: [PATCH] fix(core): keep withStartupTimeout from changing the shared default wait strategy --- .../containers/GenericContainer.java | 4 ++++ .../containers/GenericContainerTest.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 4d3778c63d1..71641132ec6 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -1219,6 +1219,10 @@ public SELF withClasspathResourceMapping( */ @Override public SELF withStartupTimeout(Duration startupTimeout) { + if (getWaitStrategy() == DEFAULT_WAIT_STRATEGY) { + // DEFAULT_WAIT_STRATEGY is shared by all containers, so give this container its own instance + waitingFor(Wait.defaultWaitStrategy()); + } getWaitStrategy().withStartupTimeout(startupTimeout); return self(); } diff --git a/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java b/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java index 37aac1b0af9..04284033a81 100644 --- a/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java +++ b/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java @@ -279,6 +279,21 @@ void shouldRespectWaitStrategy() { } } + @Test + void shouldNotChangeStartupTimeoutOfOtherContainers() { + // Never started. Only sets a short timeout for itself. + new GenericContainer<>(TestImages.ALPINE_IMAGE).withStartupTimeout(Duration.ofSeconds(2)); + + // Default wait strategy. The port opens after 5 seconds. + try ( + GenericContainer container = new GenericContainer<>(TestImages.ALPINE_IMAGE) + .withExposedPorts(8080) + .withCommand("sh", "-c", "sleep 5; while true; do nc -l -p 8080; done") + ) { + container.start(); + } + } + @Test void testStartupAttemptsDoesNotLeaveContainersRunningWhenWrongWaitStrategyIsUsed() { try (