You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/05/26 14:52:11 UTC

[commons-dbcp] branch master updated: Add Evictor threads test code. (#40)

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git


The following commit(s) were added to refs/heads/master by this push:
     new 3a0ce99  Add Evictor threads test code. (#40)
3a0ce99 is described below

commit 3a0ce998870cfbd680aa9c0fc386c433a6423130
Author: Kyohei Nakamura <na...@gmail.com>
AuthorDate: Tue May 26 23:51:30 2020 +0900

    Add Evictor threads test code. (#40)
    
    When set poolPreparedStatements to true, Evictor thread is disappeared.
    Related reports are as follows.
    - http://mail-archives.apache.org/mod_mbox/commons-user/202004.mbox/%3CCAD1sBYus4-j4i2Fs6dvzv_p4z0kaiu_8Yz8qUwXxTGAKp1ef1g%40mail.gmail.com%3E
---
 .../apache/commons/dbcp2/TestBasicDataSource.java  | 31 ++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java b/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
index 732bdf0..ff4b1bd 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestBasicDataSource.java
@@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadMXBean;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -916,6 +917,36 @@ public class TestBasicDataSource extends TestConnectionPool {
         conn.close();
         ds.close();
     }
+
+    @Test
+    public void testEvict() throws Exception {
+        long delay = 1000;
+
+        ds.setInitialSize(10);
+        ds.setMaxIdle(10);
+        ds.setMaxTotal(10);
+        ds.setMinIdle(5);
+        ds.setNumTestsPerEvictionRun(3);
+        ds.setMinEvictableIdleTimeMillis(100);
+        ds.setTimeBetweenEvictionRunsMillis(delay);
+        ds.setPoolPreparedStatements(true);
+
+        Connection conn = ds.getConnection();
+        conn.close();
+
+        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
+        while (Arrays.stream(threadBean.getThreadInfo(threadBean.getAllThreadIds()))
+                .anyMatch(t -> t.getThreadName().equals("commons-pool-evictor-thread"))) {
+            if (ds.getNumIdle() <= ds.getMinIdle()) {
+                break;
+            }
+            Thread.sleep(delay);
+        }
+        if (ds.getNumIdle() > ds.getMinIdle()) {
+            fail("EvictionTimer thread was destroyed with numIdle=" + ds.getNumIdle() + "(expected: less or equal than "
+                    + ds.getMinIdle() + ")");
+        }
+    }
 }
 
 /**