How to use ExternalPortListeningCheck class of org.testcontainers.containers.wait.internal package

Best Testcontainers-java code snippet using org.testcontainers.containers.wait.internal.ExternalPortListeningCheck

Source:SelectedPortWaitStrategy.java Github

copy

Full Screen

...17import com.google.common.primitives.Ints;18import net.jodah.failsafe.Failsafe;19import net.jodah.failsafe.RetryPolicy;20import org.testcontainers.containers.ContainerLaunchException;21import org.testcontainers.containers.wait.internal.ExternalPortListeningCheck;22import org.testcontainers.containers.wait.internal.InternalCommandPortListeningCheck;23import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;24import java.util.Optional;25import java.util.Set;26import java.util.concurrent.Callable;27import static com.google.common.base.Preconditions.checkArgument;28import static com.google.common.collect.ImmutableSet.toImmutableSet;29import static java.lang.String.format;30import static java.util.Objects.requireNonNull;31// TODO add tests, move upstream32// This not only adds "wait for selected port" functionality (https://github.com/testcontainers/testcontainers-java/issues/2227),33// but also addresses (https://github.com/testcontainers/testcontainers-java/issues/2225)34public final class SelectedPortWaitStrategy35 extends AbstractWaitStrategy36{37 private final Set<Integer> exposedPorts;38 public SelectedPortWaitStrategy(int... exposedPorts)39 {40 this(ImmutableSet.copyOf(Ints.asList(exposedPorts)));41 }42 public SelectedPortWaitStrategy(Set<Integer> exposedPorts)43 {44 requireNonNull(exposedPorts, "exposedPorts is null");45 checkArgument(!exposedPorts.isEmpty(), "exposedPorts is empty");46 exposedPorts.forEach(exposedPort -> checkArgument(exposedPort != null && exposedPort > 0, "Invalid exposedPort: %s", exposedPort));47 this.exposedPorts = ImmutableSet.copyOf(exposedPorts);48 }49 @Override50 protected void waitUntilReady()51 {52 Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, exposedPorts);53 Set<Integer> externalPorts = exposedPorts.stream()54 .map(waitStrategyTarget::getMappedPort)55 .collect(toImmutableSet());56 Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalPorts);57 Failsafe.with(new RetryPolicy<>()58 .withMaxDuration(startupTimeout)59 .withMaxAttempts(Integer.MAX_VALUE) // limited by MaxDuration60 .abortOn(e -> getExitCode().isPresent()))61 .run(() -> {62 if (!getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call())) {63 // We say "timed out" immediately. Failsafe will propagate this only when timeout reached.64 throw new ContainerLaunchException(format(65 "Timed out waiting for container port to open (%s ports: %s should be listening)",66 waitStrategyTarget.getContainerIpAddress(),67 exposedPorts));68 }69 });70 }...

Full Screen

Full Screen

Source:ExternalPortListeningCheckTest.java Github

copy

Full Screen

...3import java.net.ServerSocket;4import org.junit.Test;5import org.rnorth.visibleassertions.VisibleAssertions;6import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;7public class ExternalPortListeningCheckTest {8 private ServerSocket listeningSocket1;9 private ServerSocket listeningSocket2;10 private ServerSocket nonListeningSocket;11 private WaitStrategyTarget mockContainer;12 @Test13 public void singleListening() {14 final ExternalPortListeningCheck check = new ExternalPortListeningCheck(mockContainer, ImmutableSet.of(listeningSocket1.getLocalPort()));15 final Boolean result = check.call();16 VisibleAssertions.assertTrue("ExternalPortListeningCheck identifies a single listening port", result);17 }18 @Test19 public void multipleListening() {20 final ExternalPortListeningCheck check = new ExternalPortListeningCheck(mockContainer, ImmutableSet.of(listeningSocket1.getLocalPort(), listeningSocket2.getLocalPort()));21 final Boolean result = check.call();22 VisibleAssertions.assertTrue("ExternalPortListeningCheck identifies multiple listening port", result);23 }24 @Test25 public void oneNotListening() {26 final ExternalPortListeningCheck check = new ExternalPortListeningCheck(mockContainer, ImmutableSet.of(listeningSocket1.getLocalPort(), nonListeningSocket.getLocalPort()));27 assertThrows("ExternalPortListeningCheck detects a non-listening port among many", IllegalStateException.class, ((Runnable) (check::call)));28 }29}

Full Screen

Full Screen

Source:WaitForPortsExternallyStrategy.java Github

copy

Full Screen

1package com.dajudge.kindcontainer;2import org.testcontainers.containers.wait.internal.ExternalPortListeningCheck;3import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;4import org.testcontainers.shaded.org.awaitility.Awaitility;5import java.time.Duration;6import java.util.Set;7import static java.util.stream.Collectors.toSet;8public class WaitForPortsExternallyStrategy extends AbstractWaitStrategy {9 @Override10 public void waitUntilReady() {11 final Set<Integer> ports = waitStrategyTarget.getExposedPorts()12 .stream()13 .map(waitStrategyTarget::getMappedPort)14 .collect(toSet());15 final ExternalPortListeningCheck check = new ExternalPortListeningCheck(waitStrategyTarget, ports);16 Awaitility.await()17 .pollInSameThread()18 .pollInterval(Duration.ofMillis(100))19 .pollDelay(Duration.ZERO)20 .ignoreExceptions()21 .forever()22 .until(check);23 }24}...

Full Screen

Full Screen

ExternalPortListeningCheck

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;2import org.testcontainers.containers.wait.strategy.WaitStrategy;3import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;4import org.testcontainers.containers.wait.strategy.internal.ExternalPortListeningCheck;5import org.testcontainers.containers.wait.strategy.internal.InternalCommandPortListeningCheck;6import org.testcontainers.containers.wait.strategy.internal.InternalCommandWaitStrategy;7import org.testcontainers.containers.wait.strategy.internal.InternalCommandWaitStrategy.InternalCommandWaitStrategyBuilder;8import java.util.concurrent.TimeUnit;9public class ExternalPortListeningCheckTest {10 public static WaitStrategy externalPortListeningCheck(int port, int startupTimeoutSeconds) {11 return new AbstractWaitStrategy() {12 protected void waitUntilReady() {13 ExternalPortListeningCheck externalPortListeningCheck = new ExternalPortListeningCheck(port, startupTimeoutSeconds);14 externalPortListeningCheck.waitUntilReady(this);15 }16 };17 }18 public static void main(String[] args) {19 WaitStrategyTarget waitStrategyTarget = new WaitStrategyTarget() {20 public String getHost() {21 return "localhost";22 }23 public Integer getMappedPort(int originalPort) {24 return 8080;25 }26 };27 externalPortListeningCheck(8080, 10).waitUntilReady(waitStrategyTarget);28 }29}30org.testcontainers.containers.ContainerLaunchException: Timed out waiting for container port to open (8080 should be listening) (tried for 10 second(s) with 100 milliseconds interval)31at org.testcontainers.containers.wait.strategy.AbstractWaitStrategy.waitUntilReady(AbstractWaitStrategy.java:43)32at org.testcontainers.containers.wait.strategy.WaitAllStrategy.waitUntilReady(WaitAllStrategy.java:35)33at org.testcontainers.containers.GenericContainer.waitUntilContainerStarted(GenericContainer.java:1037)34at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:460)35at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:325)36at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)37at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:323)38at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311)39at org.testcontainers.containers.GenericContainer.starting(GenericContainer.java:1058)

Full Screen

Full Screen

ExternalPortListeningCheck

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;2import org.testcontainers.containers.wait.strategy.WaitStrategy;3import org.testcontainers.containers.wait.internal.ExternalPortListeningCheck;4public class ExternalPortListeningWaitStrategy extends AbstractWaitStrategy {5 private final int port;6 public ExternalPortListeningWaitStrategy(int port) {7 this.port = port;8 }9 protected void waitUntilReady() {10 ExternalPortListeningCheck externalPortListeningCheck = new ExternalPortListeningCheck(port);11 WaitStrategy targetStrategy = new WaitAllStrategy().withStrategy(new HostPortWaitStrategy()).withStrategy(new InternalCommandPortListeningCheck(port));12 targetStrategy.waitUntilReady(this);13 }14}15import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;16import org.testcontainers.containers.wait.strategy.WaitStrategy;17import org.testcontainers.containers.wait.internal.ExternalPortListeningCheck;18public class ExternalPortListeningWaitStrategy extends AbstractWaitStrategy {19 private final int port;20 public ExternalPortListeningWaitStrategy(int port) {21 this.port = port;22 }23 protected void waitUntilReady() {24 ExternalPortListeningCheck externalPortListeningCheck = new ExternalPortListeningCheck(port);25 WaitStrategy targetStrategy = new WaitAllStrategy().withStrategy(new HostPortWaitStrategy()).withStrategy(new InternalCommandPortListeningCheck(port));26 targetStrategy.waitUntilReady(this);27 }28}

Full Screen

Full Screen

ExternalPortListeningCheck

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers.wait.internal;2import org.testcontainers.containers.ContainerLaunchException;3import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;4import org.testcontainers.containers.wait.strategy.WaitStrategy;5import org.testcontainers.utility.TestcontainersConfiguration;6import org.testcontainers.utility.DockerI

Full Screen

Full Screen

ExternalPortListeningCheck

Using AI Code Generation

copy

Full Screen

1package org.testcontainers.containers.wait;2import org.testcontainers.containers.ContainerLaunchException;3import org.testcontainers.containers.GenericContainer;4import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;5import org.testcontainers.utility.TestcontainersConfiguration;6import java.io.IOException;7import java.time.Duration;8import java.util.concurrent.TimeoutException;9public class ExternalPortListeningCheck extends AbstractWaitStrategy {10 private final int port;11 private final String protocol;12 public ExternalPortListeningCheck(int port, String protocol) {13 this.port = port;14 this.protocol = protocol;15 this.withStartupTimeout(Duration.ofSeconds(TestcontainersConfiguration.getInstance().getStartupTimeoutSeconds()));16 }17 protected void waitUntilReady() {18 try {19 this.waitUntilReady(this.startupTimeoutStrategy);20 } catch (TimeoutException var2) {21 throw new ContainerLaunchException("Timed out waiting for container port to open (" + this.port + " should be listening)");22 }23 }24 private void waitUntilReady(StartupTimeoutStrategy startupTimeoutStrategy) throws TimeoutException {25 try {26 startupTimeoutStrategy.waitUntilReady(this);27 } catch (InterruptedException var3) {28 Thread.currentThread().interrupt();29 throw new ContainerLaunchException("Interrupted while waiting for container port to open (" + this.port + " should be listening)");30 }31 }32 public void waitUntilReady(GenericContainer container) throws InterruptedException {33 try {34 this.waitUntilReady(container, this.startupTimeoutStrategy);35 } catch (TimeoutException var3) {36 throw new ContainerLaunchException("Timed out waiting for container port to open (" + this.port + " should be listening)");37 }38 }39 public void waitUntilReady(GenericContainer container, StartupTimeoutStrategy startupTimeoutStrategy) throws InterruptedException, TimeoutException {40 startupTimeoutStrategy.waitUntilReady(this, container);41 }42 public int getPort() {43 return this.port;44 }45 public String getProtocol() {46 return this.protocol;47 }48 public void waitUntilReady(GenericContainer container, int port, String protocol) throws InterruptedException, TimeoutException {49 this.waitUntilReady(container, port, protocol, this.startupTimeoutStrategy);50 }51 public void waitUntilReady(GenericContainer container, int port, String protocol, StartupTimeoutStrategy startupTimeoutStrategy) throws InterruptedException, TimeoutException {52 startupTimeoutStrategy.waitUntilReady(this, container, port, protocol);53 }

Full Screen

Full Screen

ExternalPortListeningCheck

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.wait.internal.ExternalPortListeningCheck;2import org.testcontainers.containers.wait.strategy.WaitStrategy;3public class ExternalPortListeningCheckTest {4 public static void main(String[] args) {5 WaitStrategy waitStrategy = new ExternalPortListeningCheck(8080);6 waitStrategy.waitUntilReady(null);7 }8}9import org.testcontainers.containers.wait.ExternalPortListeningCheck;10import org.testcontainers.containers.wait.strategy.WaitStrategy;11public class ExternalPortListeningCheckTest {12 public static void main(String[] args) {13 WaitStrategy waitStrategy = new ExternalPortListeningCheck(8080);14 waitStrategy.waitUntilReady(null);15 }16}

Full Screen

Full Screen

ExternalPortListeningCheck

Using AI Code Generation

copy

Full Screen

1import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;2import java.time.Duration;3public class ExternalPortListeningCheck extends AbstractWaitStrategy {4 public ExternalPortListeningCheck() {5 this.waitDuration = Duration.ofSeconds(20);6 }7 protected void waitUntilReady() {8 try {9 Thread.sleep(10000);10 } catch (InterruptedException e) {11 throw new RuntimeException(e);12 }13 }14}15import org.testcontainers.containers.GenericContainer;16import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;17import org.testcontainers.containers.wait.strategy.Wait;18import org.testcontainers.containers.wait.strategy.WaitAllStrategy;19import org.testcontainers.containers.wait.strategy.WaitStrategy;20import java.time.Duration;21public class ExternalPortListeningCheck extends AbstractWaitStrategy {22 public ExternalPortListeningCheck() {23 this.waitDuration = Duration.ofSeconds(20);24 }25 protected void waitUntilReady() {26 try {27 Thread.sleep(10000);28 } catch (InterruptedException e) {29 throw new RuntimeException(e);30 }31 }32}33import org.testcontainers.containers.GenericContainer;34import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;35import org.testcontainers.containers.wait.strategy.Wait;36import org.testcontainers.containers.wait.strategy.WaitAllStrategy;37import org.testcontainers.containers.wait.strategy.WaitStrategy;38import java.time.Duration;39public class ExternalPortListeningCheck extends AbstractWaitStrategy {40 public ExternalPortListeningCheck() {41 this.waitDuration = Duration.ofSeconds(20);42 }43 protected void waitUntilReady() {44 try {45 Thread.sleep(10000);46 } catch (InterruptedException e) {47 throw new RuntimeException(e);48 }49 }50}51import org.testcontainers.containers.GenericContainer;52import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;53import org.testcontainers.containers.wait.strategy.Wait;54import org.testcontainers.containers.wait.strategy.WaitAllStrategy;55import org.testcontainers.containers.wait.strategy.WaitStrategy;56import java.time.Duration;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testcontainers-java automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in ExternalPortListeningCheck

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful