Skip to content

Commit

Permalink
Let PostgeSQLContainer also wait for listening port
Browse files Browse the repository at this point in the history
In some situations, TestContainers think PostgreSQLContainer is ready, but the port forwarding is not yet available.
The adjusted wait strategy takes this into account.

See also rancher-sandbox/rancher-desktop#2609 (comment)
  • Loading branch information
stefanscheidt committed May 13, 2024
1 parent d7d7e19 commit d2e15df
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.agiletestingdays.untangletestcode.unicornservice;

import java.time.Duration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitAllStrategy;
import org.testcontainers.junit.jupiter.Testcontainers;

@SpringBootApplication
Expand All @@ -14,7 +17,15 @@ class LocalTestApplication {
@Bean
@ServiceConnection
public PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>("postgres:16");
return new PostgreSQLContainer<>("postgres:16")
// see
// https://github.com/rancher-sandbox/rancher-desktop/issues/2609#issuecomment-1788871956
.waitingFor(
new WaitAllStrategy()
.withStrategy(Wait.forListeningPort())
.withStrategy(
Wait.forLogMessage(".*database system is ready to accept connections.*\\s", 2)
.withStartupTimeout(Duration.ofSeconds(60))));
}

public static void main(String[] args) {
Expand Down

0 comments on commit d2e15df

Please sign in to comment.