mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
80 lines
2.4 KiB
Java
80 lines
2.4 KiB
Java
package com.alibaba.druid.bvt.pool;
|
|
|
|
import java.sql.Connection;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.concurrent.CountDownLatch;
|
|
import java.util.concurrent.atomic.AtomicLong;
|
|
|
|
import junit.framework.Assert;
|
|
import junit.framework.TestCase;
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
import com.alibaba.druid.util.JdbcUtils;
|
|
|
|
public class TestConnectTimeout extends TestCase {
|
|
|
|
private DruidDataSource dataSource;
|
|
|
|
protected void setUp() throws Exception {
|
|
dataSource = new DruidDataSource();
|
|
|
|
dataSource.setUsername("xxx1");
|
|
dataSource.setPassword("ppp");
|
|
dataSource.setUrl("jdbc:mock:xx");
|
|
dataSource.setFilters("stat");
|
|
dataSource.setMaxOpenPreparedStatements(30);
|
|
dataSource.setMaxActive(4);
|
|
dataSource.setMaxWait(100);
|
|
dataSource.setMinIdle(0);
|
|
dataSource.setInitialSize(1);
|
|
dataSource.init();
|
|
}
|
|
|
|
public void testConnectTimeout() throws Exception {
|
|
{
|
|
Connection conn = dataSource.getConnection();
|
|
conn.close();
|
|
dataSource.shrink();
|
|
Assert.assertEquals(0, dataSource.getPoolingCount());
|
|
}
|
|
|
|
final List<Connection> connections = new ArrayList<Connection>();
|
|
for (int i = 0; i < 3; ++i) {
|
|
Connection conn = dataSource.getConnection();
|
|
connections.add(conn);
|
|
}
|
|
|
|
final AtomicLong errorCount = new AtomicLong();
|
|
final int THREAD_COUNT = 10;
|
|
final CountDownLatch latch = new CountDownLatch(THREAD_COUNT);
|
|
for (int i = 0; i < THREAD_COUNT; ++i) {
|
|
Thread thread = new Thread() {
|
|
|
|
public void run() {
|
|
try {
|
|
for (int i = 0; i < 100; ++i) {
|
|
Connection conn = dataSource.getConnection();
|
|
Thread.sleep(1);
|
|
conn.close();
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
errorCount.incrementAndGet();
|
|
} finally {
|
|
latch.countDown();
|
|
}
|
|
}
|
|
};
|
|
thread.start();
|
|
}
|
|
|
|
latch.await();
|
|
Assert.assertEquals(0, errorCount.get());
|
|
}
|
|
|
|
protected void tearDown() throws Exception {
|
|
JdbcUtils.close(dataSource);
|
|
}
|
|
}
|