[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategy

未關閉 適合新手
#12,171 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@aymenjamaaoui 已經在處理了。

開始於 2026年11月15日。

  • #12172 來自 @aymenjamaaoui —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
64/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
docker, java
領域
testing

研究方向

共享狀態是 DEFAULT_WAIT_STRATEGY,位於 core/src/main/java/org/testcontainers/containers/GenericContainer.java(大約第 177-183 行的欄位,也被 ContainerDef.java 第 61 行引用);getWaitStrategy()(約第 885 行)回傳它,withStartupTimeout(Duration)(約第 1221 行)就地修改它。首先在 core/src/test/java/org/testcontainers/containers/ 下加入報告中的重現測試,並執行 ./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest' 觀察它失敗,然後確認在套用逾時之前預設策略會按容器複製。完成意味著新測試通過,且沒有修改其他任何容器的策略實例。注意回報者已經在這裡提供了一個補丁,所以開始前先協調。

由索引模型根據 Issue 內容生成。

描述

type/bug
Module

Core

Testcontainers version

2.0.5

Using the latest Testcontainers version?

Yes

Host OS

macOS 27.2.1

Host Arch

ARM

Docker version
Client:
 Version:           29.8.2
 API version:       1.56
 Go version:        go1.26.8
 Git commit:        7fc2dff
 Built:             Wed Sep 30 19:32:03 2026
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.94.0 (241994)
 Engine:
  Version:          29.8.2
  API version:      1.56 (minimum version 1.40)
  Go version:       go1.26.8
  Git commit:       8af9fe3
  Built:            Wed Sep 30 19:31:50 2026
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          v2.3.6
  GitCommit:        ee2735368117d2eb259779949d5e75cdafec9761
 runc:
  Version:          1.5.1
  GitCommit:        v1.5.1-0-g8f2685a4
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
What happened?

withStartupTimeout(Duration) on a container without its own wait strategy changes the startup timeout of all containers in the JVM that use the default wait strategy.

Expected: the timeout applies only to the container where it is set.
Actual: other containers get this timeout too. A short timeout on one container makes another container fail. The result depends on the order of tests in the JVM, so it looks like a flaky test in another class.

Reproduction. Put this test in core/src/test/java/org/testcontainers/containers/ and run
./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest':

package org.testcontainers.containers;

import org.junit.jupiter.api.Test;

import java.time.Duration;

class DefaultWaitStrategyTimeoutTest {

    @Test
    void startupTimeoutOfOneContainerDoesNotChangeOtherContainers() {
        // Never started. Only sets a short timeout for itself.
        new GenericContainer<>("alpine:3.17").withStartupTimeout(Duration.ofSeconds(2));

        // Default wait strategy (60 seconds). The port opens after 5 seconds.
        try (
            GenericContainer<?> slow = new GenericContainer<>("alpine:3.17")
                .withExposedPorts(8080)
                .withCommand("sh", "-c", "sleep 5; while true; do nc -l -p 8080; done")
        ) {
            slow.start();
        }
    }
}
  • On main (e16dca9ea): the test fails after about 3 seconds with ContainerLaunchException (log below).
  • Without the first line (new GenericContainer<>(...).withStartupTimeout(...)): the test passes after about 6 seconds.

Cause (links to main at e16dca9ea):

  1. DEFAULT_WAIT_STRATEGY is one static instance. Every container without waitingFor(...) points to it: the waitStrategy field and ContainerDef.
  2. getWaitStrategy() returns this shared instance.
  3. withStartupTimeout(Duration) calls getWaitStrategy().withStartupTimeout(...), so it changes the shared instance itself.
Relevant log output
org.testcontainers.containers.ContainerLaunchException: Container startup failed for image alpine:3.17
Caused by: org.testcontainers.containers.ContainerLaunchException: Could not create/start container
Caused by: org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (localhost ports: [55831] should be listening)
Additional Information

A possible fix: give the container its own default strategy before changing the timeout. With this change, the test above passes:

public SELF withStartupTimeout(Duration startupTimeout) {
    if (getWaitStrategy() == DEFAULT_WAIT_STRATEGY) {
        waitingFor(Wait.defaultWaitStrategy());
    }
    getWaitStrategy().withStartupTimeout(startupTimeout);
    return self();
}

I can open a PR with this fix and the test if this direction is OK for you.

I found this while working on #12103: JdbcDatabaseContainer.withStartupTimeoutSeconds() must not change the shared instance either. #12103 is in draft until I fix it.

主要語言
Java
星號
8.8k
分支
2k
平均合併
5 天 4 小時
30 天內合併 PR
17

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

testcontainers/testcontainers-java 的其他 Issue

查看 testcontainers/testcontainers-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。