2121import org .junit .jupiter .api .extension .AfterAllCallback ;
2222import org .junit .jupiter .api .extension .BeforeAllCallback ;
2323import org .junit .jupiter .api .extension .ExtensionContext ;
24+ import org .slf4j .LoggerFactory ;
2425import org .springframework .jdbc .core .JdbcTemplate ;
2526import org .testcontainers .containers .Network ;
2627import org .testcontainers .containers .PostgreSQLContainer ;
28+ import org .testcontainers .containers .output .Slf4jLogConsumer ;
2729import org .testcontainers .containers .wait .strategy .LogMessageWaitStrategy ;
30+ import org .testcontainers .utility .DockerImageName ;
2831import org .testcontainers .utility .MountableFile ;
2932
3033import java .net .URISyntaxException ;
@@ -118,18 +121,14 @@ private void startPrimary(Network network) {
118121 }
119122
120123 private void startStandby (Network network ) {
121- this .standby = new PostgreSQLContainer <> (PostgresqlServerExtension .IMAGE_NAME )
124+ this .standby = new CustomPostgreSQLContainer (PostgresqlServerExtension .IMAGE_NAME )
122125 .withNetwork (network )
123126 .withCopyFileToContainer (getHostPath ("setup-standby.sh" , 0755 ), "/setup-standby.sh" )
124127 .withCommand ("/setup-standby.sh" )
125128 .withEnv ("PG_REP_USER" , "replication" )
126129 .withEnv ("PG_REP_PASSWORD" , "replication_password" )
127130 .withEnv ("PG_MASTER_HOST" , "postgres-primary" )
128131 .withEnv ("PG_MASTER_PORT" , "5432" );
129- this .standby .setWaitStrategy (new LogMessageWaitStrategy ()
130- .withRegEx (".*database system is ready to accept .* connections.*\\ s" )
131- .withTimes (1 )
132- .withStartupTimeout (Duration .of (60L , ChronoUnit .SECONDS )));
133132 this .standby .start ();
134133 HikariConfig standbyConfig = new HikariConfig ();
135134 standbyConfig .setJdbcUrl (this .standby .getJdbcUrl ());
@@ -138,4 +137,16 @@ private void startStandby(Network network) {
138137 this .standbyDataSource = new HikariDataSource (standbyConfig );
139138 }
140139
140+ // setWaitStrategy() doesn't seem to work, only inside constructor
141+ static class CustomPostgreSQLContainer extends PostgreSQLContainer <CustomPostgreSQLContainer > {
142+ public CustomPostgreSQLContainer (String dockerImageName ) {
143+ super (DockerImageName .parse (dockerImageName ));
144+ this .waitStrategy =
145+ new LogMessageWaitStrategy ()
146+ .withRegEx (".*database system is ready to accept .*connections.*\\ s" )
147+ .withTimes (1 )
148+ .withStartupTimeout (Duration .of (60L , ChronoUnit .SECONDS ));
149+ }
150+ }
151+
141152}
0 commit comments