mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@40 b9813039-fb51-4c41-a8b9-e21c2acb5095
102 lines
3.4 KiB
Java
102 lines
3.4 KiB
Java
/*
|
|
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package com.alibaba.druid.bvt.pool;
|
|
|
|
import java.sql.Connection;
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
import org.junit.Assert;
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
|
public class ConcurrentTest extends TestCase {
|
|
|
|
private String jdbcUrl;
|
|
private String user;
|
|
private String password;
|
|
private String driverClass;
|
|
private int minPoolSize = 50;
|
|
private int maxPoolSize = 100;
|
|
private int maxActive = 500;
|
|
|
|
protected void setUp() throws Exception {
|
|
// jdbcUrl =
|
|
// "jdbc:mysql://10.20.129.146/dragoon_v25masterdb?useUnicode=true&characterEncoding=UTF-8";
|
|
// user = "dragoon25";
|
|
// password = "dragoon25";
|
|
// driverClass = "com.mysql.jdbc.Driver";
|
|
|
|
jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
|
|
user = "dragoon25";
|
|
password = "dragoon25";
|
|
driverClass = "com.alibaba.druid.mock.MockDriver";
|
|
}
|
|
|
|
public void test_concurrent_2() throws Exception {
|
|
final DruidDataSource dataSource = new DruidDataSource();
|
|
|
|
Class.forName("com.alibaba.druid.mock.MockDriver");
|
|
|
|
dataSource.setInitialSize(10);
|
|
dataSource.setMaxActive(maxActive);
|
|
dataSource.setMinIdle(minPoolSize);
|
|
dataSource.setMaxIdle(maxPoolSize);
|
|
dataSource.setPoolPreparedStatements(true);
|
|
dataSource.setDriverClassName(driverClass);
|
|
dataSource.setUrl(jdbcUrl);
|
|
dataSource.setPoolPreparedStatements(true);
|
|
dataSource.setUsername(user);
|
|
dataSource.setPassword(password);
|
|
|
|
final int THREAD_COUNT = 2;
|
|
final int LOOP_COUNT = 1000 * 1000;
|
|
|
|
final CountDownLatch startLatch = new CountDownLatch(1);
|
|
final CountDownLatch endLatch = new CountDownLatch(THREAD_COUNT);
|
|
|
|
for (int threadIndex = 0; threadIndex < THREAD_COUNT; ++threadIndex) {
|
|
Thread thread = new Thread() {
|
|
|
|
public void run() {
|
|
try {
|
|
startLatch.await();
|
|
|
|
for (int i = 0; i < LOOP_COUNT; ++i) {
|
|
Connection conn = dataSource.getConnection();
|
|
conn.close();
|
|
}
|
|
} catch (Throwable e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
endLatch.countDown();
|
|
}
|
|
}
|
|
};
|
|
thread.start();
|
|
}
|
|
|
|
startLatch.countDown();
|
|
endLatch.await();
|
|
|
|
Assert.assertEquals(THREAD_COUNT * LOOP_COUNT, dataSource.getConnectCount());
|
|
Assert.assertEquals(THREAD_COUNT * LOOP_COUNT, dataSource.getCloseCount());
|
|
Assert.assertEquals(0, dataSource.getConnectErrorCount());
|
|
Assert.assertEquals(0, dataSource.getActiveCount());
|
|
}
|
|
}
|