From b6adf85ccab735aed4a56414526c63b7d46803e8 Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 23 Oct 2012 20:54:03 +0800 Subject: [PATCH 1/2] add lock fair testcase --- .../alibaba/druid/bvt/pool/LockFairTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java diff --git a/src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java b/src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java new file mode 100644 index 0000000000..7193efbc2b --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java @@ -0,0 +1,29 @@ +package com.alibaba.druid.bvt.pool; + +import java.util.concurrent.locks.ReentrantLock; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.pool.DruidDataSource; +import com.alibaba.druid.util.JdbcUtils; + +public class LockFairTest extends TestCase { + + private DruidDataSource dataSource; + + protected void setUp() throws Exception { + dataSource = new DruidDataSource(); + } + + protected void tearDown() throws Exception { + JdbcUtils.close(dataSource); + } + + public void test_fair() throws Exception { + Assert.assertEquals(false, ((ReentrantLock)dataSource.getLock()).isFair()); + dataSource.setMaxWait(100); + + Assert.assertEquals(true, ((ReentrantLock)dataSource.getLock()).isFair()); + } +} From a2e3e881064cba8308e6e7601ccace49444f2aec Mon Sep 17 00:00:00 2001 From: wenshao Date: Tue, 23 Oct 2012 20:59:16 +0800 Subject: [PATCH 2/2] add lock fair testcase --- .../alibaba/druid/bvt/pool/LockFairTest.java | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java b/src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java index 7193efbc2b..16547efc1f 100644 --- a/src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java +++ b/src/test/java/com/alibaba/druid/bvt/pool/LockFairTest.java @@ -1,5 +1,6 @@ package com.alibaba.druid.bvt.pool; +import java.sql.Connection; import java.util.concurrent.locks.ReentrantLock; import junit.framework.Assert; @@ -11,19 +12,50 @@ import com.alibaba.druid.util.JdbcUtils; public class LockFairTest extends TestCase { private DruidDataSource dataSource; - + protected void setUp() throws Exception { dataSource = new DruidDataSource(); + dataSource.setUrl("jdbc:mock:xx"); } - + protected void tearDown() throws Exception { JdbcUtils.close(dataSource); } public void test_fair() throws Exception { - Assert.assertEquals(false, ((ReentrantLock)dataSource.getLock()).isFair()); + Assert.assertEquals(false, ((ReentrantLock) dataSource.getLock()).isFair()); dataSource.setMaxWait(100); + + Assert.assertEquals(true, ((ReentrantLock) dataSource.getLock()).isFair()); + { + Connection conn = dataSource.getConnection(); + conn.close(); + } + dataSource.setMaxWait(110); + + Assert.assertEquals(true, ((ReentrantLock) dataSource.getLock()).isFair()); + { + Connection conn = dataSource.getConnection(); + conn.close(); + } - Assert.assertEquals(true, ((ReentrantLock)dataSource.getLock()).isFair()); + dataSource.setMaxWait(0); + + Assert.assertEquals(true, ((ReentrantLock) dataSource.getLock()).isFair()); + { + Connection conn = dataSource.getConnection(); + conn.close(); + } + } + + public void test_fair_1() throws Exception { + + Connection conn = dataSource.getConnection(); + conn.close(); + + Assert.assertEquals(false, ((ReentrantLock) dataSource.getLock()).isFair()); + dataSource.setMaxWait(100); + + Assert.assertEquals(false, ((ReentrantLock) dataSource.getLock()).isFair()); } }