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 332e59a  Add test cases for DataSourceDestroyer for #14039 (#14561)
332e59a is described below

commit 332e59a1f2b9aab49ab928ed37db3f7a3233df7a
Author: Takuya Fujiwara <[email protected]>
AuthorDate: Mon Jan 31 03:33:50 2022 +0900

    Add test cases for DataSourceDestroyer for #14039 (#14561)
    
    * Add testcases for #14039.
    
    * Ensure that dataSource is closed.
    
    * Fix test
    
    * Fix namespace
---
 .../DataSourcePoolDestroyerFactoryTest.java        | 46 +++++++++++++++++++++
 .../impl/DefaultDataSourcePoolDestroyerTest.java   | 48 ++++++++++++++++++++++
 .../impl/HikariDataSourcePoolDestroyerTest.java    | 34 +++++++++++++++
 3 files changed, 128 insertions(+)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/DataSourcePoolDestroyerFactoryTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/DataSourcePoolDestroyerFactoryTest.java
new file mode 100644
index 0000000..593a34a
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/DataSourcePoolDestroyerFactoryTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertTrue;
+
+import java.sql.SQLException;
+
+import com.zaxxer.hikari.HikariDataSource;
+
+import org.apache.commons.dbcp2.BasicDataSource;
+import org.junit.Test;
+
+public final class DataSourcePoolDestroyerFactoryTest {
+    
+    @Test(timeout = 60000L)
+    public void assertDestroyForHikari() throws InterruptedException, 
SQLException {
+        HikariDataSource dataSource = new HikariDataSource();
+        DataSourcePoolDestroyerFactory.destroy(dataSource);
+        while (!dataSource.isClosed()) {
+            Thread.sleep(10L);
+        }
+    }
+
+    @Test
+    public void assertDestroyForDefault() throws InterruptedException, 
SQLException {
+        BasicDataSource dataSource = new BasicDataSource();
+        DataSourcePoolDestroyerFactory.destroy(dataSource);
+        assertTrue(dataSource.isClosed());
+    }
+}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/impl/DefaultDataSourcePoolDestroyerTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/impl/DefaultDataSourcePoolDestroyerTest.java
new file mode 100644
index 0000000..111b789
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/impl/DefaultDataSourcePoolDestroyerTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.impl;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+
+import java.sql.SQLException;
+
+import com.zaxxer.hikari.HikariDataSource;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class DefaultDataSourcePoolDestroyerTest {
+
+    @Test
+    public void assertDestroy() throws SQLException {
+        HikariDataSource dataSource = new HikariDataSource();
+        new DefaultDataSourcePoolDestroyer().destroy(dataSource);
+        assertTrue(dataSource.isClosed());
+    }
+
+    @Test(expected = SQLException.class)
+    public void assertDestroyWithException() throws SQLException {
+        HikariDataSource dataSource = mock(HikariDataSource.class);
+        doThrow(new RuntimeException()).when(dataSource).close();
+        new DefaultDataSourcePoolDestroyer().destroy(dataSource);
+    }
+}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/impl/HikariDataSourcePoolDestroyerTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/impl/HikariDataSourcePoolDestroyerTest.java
new file mode 100644
index 0000000..37d3d2d
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/impl/HikariDataSourcePoolDestroyerTest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.impl;
+
+import com.zaxxer.hikari.HikariDataSource;
+
+import org.junit.Test;
+
+public final class HikariDataSourcePoolDestroyerTest {
+
+    @Test(timeout = 60000L)
+    public void assertDestroy() throws InterruptedException {
+        HikariDataSource dataSource = new HikariDataSource();
+        new HikariDataSourcePoolDestroyer().destroy(dataSource);
+        while (!dataSource.isClosed()) {
+            Thread.sleep(10L);
+        }
+    }
+}

Reply via email to