diff --git a/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/DatabaseContainer.java b/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/DatabaseContainer.java index b9d6b23fd1a..324ba527e99 100644 --- a/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/DatabaseContainer.java +++ b/util/internal/integrationtest/mapper/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/mapper/orm/DatabaseContainer.java @@ -11,11 +11,15 @@ import java.util.Collections; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.function.BiConsumer; import org.hibernate.cfg.JdbcSettings; import org.testcontainers.containers.JdbcDatabaseContainer; +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; +import org.testcontainers.containers.wait.strategy.WaitStrategy; import org.testcontainers.images.builder.ImageFromDockerfile; /* @@ -102,7 +106,10 @@ HibernateSearchJdbcDatabaseContainer container(Path dockerfile, String name) { 5432, "hibernate_orm_test", "hibernate_orm_test", - "select 1" + "select 1", + new LogMessageWaitStrategy() + .withRegEx( ".*database system is ready to accept connections.*\\s" ) + .withTimes( 2 ) ).withEnv( "POSTGRES_USER", "hibernate_orm_test" ) .withEnv( "POSTGRES_PASSWORD", "hibernate_orm_test" ) .withEnv( "POSTGRES_DB", "hibernate_orm_test" ); @@ -171,8 +178,9 @@ HibernateSearchJdbcDatabaseContainer container(Path dockerfile, String name) { 50000, "hreact", "hreact", - "SELECT 1 FROM SYSIBM.SYSDUMMY1" - + "SELECT 1 FROM SYSIBM.SYSDUMMY1", + new LogMessageWaitStrategy() + .withRegEx( ".*INSTANCE.*" ) ).withNetworkMode( "bridge" ) .withEnv( "DB2INSTANCE", "hreact" ) .withEnv( "DB2INST1_PASSWORD", "hreact" ) @@ -203,9 +211,11 @@ HibernateSearchJdbcDatabaseContainer container(Path dockerfile, String name) { 1521, "SYSTEM", "hibernate_orm_test", - "select 1 from dual" - ).withEnv( "ORACLE_PASSWORD", "hibernate_orm_test" ) - .withStartupTimeout( EXTENDED_TIMEOUT ); + "select 1 from dual", + new LogMessageWaitStrategy() + .withRegEx( ".*DATABASE IS READY TO USE!.*\\s" ) + .withTimes( 1 ) + ).withEnv( "ORACLE_PASSWORD", "hibernate_orm_test" ); } }, MSSQL { @@ -244,7 +254,11 @@ HibernateSearchJdbcDatabaseContainer container(Path dockerfile, String name) { 26257, "root", "", - "select 1" + "select 1", + new HttpWaitStrategy() + .forPath( "/health" ) + .forPort( 8080 ) + .forStatusCode( 200 ) ).withCommand( "start-single-node --insecure" ); } }; @@ -283,9 +297,15 @@ private static class HibernateSearchJdbcDatabaseContainer private final String username; private final String password; private final String testQueryString; + private final Optional customWaitStrategy; public HibernateSearchJdbcDatabaseContainer(Path dockerfile, String name, String driverClassName, String jdbcUrlPattern, int port, String username, String password, String testQueryString) { + this( dockerfile, name, driverClassName, jdbcUrlPattern, port, username, password, testQueryString, null ); + } + + public HibernateSearchJdbcDatabaseContainer(Path dockerfile, String name, String driverClassName, String jdbcUrlPattern, + int port, String username, String password, String testQueryString, WaitStrategy waitStrategy) { // IMPORTANT: we do not want to delete the image on exit as then we cannot use container reuse. // (these two options are somewhat mutually exclusive). super( new ImageFromDockerfile( "hibernate-search-" + name, false ).withDockerfile( dockerfile ) ); @@ -295,12 +315,31 @@ public HibernateSearchJdbcDatabaseContainer(Path dockerfile, String name, String this.username = username; this.password = password; this.testQueryString = testQueryString; + this.customWaitStrategy = Optional.ofNullable( waitStrategy ); withExposedPorts( port ); withReuse( true ); withStartupTimeout( REGULAR_TIMEOUT ); } + @Override + protected void waitUntilContainerStarted() { + if ( customWaitStrategy.isPresent() ) { + customWaitStrategy.get().waitUntilReady( this ); + } + else { + super.waitUntilContainerStarted(); + } + } + + @Override + public HibernateSearchJdbcDatabaseContainer withStartupTimeout(Duration startupTimeout) { + if ( customWaitStrategy.isPresent() ) { + customWaitStrategy.get().withStartupTimeout( startupTimeout ); + } + return super.withStartupTimeout( startupTimeout ); + } + @Override public String getDriverClassName() { return driverClassName;