Skip to content

Commit 52bc71f

Browse files
jchryssaurabhyadav1985mirromutth
authored
Apply TestContainer (#264)
Motivation: Enhance test reliability and environment consistency by integrating TestContainers Modification: Added TestContainers dependency and updated tests to run with containerized DBs. Expanded test scope to include MySQL and MariaDB versions to cover conditional test cases. Result: Improved test suite stability and simplified setup for contributors. Resolves #76 Co-authored-by: saurabhyadav1985 <49230235+saurabhyadav1985@users.noreply.github.com> Co-authored-by: Mirro Mutth <mirromutth@gmail.com>
1 parent faa30a7 commit 52bc71f

20 files changed

+493
-175
lines changed

.github/workflows/ci-graalvm-tests.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,16 @@ jobs:
2424
steps:
2525
- uses: actions/checkout@v4
2626

27-
- name: Set up MySQL
28-
env:
29-
MYSQL_DATABASE: r2dbc
30-
MYSQL_ROOT_PASSWORD: r2dbc-password!@
31-
MYSQL_VERSION: 8.1
32-
run: docker-compose -f ${{ github.workspace }}/containers/mysql-compose.yml up -d
33-
3427
- uses: graalvm/setup-graalvm@v1
3528
with:
3629
java-version: 21
3730
distribution: 'graalvm'
3831
native-image-job-reports: true
3932
github-token: ${{ secrets.GITHUB_TOKEN }}
4033

34+
- name: Start Bundled MySQL
35+
run: sudo service mysql start
36+
4137
- name: Cache & Load Local Maven Repository
4238
uses: actions/cache@v3
4339
with:
@@ -50,4 +46,4 @@ jobs:
5046
echo "JAVA_HOME=$JAVA_HOME"
5147
echo "./mvnw -Pgraalvm package -Dmaven.javadoc.skip=true"
5248
./mvnw -Pgraalvm package -Dmaven.javadoc.skip=true
53-
./test-native-image/target/test-native-image -Dtest.mysql.password=r2dbc-password!@
49+
./test-native-image/target/test-native-image

.github/workflows/ci-integration-tests.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-20.04
1010
strategy:
1111
matrix:
12-
mysql-version: [ 5.5, 5.6, 5.7, 8.0, 8.1, 8.2 ]
12+
mysql-version: [ 5.5, 5.6.45, 5.6, 5.7.28, 5.7, 8.0, 8.1, 8.2, 8.3]
1313
name: Integration test with MySQL ${{ matrix.mysql-version }}
1414
steps:
1515
- uses: actions/checkout@v3
@@ -21,19 +21,13 @@ jobs:
2121
cache: maven
2222
- name: Shutdown the Default MySQL
2323
run: sudo service mysql stop
24-
- name: Set up MySQL ${{ matrix.mysql-version }}
25-
env:
26-
MYSQL_DATABASE: r2dbc
27-
MYSQL_ROOT_PASSWORD: r2dbc-password!@
28-
MYSQL_VERSION: ${{ matrix.mysql-version }}
29-
run: docker-compose -f ${{ github.workspace }}/containers/mysql-compose.yml up -d
3024
- name: Integration test with MySQL ${{ matrix.mysql-version }}
3125
run: |
3226
set -o pipefail
3327
./mvnw -B verify -Dmaven.javadoc.skip=true \
3428
-Dmaven.surefire.skip=true \
35-
-Dtest.mysql.password=r2dbc-password!@ \
36-
-Dtest.mysql.version=${{ matrix.mysql-version }} \
29+
-Dtest.db.type=mysql \
30+
-Dtest.db.version=${{ matrix.mysql-version }} \
3731
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN \
3832
-Dio.netty.leakDetectionLevel=paranoid \
3933
-Dio.netty.leakDetection.targetRecords=32 \

.github/workflows/ci-mariadb-intergration-tests.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-20.04
1010
strategy:
1111
matrix:
12-
mariadb-version: [ 10.6, 10.11 ]
12+
mariadb-version: [ 10.0, 10.1, 10.2.15, 10.2, 10.3.7, 10.3, 10.5.1, 10.5, 10.6, 10.11]
1313
name: Integration test with MariaDB ${{ matrix.mariadb-version }}
1414
steps:
1515
- uses: actions/checkout@v3
@@ -21,21 +21,13 @@ jobs:
2121
cache: maven
2222
- name: Shutdown the Default MySQL
2323
run: sudo service mysql stop
24-
- name: Set up MariaDB ${{ matrix.mariadb-version }}
25-
env:
26-
MYSQL_DATABASE: r2dbc
27-
MYSQL_ROOT_PASSWORD: r2dbc-password!@
28-
MARIADB_VERSION: ${{ matrix.mariadb-version }}
29-
run: docker-compose -f ${{ github.workspace }}/containers/mariadb-compose.yml up -d
30-
- name: Integration test with MySQL ${{ matrix.mysql-version }}
24+
- name: Integration test with MariaDB ${{ matrix.mysql-version }}
3125
run: |
3226
set -o pipefail
3327
./mvnw -B verify -Dmaven.javadoc.skip=true \
3428
-Dmaven.surefire.skip=true \
35-
-Dtest.mysql.password=r2dbc-password!@ \
36-
-Dtest.mysql.version=${{ matrix.mariadb-version }} \
3729
-Dtest.db.type=mariadb \
38-
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN \
30+
-Dtest.db.version=${{ matrix.mariadb-version }} \
3931
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN \
4032
-Dio.netty.leakDetectionLevel=paranoid \
4133
-Dio.netty.leakDetection.targetRecords=32 \

containers/mariadb-compose.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

containers/mysql-compose.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

r2dbc-mysql/pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@
8383
<logback.version>1.5.3</logback.version>
8484
<mockito.version>4.11.0</mockito.version>
8585
<mysql.version>8.3.0</mysql.version>
86-
<testcontainers.version>1.19.6</testcontainers.version>
86+
<mariadb.version>3.3.3</mariadb.version>
87+
<testcontainers.version>1.19.7</testcontainers.version>
8788
<hikari-cp.version>4.0.3</hikari-cp.version>
8889
<spring-framework.version>5.3.32</spring-framework.version>
8990
<jackson.version>2.16.1</jackson.version>
@@ -241,6 +242,12 @@
241242
<version>${mysql.version}</version>
242243
<scope>test</scope>
243244
</dependency>
245+
<dependency>
246+
<groupId>org.mariadb.jdbc</groupId>
247+
<artifactId>mariadb-java-client</artifactId>
248+
<version>${mariadb.version}</version>
249+
<scope>test</scope>
250+
</dependency>
244251
<dependency>
245252
<groupId>com.zaxxer</groupId>
246253
<artifactId>HikariCP</artifactId>
@@ -270,6 +277,17 @@
270277
</exclusion>
271278
</exclusions>
272279
</dependency>
280+
<dependency>
281+
<groupId>org.testcontainers</groupId>
282+
<artifactId>mariadb</artifactId>
283+
<scope>test</scope>
284+
<exclusions>
285+
<exclusion>
286+
<groupId>org.slf4j</groupId>
287+
<artifactId>slf4j-api</artifactId>
288+
</exclusion>
289+
</exclusions>
290+
</dependency>
273291
<dependency>
274292
<groupId>com.fasterxml.jackson.core</groupId>
275293
<artifactId>jackson-core</artifactId>

r2dbc-mysql/src/main/java/io/asyncer/r2dbc/mysql/message/server/ErrorMessage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public R2dbcException toException(@Nullable String sql) {
9797
case 1907: // Statement executing timeout
9898
case 3024: // Query execution was interrupted, maximum statement execution time exceeded
9999
case 1969: // Query execution was interrupted
100+
case 1968: // Query execution was interrupted (max_statement_time exceeded)
100101
return new R2dbcTimeoutException(message, sqlState, code);
101102
case 1613: // Transaction rollback because of took too long
102103
return new R2dbcRollbackException(message, sqlState, code);

r2dbc-mysql/src/test/java/io/asyncer/r2dbc/mysql/IntegrationTestSupport.java

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package io.asyncer.r2dbc.mysql;
1818

1919
import io.asyncer.r2dbc.mysql.api.MySqlConnection;
20+
import io.asyncer.r2dbc.mysql.internal.util.TestContainerExtension;
21+
import io.asyncer.r2dbc.mysql.internal.util.TestServerUtil;
2022
import io.r2dbc.spi.R2dbcBadGrammarException;
2123
import io.r2dbc.spi.R2dbcTimeoutException;
2224
import io.r2dbc.spi.Result;
25+
import org.junit.jupiter.api.extension.ExtendWith;
2326
import org.reactivestreams.Publisher;
2427
import reactor.core.publisher.Flux;
2528
import reactor.core.publisher.Mono;
@@ -38,6 +41,7 @@
3841
/**
3942
* Base class considers connection factory and general function for integration tests.
4043
*/
44+
@ExtendWith(TestContainerExtension.class)
4145
abstract class IntegrationTestSupport {
4246

4347
private final MySqlConnectionFactory connectionFactory;
@@ -82,11 +86,6 @@ static Mono<Long> extractRowsUpdated(Result result) {
8286
static MySqlConnectionConfiguration configuration(
8387
Function<MySqlConnectionConfiguration.Builder, MySqlConnectionConfiguration.Builder> customizer
8488
) {
85-
String password = System.getProperty("test.mysql.password");
86-
87-
assertThat(password).withFailMessage("Property test.mysql.password must exists and not be empty")
88-
.isNotNull()
89-
.isNotEmpty();
9089

9190
String localInfilePath;
9291

@@ -99,73 +98,47 @@ static MySqlConnectionConfiguration configuration(
9998
}
10099

101100
MySqlConnectionConfiguration.Builder builder = MySqlConnectionConfiguration.builder()
102-
.host("127.0.0.1")
101+
.host(TestServerUtil.getHost())
102+
.port(TestServerUtil.getPort())
103+
.user(TestServerUtil.getUsername())
104+
.password(TestServerUtil.getPassword())
105+
.database(TestServerUtil.getDatabase())
103106
.connectTimeout(Duration.ofSeconds(3))
104-
.user("root")
105-
.password(password)
106-
.database("r2dbc")
107107
.allowLoadLocalInfileInPath(localInfilePath);
108108

109109
return customizer.apply(builder).build();
110110
}
111111

112112
boolean envIsLessThanMySql56() {
113-
String version = System.getProperty("test.mysql.version");
114-
115-
if (version == null || version.isEmpty()) {
116-
return true;
117-
}
118-
119-
ServerVersion ver = ServerVersion.parse(version);
120-
String type = System.getProperty("test.db.type");
121-
122-
if ("mariadb".equalsIgnoreCase(type)) {
113+
if (TestServerUtil.isMariaDb()) {
123114
return false;
124115
}
125-
116+
final ServerVersion ver = TestServerUtil.getServerVersion();
126117
return ver.isLessThan(ServerVersion.create(5, 6, 0));
127118
}
128119

129-
boolean envIsLessThanMySql57OrMariaDb102() {
130-
String version = System.getProperty("test.mysql.version");
131-
132-
if (version == null || version.isEmpty()) {
133-
return true;
134-
}
135-
136-
ServerVersion ver = ServerVersion.parse(version);
137-
String type = System.getProperty("test.db.type");
138-
139-
if ("mariadb".equalsIgnoreCase(type)) {
120+
boolean envIsLessThanMySql578OrMariaDb102() {
121+
final ServerVersion ver = TestServerUtil.getServerVersion();
122+
if (TestServerUtil.isMariaDb()) {
140123
return ver.isLessThan(ServerVersion.create(10, 2, 0));
141124
}
142125

143-
return ver.isLessThan(ServerVersion.create(5, 7, 0));
126+
return ver.isLessThan(ServerVersion.create(5, 7, 8));
144127
}
145128

146129
static boolean envIsMariaDb10_5_1() {
147-
String type = System.getProperty("test.db.type");
148-
149-
if (!"mariadb".equalsIgnoreCase(type)) {
130+
if (!TestServerUtil.isMariaDb()) {
150131
return false;
151132
}
152133

153-
ServerVersion ver = ServerVersion.parse(System.getProperty("test.mysql.version"));
134+
final ServerVersion ver = TestServerUtil.getServerVersion();
154135

155136
return ver.isGreaterThanOrEqualTo(ServerVersion.create(10, 5, 1));
156137
}
157138

158139
boolean envIsLessThanMySql574OrMariaDb1011() {
159-
String version = System.getProperty("test.mysql.version");
160-
161-
if (version == null || version.isEmpty()) {
162-
return true;
163-
}
164-
165-
ServerVersion ver = ServerVersion.parse(version);
166-
String type = System.getProperty("test.db.type");
167-
168-
if ("mariadb".equalsIgnoreCase(type)) {
140+
final ServerVersion ver = TestServerUtil.getServerVersion();
141+
if (TestServerUtil.isMariaDb()) {
169142
return ver.isLessThan(ServerVersion.create(10, 1, 1));
170143
}
171144

r2dbc-mysql/src/test/java/io/asyncer/r2dbc/mysql/JacksonIntegrationTestSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void tearDown() {
6767
JacksonCodecRegistrar.tearDown();
6868
}
6969

70-
@DisabledIf("envIsLessThanMySql57OrMariaDb102")
70+
@DisabledIf("envIsLessThanMySql578OrMariaDb102")
7171
@Test
7272
void json() {
7373
create().flatMap(connection -> Mono.from(connection.createStatement(TDL).execute())

r2dbc-mysql/src/test/java/io/asyncer/r2dbc/mysql/MySqlTestKitSupport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package io.asyncer.r2dbc.mysql;
1818

1919
import com.zaxxer.hikari.HikariDataSource;
20+
import io.asyncer.r2dbc.mysql.internal.util.TestContainerExtension;
2021
import io.r2dbc.spi.test.TestKit;
22+
import org.junit.jupiter.api.extension.ExtendWith;
2123
import org.springframework.jdbc.core.JdbcTemplate;
2224

2325
import java.time.Duration;
@@ -26,6 +28,7 @@
2628
/**
2729
* Base class considers integration tests of {@link TestKit}.
2830
*/
31+
@ExtendWith(TestContainerExtension.class)
2932
abstract class MySqlTestKitSupport implements TestKit<String> {
3033

3134
private final MySqlConnectionFactory connectionFactory;

r2dbc-mysql/src/test/java/io/asyncer/r2dbc/mysql/QueryIntegrationTestSupport.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void set() {
286286
EnumSet.of(EnumData.ONE, EnumData.THREE));
287287
}
288288

289-
@DisabledIf("envIsLessThanMySql57OrMariaDb102")
289+
@DisabledIf("envIsLessThanMySql578OrMariaDb102")
290290
@Test
291291
void json() {
292292
testType(String.class, false, "JSON", null, "{\"data\": 1}", "[\"data\", 1]", "1", "null",
@@ -576,7 +576,7 @@ void insertOnDuplicate() {
576576
/**
577577
* ref: <a href="https://github.com/asyncer-io/r2dbc-mysql/issues/91">Issue 91</a>
578578
*/
579-
@DisabledIf("envIsLessThanMySql57OrMariaDb102")
579+
@DisabledIf("envIsLessThanMySql578OrMariaDb102")
580580
@Test
581581
void testUnionQueryWithJsonColumnDecodedAsString() {
582582
complete(connection ->
@@ -619,11 +619,18 @@ void testUnionQueryWithJsonColumnDecodedAsString() {
619619
@Test
620620
@DisabledIf("envIsLessThanMySql574OrMariaDb1011")
621621
void setStatementTimeoutTest() {
622-
final String sql = "SELECT 1 WHERE SLEEP(1) > 1";
622+
final String sql = "SELECT COUNT(*) " +
623+
"FROM information_schema.tables a cross join " +
624+
"information_schema.tables b cross join " +
625+
"information_schema.tables c cross join " +
626+
"information_schema.tables d cross join " +
627+
"information_schema.tables e";
628+
623629
timeout(connection -> connection.setStatementTimeout(Duration.ofMillis(500))
624630
.then(Mono.from(connection.createStatement(sql).execute()))
625631
.flatMapMany(result -> Mono.from(result.map((row, metadata) -> row.get(0, String.class))))
626632
.collectList()
633+
.doOnNext(System.out::println)
627634
);
628635
}
629636

0 commit comments

Comments
 (0)