Skip to content

Commit a1987ee

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

File tree

25 files changed

+477
-342
lines changed

25 files changed

+477
-342
lines changed

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

+1-6
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/0x00007fe21bdf9c28"},
3232
"name":"[Lcom.zaxxer.hikari.util.ConcurrentBag$IConcurrentBagEntry;"
3333
},
3434
{
@@ -749,11 +749,6 @@
749749
"queryAllPublicConstructors":true,
750750
"methods":[{"name":"<init>","parameterTypes":[] }, {"name":"add","parameterTypes":["long"] }, {"name":"sum","parameterTypes":[] }]
751751
},
752-
{
753-
"condition":{"typeReachable":"org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask"},
754-
"name":"java.util.concurrent.atomic.Striped64$Cell",
755-
"fields":[{"name":"value"}]
756-
},
757752
{
758753
"condition":{"typeReachable":"org.apache.shardingsphere.infra.expr.groovy.GroovyInlineExpressionParser"},
759754
"name":"java.util.function.DoubleFunction",

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/0x00007fe21bca2710"},
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

+34-27
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
import org.apache.curator.framework.CuratorFrameworkFactory;
2424
import org.apache.curator.retry.ExponentialBackoffRetry;
2525
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
26+
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
27+
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
28+
import org.apache.shardingsphere.mode.manager.ContextManager;
2629
import org.apache.shardingsphere.test.natived.commons.TestShardingService;
2730
import org.awaitility.Awaitility;
28-
import org.junit.jupiter.api.AfterAll;
29-
import org.junit.jupiter.api.BeforeAll;
31+
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.BeforeEach;
3033
import org.junit.jupiter.api.Test;
3134
import org.junit.jupiter.api.condition.EnabledInNativeImage;
3235
import org.testcontainers.containers.GenericContainer;
@@ -58,53 +61,57 @@
5861
@Testcontainers
5962
class ClickHouseTest {
6063

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

6366
@Container
64-
private static final GenericContainer<?> CLICKHOUSE_KEEPER_CONTAINER = new GenericContainer<>("clickhouse/clickhouse-keeper:24.11.1.2557")
67+
private final GenericContainer<?> clickhouseKeeperContainer = new GenericContainer<>("clickhouse/clickhouse-keeper:24.11.1.2557")
6568
.withCopyFileToContainer(
6669
MountableFile.forHostPath(Paths.get("src/test/resources/test-native/xml/keeper_config.xml").toAbsolutePath()),
6770
"/etc/clickhouse-keeper/keeper_config.xml")
68-
.withNetwork(NETWORK)
71+
.withNetwork(network)
6972
.withExposedPorts(9181)
7073
.withNetworkAliases("clickhouse-keeper-01");
7174

7275
@Container
73-
public static final GenericContainer<?> CONTAINER = new GenericContainer<>("clickhouse/clickhouse-server:24.11.1.2557")
76+
private final GenericContainer<?> container = new GenericContainer<>("clickhouse/clickhouse-server:24.11.1.2557")
7477
.withCopyFileToContainer(
7578
MountableFile.forHostPath(Paths.get("src/test/resources/test-native/xml/transactions.xml").toAbsolutePath()),
7679
"/etc/clickhouse-server/config.d/transactions.xml")
77-
.withNetwork(NETWORK)
80+
.withNetwork(network)
7881
.withExposedPorts(8123)
79-
.dependsOn(CLICKHOUSE_KEEPER_CONTAINER);
82+
.dependsOn(clickhouseKeeperContainer);
8083

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

83-
private static DataSource logicDataSource;
86+
private DataSource logicDataSource;
8487

8588
private String jdbcUrlPrefix;
8689

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()));
90+
@BeforeEach
91+
void beforeEach() {
92+
assertThat(System.getProperty(systemPropKeyPrefix + "ds0.jdbc-url"), is(nullValue()));
93+
assertThat(System.getProperty(systemPropKeyPrefix + "ds1.jdbc-url"), is(nullValue()));
94+
assertThat(System.getProperty(systemPropKeyPrefix + "ds2.jdbc-url"), is(nullValue()));
9295
}
9396

94-
@AfterAll
95-
static void afterAll() throws SQLException {
97+
@AfterEach
98+
void afterEach() throws SQLException {
9699
try (Connection connection = logicDataSource.getConnection()) {
97-
connection.unwrap(ShardingSphereConnection.class).getContextManager().close();
100+
ContextManager contextManager = connection.unwrap(ShardingSphereConnection.class).getContextManager();
101+
for (StorageUnit each : contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).values()) {
102+
each.getDataSource().unwrap(HikariDataSource.class).close();
103+
}
104+
contextManager.close();
98105
}
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");
106+
network.close();
107+
System.clearProperty(systemPropKeyPrefix + "ds0.jdbc-url");
108+
System.clearProperty(systemPropKeyPrefix + "ds1.jdbc-url");
109+
System.clearProperty(systemPropKeyPrefix + "ds2.jdbc-url");
103110
}
104111

105112
@Test
106113
void assertShardingInLocalTransactions() throws SQLException {
107-
jdbcUrlPrefix = "jdbc:ch://localhost:" + CONTAINER.getMappedPort(8123) + "/";
114+
jdbcUrlPrefix = "jdbc:ch://localhost:" + container.getMappedPort(8123) + "/";
108115
logicDataSource = createDataSource();
109116
TestShardingService testShardingService = new TestShardingService(logicDataSource);
110117
testShardingService.processSuccessInClickHouse();
@@ -118,7 +125,7 @@ private Connection openConnection(final String databaseName) throws SQLException
118125
}
119126

120127
private DataSource createDataSource() throws SQLException {
121-
String connectionString = CLICKHOUSE_KEEPER_CONTAINER.getHost() + ":" + CLICKHOUSE_KEEPER_CONTAINER.getMappedPort(9181);
128+
String connectionString = clickhouseKeeperContainer.getHost() + ":" + clickhouseKeeperContainer.getMappedPort(9181);
122129
Awaitility.await().atMost(Duration.ofMinutes(1L)).ignoreExceptions().until(() -> {
123130
try (
124131
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectionString)
@@ -139,9 +146,9 @@ private DataSource createDataSource() throws SQLException {
139146
HikariConfig config = new HikariConfig();
140147
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver");
141148
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");
149+
System.setProperty(systemPropKeyPrefix + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0?transactionSupport=true");
150+
System.setProperty(systemPropKeyPrefix + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1?transactionSupport=true");
151+
System.setProperty(systemPropKeyPrefix + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2?transactionSupport=true");
145152
return new HikariDataSource(config);
146153
}
147154

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

+36-29
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
import com.zaxxer.hikari.HikariConfig;
2121
import com.zaxxer.hikari.HikariDataSource;
2222
import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
23+
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
24+
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
25+
import org.apache.shardingsphere.mode.manager.ContextManager;
2326
import org.apache.shardingsphere.test.natived.commons.TestShardingService;
2427
import org.awaitility.Awaitility;
2528
import org.firebirdsql.management.FBManager;
2629
import org.firebirdsql.management.PageSizeConstants;
27-
import org.junit.jupiter.api.AfterAll;
28-
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.AfterEach;
31+
import org.junit.jupiter.api.BeforeEach;
2932
import org.junit.jupiter.api.Test;
3033
import org.junit.jupiter.api.condition.EnabledInNativeImage;
3134
import org.testcontainers.containers.GenericContainer;
@@ -47,46 +50,50 @@
4750
@Testcontainers
4851
class FirebirdTest {
4952

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

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

5457
@SuppressWarnings("resource")
5558
@Container
56-
private static final GenericContainer<?> CONTAINER = new GenericContainer<>("ghcr.io/fdcastel/firebird:5.0.1")
57-
.withEnv("FIREBIRD_ROOT_PASSWORD", PASSWORD)
59+
private final GenericContainer<?> container = new GenericContainer<>("ghcr.io/fdcastel/firebird:5.0.1")
60+
.withEnv("FIREBIRD_ROOT_PASSWORD", password)
5861
.withEnv("FIREBIRD_USER", "alice")
59-
.withEnv("FIREBIRD_PASSWORD", PASSWORD)
62+
.withEnv("FIREBIRD_PASSWORD", password)
6063
.withEnv("FIREBIRD_DATABASE", "mirror.fdb")
6164
.withEnv("FIREBIRD_DATABASE_DEFAULT_CHARSET", "UTF8")
6265
.withExposedPorts(3050);
6366

64-
private static DataSource logicDataSource;
67+
private DataSource logicDataSource;
6568

6669
private String jdbcUrlPrefix;
6770

6871
private TestShardingService testShardingService;
6972

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()));
73+
@BeforeEach
74+
void beforeEach() {
75+
assertThat(System.getProperty(systemPropKeyPrefix + "ds0.jdbc-url"), is(nullValue()));
76+
assertThat(System.getProperty(systemPropKeyPrefix + "ds1.jdbc-url"), is(nullValue()));
77+
assertThat(System.getProperty(systemPropKeyPrefix + "ds2.jdbc-url"), is(nullValue()));
7578
}
7679

77-
@AfterAll
78-
static void afterAll() throws SQLException {
80+
@AfterEach
81+
void afterEach() throws SQLException {
7982
try (Connection connection = logicDataSource.getConnection()) {
80-
connection.unwrap(ShardingSphereConnection.class).getContextManager().close();
83+
ContextManager contextManager = connection.unwrap(ShardingSphereConnection.class).getContextManager();
84+
for (StorageUnit each : contextManager.getStorageUnits(DefaultDatabase.LOGIC_NAME).values()) {
85+
each.getDataSource().unwrap(HikariDataSource.class).close();
86+
}
87+
contextManager.close();
8188
}
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");
89+
System.clearProperty(systemPropKeyPrefix + "ds0.jdbc-url");
90+
System.clearProperty(systemPropKeyPrefix + "ds1.jdbc-url");
91+
System.clearProperty(systemPropKeyPrefix + "ds2.jdbc-url");
8592
}
8693

8794
@Test
8895
void assertShardingInLocalTransactions() throws Exception {
89-
jdbcUrlPrefix = "jdbc:firebird://localhost:" + CONTAINER.getMappedPort(3050) + "//var/lib/firebird/data/";
96+
jdbcUrlPrefix = "jdbc:firebird://localhost:" + container.getMappedPort(3050) + "//var/lib/firebird/data/";
9097
logicDataSource = createDataSource();
9198
testShardingService = new TestShardingService(logicDataSource);
9299
initEnvironment();
@@ -109,7 +116,7 @@ private void initEnvironment() throws SQLException {
109116
private Connection openConnection() throws SQLException {
110117
Properties props = new Properties();
111118
props.setProperty("user", "alice");
112-
props.setProperty("password", PASSWORD);
119+
props.setProperty("password", password);
113120
return DriverManager.getConnection(jdbcUrlPrefix + "mirror.fdb", props);
114121
}
115122

@@ -131,22 +138,22 @@ private DataSource createDataSource() throws Exception {
131138
try (FBManager fbManager = new FBManager()) {
132139
fbManager.setServer("localhost");
133140
fbManager.setUserName("alice");
134-
fbManager.setPassword(PASSWORD);
141+
fbManager.setPassword(password);
135142
fbManager.setFileName("/var/lib/firebird/data/mirror.fdb");
136143
fbManager.setPageSize(PageSizeConstants.SIZE_16K);
137144
fbManager.setDefaultCharacterSet("UTF8");
138-
fbManager.setPort(CONTAINER.getMappedPort(3050));
145+
fbManager.setPort(container.getMappedPort(3050));
139146
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);
147+
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_0.fdb", "alice", password);
148+
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_1.fdb", "alice", password);
149+
fbManager.createDatabase("/var/lib/firebird/data/demo_ds_2.fdb", "alice", password);
143150
}
144151
HikariConfig config = new HikariConfig();
145152
config.setDriverClassName("org.apache.shardingsphere.driver.ShardingSphereDriver");
146153
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");
154+
System.setProperty(systemPropKeyPrefix + "ds0.jdbc-url", jdbcUrlPrefix + "demo_ds_0.fdb");
155+
System.setProperty(systemPropKeyPrefix + "ds1.jdbc-url", jdbcUrlPrefix + "demo_ds_1.fdb");
156+
System.setProperty(systemPropKeyPrefix + "ds2.jdbc-url", jdbcUrlPrefix + "demo_ds_2.fdb");
150157
return new HikariDataSource(config);
151158
}
152159
}

0 commit comments

Comments
 (0)