-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
TestIdel3.java
155 lines (127 loc) · 5.87 KB
/
TestIdel3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 1999-2018 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;
import java.lang.management.ManagementFactory;
import java.sql.Connection;
import java.util.concurrent.CountDownLatch;
import javax.management.ObjectName;
import org.junit.Assert;
import junit.framework.TestCase;
import com.alibaba.druid.mock.MockDriver;
import com.alibaba.druid.pool.DruidDataSource;
public class TestIdel3 extends TestCase {
public void test_idle2() throws Exception {
MockDriver driver = new MockDriver();
final DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setDriver(driver);
dataSource.setInitialSize(1);
dataSource.setMaxActive(14);
dataSource.setMaxIdle(14);
dataSource.setMinIdle(1);
dataSource.setMinEvictableIdleTimeMillis(3 * 100); // 300 / 10
dataSource.setTimeBetweenEvictionRunsMillis(18 * 10); // 180 / 10
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(false);
dataSource.setValidationQuery("SELECT 1");
dataSource.setFilters("stat");
ManagementFactory.getPlatformMBeanServer().registerMBean(dataSource,
new ObjectName("com.alibaba:type=DataSource"));
// 第一次创建连接
{
Assert.assertEquals(0, dataSource.getCreateCount());
Assert.assertEquals(0, dataSource.getActiveCount());
Connection conn = dataSource.getConnection();
Assert.assertEquals(dataSource.getInitialSize(), dataSource.getCreateCount());
Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size());
Assert.assertEquals(1, dataSource.getActiveCount());
conn.close();
Assert.assertEquals(0, dataSource.getDestroyCount());
Assert.assertEquals(2, driver.getConnections().size());
Assert.assertEquals(2, dataSource.getCreateCount());
Assert.assertEquals(0, dataSource.getActiveCount());
}
{
// 并发创建14个
int count = 14;
Connection[] connections = new Connection[count];
for (int i = 0; i < count; ++i) {
connections[i] = dataSource.getConnection();
Assert.assertEquals(i + 1, dataSource.getActiveCount());
}
Assert.assertEquals(dataSource.getMaxActive(), dataSource.getCreateCount());
Assert.assertEquals(count, driver.getConnections().size());
// 全部关闭
for (int i = 0; i < count; ++i) {
connections[i].close();
Assert.assertEquals(count - i - 1, dataSource.getActiveCount());
}
Assert.assertEquals(dataSource.getMaxActive(), dataSource.getCreateCount());
Assert.assertEquals(0, dataSource.getActiveCount());
Assert.assertEquals(14, driver.getConnections().size());
}
concurrent(dataSource, 100, 1000);
concurrent(dataSource, 1, 1000 * 1000);
Thread.sleep(1000 * 10);
concurrent(dataSource, 1, 1000 * 1000);
Thread.sleep(1000 * 10);
concurrent(dataSource, 1000, 1000 * 1000);
Assert.assertEquals(driver.getConnections().size(), dataSource.getPoolingCount());
Assert.assertEquals(0, dataSource.getActiveCount());
// 连续打开关闭单个连接
for (int i = 0; i < 1000; ++i) {
Assert.assertEquals(0, dataSource.getActiveCount());
Connection conn = dataSource.getConnection();
Assert.assertEquals(1, dataSource.getActiveCount());
Thread.sleep(10);
conn.close();
}
// Assert.assertEquals(2, dataSource.getPoolingCount());
Thread.sleep(1000 * 100);
dataSource.close();
}
private void concurrent(final DruidDataSource dataSource, int threadCount, final int loopCount)
throws InterruptedException {
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch endLatch = new CountDownLatch(threadCount);
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; ++i) {
threads[i] = new Thread("thread-" + i) {
public void run() {
try {
startLatch.await();
for (int i = 0; i < loopCount; ++i) {
Connection conn = null;
conn = dataSource.getConnection();
conn.close();
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
endLatch.countDown();
}
}
};
}
for (int i = 0; i < threadCount; ++i) {
threads[i].start();
}
startLatch.countDown();
System.out.println("concurrent start...");
endLatch.await();
System.out.println("concurrent end");
}
}