[Bug]: withStartupTimeout() changes the startup timeout of all containers that use the default wait strategy
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 64/100
Hướng nghiên cứu
Trạng thái dùng chung là DEFAULT_WAIT_STRATEGY trong core/src/main/java/org/testcontainers/containers/GenericContainer.java (các trường quanh dòng 177-183, cũng được tham chiếu từ ContainerDef.java dòng 61); getWaitStrategy() (~dòng 885) trả về nó và withStartupTimeout(Duration) (~dòng 1221) thay đổi nó tại chỗ. Bắt đầu bằng việc thêm bài test tái hiện từ báo cáo dưới core/src/test/java/org/testcontainers/containers/ và chạy ./gradlew :testcontainers:test --tests 'org.testcontainers.containers.DefaultWaitStrategyTimeoutTest' để xem nó thất bại, sau đó xác nhận rằng chiến lược mặc định được sao chép cho từng container trước khi timeout được áp dụng. Hoàn thành nghĩa là bài test mới vượt qua và không có instance chiến lược của container nào khác bị thay đổi. Lưu ý rằng người báo cáo đã đề xuất một patch ở đây, nên hãy phối hợp trước khi bắt đầu.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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 withContainerLaunchException(log below). - Without the first line (
new GenericContainer<>(...).withStartupTimeout(...)): the test passes after about 6 seconds.
Cause (links to main at e16dca9ea):
DEFAULT_WAIT_STRATEGYis onestaticinstance. Every container withoutwaitingFor(...)points to it: thewaitStrategyfield andContainerDef.getWaitStrategy()returns this shared instance.withStartupTimeout(Duration)callsgetWaitStrategy().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.
- Ngôn ngữ chính
- Java
- Star
- 8.8k
- Fork
- 2k
- Merge trung bình
- 5 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 17
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Có Dockerfile hoặc tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của testcontainers/testcontainers-java
-
[Enhancement]: Document why singleton containers are required under Spring's test context cachingCó thể đã có người làm @zakaullah075 đã nhận 93 ngày trước. Đang mởtype/enhancement
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
testcontainers/testcontainers-java#11967 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: Cannot reuse Selenium BrowserWebDriverContainers under LinuxCó thể đã có người làm @kdelay đã nhận 115 ngày trước. Đang mởtype/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
testcontainers/testcontainers-java#11941 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: DockerDesktopClientProviderStrategy is always applicableCó thể đã có người làm @seonwooj0810 đã nhận 164 ngày trước. Đang mởtype/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
testcontainers/testcontainers-java#11829 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Enhancement]: cleanup document after removing junit4 supportCó thể đã có người làm @SJvaca30 đã nhận 171 ngày trước. Đang mởtype/enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
testcontainers/testcontainers-java#11578 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Feature]: Presto Module uses the outdated Docker Image of `ghcr.io/trinodb/presto` and needs to be updated to `prestodb/presto`Có thể đã có người làm @linghengqian đã nhận 854 ngày trước. Đang mởhelp wanted type/feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
testcontainers/testcontainers-java#8657 · 4 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của testcontainers/testcontainers-java
Issue tương tự
-
triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
xerial/sqlite-jdbc#1515 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Area/Configuration Type/Improvement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ballerina-platform/static-code-analysis-tool#217 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
eclipse-dirigible/dirigible#8636 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug severity-medium XS
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 80/100
nikolas-charalambidis/facepalm-maven-plugin#291 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
OakLoaf/PluginUpdater#264 ·
Maintainer thường phản hồi trong vòng 1 ngày