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 2023/05/26 05:17:45 UTC

[shardingsphere] branch master updated: Fix occasional failure of DataSourcePoolDestroyerTest (#25905)

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

zhaojinchao 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 89aaebaec42 Fix occasional failure of DataSourcePoolDestroyerTest (#25905)
89aaebaec42 is described below

commit 89aaebaec4243da01c67b4cd123e9a4423c00f3e
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Fri May 26 13:17:30 2023 +0800

    Fix occasional failure of DataSourcePoolDestroyerTest (#25905)
    
    * Fix occasional failure of DataSourcePoolDestroyerTest
    
    * Fix ShowStorageUnitExecutorTest
---
 .../MockedDataSourcePoolActiveDetector.java        | 39 ++++++++++++++++++++++
 ...destroyer.detector.DataSourcePoolActiveDetector | 18 ++++++++++
 .../storage/unit/ShowStorageUnitExecutorTest.java  |  4 +--
 .../test/fixture/jdbc/MockedDataSource.java        | 16 +++++++--
 4 files changed, 72 insertions(+), 5 deletions(-)

diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/fixture/MockedDataSourcePoolActiveDetector.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/fixture/MockedDataSourcePoolActiveDetector.java
new file mode 100644
index 00000000000..3f3bd387ff9
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/fixture/MockedDataSourcePoolActiveDetector.java
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.infra.datasource.pool.destroyer.fixture;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.datasource.pool.destroyer.detector.DataSourcePoolActiveDetector;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public final class MockedDataSourcePoolActiveDetector implements DataSourcePoolActiveDetector {
+    
+    @SneakyThrows(SQLException.class)
+    @Override
+    public boolean containsActiveConnection(final DataSource dataSource) {
+        return !dataSource.unwrap(MockedDataSource.class).getOpenedConnections().isEmpty();
+    }
+    
+    @Override
+    public String getType() {
+        return MockedDataSource.class.getName();
+    }
+}
diff --git a/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.destroyer.detector.DataSourcePoolActiveDetector b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.destroyer.detector.DataSourcePoolActiveDetector
new file mode 100644
index 00000000000..260927a6ce7
--- /dev/null
+++ b/infra/common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.destroyer.detector.DataSourcePoolActiveDetector
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.datasource.pool.destroyer.fixture.MockedDataSourcePoolActiveDetector
diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutorTest.java
index a780af6d42d..f6344caa291 100644
--- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutorTest.java
+++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/storage/unit/ShowStorageUnitExecutorTest.java
@@ -129,7 +129,7 @@ class ShowStorageUnitExecutorTest {
             assertThat(data.getCell(9), is("100"));
             assertThat(data.getCell(10), is("10"));
             assertThat(data.getCell(11), is(""));
-            assertThat(data.getCell(12), is(""));
+            assertThat(data.getCell(12), is("{\"openedConnections\":[]}"));
             index++;
         }
     }
@@ -153,7 +153,7 @@ class ShowStorageUnitExecutorTest {
         assertThat(data.getCell(9), is("100"));
         assertThat(data.getCell(10), is("10"));
         assertThat(data.getCell(11), is(""));
-        assertThat(data.getCell(12), is(""));
+        assertThat(data.getCell(12), is("{\"openedConnections\":[]}"));
     }
     
     @Test
diff --git a/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDataSource.java b/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDataSource.java
index 9425e34fc94..fbd3c023b97 100644
--- a/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDataSource.java
+++ b/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDataSource.java
@@ -26,12 +26,15 @@ import javax.sql.DataSource;
 import java.io.PrintWriter;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Properties;
 import java.util.logging.Logger;
 
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -66,6 +69,8 @@ public final class MockedDataSource implements DataSource, AutoCloseable {
     @Setter(AccessLevel.NONE)
     private boolean closed;
     
+    private final Collection<Connection> openedConnections = new HashSet<>();
+    
     public MockedDataSource(final Connection connection) {
         this.connection = connection;
     }
@@ -79,6 +84,8 @@ public final class MockedDataSource implements DataSource, AutoCloseable {
         Connection result = mock(Connection.class, RETURNS_DEEP_STUBS);
         when(result.getMetaData().getURL()).thenReturn(url);
         when(result.createStatement(anyInt(), anyInt(), anyInt()).getConnection()).thenReturn(result);
+        doAnswer(invocation -> openedConnections.remove(result)).when(result).close();
+        openedConnections.add(result);
         return result;
     }
     
@@ -87,10 +94,13 @@ public final class MockedDataSource implements DataSource, AutoCloseable {
         return getConnection();
     }
     
-    @SuppressWarnings("ReturnOfNull")
+    @SuppressWarnings("unchecked")
     @Override
-    public <T> T unwrap(final Class<T> iface) {
-        return null;
+    public <T> T unwrap(final Class<T> iface) throws SQLException {
+        if (iface.isInstance(this)) {
+            return (T) this;
+        }
+        throw new SQLException("Wrapped DataSource is not an instance of " + iface);
     }
     
     @Override