Home > Software design >  TestContainer what is the difference between withStartupTimeout and waitingFor(Wait.forListeningPort
TestContainer what is the difference between withStartupTimeout and waitingFor(Wait.forListeningPort

Time:06-07

TestContainers doc states

Ordinarily Testcontainers will wait for up to 60 seconds for the container's first mapped network port to start listening.

This simple measure provides a basic check whether a container is ready for use.

If I am starting my container like below

var sharedKafkaContainer = new KafkaContainer(DockerImageName.parse(DOCKER_KAFKA_IMAGE_NAME));
sharedKafkaContainer.withStartupTimeout(Duration.of(3, ChronoUnit.MINUTES));

Then the container will wait for 3 mins for the exposed or mapped port to start listening.

The below code is a succinct representation of the above and there is no difference between withStartupTimeout and waitingFor(Wait.forListeningPort()) Is my understanding correct?

var sharedKafkaContainer = new KafkaContainer(DockerImageName.parse(DOCKER_KAFKA_IMAGE_NAME));
sharedKafkaContainer.withStartupTimeout(Duration.of(3, ChronoUnit.MINUTES));
sharedKafkaContainer.waitingFor(Wait.forListeningPort()).start()

CodePudding user response:

They're same. The waitingFor method allows you to pass your custom implementation of the WaitStrategy interface which can check for any arbitrary condition to see if given container is up or not. The Wait.forListeningPort returns an instance of HostPortWaitStrategy which is one implementation of the WaitStrategy, checking for a specific port to be available and consider that as a criteria to mark a container as ready.

  • Related