mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
增强mock
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@507 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package com.alibaba.druid.bvt.pool;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class TestIdle extends TestCase {
|
||||
|
||||
public void test_idle() throws Exception {
|
||||
MockDriver driver = new MockDriver();
|
||||
|
||||
DruidDataSource dataSource = new DruidDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setDriver(driver);
|
||||
dataSource.setInitialSize(2);
|
||||
dataSource.setMaxActive(4);
|
||||
dataSource.setMaxIdle(4);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMinEvictableIdleTimeMillis(50 * 1);
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(10);
|
||||
dataSource.setTestWhileIdle(true);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setValidationQuery("SELECT 1");
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
|
||||
|
||||
conn.close();
|
||||
Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
|
||||
}
|
||||
|
||||
{
|
||||
int count = 4;
|
||||
Connection[] connections = new Connection[4];
|
||||
for (int i = 0; i < count; ++i) {
|
||||
connections[i] = dataSource.getConnection();
|
||||
}
|
||||
Assert.assertEquals(4, driver.getConnections().size());
|
||||
for (int i = 0; i < count; ++i) {
|
||||
connections[i].close();
|
||||
}
|
||||
Assert.assertEquals(4, driver.getConnections().size());
|
||||
|
||||
Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
|
||||
Assert.assertEquals(dataSource.getMinIdle(), driver.getConnections().size());
|
||||
}
|
||||
|
||||
System.out.println("----------raw close all connection");
|
||||
for (MockConnection rawConn : driver.getConnections()) {
|
||||
rawConn.close();
|
||||
}
|
||||
|
||||
Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
|
||||
Assert.assertEquals(0, driver.getConnections().size());
|
||||
Assert.assertEquals(0, dataSource.getPoolingCount());
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
Assert.assertEquals(dataSource.getMinIdle(), driver.getConnections().size());
|
||||
conn.close();
|
||||
Assert.assertEquals(dataSource.getMinIdle(), driver.getConnections().size());
|
||||
}
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.alibaba.druid.pool.dbcp;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
|
||||
public class Test0 extends TestCase {
|
||||
|
||||
public void test_idle() throws Exception {
|
||||
MockDriver driver = MockDriver.instance;
|
||||
|
||||
BasicDataSource dataSource = new BasicDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
|
||||
dataSource.setInitialSize(0);
|
||||
dataSource.setMaxActive(4);
|
||||
dataSource.setMaxIdle(4);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMinEvictableIdleTimeMillis(5000 * 1);
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(10);
|
||||
dataSource.setTestWhileIdle(false);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setValidationQuery("SELECT 1");
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
// Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
|
||||
conn.close();
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
}
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
// Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
|
||||
conn.close();
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
}
|
||||
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.alibaba.druid.pool.dbcp;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.apache.commons.dbcp.BasicDataSource;
|
||||
|
||||
import com.alibaba.druid.mock.MockConnection;
|
||||
import com.alibaba.druid.mock.MockDriver;
|
||||
|
||||
public class TestIdle extends TestCase {
|
||||
|
||||
public void test_idle() throws Exception {
|
||||
MockDriver driver = MockDriver.instance;
|
||||
|
||||
BasicDataSource dataSource = new BasicDataSource();
|
||||
dataSource.setUrl("jdbc:mock:xxx");
|
||||
dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver");
|
||||
dataSource.setInitialSize(0);
|
||||
dataSource.setMaxActive(4);
|
||||
dataSource.setMaxIdle(4);
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setMinEvictableIdleTimeMillis(50 * 1);
|
||||
dataSource.setTimeBetweenEvictionRunsMillis(10);
|
||||
dataSource.setTestWhileIdle(false);
|
||||
dataSource.setTestOnBorrow(false);
|
||||
dataSource.setValidationQuery("SELECT 1");
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
// Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
|
||||
conn.close();
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
}
|
||||
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
// Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
|
||||
conn.close();
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
}
|
||||
|
||||
{
|
||||
int count = 4;
|
||||
Connection[] connections = new Connection[4];
|
||||
for (int i = 0; i < count; ++i) {
|
||||
connections[i] = dataSource.getConnection();
|
||||
}
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
for (int i = 0; i < count; ++i) {
|
||||
connections[i].close();
|
||||
}
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
|
||||
System.out.println("----------sleep for evict");
|
||||
Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
}
|
||||
|
||||
System.out.println("----------raw close all connection");
|
||||
for (MockConnection rawConn : driver.getConnections()) {
|
||||
rawConn.close();
|
||||
}
|
||||
|
||||
Thread.sleep(dataSource.getMinEvictableIdleTimeMillis() * 2);
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
conn.close();
|
||||
System.out.println("raw size : " + driver.getConnections().size());
|
||||
}
|
||||
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user