diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_closeError.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_closeError.java new file mode 100644 index 0000000000..c96e03d36b --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_closeError.java @@ -0,0 +1,41 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.SQLException; + +import junit.framework.TestCase; + +import com.alibaba.druid.filter.FilterAdapter; +import com.alibaba.druid.filter.FilterChain; +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.proxy.jdbc.ConnectionProxy; + +/** + * 这个场景测试initialSize > maxActive + * + * @author wenshao + */ +public class DruidDataSourceTest_closeError extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setInitialSize(1); + dataSource.getProxyFilters().add(new FilterAdapter() { + + public void connection_close(FilterChain chain, ConnectionProxy connection) throws SQLException { + throw new SQLException(); + } + }); + dataSource.init(); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_closeError() throws Exception { + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_getProperties.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_getProperties.java new file mode 100644 index 0000000000..2318c7c63a --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_getProperties.java @@ -0,0 +1,34 @@ +package com.alibaba.druid.bvt.pool; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; + +/** + * 这个场景测试initialSize > maxActive + * + * @author wenshao + */ +public class DruidDataSourceTest_getProperties extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.getConnectProperties().put("user", "jobs"); + dataSource.getConnectProperties().put("password", "xxx"); + dataSource.init(); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_error() throws Exception { + Assert.assertEquals(-1, dataSource.getProperties().indexOf("xxx")); + Assert.assertEquals(true, dataSource.getProperties().indexOf("******") != -1); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_maxWait.java b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_maxWait.java new file mode 100644 index 0000000000..8f4631557b --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/DruidDataSourceTest_maxWait.java @@ -0,0 +1,79 @@ +package com.alibaba.druid.bvt.pool; + +import java.sql.Connection; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; + +/** + * 这个场景测试initialSize > maxActive + * + * @author wenshao + */ +public class DruidDataSourceTest_maxWait extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xxx"); + dataSource.setTestOnBorrow(false); + dataSource.setMaxActive(1); + dataSource.setMaxWait(30); + dataSource.setInitialSize(1); + dataSource.init(); + } + + protected void tearDown() throws Exception { + dataSource.close(); + } + + public void test_maxWait() throws Exception { + Connection conn = dataSource.getConnection(); + + final AtomicInteger errorCount = new AtomicInteger(); + final CountDownLatch endLatch = new CountDownLatch(2); + + Thread t1 = new Thread("t-1") { + + public void run() { + try { + dataSource.getConnection(); + } catch (Exception e) { + errorCount.incrementAndGet(); + } finally { + endLatch.countDown(); + } + } + }; + + Thread t2 = new Thread("t-2") { + + public void run() { + try { + dataSource.getConnection(); + } catch (Exception e) { + errorCount.incrementAndGet(); + } finally { + endLatch.countDown(); + } + } + }; + + t1.start(); + t2.start(); + + Thread.sleep(10); + t1.interrupt(); + + endLatch.await(); + + Assert.assertEquals(2, errorCount.get()); + + conn.close(); + } +}