Skip to content

Keep liveness check ports in the order of the exposed ports - #12197

Open
suvvvv wants to merge 1 commit into
testcontainers:mainfrom
suvvvv:fix-liveness-check-port-order
Open

suvvvv wants to merge 1 commit into
testcontainers:mainfrom
suvvvv:fix-liveness-check-port-order

Conversation

@suvvvv

@suvvvv suvvvv commented Dec 1, 2026 •

Copy link
Copy Markdown

Fixes #12196

WaitStrategyTarget.getLivenessCheckPortNumbers() collected the mapped ports into a HashSet, so their order depended on the random host ports Docker picked, not on the order passed to withExposedPorts(...). HttpWaitStrategy (without forPort) uses the first element, so with two exposed ports it sometimes probed the second one, while the docs say it waits for the first mapped port. That's the intermittent startup failure from the issue (8090 for HTTP, 8091 for SMTP).

The ports are now collected into a LinkedHashSet, so they keep the exposed ports' order, followed by the bound ports. The return type is still Set<Integer>, so there's no API change, and GenericContainer.getLivenessCheckPorts() gets the same order since it builds on this method.

Tests

New WaitStrategyTargetTest, which doesn't need Docker. It uses mapped ports that a HashSet iterates in the wrong order (8090 -> 50001, 8091 -> 50000), and checks that bound ports come after the exposed ones. Both tests fail on main and pass with this change.

Ran locally:

  • ./gradlew :testcontainers:test --tests 'org.testcontainers.containers.wait.strategy.WaitStrategyTargetTest'
  • ./gradlew :testcontainers:checkstyleMain :testcontainers:checkstyleTest :testcontainers:spotlessCheck :testcontainers:japicmp

I couldn't run the container-based wait strategy tests (HttpWaitStrategyTest, HostPortWaitStrategyTest, ...) locally: my Docker's image store is read-only right now, so pulling testcontainers/ryuk fails for every container test, independent of this change. CI should cover those.

Summary by CodeRabbit

  • Bug Fixes
    • Liveness-check ports are now reported in a consistent order: mapped exposed ports first, followed by bound ports. Duplicate mapped ports remain deduplicated, helping make port selection predictable across checks.

getLivenessCheckPortNumbers() collected the mapped ports into a HashSet, so their order depended on the random host ports. HttpWaitStrategy uses the first one, so with several exposed ports it could probe the wrong port.

Fixes testcontainers#12196
@suvvvv
suvvvv requested a review from a team as a code owner December 1, 2026 13:35
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

getLivenessCheckPortNumbers now preserves the order of mapped exposed ports, removes duplicate mapped ports, and adds bound ports afterward. Tests verify the exposed-port order and bound-port placement.

Changes: Liveness check port ordering

Layer / File(s) Summary
Preserve liveness-check port order
core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java, core/src/test/java/org/testcontainers/containers/wait/strategy/WaitStrategyTargetTest.java
The method collects mapped exposed ports in insertion order and adds bound ports afterward. The tests verify both ordering rules.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: eddumelendez, kiview

Merge Risk: Minimal · up to 617c3

This change makes the wait strategy check the first port passed to withExposedPorts, as documented, instead of sometimes picking another mapped port. The API is unchanged and the new tests cover the ordering behavior. I found no merge-blocking risk.

Pre-merge checks: Passed 7

Check name Status Explanation
Title check Passed The title "Keep liveness check ports in the order of the exposed ports" matches the main change: getLivenessCheckPortNumbers now uses a LinkedHashSet so mapped ports keep the exposed-port order. It is…
Description check Passed The description follows the template's intent. It explains the problem (HashSet ordering, HttpWaitStrategy using the first port), the fix (LinkedHashSet, same Set return type), links issue #1…
Linked Issues check Passed [#12196] The issue asks that HttpWaitStrategy without forPort check the first port passed to withExposedPorts(...). HttpWaitStrategy.waitUntilReady() takes getLivenessCheckPorts().iterator().next() wh…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes two areas: WaitStrategyTarget.java (import of LinkedHashSet, collection change, and Javadoc update describing order) and a new WaitStrategyTargetTest.j…
New Module Registration Passed The "New module registration" check applies only when the PR adds a new directory under modules/. The PR (testcontainers/testcontainers-java #12197, author suvvvv, head fix-liveness-check-port-order →…
Public Api Compatibility Passed The PR changes one file under src/main/java: core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java. In getLivenessCheckPortNumbers(), the collector changes from `.di…
Tests For Behaviour Changes Passed The PR changes runtime behavior in a src/main/java file. core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java changes getLivenessCheckPortNumbers() from a Set…
Full details: Title check

Explanation

The title "Keep liveness check ports in the order of the exposed ports" matches the main change: getLivenessCheckPortNumbers now uses a LinkedHashSet so mapped ports keep the exposed-port order. It is specific and describes the primary behavior change.

Full details: Description check

Explanation

The description follows the template's intent. It explains the problem (HashSet ordering, HttpWaitStrategy using the first port), the fix (LinkedHashSet, same Set<Integer> return type), links issue #12196, and lists the tests and commands run. It also states that container-based tests could not run locally because of a read-only Docker image store. The template's general guidance on notes such as new modules and dependency upgrades does not apply to this change, and the description does not need to address them.

Full details: Linked Issues check

Explanation

[#12196] The issue asks that HttpWaitStrategy without forPort check the first port passed to withExposedPorts(...). HttpWaitStrategy.waitUntilReady() takes getLivenessCheckPorts().iterator().next() when no liveness port is set. At the reviewed head, WaitStrategyTarget.getLivenessCheckPortNumbers() maps getExposedPorts() through getMappedPort() into a LinkedHashSet (Collectors.toCollection(LinkedHashSet::new)), then adds getBoundPortNumbers(). Insertion order therefore follows the exposed-port order, and the first element is the first exposed port's mapped port. Duplicate mapped ports are still removed by the set. The return type is unchanged. The PR description states that GenericContainer.getLivenessCheckPorts() builds on this method, but I did not read GenericContainer directly. The added WaitStrategyTargetTest was described in the PR but its contents were not read; the test result is therefore reported by the author, not verified here. The fix addresses the coding requirement.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes two areas: WaitStrategyTarget.java (import of LinkedHashSet, collection change, and Javadoc update describing order) and a new WaitStrategyTargetTest.java (ordering and bound-port tests, per the PR summary). Both directly support the ordering requirement of #12196. No unrelated changes were identified in the inspected diff. The test file contents were not inspected directly.

Full details: New Module Registration

Explanation

The "New module registration" check applies only when the PR adds a new directory under modules/. The PR (testcontainers/testcontainers-java #12197, author suvvvv, head fix-liveness-check-port-order → main) changes only two files: core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java (modified) and core/src/test/java/org/testcontainers/containers/wait/strategy/WaitStrategyTargetTest.java (added). Neither path is under modules/, so no new module directory is added. The check is inapplicable and passes.

Full details: Public Api Compatibility

Explanation

The PR changes one file under src/main/java: core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java. In getLivenessCheckPortNumbers(), the collector changes from .distinct().collect(Collectors.toSet()) to .collect(Collectors.toCollection(LinkedHashSet::new)). The method name, the Set&lt;Integer&gt; return type, the default-method visibility, and the parameter list are all unchanged. The .distinct() removal does not change the public contract, because a Set already deduplicates. The only other change is a Javadoc update and the new test file WaitStrategyTargetTest.java, which is under src/test and out of scope for this check. No public or protected class, method, constructor, or field is removed, renamed, or has its signature, return type, or visibility changed. The check passes, and no deprecation is required because the change is compatible.

Full details: Tests For Behaviour Changes

Explanation

The PR changes runtime behavior in a src/main/java file. core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java changes getLivenessCheckPortNumbers() from a Set built by Collectors.toSet() to a LinkedHashSet. Port order now follows the exposed-port order, which changes which port HttpWaitStrategy probes first. This is a bug fix (issue #12196), so the check applies. The PR adds a test under src/test: core/src/test/java/org/testcontainers/containers/wait/strategy/WaitStrategyTargetTest.java (new file). It has two tests. livenessCheckPortsFollowTheExposedPortsOrder asserts containsExactly(50001, 50000) for exposed ports 8090 and 8091. boundPortsComeAfterTheExposedPorts asserts that bound ports follow exposed ports. Both tests exercise the changed ordering behavior. The check fails only when a behavior change lacks a test. This PR includes one, so the check passes. The PR also removed the redundant .distinct(). Deduplication still holds because LinkedHashSet removes duplicates.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between a736c02 and 617c351.

Files selected for processing: 2

  • core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java
  • core/src/test/java/org/testcontainers/containers/wait/strategy/WaitStrategyTargetTest.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: WaitStrategyTarget Handles Multiple Ports Differently to Documentation

1 participant