Skip to content

Commit ec5290c

Browse files
[yugabytedb] Change SQL wait strategy (#7784)
Co-authored-by: Eddú Meléndez <[email protected]>
1 parent 2a6d5a8 commit ec5290c

File tree

6 files changed

+65
-5
lines changed

6 files changed

+65
-5
lines changed

modules/yugabytedb/src/main/java/org/testcontainers/containers/YugabyteDBYSQLContainer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,9 @@ public YugabyteDBYSQLContainer withPassword(final String password) {
154154
this.password = password;
155155
return this;
156156
}
157+
158+
@Override
159+
protected void waitUntilContainerStarted() {
160+
getWaitStrategy().waitUntilReady(this);
161+
}
157162
}

modules/yugabytedb/src/main/java/org/testcontainers/containers/delegate/YugabyteDBYCQLDelegate.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@ public void execute(
3333
boolean continueOnError,
3434
boolean ignoreFailedDrops
3535
) {
36+
final String containerInterfaceIP = container
37+
.getContainerInfo()
38+
.getNetworkSettings()
39+
.getNetworks()
40+
.entrySet()
41+
.stream()
42+
.findFirst()
43+
.get()
44+
.getValue()
45+
.getIpAddress();
3646
try {
3747
ExecResult result = container.execInContainer(
3848
BIN_PATH,
49+
containerInterfaceIP,
3950
"-u",
4051
container.getUsername(),
4152
"-p",

modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYCQLWaitStrategy.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public final class YugabyteDBYCQLWaitStrategy extends AbstractWaitStrategy {
3737
public void waitUntilReady(WaitStrategyTarget target) {
3838
YugabyteDBYCQLContainer container = (YugabyteDBYCQLContainer) target;
3939
AtomicBoolean status = new AtomicBoolean(true);
40+
final String containerInterfaceIP = container
41+
.getContainerInfo()
42+
.getNetworkSettings()
43+
.getNetworks()
44+
.entrySet()
45+
.stream()
46+
.findFirst()
47+
.get()
48+
.getValue()
49+
.getIpAddress();
4050
retryUntilSuccess(
4151
(int) startupTimeout.getSeconds(),
4252
TimeUnit.SECONDS,
@@ -46,6 +56,7 @@ public void waitUntilReady(WaitStrategyTarget target) {
4656
try {
4757
ExecResult result = container.execInContainer(
4858
BIN_PATH,
59+
containerInterfaceIP,
4960
"-u",
5061
container.getUsername(),
5162
"-p",

modules/yugabytedb/src/main/java/org/testcontainers/containers/strategy/YugabyteDBYSQLWaitStrategy.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.sql.Connection;
1010
import java.sql.SQLException;
11+
import java.sql.Statement;
1112
import java.util.concurrent.TimeUnit;
1213

1314
import static org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess;
@@ -27,10 +28,13 @@
2728
@Slf4j
2829
public final class YugabyteDBYSQLWaitStrategy extends AbstractWaitStrategy {
2930

30-
private static final String YSQL_TEST_QUERY = "SELECT 1";
31-
3231
private final WaitStrategyTarget target;
3332

33+
private static final String YSQL_EXTENDED_PROBE =
34+
"CREATE TABLE IF NOT EXISTS YB_SAMPLE(k int, v int, primary key(k, v))";
35+
36+
private static final String YSQL_EXTENDED_PROBE_DROP_TABLE = "DROP TABLE IF EXISTS YB_SAMPLE";
37+
3438
@Override
3539
public void waitUntilReady(WaitStrategyTarget target) {
3640
YugabyteDBYSQLContainer container = (YugabyteDBYSQLContainer) target;
@@ -40,10 +44,11 @@ public void waitUntilReady(WaitStrategyTarget target) {
4044
() -> {
4145
getRateLimiter()
4246
.doWhenReady(() -> {
43-
try (Connection con = container.createConnection(container.getJdbcUrl())) {
44-
con.createStatement().execute(YSQL_TEST_QUERY);
47+
try (Connection con = container.createConnection(""); Statement stmt = con.createStatement()) {
48+
stmt.execute(YSQL_EXTENDED_PROBE);
49+
stmt.execute(YSQL_EXTENDED_PROBE_DROP_TABLE);
4550
} catch (SQLException ex) {
46-
log.error("Error connecting to the database", ex);
51+
throw new RuntimeException(ex);
4752
}
4853
});
4954
return true;

modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYCQLTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class YugabyteDBYCQLTest {
1515

1616
private static final String IMAGE_NAME = "yugabytedb/yugabyte:2.14.4.0-b26";
1717

18+
private static final String IMAGE_NAME_2_18 = "yugabytedb/yugabyte:2.18.3.0-b75";
19+
1820
private static final DockerImageName YBDB_TEST_IMAGE = DockerImageName.parse(IMAGE_NAME);
1921

2022
@Test
@@ -106,6 +108,16 @@ public void testInitScript() {
106108
}
107109
}
108110

111+
@Test
112+
public void shouldStartWhenContainerIpIsUsedInWaitStrategy() {
113+
try (final YugabyteDBYCQLContainer ycqlContainer = new YugabyteDBYCQLContainer(IMAGE_NAME_2_18)) {
114+
ycqlContainer.start();
115+
boolean isQueryExecuted = performQuery(ycqlContainer, "SELECT release_version FROM system.local")
116+
.wasApplied();
117+
assertThat(isQueryExecuted).isTrue();
118+
}
119+
}
120+
109121
private ResultSet performQuery(YugabyteDBYCQLContainer ycqlContainer, String cql) {
110122
try (
111123
CqlSession session = CqlSession

modules/yugabytedb/src/test/java/org/testcontainers/junit/yugabytedb/YugabyteDBYSQLTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,20 @@ public void testWithCustomRole() throws SQLException {
9595
.isEqualTo(1);
9696
}
9797
}
98+
99+
@Test
100+
public void testWaitStrategy() throws SQLException {
101+
try (final YugabyteDBYSQLContainer ysqlContainer = new YugabyteDBYSQLContainer(YBDB_TEST_IMAGE)) {
102+
ysqlContainer.start();
103+
assertThat(performQuery(ysqlContainer, "SELECT 1").getInt(1))
104+
.as("A sample test query succeeds")
105+
.isEqualTo(1);
106+
boolean tableExists = performQuery(
107+
ysqlContainer,
108+
"SELECT EXISTS (SELECT FROM pg_tables WHERE tablename = 'YB_SAMPLE')"
109+
)
110+
.getBoolean(1);
111+
assertThat(tableExists).as("yb_sample table does not exists").isFalse();
112+
}
113+
}
98114
}

0 commit comments

Comments
 (0)