补充测试

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@547 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-08-31 16:25:50 +00:00
parent 26090dfcb2
commit 1fb466f75d
@@ -0,0 +1,117 @@
package com.alibaba.druid.bvt.pool;
import java.lang.management.ManagementFactory;
import java.sql.Connection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;
import javax.management.ObjectName;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.pool.DruidDataSource;
public class TestIdle3_Concurrent extends TestCase {
public void test_idle2() throws Exception {
MockDriver driver = new MockDriver();
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setDriver(driver);
dataSource.setInitialSize(1);
dataSource.setMaxActive(14);
dataSource.setMaxIdle(14);
dataSource.setMinIdle(1);
dataSource.setMinEvictableIdleTimeMillis(30 * 100); // 300 / 10
dataSource.setTimeBetweenEvictionRunsMillis(18 * 100); // 180 / 10
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(false);
dataSource.setValidationQuery("SELECT 1");
dataSource.setFilters("stat");
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource, new ObjectName("com.alibaba:type=DataSource"));
// 第一次创建连接
{
Assert.assertEquals(0, dataSource.getCreateCount());
Assert.assertEquals(0, dataSource.getActiveCount());
Connection conn = dataSource.getConnection();
Assert.assertEquals(dataSource.getInitialSize(), dataSource.getCreateCount());
Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
Assert.assertEquals(1, dataSource.getActiveCount());
conn.close();
Assert.assertEquals(0, dataSource.getDestroyCount());
Assert.assertEquals(2, driver.getConnections().size());
Assert.assertEquals(2, dataSource.getCreateCount());
Assert.assertEquals(0, dataSource.getActiveCount());
}
{
// 并发创建14个
concurrent(driver, dataSource, 100);
}
// 连续打开关闭单个连接
for (int i = 0; i < 1000; ++i) {
Assert.assertEquals(0, dataSource.getActiveCount());
Connection conn = dataSource.getConnection();
Assert.assertEquals(1, dataSource.getActiveCount());
conn.close();
}
Assert.assertEquals(2, dataSource.getPoolingCount());
dataSource.close();
}
private void concurrent(final MockDriver driver, final DruidDataSource dataSource, final int count) throws Exception {
final int LOOP_COUNT = 1000;
Thread[] threads = new Thread[count];
final CyclicBarrier barrier = new CyclicBarrier(count);
final CountDownLatch endLatch = new CountDownLatch(count);
for (int i = 0; i < count; ++i) {
threads[i] = new Thread("thread-" + i) {
public void run() {
try {
for (int i = 0; i < LOOP_COUNT; ++i) {
barrier.await();
Connection conn = dataSource.getConnection();
{
AtomicInteger c = new AtomicInteger();
for (int j = 0; j < 1000 * 100; ++j) {
c.incrementAndGet();
}
c.set(0);
Thread.sleep(5);
}
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
endLatch.countDown();
}
}
};
}
for (int i = 0; i < count; ++i) {
threads[i].start();
}
endLatch.await();
System.out.println("concurrent end");
int max = count > dataSource.getMaxActive() ? dataSource.getMaxActive() : count;
Assert.assertEquals(max, driver.getConnections().size());
}
}