Skip to content

Commit

Permalink
HSEARCH-4674 Allow custom wait conditions for database containers
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Oct 26, 2023
1 parent 579b208 commit 5c58d40
Showing 1 changed file with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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" )
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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" );
}
};
Expand Down Expand Up @@ -283,9 +297,15 @@ private static class HibernateSearchJdbcDatabaseContainer
private final String username;
private final String password;
private final String testQueryString;
private final Optional<WaitStrategy> 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 ) );
Expand All @@ -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;
Expand Down

0 comments on commit 5c58d40

Please sign in to comment.