Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PostgreSQLWaitStrategy #5368

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Read version from postgres -V
eddumelendez committed May 17, 2022
commit fc195601b84005cb98f5239a7c0ae8173164a886
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ public PostgreSQLContainer(final DockerImageName dockerImageName) {

dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);

this.waitStrategy = new PostgreSQLWaitStrategy(dockerImageName.getVersionPart());
this.waitStrategy = new PostgreSQLWaitStrategy();
this.setCommand("postgres", "-c", FSYNC_OFF_OPTION);

addExposedPort(POSTGRESQL_PORT);
Original file line number Diff line number Diff line change
@@ -3,35 +3,43 @@
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.testcontainers.utility.ComparableVersion;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PostgreSQLWaitStrategy extends AbstractWaitStrategy {

private final String version;

public PostgreSQLWaitStrategy(String version) {
this.version = version;
}
private final Pattern pattern = Pattern.compile("(?s)(?:\\d\\S*)");

@Override
protected void waitUntilReady() {
boolean isAtLeastMajorVersion94 = new ComparableVersion(this.version).isGreaterThanOrEqualTo("9.4");

List<String> firstAttempt = new ArrayList<>();
firstAttempt.add(".*PostgreSQL init process complete.*$");
firstAttempt.add(".*database system is ready to accept connections.*$");

List<String> secondAttempt = new ArrayList<>();
if (isAtLeastMajorVersion94) {
secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$");
try {
String postgresVersion = this.waitStrategyTarget.execInContainer("postgres", "-V").getStdout();
Matcher matcher = this.pattern.matcher(postgresVersion);
if (matcher.find()) {
String version = matcher.group();
boolean isAtLeastMajorVersion94 = new ComparableVersion(version).isGreaterThanOrEqualTo("9.4");

List<String> firstAttempt = new ArrayList<>();
firstAttempt.add(".*PostgreSQL init process complete.*$");
firstAttempt.add(".*database system is ready to accept connections.*$");

List<String> secondAttempt = new ArrayList<>();
if (isAtLeastMajorVersion94) {
secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$");
}
secondAttempt.add(".*database system is ready to accept connections.*$");

new MultiLogMessageWaitStrategy()
.withRegEx(firstAttempt)
.withRegEx(secondAttempt)
.waitUntilReady(this.waitStrategyTarget);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
secondAttempt.add(".*database system is ready to accept connections.*$");

new MultiLogMessageWaitStrategy()
.withRegEx(firstAttempt)
.withRegEx(secondAttempt)
.waitUntilReady(this.waitStrategyTarget);
}

}