Skip to content

Commit 31052ed

Browse files
committed
Fix the connection leak problem caused by improper configuration of unit test lifecycle
1 parent 64ef5d5 commit 31052ed

File tree

25 files changed

+317
-317
lines changed

25 files changed

+317
-317
lines changed

infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/reflect-config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"name":"[Lcom.github.dockerjava.api.model.VolumesFrom;"
2929
},
3030
{
31-
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager$$Lambda/0x00007f258bdf9c28"},
31+
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager$$Lambda/0x00007f20dfdf0800"},
3232
"name":"[Lcom.zaxxer.hikari.util.ConcurrentBag$IConcurrentBagEntry;"
3333
},
3434
{

infra/reachability-metadata/src/main/resources/META-INF/native-image/org.apache.shardingsphere/generated-reachability-metadata/resource-config.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager"},
9292
"pattern":"\\QMETA-INF/services/com.clickhouse.client.ClickHouseClient\\E"
9393
}, {
94-
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager$$Lambda/0x00007f258bca2b10"},
94+
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.connection.DriverDatabaseConnectionManager$$Lambda/0x00007f20dfca2298"},
9595
"pattern":"\\QMETA-INF/services/com.clickhouse.client.ClickHouseClient\\E"
9696
}, {
9797
"condition":{"typeReachable":"org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource"},

test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/ClickHouseTest.java

+26-26
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
2626
import org.apache.shardingsphere.test.natived.commons.TestShardingService;
2727
import org.awaitility.Awaitility;
28-
import org.junit.jupiter.api.AfterAll;
29-
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
3030
import org.junit.jupiter.api.Test;
3131
import org.junit.jupiter.api.condition.EnabledInNativeImage;
3232
import org.testcontainers.containers.GenericContainer;
@@ -58,53 +58,53 @@
5858
@Testcontainers
5959
class ClickHouseTest {
6060

61-
private static final Network NETWORK = Network.newNetwork();
61+
private final Network network = Network.newNetwork();
6262

6363
@Container
64-
private static final GenericContainer<?> CLICKHOUSE_KEEPER_CONTAINER = new GenericContainer<>("clickhouse/clickhouse-keeper:24.11.1.2557")
64+
private final GenericContainer<?> clickhouseKeeperContainer = new GenericContainer<>("clickhouse/clickhouse-keeper:24.11.1.2557")
6565
.withCopyFileToContainer(
6666
MountableFile.forHostPath(Paths.get("src/test/resources/test-native/xml/keeper_config.xml").toAbsolutePath()),
6767
"/etc/clickhouse-keeper/keeper_config.xml")
68-
.withNetwork(NETWORK)
68+
.withNetwork(network)
6969
.withExposedPorts(9181)
7070
.withNetworkAliases("clickhouse-keeper-01");
7171

7272
@Container
73-
public static final GenericContainer<?> CONTAINER = new GenericContainer<>("clickhouse/clickhouse-server:24.11.1.2557")
73+
private final GenericContainer<?> container = new GenericContainer<>("clickhouse/clickhouse-server:24.11.1.2557")
7474
.withCopyFileToContainer(
7575
MountableFile.forHostPath(Paths.get("src/test/resources/test-native/xml/transactions.xml").toAbsolutePath()),
7676
"/etc/clickhouse-server/config.d/transactions.xml")
77-
.withNetwork(NETWORK)
77+
.withNetwork(network)
7878
.withExposedPorts(8123)
79-
.dependsOn(CLICKHOUSE_KEEPER_CONTAINER);
79+
.dependsOn(clickhouseKeeperContainer);
8080

81-
private static final String SYSTEM_PROP_KEY_PREFIX = "fixture.test-native.yaml.database.clickhouse.";
81+
private final String systemPropKeyPrefix = "fixture.test-native.yaml.database.clickhouse.";
8282

83-
private static DataSource logicDataSource;
83+
private DataSource logicDataSource;
8484

8585
private String jdbcUrlPrefix;
8686

87-
@BeforeAll
88-
static void beforeAll() {
89-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url"), is(nullValue()));
90-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url"), is(nullValue()));
91-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url"), is(nullValue()));
87+
@BeforeEach
88+
void beforeEach() {
89+
assertThat(System.getProperty(systemPropKeyPrefix + "ds0.jdbc-url"), is(nullValue()));
90+
assertThat(System.getProperty(systemPropKeyPrefix + "ds1.jdbc-url"), is(nullValue()));
91+
assertThat(System.getProperty(systemPropKeyPrefix + "ds2.jdbc-url"), is(nullValue()));
9292
}
9393

94-
@AfterAll
95-
static void afterAll() throws SQLException {
94+
@AfterEach
95+
void afterEach() throws SQLException {
9696
try (Connection connection = logicDataSource.getConnection()) {
9797
connection.unwrap(ShardingSphereConnection.class).getContextManager().close();
9898
}
99-
NETWORK.close();
100-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url");
101-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url");
102-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url");
99+
network.close();
100+
System.clearProperty(systemPropKeyPrefix + "ds0.jdbc-url");
101+
System.clearProperty(systemPropKeyPrefix + "ds1.jdbc-url");
102+
System.clearProperty(systemPropKeyPrefix + "ds2.jdbc-url");
103103
}
104104

105105
@Test
106106
void assertShardingInLocalTransactions() throws SQLException {
107-
jdbcUrlPrefix = "jdbc:ch://localhost:" + CONTAINER.getMappedPort(8123) + "/";
107+
jdbcUrlPrefix = "jdbc:ch://localhost:" + container.getMappedPort(8123) + "/";
108108
logicDataSource = createDataSource();
109109
TestShardingService testShardingService = new TestShardingService(logicDataSource);
110110
testShardingService.processSuccessInClickHouse();
@@ -118,7 +118,7 @@ private Connection openConnection(final String databaseName) throws SQLException
118118
}
119119

120120
private DataSource createDataSource() throws SQLException {
121-
String connectionString = CLICKHOUSE_KEEPER_CONTAINER.getHost() + ":" + CLICKHOUSE_KEEPER_CONTAINER.getMappedPort(9181);
121+
String connectionString = clickhouseKeeperContainer.getHost() + ":" + clickhouseKeeperContainer.getMappedPort(9181);
122122
Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptions().until(() -> {
123123
try (
124124
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectionString)
@@ -139,9 +139,9 @@ private DataSource createDataSource() throws SQLException {
139139
HikariConfig config = new HikariConfig();
140140
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver");
141141
config.setJdbcUrl("jdbc:shardingsphere:classpath:test-native/yaml/jdbc/databases/clickhouse.yaml?placeholder-type=system_props");
142-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0?transactionSupport=true");
143-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1?transactionSupport=true");
144-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2?transactionSupport=true");
142+
System.setProperty(systemPropKeyPrefix + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0?transactionSupport=true");
143+
System.setProperty(systemPropKeyPrefix + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1?transactionSupport=true");
144+
System.setProperty(systemPropKeyPrefix + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2?transactionSupport=true");
145145
return new HikariDataSource(config);
146146
}
147147

test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/FirebirdTest.java

+28-28
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import org.awaitility.Awaitility;
2525
import org.firebirdsql.management.FBManager;
2626
import org.firebirdsql.management.PageSizeConstants;
27-
import org.junit.jupiter.api.AfterAll;
28-
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.AfterEach;
28+
import org.junit.jupiter.api.BeforeEach;
2929
import org.junit.jupiter.api.Test;
3030
import org.junit.jupiter.api.condition.EnabledInNativeImage;
3131
import org.testcontainers.containers.GenericContainer;
@@ -47,46 +47,46 @@
4747
@Testcontainers
4848
class FirebirdTest {
4949

50-
private static final String SYSTEM_PROP_KEY_PREFIX = "fixture.test-native.yaml.database.firebird.";
50+
private final String systemPropKeyPrefix = "fixture.test-native.yaml.database.firebird.";
5151

52-
private static final String PASSWORD = "masterkey";
52+
private final String password = "masterkey";
5353

5454
@SuppressWarnings("resource")
5555
@Container
56-
private static final GenericContainer<?> CONTAINER = new GenericContainer<>("ghcr.io/fdcastel/firebird:5.0.1")
57-
.withEnv("FIREBIRD_ROOT_PASSWORD", PASSWORD)
56+
private final GenericContainer<?> container = new GenericContainer<>("ghcr.io/fdcastel/firebird:5.0.1")
57+
.withEnv("FIREBIRD_ROOT_PASSWORD", password)
5858
.withEnv("FIREBIRD_USER", "alice")
59-
.withEnv("FIREBIRD_PASSWORD", PASSWORD)
59+
.withEnv("FIREBIRD_PASSWORD", password)
6060
.withEnv("FIREBIRD_DATABASE", "mirror.fdb")
6161
.withEnv("FIREBIRD_DATABASE_DEFAULT_CHARSET", "UTF8")
6262
.withExposedPorts(3050);
6363

64-
private static DataSource logicDataSource;
64+
private DataSource logicDataSource;
6565

6666
private String jdbcUrlPrefix;
6767

6868
private TestShardingService testShardingService;
6969

70-
@BeforeAll
71-
static void beforeAll() {
72-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url"), is(nullValue()));
73-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url"), is(nullValue()));
74-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url"), is(nullValue()));
70+
@BeforeEach
71+
void beforeEach() {
72+
assertThat(System.getProperty(systemPropKeyPrefix + "ds0.jdbc-url"), is(nullValue()));
73+
assertThat(System.getProperty(systemPropKeyPrefix + "ds1.jdbc-url"), is(nullValue()));
74+
assertThat(System.getProperty(systemPropKeyPrefix + "ds2.jdbc-url"), is(nullValue()));
7575
}
7676

77-
@AfterAll
78-
static void afterAll() throws SQLException {
77+
@AfterEach
78+
void afterEach() throws SQLException {
7979
try (Connection connection = logicDataSource.getConnection()) {
8080
connection.unwrap(ShardingSphereConnection.class).getContextManager().close();
8181
}
82-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url");
83-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url");
84-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url");
82+
System.clearProperty(systemPropKeyPrefix + "ds0.jdbc-url");
83+
System.clearProperty(systemPropKeyPrefix + "ds1.jdbc-url");
84+
System.clearProperty(systemPropKeyPrefix + "ds2.jdbc-url");
8585
}
8686

8787
@Test
8888
void assertShardingInLocalTransactions() throws Exception {
89-
jdbcUrlPrefix = "jdbc:firebird://localhost:" + CONTAINER.getMappedPort(3050) + "//var/lib/firebird/data/";
89+
jdbcUrlPrefix = "jdbc:firebird://localhost:" + container.getMappedPort(3050) + "//var/lib/firebird/data/";
9090
logicDataSource = createDataSource();
9191
testShardingService = new TestShardingService(logicDataSource);
9292
initEnvironment();
@@ -109,7 +109,7 @@ private void initEnvironment() throws SQLException {
109109
private Connection openConnection() throws SQLException {
110110
Properties props = new Properties();
111111
props.setProperty("user", "alice");
112-
props.setProperty("password", PASSWORD);
112+
props.setProperty("password", password);
113113
return DriverManager.getConnection(jdbcUrlPrefix + "mirror.fdb", props);
114114
}
115115

@@ -131,22 +131,22 @@ private DataSource createDataSource() throws Exception {
131131
try (FBManager fbManager = new FBManager()) {
132132
fbManager.setServer("localhost");
133133
fbManager.setUserName("alice");
134-
fbManager.setPassword(PASSWORD);
134+
fbManager.setPassword(password);
135135
fbManager.setFileName("/var/lib/firebird/data/mirror.fdb");
136136
fbManager.setPageSize(PageSizeConstants.SIZE_16K);
137137
fbManager.setDefaultCharacterSet("UTF8");
138-
fbManager.setPort(CONTAINER.getMappedPort(3050));
138+
fbManager.setPort(container.getMappedPort(3050));
139139
fbManager.start();
140-
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_0.fdb", "alice", PASSWORD);
141-
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_1.fdb", "alice", PASSWORD);
142-
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_2.fdb", "alice", PASSWORD);
140+
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_0.fdb", "alice", password);
141+
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_1.fdb", "alice", password);
142+
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_2.fdb", "alice", password);
143143
}
144144
HikariConfig config = new HikariConfig();
145145
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver");
146146
config.setJdbcUrl("jdbc:shardingsphere:classpath:test-native/yaml/jdbc/databases/firebird.yaml?placeholder-type=system_props");
147-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0.fdb");
148-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1.fdb");
149-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2.fdb");
147+
System.setProperty(systemPropKeyPrefix + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0.fdb");
148+
System.setProperty(systemPropKeyPrefix + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1.fdb");
149+
System.setProperty(systemPropKeyPrefix + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2.fdb");
150150
return new HikariDataSource(config);
151151
}
152152
}

test/native/src/test/java/org/apache/shardingsphere/test/natived/jdbc/databases/MySQLTest.java

+22-22
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
2424
import org.apache.shardingsphere.test.natived.commons.TestShardingService;
2525
import org.awaitility.Awaitility;
26-
import org.junit.jupiter.api.AfterAll;
27-
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
2929
import org.junit.jupiter.api.condition.EnabledInNativeImage;
3030
import org.testcontainers.containers.GenericContainer;
@@ -51,42 +51,42 @@
5151
@Testcontainers
5252
class MySQLTest {
5353

54-
private static final String SYSTEM_PROP_KEY_PREFIX = "fixture.test-native.yaml.database.mysql.";
54+
private final String systemPropKeyPrefix = "fixture.test-native.yaml.database.mysql.";
5555

56-
private static final String PASSWORD = "example";
56+
private final String password = "example";
5757

5858
@SuppressWarnings("resource")
5959
@Container
60-
private static final GenericContainer<?> CONTAINER = new GenericContainer<>("mysql:9.1.0-oraclelinux9")
61-
.withEnv("MYSQL_ROOT_PASSWORD", PASSWORD)
60+
private final GenericContainer<?> container = new GenericContainer<>("mysql:9.1.0-oraclelinux9")
61+
.withEnv("MYSQL_ROOT_PASSWORD", password)
6262
.withExposedPorts(3306);
6363

64-
private static DataSource logicDataSource;
64+
private DataSource logicDataSource;
6565

6666
private String jdbcUrlPrefix;
6767

6868
private TestShardingService testShardingService;
6969

70-
@BeforeAll
71-
static void beforeAll() {
72-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url"), is(nullValue()));
73-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url"), is(nullValue()));
74-
assertThat(System.getProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url"), is(nullValue()));
70+
@BeforeEach
71+
void beforeEach() {
72+
assertThat(System.getProperty(systemPropKeyPrefix + "ds0.jdbc-url"), is(nullValue()));
73+
assertThat(System.getProperty(systemPropKeyPrefix + "ds1.jdbc-url"), is(nullValue()));
74+
assertThat(System.getProperty(systemPropKeyPrefix + "ds2.jdbc-url"), is(nullValue()));
7575
}
7676

77-
@AfterAll
78-
static void afterAll() throws SQLException {
77+
@AfterEach
78+
void afterEach() throws SQLException {
7979
try (Connection connection = logicDataSource.getConnection()) {
8080
connection.unwrap(ShardingSphereConnection.class).getContextManager().close();
8181
}
82-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url");
83-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url");
84-
System.clearProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url");
82+
System.clearProperty(systemPropKeyPrefix + "ds0.jdbc-url");
83+
System.clearProperty(systemPropKeyPrefix + "ds1.jdbc-url");
84+
System.clearProperty(systemPropKeyPrefix + "ds2.jdbc-url");
8585
}
8686

8787
@Test
8888
void assertShardingInLocalTransactions() throws SQLException {
89-
jdbcUrlPrefix = "jdbc:mysql://localhost:" + CONTAINER.getMappedPort(3306) + "/";
89+
jdbcUrlPrefix = "jdbc:mysql://localhost:" + container.getMappedPort(3306) + "/";
9090
logicDataSource = createDataSource();
9191
testShardingService = new TestShardingService(logicDataSource);
9292
initEnvironment();
@@ -106,7 +106,7 @@ private void initEnvironment() throws SQLException {
106106
private Connection openConnection() throws SQLException {
107107
Properties props = new Properties();
108108
props.setProperty("user", "root");
109-
props.setProperty("password", PASSWORD);
109+
props.setProperty("password", password);
110110
return DriverManager.getConnection(jdbcUrlPrefix, props);
111111
}
112112

@@ -126,9 +126,9 @@ private DataSource createDataSource() throws SQLException {
126126
HikariConfig config = new HikariConfig();
127127
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver");
128128
config.setJdbcUrl("jdbc:shardingsphere:classpath:test-native/yaml/jdbc/databases/mysql.yaml?placeholder-type=system_props");
129-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0");
130-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1");
131-
System.setProperty(SYSTEM_PROP_KEY_PREFIX + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2");
129+
System.setProperty(systemPropKeyPrefix + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0");
130+
System.setProperty(systemPropKeyPrefix + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1");
131+
System.setProperty(systemPropKeyPrefix + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2");
132132
return new HikariDataSource(config);
133133
}
134134
}

0 commit comments

Comments
 (0)