diff --git a/core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java b/core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java index 778c252009b..3b243c0c17e 100644 --- a/core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java +++ b/core/src/main/java/org/testcontainers/containers/wait/strategy/WaitStrategyTarget.java @@ -2,19 +2,20 @@ import org.testcontainers.containers.ContainerState; +import java.util.LinkedHashSet; import java.util.Set; import java.util.stream.Collectors; public interface WaitStrategyTarget extends ContainerState { /** - * @return the ports on which to check if the container is ready + * @return the ports on which to check if the container is ready, in the order of the exposed ports + * followed by the bound ports, so that the first one is the first exposed port */ default Set getLivenessCheckPortNumbers() { final Set result = getExposedPorts() .stream() .map(this::getMappedPort) - .distinct() - .collect(Collectors.toSet()); + .collect(Collectors.toCollection(LinkedHashSet::new)); result.addAll(getBoundPortNumbers()); return result; } diff --git a/core/src/test/java/org/testcontainers/containers/wait/strategy/WaitStrategyTargetTest.java b/core/src/test/java/org/testcontainers/containers/wait/strategy/WaitStrategyTargetTest.java new file mode 100644 index 00000000000..045ee81b94c --- /dev/null +++ b/core/src/test/java/org/testcontainers/containers/wait/strategy/WaitStrategyTargetTest.java @@ -0,0 +1,36 @@ +package org.testcontainers.containers.wait.strategy; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +class WaitStrategyTargetTest { + + @Test + void livenessCheckPortsFollowTheExposedPortsOrder() { + WaitStrategyTarget target = mock(WaitStrategyTarget.class, CALLS_REAL_METHODS); + doReturn(Arrays.asList(8090, 8091)).when(target).getExposedPorts(); + // a HashSet would iterate 50000 first, making 8091 the "first" port + doReturn(50001).when(target).getMappedPort(8090); + doReturn(50000).when(target).getMappedPort(8091); + doReturn(Collections.emptyList()).when(target).getBoundPortNumbers(); + + assertThat(target.getLivenessCheckPortNumbers()).containsExactly(50001, 50000); + } + + @Test + void boundPortsComeAfterTheExposedPorts() { + WaitStrategyTarget target = mock(WaitStrategyTarget.class, CALLS_REAL_METHODS); + doReturn(Collections.singletonList(8090)).when(target).getExposedPorts(); + doReturn(50001).when(target).getMappedPort(8090); + doReturn(Collections.singletonList(40000)).when(target).getBoundPortNumbers(); + + assertThat(target.getLivenessCheckPortNumbers()).containsExactly(50001, 40000); + } +}