Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> getLivenessCheckPortNumbers() {
final Set<Integer> result = getExposedPorts()
.stream()
.map(this::getMappedPort)
.distinct()
.collect(Collectors.toSet());
.collect(Collectors.toCollection(LinkedHashSet::new));
result.addAll(getBoundPortNumbers());
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}