Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wait.forListeningPort(port) #7402

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}
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
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() {}
}
}