Skip to content

Commit a736c02

Browse files
kiviewclaude
andauthored
Fix flaky BaseComposeTest network check by scoping it to the compose project (#12181)
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 that other tests created or removed made the assertion fail intermittently. This was seen in ComposeContainerTest.simpleTest, ComposeContainerPortViaEnvTest.simpleTest and ComposeContainerTest.shouldCreateContainerWhenFileNotPrefixedWithPath. Only list the networks labelled with the test's own com.docker.compose.project, read from the redis-1 service container. Also assert that this list is non-empty before the test, so the check can't pass vacuously. Test-only change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9f56948 commit a736c02

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

‎core/src/test/java/org/testcontainers/junit/BaseComposeTest.java‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import redis.clients.jedis.Jedis;
1313

1414
import java.util.ArrayList;
15+
import java.util.Collections;
1516
import java.util.List;
1617
import java.util.stream.Collectors;
1718

@@ -23,8 +24,12 @@ public abstract class BaseComposeTest {
2324

2425
protected abstract ComposeContainer getEnvironment();
2526

27+
private static final String COMPOSE_PROJECT_LABEL = "com.docker.compose.project";
28+
2629
private List<String> existingNetworks = new ArrayList<>();
2730

31+
private String composeProject;
32+
2833
@BeforeAll
2934
public static void checkVersion() {
3035
Assumptions.assumeTrue(TestEnvironment.dockerApiAtLeast("1.22"));
@@ -63,19 +68,31 @@ void secondTest() {
6368

6469
@BeforeEach
6570
public void captureNetworks() {
66-
existingNetworks.addAll(findAllNetworks());
71+
composeProject =
72+
getEnvironment()
73+
.getContainerByServiceName("redis-1")
74+
.map(container -> container.getContainerInfo().getConfig().getLabels().get(COMPOSE_PROJECT_LABEL))
75+
.orElseThrow(() -> new IllegalStateException("Compose service redis-1 is not running"));
76+
existingNetworks.addAll(findComposeProjectNetworks());
77+
assertThat(existingNetworks).as("The networks of compose project %s", composeProject).isNotEmpty();
6778
}
6879

6980
@AfterEach
7081
public void verifyNoNetworks() {
71-
assertThat(findAllNetworks()).as("The networks").isEqualTo(existingNetworks);
82+
assertThat(findComposeProjectNetworks()).as("The networks").isEqualTo(existingNetworks);
7283
}
7384

74-
private List<String> findAllNetworks() {
85+
/**
86+
* Only look at the networks of this test's compose project. Core tests run in parallel forks against the same
87+
* Docker daemon, so other tests create and remove networks while this test runs; comparing the daemon-wide network
88+
* list made this check flaky.
89+
*/
90+
private List<String> findComposeProjectNetworks() {
7591
return DockerClientFactory
7692
.instance()
7793
.client()
7894
.listNetworksCmd()
95+
.withFilter("label", Collections.singletonList(COMPOSE_PROJECT_LABEL + "=" + composeProject))
7996
.exec()
8097
.stream()
8198
.map(Network::getName)

0 commit comments

Comments
 (0)