mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add api tryGetConnection and fill
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user