You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/08/31 13:50:53 UTC

[shardingsphere] branch master updated: Fixing connection leaks from integration tests (#12134)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ea398a  Fixing connection leaks from integration tests (#12134)
0ea398a is described below

commit 0ea398adc170f5144f9b2dc2357993e3cc4f73c4
Author: Jieker <ji...@163.com>
AuthorDate: Tue Aug 31 21:50:18 2021 +0800

    Fixing connection leaks from integration tests (#12134)
    
    * Fixing connection leaks
    
    * fix ci
---
 ...ronmentUtil.java => DataSourceEnvironment.java} |  4 ++--
 .../junit/compose/GovernanceContainerCompose.java  |  7 ++++++-
 .../adapter/impl/ShardingSphereProxyContainer.java |  6 +++---
 .../storage/ShardingSphereStorageContainer.java    |  4 ++--
 .../junit/container/storage/impl/H2Container.java  |  4 ++--
 .../container/storage/impl/MySQLContainer.java     |  4 ++--
 .../storage/impl/PostgreSQLContainer.java          |  6 +++---
 .../test/integration/engine/it/BaseITCase.java     | 16 ++++++++++++++-
 .../sharding_governance/unchanged_table.xml        | 23 ++++++++++++++++++++++
 9 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/env/datasource/DataSourceEnvironmentUtil.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/env/DataSourceEnvironment.java
similarity index 97%
rename from shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/env/datasource/DataSourceEnvironmentUtil.java
rename to shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/env/DataSourceEnvironment.java
index 28b80a0..a3a3e6d 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/env/datasource/DataSourceEnvironmentUtil.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/env/DataSourceEnvironment.java
@@ -15,12 +15,12 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.test.integration.env.datasource;
+package org.apache.shardingsphere.test.integration.env;
 
 /**
  * Data source environment.
  */
-public final class DataSourceEnvironmentUtil {
+public final class DataSourceEnvironment {
 
     /**
      * Get driver class name.
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/compose/GovernanceContainerCompose.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/compose/GovernanceContainerCompose.java
index 0a508a3..909ed89 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/compose/GovernanceContainerCompose.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/compose/GovernanceContainerCompose.java
@@ -39,9 +39,12 @@ public final class GovernanceContainerCompose extends ContainerCompose {
     @Getter
     private final ShardingSphereAdapterContainer adapterContainer;
 
+    @Getter
     private final ShardingSphereAdapterContainer adapterContainerForReader;
 
     private final ZookeeperContainer zookeeperContainer;
+
+    private final Map<String, DataSource> result = new HashMap<>(2, 1);
     
     public GovernanceContainerCompose(final String clusterName, final ParameterizedArray parameterizedArray) {
         super(clusterName, parameterizedArray);
@@ -66,7 +69,9 @@ public final class GovernanceContainerCompose extends ContainerCompose {
     
     @Override
     public Map<String, DataSource> getDataSourceMap() {
-        Map<String, DataSource> result = new HashMap<>(2, 1);
+        if (!result.isEmpty()) {
+            return result;
+        }
         String serverLists = zookeeperContainer.getServerLists();
         result.put("adapterForWriter", adapterContainer.getGovernanceDataSource(serverLists));
         result.put("adapterForReader", adapterContainerForReader.getGovernanceDataSource(serverLists));
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/adapter/impl/ShardingSphereProxyContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/adapter/impl/ShardingSphereProxyContainer.java
index 40ce85e..747a283 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/adapter/impl/ShardingSphereProxyContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/adapter/impl/ShardingSphereProxyContainer.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.test.integration.junit.container.adapter.impl;
 import com.zaxxer.hikari.HikariConfig;
 import com.zaxxer.hikari.HikariDataSource;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.test.integration.env.datasource.DataSourceEnvironmentUtil;
+import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
 import org.apache.shardingsphere.test.integration.junit.container.adapter.ShardingSphereAdapterContainer;
 import org.apache.shardingsphere.test.integration.junit.param.model.ParameterizedArray;
 import org.testcontainers.containers.BindMode;
@@ -124,8 +124,8 @@ public final class ShardingSphereProxyContainer extends ShardingSphereAdapterCon
     private DataSource createDataSource() {
         String databaseType = getParameterizedArray().getDatabaseType().getName();
         HikariConfig result = new HikariConfig();
-        result.setDriverClassName(DataSourceEnvironmentUtil.getDriverClassName(databaseType));
-        result.setJdbcUrl(DataSourceEnvironmentUtil.getURL(databaseType, getHost(), getMappedPort(3307), getParameterizedArray().getScenario()));
+        result.setDriverClassName(DataSourceEnvironment.getDriverClassName(databaseType));
+        result.setJdbcUrl(DataSourceEnvironment.getURL(databaseType, getHost(), getMappedPort(3307), getParameterizedArray().getScenario()));
         result.setUsername(getAuthentication().getUsername());
         result.setPassword(getAuthentication().getPassword());
         result.setMaximumPoolSize(2);
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/ShardingSphereStorageContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/ShardingSphereStorageContainer.java
index bcd014a..e3c9600 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/ShardingSphereStorageContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/ShardingSphereStorageContainer.java
@@ -23,7 +23,7 @@ import lombok.Getter;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.test.integration.env.database.DatabaseEnvironmentManager;
-import org.apache.shardingsphere.test.integration.env.datasource.DataSourceEnvironmentUtil;
+import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
 import org.apache.shardingsphere.test.integration.junit.container.ShardingSphereContainer;
 import org.apache.shardingsphere.test.integration.junit.param.model.ParameterizedArray;
 import org.testcontainers.containers.BindMode;
@@ -99,7 +99,7 @@ public abstract class ShardingSphereStorageContainer extends ShardingSphereConta
     }
     
     protected String getDriverClassName() {
-        return DataSourceEnvironmentUtil.getDriverClassName(databaseType.getName());
+        return DataSourceEnvironment.getDriverClassName(databaseType.getName());
     }
     
     protected abstract String getUrl(String dataSourceName);
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/H2Container.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/H2Container.java
index 982d846..c2af863 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/H2Container.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/H2Container.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.test.integration.junit.container.storage.impl;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
 import org.apache.shardingsphere.test.integration.env.EnvironmentPath;
-import org.apache.shardingsphere.test.integration.env.datasource.DataSourceEnvironmentUtil;
+import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
 import org.apache.shardingsphere.test.integration.junit.container.storage.ShardingSphereStorageContainer;
 import org.apache.shardingsphere.test.integration.junit.param.model.ParameterizedArray;
 import org.h2.tools.RunScript;
@@ -73,7 +73,7 @@ public final class H2Container extends ShardingSphereStorageContainer {
     
     @Override
     protected String getUrl(final String dataSourceName) {
-        return DataSourceEnvironmentUtil.getURL("H2", null, 0, Objects.isNull(dataSourceName) ? "test_db" : dataSourceName);
+        return DataSourceEnvironment.getURL("H2", null, 0, Objects.isNull(dataSourceName) ? "test_db" : dataSourceName);
     }
     
     @Override
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/MySQLContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/MySQLContainer.java
index d90f816..b543d9c 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/MySQLContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/MySQLContainer.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.test.integration.junit.container.storage.impl;
 
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.test.integration.env.datasource.DataSourceEnvironmentUtil;
+import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
 import org.apache.shardingsphere.test.integration.junit.container.storage.ShardingSphereStorageContainer;
 import org.apache.shardingsphere.test.integration.junit.param.model.ParameterizedArray;
 
@@ -49,7 +49,7 @@ public final class MySQLContainer extends ShardingSphereStorageContainer {
     
     @Override
     protected String getUrl(final String dataSourceName) {
-        return DataSourceEnvironmentUtil.getURL("MySQL", getHost(), getPort(), dataSourceName);
+        return DataSourceEnvironment.getURL("MySQL", getHost(), getPort(), dataSourceName);
     }
     
     @Override
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/PostgreSQLContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/PostgreSQLContainer.java
index 7b4ac77..eef5d62 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/PostgreSQLContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-fixture/src/test/java/org/apache/shardingsphere/test/integration/junit/container/storage/impl/PostgreSQLContainer.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.test.integration.junit.container.storage.impl;
 
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
-import org.apache.shardingsphere.test.integration.env.datasource.DataSourceEnvironmentUtil;
+import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
 import org.apache.shardingsphere.test.integration.junit.container.storage.ShardingSphereStorageContainer;
 import org.apache.shardingsphere.test.integration.junit.param.model.ParameterizedArray;
 import org.postgresql.util.PSQLException;
@@ -51,7 +51,7 @@ public final class PostgreSQLContainer extends ShardingSphereStorageContainer {
         // TODO logic need prefect
         while (time++ < 20) {
             Class.forName(getDriverClassName());
-            try (Connection ignored = DriverManager.getConnection(DataSourceEnvironmentUtil.getURL("PostgreSQL", getHost(), getPort()), getUsername(), getPassword())) {
+            try (Connection ignored = DriverManager.getConnection(DataSourceEnvironment.getURL("PostgreSQL", getHost(), getPort()), getUsername(), getPassword())) {
                 break;
             } catch (PSQLException ex) {
                 Thread.sleep(1000);
@@ -61,7 +61,7 @@ public final class PostgreSQLContainer extends ShardingSphereStorageContainer {
 
     @Override
     protected String getUrl(final String dataSourceName) {
-        return DataSourceEnvironmentUtil.getURL("PostgreSQL", getHost(), getPort(), dataSourceName);
+        return DataSourceEnvironment.getURL("PostgreSQL", getHost(), getPort(), dataSourceName);
     }
     
     @Override
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java
index 8d674d5..479ba1b 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java
@@ -24,6 +24,7 @@ import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.test.integration.cases.SQLCommandType;
 import org.apache.shardingsphere.test.integration.cases.assertion.IntegrationTestCase;
 import org.apache.shardingsphere.test.integration.junit.compose.ContainerCompose;
+import org.apache.shardingsphere.test.integration.junit.compose.GovernanceContainerCompose;
 import org.apache.shardingsphere.test.integration.junit.container.adapter.ShardingSphereAdapterContainer;
 import org.apache.shardingsphere.test.integration.junit.container.storage.ShardingSphereStorageContainer;
 import org.apache.shardingsphere.test.integration.junit.param.model.ParameterizedArray;
@@ -35,6 +36,7 @@ import org.junit.runner.RunWith;
 
 import javax.sql.DataSource;
 import java.text.ParseException;
+import java.util.Map;
 import java.util.TimeZone;
 
 @Getter(AccessLevel.PROTECTED)
@@ -59,8 +61,12 @@ public abstract class BaseITCase {
     private final ShardingSphereStorageContainer storageContainer;
     
     private final ShardingSphereAdapterContainer adapterContainer;
+
+    private Map<String, DataSource> dataSourceMap;
     
     private DataSource targetDataSource;
+
+    private DataSource dataSourceForReader;
     
     static {
         TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
@@ -79,7 +85,11 @@ public abstract class BaseITCase {
     
     @Before
     public final void createDataSource() {
-        targetDataSource = compose.getDataSourceMap().get("adapterForWriter");
+        dataSourceMap = compose.getDataSourceMap();
+        targetDataSource = dataSourceMap.get("adapterForWriter");
+        if (compose instanceof GovernanceContainerCompose) {
+            dataSourceForReader = dataSourceMap.get("adapterForReader");
+        }
     }
     
     @After
@@ -87,6 +97,10 @@ public abstract class BaseITCase {
         if (targetDataSource instanceof ShardingSphereDataSource) {
             ((ShardingSphereDataSource) targetDataSource).getContextManager().close();
         }
+        if (dataSourceForReader != null) {
+            ((ShardingSphereDataSource) dataSourceForReader).getContextManager().close();
+            dataSourceMap.clear();
+        }
     }
     
     protected abstract String getSQL() throws ParseException;
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ddl/dataset/sharding_governance/unchanged_table.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ddl/dataset/sharding_governance/unchanged_table.xml
new file mode 100644
index 0000000..802a415
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ddl/dataset/sharding_governance/unchanged_table.xml
@@ -0,0 +1,23 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one or more
+  ~ contributor license agreements.  See the NOTICE file distributed with
+  ~ this work for additional information regarding copyright ownership.
+  ~ The ASF licenses this file to You under the Apache License, Version 2.0
+  ~ (the "License"); you may not use this file except in compliance with
+  ~ the License.  You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+
+<dataset>
+    <metadata table-name="t_order_details" data-nodes="governance_db.t_order_details">
+        <column name="id" type="integer" />
+        <column name="description" type="varchar" />
+    </metadata>
+</dataset>