Skip to content

Commit

Permalink
Add Wait.forListeningPort(port) (#7402)
Browse files Browse the repository at this point in the history
Add utility method to check on specific ports.
  • Loading branch information
eddumelendez committed Aug 9, 2023
1 parent fb2fbbc commit bf5605a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,29 @@
@Slf4j
public class HostPortWaitStrategy extends AbstractWaitStrategy {

private int[] port;

@Override
@SneakyThrows(InterruptedException.class)
protected void waitUntilReady() {
final Set<Integer> externalLivenessCheckPorts = getLivenessCheckPorts();
if (externalLivenessCheckPorts.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug(
"Liveness check ports of {} is empty. Not waiting.",
waitStrategyTarget.getContainerInfo().getName()
);
final Set<Integer> externalLivenessCheckPorts;
if (this.port == null) {
externalLivenessCheckPorts = getLivenessCheckPorts();
if (externalLivenessCheckPorts.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug(
"Liveness check ports of {} is empty. Not waiting.",
waitStrategyTarget.getContainerInfo().getName()
);
}
return;
}
return;
} else {
externalLivenessCheckPorts =
Arrays
.stream(this.port)
.mapToObj(port -> waitStrategyTarget.getMappedPort(port))
.collect(Collectors.toSet());
}

List<Integer> exposedPorts = waitStrategyTarget.getExposedPorts();
Expand Down Expand Up @@ -112,4 +123,9 @@ private Set<Integer> getInternalPorts(Set<Integer> externalLivenessCheckPorts, L
.filter(it -> externalLivenessCheckPorts.contains(waitStrategyTarget.getMappedPort(it)))
.collect(Collectors.toSet());
}

public HostPortWaitStrategy forPort(int... port) {
this.port = port;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public static HostPortWaitStrategy forListeningPort() {
return new HostPortWaitStrategy();
}

/**
* Convenience method to return a WaitStrategy for an exposed or mapped port.
*
* @param port the port to check
* @return the WaitStrategy
*/
public static HostPortWaitStrategy forListeningPort(int... port) {
return new HostPortWaitStrategy().forPort(port);
}

/**
* Convenience method to return a WaitStrategy for an HTTP endpoint.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.testcontainers.TestImages;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
Expand All @@ -12,15 +14,32 @@
* Test wait strategy with overloaded waitingFor methods.
* Other implementations of WaitStrategy are tested through backwards compatible wait strategy tests
*/
@RunWith(Enclosed.class)
public class HostPortWaitStrategyTest {

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

@Test
public void testWaiting() {}
@ClassRule
public static GenericContainer<?> container = new GenericContainer<>(TestImages.ALPINE_IMAGE)
.withExposedPorts()
.withCommand("sh", "-c", "while true; do nc -lp 8080; done")
.withExposedPorts(8080)
.waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofSeconds(10)));

@Test
public void testWaiting() {}
}

public static class ExplicitHostPortWaitStrategyTest {

@ClassRule
public static GenericContainer<?> container = new GenericContainer<>(TestImages.ALPINE_IMAGE)
.withExposedPorts()
.withCommand("sh", "-c", "while true; do nc -lp 8080; done")
.withExposedPorts(8080)
.waitingFor(Wait.forListeningPort(8080).withStartupTimeout(Duration.ofSeconds(10)));

@Test
public void testWaiting() {}
}
}

0 comments on commit bf5605a

Please sign in to comment.