Skip to content

fix(flaky): BaseComposeTest.verifyNoNetworks (ComposeContainerTest / ComposeContainerPortViaEnvTest) - #12181

Open
kiview wants to merge 1 commit into
mainfrom
claude/intelligent-fermat-obb3md
Open

kiview wants to merge 1 commit into
mainfrom
claude/intelligent-fermat-obb3md

Conversation

@kiview

@kiview kiview commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Broken behaviour

BaseComposeTest.verifyNoNetworks (@AfterEach) intermittently fails with:

org.opentest4j.AssertionFailedError: [The networks] expected: [...] but was: [...]
    at org.testcontainers.junit.BaseComposeTest.verifyNoNetworks(BaseComposeTest.java:71)

In the last 7 days on main this was the most common flake in core. Each failure was then hidden by the Develocity test-retry plugin:

Test Run SHA Diff in network list
ComposeContainerTest.simpleTest 44841193639 (ROOTFUL) 9f7ada8 extra so6sa26paxfd_default
ComposeContainerPortViaEnvTest.simpleTest 44425905204 (ROOTFUL) e16dca9 missing ey9gm7tkaays_default
ComposeContainerTest.shouldCreateContainerWhenFileNotPrefixedWithPath 44425905204 (STABLE) e16dca9 extra zzahb2sc1n3o_default

Each one passed when retried in the same job.

Root cause

The check lists every network on the Docker daemon before and after each test method and asserts that the two lists are equal. Core tests run with maxParallelForks = 4 against one daemon. Other forks create and remove networks the whole time, for example their own Compose projects' <project>_default networks and Network.newNetwork(). When one of those changes lands between the two snapshots, the assertion fails. The networks in the diffs above belong to other Compose projects, sometimes added and sometimes removed, which fits a cross-fork race rather than a leak.

Fix

Scope the check to the test's own Compose project:

  • Read com.docker.compose.project from the redis-1 service container. Every subclass exposes that service.
  • List networks with a label=com.docker.compose.project=<project> filter.
  • Assert the before-snapshot is non-empty, so the check can't pass vacuously if the filter ever stops matching.

It still catches a test that changes its own project's networks, but ignores activity from unrelated tests. Test-only change; no main code touched.

Verification (local, Docker 29.10.0)

To simulate the other forks, a background loop ran docker network create / docker network rm every ~0.3s. I then ran the four subclasses (ComposeContainerTest, ComposeWithIdentifierTest, ComposeWithNetworkTest, ComposeContainerPortViaEnvTest):

  • Before: 1 run with network churn, 2 of 12 tests failed (ComposeWithIdentifierTest.simpleTest, ComposeContainerTest.simpleTest) with the same [The networks] error as in CI.
  • After: 3 runs with network churn and 2 without, 0 of 12 failed in every run.
  • ./gradlew :testcontainers:checkstyleTest passes.
  • spotlessApply couldn't run here because Spotless couldn't find Node. The file was formatted with the pinned prettier 2.5.1 + prettier-plugin-java 1.6.1 (tab 4, width 120), so CI's Spotless check should agree.

This came out of an automated weekly flaky-test report covering 2026-11-16 to 2026-11-23.


Generated by Claude Code

Summary by CodeRabbit

  • Tests
    • Compose network checks and cleanup are now scoped to the project running the test service.
    • Test setup now reports an error if the required Redis container is not running.

verifyNoNetworks compared the daemon-wide Docker network list before and
after each test. Core tests run in 4 parallel forks against one daemon, so
networks created or removed by other tests made the assertion fail
intermittently (seen in ComposeContainerTest.simpleTest,
ComposeContainerPortViaEnvTest.simpleTest and
ComposeContainerTest.shouldCreateContainerWhenFileNotPrefixedWithPath).

Filter the listed networks by the com.docker.compose.project label of the
test's own compose project, and assert that the filter matches at least
one network so the check can't pass vacuously.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015e4ZbXupAdHuvgfiSLVmKn
@kiview
kiview requested a review from a team as a code owner November 23, 2026 16:19
@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 Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Compose test records the project label from the running redis-1 container. It limits network snapshots and cleanup checks to networks with that label.

Changes: Compose Network Scope

Layer / File(s) Summary
Project-scoped network checks
core/src/test/java/org/testcontainers/junit/BaseComposeTest.java
Setup reads the Compose project label from the running redis-1 container and snapshots that project's networks. If the service is absent, setup throws IllegalStateException. Cleanup checks only networks with the saved project label.

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

Change: Other

Suggested reviewers: eddumelendez

Merge Risk: Minimal · up to d8b54

This change only makes a flaky Compose test more reliable by checking networks for its own project, and it has no production impact. It looks safe to merge.

Pre-merge checks: Passed 7

Check name Status Explanation
Title check Passed The title names the fixed method and the affected tests, and it says the change is a flaky-test fix. The changeset scopes the BaseComposeTest.verifyNoNetworks check to the test's Compose project, whic…
Description check Passed The description covers the broken behaviour with the failure text, affected test runs, and a root-cause analysis. It explains the fix, gives local verification steps and results, and references the ch…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
New Module Registration Passed The "New module registration" check applies only when the PR adds a new directory under modules/. The PR diff range (refs/coderabbit/pre-merge-check/base to head) changes one file: `M core/src/t…
Public Api Compatibility Passed The check applies to changes under src/main/java. The authoritative changed-file inventory (refs/coderabbit/pre-merge-check/base..head) contains one modified file: core/src/test/java/org/testcontainer…
Tests For Behaviour Changes Passed The check applies only when the PR changes behaviour in src/main/java without a matching src/test change. The reviewed range (refs/coderabbit/pre-merge-check/base..head) contains one changed file: cor…
Full details: Title check

Explanation

The title names the fixed method and the affected tests, and it says the change is a flaky-test fix. The changeset scopes the BaseComposeTest.verifyNoNetworks check to the test's Compose project, which matches the title's main point. The title is long (100 characters) and uses a conventional-commit prefix, but it stays on topic and specific.

Full details: Description check

Explanation

The description covers the broken behaviour with the failure text, affected test runs, and a root-cause analysis. It explains the fix, gives local verification steps and results, and references the checkstyle and Spotless status. It does not follow the repository template's sections, such as the new-module checklist and the dependency-upgrade note. Those sections do not apply to this change, so their absence is not a gap. The template asks to reference related issues with #; the description cites a PR and CI runs but no issue number, which is acceptable since no issue is linked.

Full details: New Module Registration

Explanation

The "New module registration" check applies only when the PR adds a new directory under modules/. The PR diff range (refs/coderabbit/pre-merge-check/base to head) changes one file: M core/src/test/java/org/testcontainers/junit/BaseComposeTest.java. No file under modules/ is added, so the check does not apply and passes. The PR is a test-only change in core, scoping the network check to the Compose project label.

Full details: Public Api Compatibility

Explanation

The check applies to changes under src/main/java. The authoritative changed-file inventory (refs/coderabbit/pre-merge-check/base..head) contains one modified file: core/src/test/java/org/testcontainers/junit/BaseComposeTest.java. It is a test class and lies outside src/main/java. No public or protected class, method, constructor, or field under src/main/java is removed, renamed, re-signed, or re-scoped. The PR description also says "Test-only change; no main code touched," which matches the inventory. Under the stated policy, additive or non-main changes pass.

Full details: Tests For Behaviour Changes

Explanation

The check applies only when the PR changes behaviour in src/main/java without a matching src/test change. The reviewed range (refs/coderabbit/pre-merge-check/base..head) contains one changed file: core/src/test/java/org/testcontainers/junit/BaseComposeTest.java (status M). No file under src/main/java changed, so no main-code behaviour change exists to trigger the failure condition. The PR is a test-only flaky-test fix that scopes the network snapshot check to the test's Compose project. The check is inapplicable, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • 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 9f7ada8 and d8b54db.

Files selected for processing: 1

  • core/src/test/java/org/testcontainers/junit/BaseComposeTest.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.

2 participants