add api tryGetConnection and fill

This commit is contained in:
yakolee
2014-07-01 18:44:14 +08:00
parent 961282db9d
commit 62c4f7df9e
5 changed files with 273 additions and 0 deletions
@@ -0,0 +1,56 @@
package com.alibaba.druid.bvt.pool;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.pool.DruidDataSource;
public class DruidDataSourceTest_fill extends TestCase {
private DruidDataSource dataSource;
private int maxActive = 10;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setMaxActive(maxActive);
dataSource.setTestOnBorrow(false);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_fill_0() throws Exception {
int fillCount = dataSource.fill(3);
Assert.assertEquals(3, fillCount);
}
public void test_fill_1() throws Exception {
int fillCount = dataSource.fill(1000);
Assert.assertEquals(maxActive, fillCount);
}
public void test_fill_2() throws Exception {
int fillCount = dataSource.fill(maxActive);
Assert.assertEquals(maxActive, fillCount);
}
public void test_fill_3() throws Exception {
int fillCount = dataSource.fill();
Assert.assertEquals(maxActive, fillCount);
}
public void test_fill_5() throws Exception {
Exception error = null;
try {
dataSource.fill(-1);
} catch (IllegalArgumentException e) {
error = e;
}
Assert.assertNotNull(error);
}
}
@@ -0,0 +1,86 @@
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.TestCase;
import org.junit.Assert;
import com.alibaba.druid.pool.DruidDataSource;
public class DruidDataSourceTest_fill2 extends TestCase {
private DruidDataSource dataSource;
private int maxActive = 100;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setMaxActive(maxActive);
dataSource.setTestOnBorrow(false);
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_fill() throws Exception {
final AtomicLong errorCount = new AtomicLong();
final int THREAD_COUNT = 100;
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(2);
conn.close();
} catch (Exception e) {
errorCount.incrementAndGet();
} finally {
endLatch.countDown();
}
}
};
threads[i].start();
}
startLatch.await(1000, TimeUnit.MILLISECONDS);
final CountDownLatch fillLatch = new CountDownLatch(THREAD_COUNT);
final AtomicLong fillErrorCount = new AtomicLong();
final AtomicLong fillCount = new AtomicLong();
Thread fillThread = new Thread() {
public void run() {
try {
int count = dataSource.fill();
fillCount.getAndSet(count);
} catch (Exception e) {
fillErrorCount.incrementAndGet();
} finally {
fillLatch.countDown();
}
}
};
fillThread.start();
fillLatch.await(1000, TimeUnit.MILLISECONDS);
Assert.assertEquals(0, fillErrorCount.get());
endLatch.await(1000, TimeUnit.MILLISECONDS);
Assert.assertEquals(true, fillCount.get() > 0);
}
}
@@ -0,0 +1,37 @@
package com.alibaba.druid.bvt.pool;
import org.junit.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
public class DruidDataSourceTest_tryGet extends TestCase {
private DruidDataSource dataSource;
protected void setUp() throws Exception {
dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setMaxActive(1);
dataSource.setInitialSize(1);
dataSource.setTestOnBorrow(false);
dataSource.init();
}
protected void tearDown() throws Exception {
dataSource.close();
}
public void test_0() throws Exception {
DruidPooledConnection conn1 = this.dataSource.tryGetConnection();
Assert.assertNotNull(conn1);
DruidPooledConnection conn2 = this.dataSource.tryGetConnection();
Assert.assertNull(conn2);
conn1.close();
}
}