|
3 | 3 | import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
|
4 | 4 | import org.testcontainers.utility.ComparableVersion;
|
5 | 5 |
|
| 6 | +import java.io.IOException; |
6 | 7 | import java.util.ArrayList;
|
7 | 8 | import java.util.List;
|
| 9 | +import java.util.regex.Matcher; |
| 10 | +import java.util.regex.Pattern; |
8 | 11 |
|
9 | 12 | public class PostgreSQLWaitStrategy extends AbstractWaitStrategy {
|
10 | 13 |
|
11 |
| - private final String version; |
12 |
| - |
13 |
| - public PostgreSQLWaitStrategy(String version) { |
14 |
| - this.version = version; |
15 |
| - } |
| 14 | + private final Pattern pattern = Pattern.compile("(?s)(?:\\d\\S*)"); |
16 | 15 |
|
17 | 16 | @Override
|
18 | 17 | protected void waitUntilReady() {
|
19 |
| - boolean isAtLeastMajorVersion94 = new ComparableVersion(this.version).isGreaterThanOrEqualTo("9.4"); |
20 |
| - |
21 |
| - List<String> firstAttempt = new ArrayList<>(); |
22 |
| - firstAttempt.add(".*PostgreSQL init process complete.*$"); |
23 |
| - firstAttempt.add(".*database system is ready to accept connections.*$"); |
24 |
| - |
25 |
| - List<String> secondAttempt = new ArrayList<>(); |
26 |
| - if (isAtLeastMajorVersion94) { |
27 |
| - secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$"); |
| 18 | + try { |
| 19 | + String postgresVersion = this.waitStrategyTarget.execInContainer("postgres", "-V").getStdout(); |
| 20 | + Matcher matcher = this.pattern.matcher(postgresVersion); |
| 21 | + if (matcher.find()) { |
| 22 | + String version = matcher.group(); |
| 23 | + boolean isAtLeastMajorVersion94 = new ComparableVersion(version).isGreaterThanOrEqualTo("9.4"); |
| 24 | + |
| 25 | + List<String> firstAttempt = new ArrayList<>(); |
| 26 | + firstAttempt.add(".*PostgreSQL init process complete.*$"); |
| 27 | + firstAttempt.add(".*database system is ready to accept connections.*$"); |
| 28 | + |
| 29 | + List<String> secondAttempt = new ArrayList<>(); |
| 30 | + if (isAtLeastMajorVersion94) { |
| 31 | + secondAttempt.add(".*PostgreSQL Database directory appears to contain a database.*$"); |
| 32 | + } |
| 33 | + secondAttempt.add(".*database system is ready to accept connections.*$"); |
| 34 | + |
| 35 | + new MultiLogMessageWaitStrategy() |
| 36 | + .withRegEx(firstAttempt) |
| 37 | + .withRegEx(secondAttempt) |
| 38 | + .waitUntilReady(this.waitStrategyTarget); |
| 39 | + } |
| 40 | + } catch (IOException | InterruptedException e) { |
| 41 | + throw new RuntimeException(e); |
28 | 42 | }
|
29 |
| - secondAttempt.add(".*database system is ready to accept connections.*$"); |
30 |
| - |
31 |
| - new MultiLogMessageWaitStrategy() |
32 |
| - .withRegEx(firstAttempt) |
33 |
| - .withRegEx(secondAttempt) |
34 |
| - .waitUntilReady(this.waitStrategyTarget); |
35 | 43 | }
|
36 | 44 |
|
37 | 45 | }
|
0 commit comments