add testcase & bug fixed

This commit is contained in:
wenshao
2012-08-25 08:34:13 +08:00
parent dcfdf5449e
commit 012899d9d7
3 changed files with 117 additions and 12 deletions
@@ -32,6 +32,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import javax.management.JMException;
import javax.management.ObjectName;
@@ -148,7 +149,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
if (LOG.isInfoEnabled()) {
LOG.info("{dataSource-" + this.getID() + "} restart");
}
this.close();
this.resetStat();
this.inited = false;
@@ -347,7 +348,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
if (getInitialSize() > maxActive) {
throw new IllegalArgumentException("illegal initialSize " + this.initialSize + ", maxActieve " + maxActive);
throw new IllegalArgumentException("illegal initialSize " + this.initialSize + ", maxActieve "
+ maxActive);
}
if (this.driverClass != null) {
@@ -590,15 +592,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
public DruidPooledConnection getConnectionDirect(long maxWaitMillis) throws SQLException {
final int maxWaitThreadCount = getMaxWaitThreadCount();
if (maxWaitThreadCount > 0) {
if (notEmptyWaitThreadCount > maxWaitThreadCount) {
connectErrorCount.incrementAndGet();
throw new SQLException("maxWaitThreadCount " + maxWaitThreadCount + ", current wait Thread count "
+ lock.getQueueLength());
}
}
for (;;) {
DruidPooledConnection poolalbeConnection = getConnectionInternal(maxWaitMillis);
@@ -692,6 +685,15 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
throw new DataSourceDisableException();
}
final int maxWaitThreadCount = getMaxWaitThreadCount();
if (maxWaitThreadCount > 0) {
if (notEmptyWaitThreadCount >= maxWaitThreadCount) {
connectErrorCount.incrementAndGet();
throw new SQLException("maxWaitThreadCount " + maxWaitThreadCount + ", current wait Thread count "
+ lock.getQueueLength());
}
}
connectCount++;
DruidConnectionHolder holder;
@@ -1441,7 +1443,12 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
public int getNotEmptyWaitThreadCount() {
return notEmptyWaitThreadCount;
lock.lock();
try {
return notEmptyWaitThreadCount;
} finally {
lock.unlock();
}
}
public int getNotEmptyWaitThreadPeak() {
@@ -1814,4 +1821,8 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
public Map<String, JdbcSqlStat> getSqlStatMap() {
return this.getDataSourceStat().getSqlStatMap();
}
public Lock getLock() {
return lock;
}
}
@@ -56,6 +56,7 @@ public class DruidDataSourceTest_notEmptyWait extends TestCase {
startLatch.countDown();
try {
Connection conn = dataSource.getConnection();
Thread.sleep(1);
conn.close();
} catch (Exception e) {
//e.printStackTrace();
@@ -75,6 +76,7 @@ public class DruidDataSourceTest_notEmptyWait extends TestCase {
conn.close();
endLatch.await(100, TimeUnit.MILLISECONDS);
Assert.assertEquals(0, dataSource.getNotEmptyWaitThreadCount());
Assert.assertEquals(10, dataSource.getNotEmptyWaitThreadPeak());
}
@@ -0,0 +1,92 @@
package com.alibaba.druid.bvt.pool;
import java.sql.Connection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
public class DruidDataSourceTest_notEmptyWait2 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.setMaxWaitThreadCount(10);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_maxWaitThread() throws Exception {
{
Connection conn = dataSource.getConnection();
conn.close();
}
Connection conn = dataSource.getConnection();
final AtomicLong errorCount = new AtomicLong();
final int THREAD_COUNT = 10;
final CountDownLatch startLatch = new CountDownLatch(THREAD_COUNT);
final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; ++i) {
threads[i] = new Thread("thread-" + i) {
public void run() {
startLatch.countDown();
try {
Connection conn = dataSource.getConnection();
Thread.sleep(1);
conn.close();
} catch (Exception e) {
// e.printStackTrace();
errorCount.incrementAndGet();
} finally {
endLatch.countDown();
}
}
};
threads[i].start();
}
startLatch.await(100, TimeUnit.MILLISECONDS);
final CountDownLatch errorThreadEndLatch = new CountDownLatch(THREAD_COUNT);
final AtomicLong maxWaitErrorCount = new AtomicLong();
Thread errorThread = new Thread() {
public void run() {
try {
Connection conn = dataSource.getConnection();
Thread.sleep(1);
conn.close();
} catch (Exception e) {
maxWaitErrorCount.incrementAndGet();
} finally {
errorThreadEndLatch.countDown();
}
}
};
errorThread.start();
errorThreadEndLatch.await(100, TimeUnit.MILLISECONDS);
Assert.assertEquals(1, maxWaitErrorCount.get());
conn.close();
endLatch.await(100, TimeUnit.MILLISECONDS);
Assert.assertEquals(0, errorCount.get());
}
}