Repository navigation
Conversation
cristianrgreco
left a comment
There was a problem hiding this comment.
Thanks for picking this up! A few things before this can go in:
CouchDBContainersetswithStartupTimeout(120_000)on the container andwithStartupTimeout(60_000)on its HTTP wait strategy. Today the wait gets 120s. With this change it gets 60s, andnew CouchDBContainer(image).withStartupTimeout(...)no longer extends it. Drop thewithStartupTimeout(60_000)from the module's wait strategy so it keeps the container's 120s.KafkaContainerandRedpandaContainerrun the user's wait strategy incontainerStarted, using the same lines this PR replaces (kafka-container.ts:197-200,redpanda-container.ts:83-86). Until they usewithDefaultStartupTimeouttoo, a timeout set on a Kafka or Redpanda user's wait strategy is still overridden, so please include them here rather than in a follow-up.
|
|
||
| getStartupTimeout(): number; | ||
|
|
||
| withDefaultStartupTimeout(startupTimeoutMs: number): WaitStrategy; |
There was a problem hiding this comment.
WaitStrategy is exported and AbstractWaitStrategy isn't, so a custom strategy that isn't a StartupCheckStrategy implements this interface directly. Adding a required method breaks those: they no longer compile, and from JS start() throws withDefaultStartupTimeout is not a function whenever the container has a startup timeout, which most modules set. Keep the interface unchanged, handling strategies that don't extend AbstractWaitStrategy with the existing isStartupTimeoutSet()/withStartupTimeout(), or this needs major.
There was a problem hiding this comment.
Fixed - reverted interface to its original shape and moved this to a standalone applyDefaultStartupTimeout() function instead. It checks instance of AbstractWaitStrategy first and falls back to the existing isStartupTimeoutSet()/withStartupTimeout() for anything else, so there's no interface break. See wait-strategy.ts in lestest pr commit
| if (this.startupTimeoutSet) { | ||
| return this; | ||
| } | ||
| const clone: this = Object.assign(Object.create(Object.getPrototypeOf(this)), this); |
There was a problem hiding this comment.
This is a shallow copy, so for Wait.forAll([...]) the clone shares the children, and CompositeWaitStrategy.withStartupTimeout sets the timeout on the caller's children. A composite reused across containers keeps the first container's timeout: start one with withStartupTimeout(1_000), then another with 2_000, and the second fails after 1000ms. Override this in CompositeWaitStrategy to build a new composite from each child's withDefaultStartupTimeout, keeping the deadline, and cover Wait.forAll in the reuse test.
There was a problem hiding this comment.
Fixed- CompositeWaitStrategy now overrides withDefaultStartupTimeout to build new composite from each childs own applyDefaultStartupTimeout(...) result instead of cloning this so the original children are never mutated. Added a test covering the exact repro (reuse with withStartupTimeout(1_000) then 2_000) in composite-wait-strategy.test.ts
| .start(); | ||
| ``` | ||
|
|
||
| `GenericContainer.withStartupTimeout` only applies to a wait strategy that has not set its own startup timeout. A timeout set explicitly on the wait strategy itself always takes precedence: |
There was a problem hiding this comment.
Nit: this applies to DockerComposeEnvironment.withStartupTimeout too. Something like: "A startup timeout set on the wait strategy itself takes precedence over withStartupTimeout on the container or Compose environment:"
There was a problem hiding this comment.
Updated- now reads 'A startup timeout set on the wait strategy itself takes precedence over withStartupTimeout on the container or Compose environment'
|
Thanks for the detailed review, @cristianrgreco
Could you also approve the latest workflow run when you get a chance? |
Summary
GenericContainer.start()andDockerComposeEnvironment.up()unconditionallycalled
waitStrategy.withStartupTimeout(this.startupTimeoutMs)just beforewaiting for the container, overwriting any timeout the caller had already set
explicitly on the wait strategy - and mutating the caller's wait strategy
instance in the process.
CompositeWaitStrategyalready gets this right forits own children via
isStartupTimeoutSet(); this brings the same precedenceto the container/environment level.
AbstractWaitStrategygainswithDefaultStartupTimeout(startupTimeoutMs):a no-op if the strategy already has its own timeout set, otherwise it
applies the default to a clone and returns that, leaving the caller's
original instance untouched.
GenericContainer.startContainer/reuseContainerandDockerComposeEnvironment'stoStartedGenericContainernow call thisinstead of mutating the wait strategy directly.
docs/features/wait-strategies.mddocuments the precedence.Not in scope: Kafka and Redpanda modules duplicate the old
unconditional-mutation pattern internally and still have this bug - a
candidate for a follow-up rather than expanding this PR's blast radius.
Not implemented: what happens when a wait strategy's own timeout is
longer than the container's timeout (ceiling vs. override) - per the
issue's own note, that's a separate design decision to coordinate with #1582.
Verification
npx tsc -b packages/testcontainers packages/modules/*,npm run knip,biome lint/biome formaton changed files - all clean.Red, on the unfixed code (temporarily reverted
generic-container.ts,docker-compose-environment.ts,wait-strategy.tsto their pre-fix state,keeping the new tests):
FAIL GenericContainer > should respect the wait strategy's own startup
timeout over the container's startup timeout
Expected: "Log message "this never appears" not received after 1000ms"
Received: "Log message "this never appears" not received after 6000ms"
FAIL GenericContainer > should not mutate a reused wait strategy's own
startup timeout when applying the container's default
Expected: false
Received: true
FAIL DockerComposeEnvironment > should respect the wait strategy's own
startup timeout over the environment's startup timeout
Expected: "Log message "unexpected" not received after 1000ms"
Received: "Log message "unexpected" not received after 60000ms"
Test Files 2 failed | Tests 3 failed | 81 skipped (84)
Green, with the fix restored:
Test Files 2 passed (2)
Tests 3 passed | 81 skipped (84)
Duration 6.90s
Full regression run on both suites:
Test Files 2 passed | Tests 83 passed | 1 skipped (84).Not breaking
Existing callers who only set a timeout on one side (container or
strategy, not both) see no change. Callers who previously set both and were
relying on the container's timeout silently winning will now see the
strategy's explicit timeout take effect instead - this is the bug being
fixed.
Closes #1523