Skip to content

Commit bf5605a

Browse files
authored
Add Wait.forListeningPort(port) (#7402)
Add utility method to check on specific ports.
1 parent fb2fbbc commit bf5605a

File tree

3 files changed

+61
-16
lines changed

3 files changed

+61
-16
lines changed

core/src/main/java/org/testcontainers/containers/wait/strategy/HostPortWaitStrategy.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,29 @@
2626
@Slf4j
2727
public class HostPortWaitStrategy extends AbstractWaitStrategy {
2828

29+
private int[] port;
30+
2931
@Override
3032
@SneakyThrows(InterruptedException.class)
3133
protected void waitUntilReady() {
32-
final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts();
33-
if (externalLivenessCheckPorts.isEmpty()) {
34-
if (log.isDebugEnabled()) {
35-
log.debug(
36-
"Liveness check ports of {} is empty. Not waiting.",
37-
waitStrategyTarget.getContainerInfo().getName()
38-
);
34+
final Set<Integer> externalLivenessCheckPorts;
35+
if (this.port == null) {
36+
externalLivenessCheckPorts = getLivenessCheckPorts();
37+
if (externalLivenessCheckPorts.isEmpty()) {
38+
if (log.isDebugEnabled()) {
39+
log.debug(
40+
"Liveness check ports of {} is empty. Not waiting.",
41+
waitStrategyTarget.getContainerInfo().getName()
42+
);
43+
}
44+
return;
3945
}
40-
return;
46+
} else {
47+
externalLivenessCheckPorts =
48+
Arrays
49+
.stream(this.port)
50+
.mapToObj(port -> waitStrategyTarget.getMappedPort(port))
51+
.collect(Collectors.toSet());
4152
}
4253

4354
List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts();
@@ -112,4 +123,9 @@ private Set<Integer> getInternalPorts(Set<Integer> externalLivenessCheckPorts, L
112123
.filter(it -> externalLivenessCheckPorts.contains(waitStrategyTarget.getMappedPort(it)))
113124
.collect(Collectors.toSet());
114125
}
126+
127+
public HostPortWaitStrategy forPort(int... port) {
128+
this.port = port;
129+
return this;
130+
}
115131
}

core/src/main/java/org/testcontainers/containers/wait/strategy/Wait.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public static HostPortWaitStrategy forListeningPort() {
2525
return new HostPortWaitStrategy();
2626
}
2727

28+
/**
29+
* Convenience method to return a WaitStrategy for an exposed or mapped port.
30+
*
31+
* @param port the port to check
32+
* @return the WaitStrategy
33+
*/
34+
public static HostPortWaitStrategy forListeningPort(int... port) {
35+
return new HostPortWaitStrategy().forPort(port);
36+
}
37+
2838
/**
2939
* Convenience method to return a WaitStrategy for an HTTP endpoint.
3040
*

core/src/test/java/org/testcontainers/junit/wait/strategy/HostPortWaitStrategyTest.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.junit.ClassRule;
44
import org.junit.Test;
5+
import org.junit.experimental.runners.Enclosed;
6+
import org.junit.runner.RunWith;
57
import org.testcontainers.TestImages;
68
import org.testcontainers.containers.GenericContainer;
79
import org.testcontainers.containers.wait.strategy.Wait;
@@ -12,15 +14,32 @@
1214
* Test wait strategy with overloaded waitingFor methods.
1315
* Other implementations of WaitStrategy are tested through backwards compatible wait strategy tests
1416
*/
17+
@RunWith(Enclosed.class)
1518
public class HostPortWaitStrategyTest {
1619

17-
@ClassRule
18-
public static GenericContainer<?> container = new GenericContainer<>(TestImages.ALPINE_IMAGE)
19-
.withExposedPorts()
20-
.withCommand("sh", "-c", "while true; do nc -lp 8080; done")
21-
.withExposedPorts(8080)
22-
.waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(10)));
20+
public static class DefaultHostPortWaitStrategyTest {
2321

24-
@Test
25-
public void testWaiting() {}
22+
@ClassRule
23+
public static GenericContainer<?> container = new GenericContainer<>(TestImages.ALPINE_IMAGE)
24+
.withExposedPorts()
25+
.withCommand("sh", "-c", "while true; do nc -lp 8080; done")
26+
.withExposedPorts(8080)
27+
.waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(10)));
28+
29+
@Test
30+
public void testWaiting() {}
31+
}
32+
33+
public static class ExplicitHostPortWaitStrategyTest {
34+
35+
@ClassRule
36+
public static GenericContainer<?> container = new GenericContainer<>(TestImages.ALPINE_IMAGE)
37+
.withExposedPorts()
38+
.withCommand("sh", "-c", "while true; do nc -lp 8080; done")
39+
.withExposedPorts(8080)
40+
.waitingFor(Wait.forListeningPort(8080).withStartupTimeout(Duration.ofSeconds(10)));
41+
42+
@Test
43+
public void testWaiting() {}
44+
}
2645
}

0 commit comments

Comments
 (0)