Debian Codegraph @

src/test/java/com/zaxxer/hikari/pool/ShutdownTest.java

raw
1 /*
2 * Copyright (C) 2014 Brett Wooldridge
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.zaxxer.hikari.pool;
18
19 import static com.zaxxer.hikari.pool.TestElf.getPool;
20 import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
21 import static com.zaxxer.hikari.pool.TestElf.setSlf4jLogLevel;
22 import static com.zaxxer.hikari.util.ClockSource.currentTime;
23 import static com.zaxxer.hikari.util.ClockSource.elapsedMillis;
24 import static com.zaxxer.hikari.util.UtilityElf.quietlySleep;
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28
29 import java.sql.Connection;
30 import java.sql.PreparedStatement;
31 import java.sql.SQLException;
32
33 import org.apache.logging.log4j.Level;
34 import org.junit.After;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38
39 import com.zaxxer.hikari.HikariConfig;
40 import com.zaxxer.hikari.HikariDataSource;
41 import com.zaxxer.hikari.mocks.StubConnection;
42 import com.zaxxer.hikari.util.UtilityElf;
43
44 /**
45 * @author Brett Wooldridge
46 */
47 public class ShutdownTest
48 {
49 @Before
50 public void beforeTest()
51 {
52 setSlf4jLogLevel(PoolBase.class, Level.DEBUG);
53 setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
54 StubConnection.count.set(0);
55 }
56
57 @After
58 public void afterTest()
59 {
60 setSlf4jLogLevel(PoolBase.class, Level.WARN);
61 setSlf4jLogLevel(HikariPool.class, Level.WARN);
62 StubConnection.slowCreate = false;
63 }
64
65 @Test
66 public void testShutdown1() throws SQLException
67 {
68 Assert.assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
69
70 StubConnection.slowCreate = true;
71
72 HikariConfig config = newHikariConfig();
73 config.setMinimumIdle(0);
74 config.setMaximumPoolSize(10);
75 config.setInitializationFailTimeout(Long.MAX_VALUE);
76 config.setConnectionTestQuery("VALUES 1");
77 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
78
79 try (HikariDataSource ds = new HikariDataSource(config)) {
80 HikariPool pool = getPool(ds);
81
82 Thread[] threads = new Thread[10];
83 for (int i = 0; i < 10; i++) {
84 threads[i] = new Thread() {
85 @Override
86 public void run()
87 {
88 try {
89 if (ds.getConnection() != null) {
90 quietlySleep(SECONDS.toMillis(1));
91 }
92 }
93 catch (SQLException e) {
94 }
95 }
96 };
97 threads[i].setDaemon(true);
98 }
99 for (int i = 0; i < 10; i++) {
100 threads[i].start();
101 }
102
103 quietlySleep(1800L);
104
105 assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
106
107 ds.close();
108
109 assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
110 assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
111 assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
112 assertTrue(ds.isClosed());
113 }
114 }
115
116 @Test
117 public void testShutdown2() throws SQLException
118 {
119 assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
120
121 StubConnection.slowCreate = true;
122
123 HikariConfig config = newHikariConfig();
124 config.setMinimumIdle(10);
125 config.setMaximumPoolSize(10);
126 config.setInitializationFailTimeout(0);
127 config.setConnectionTestQuery("VALUES 1");
128 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
129
130 try (HikariDataSource ds = new HikariDataSource(config)) {
131 HikariPool pool = getPool(ds);
132
133 quietlySleep(1200L);
134
135 assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
136
137 ds.close();
138
139 assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
140 assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
141 assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
142 assertTrue(ds.toString().startsWith("HikariDataSource (") && ds.toString().endsWith(")"));
143 }
144 }
145
146 @Test
147 public void testShutdown3() throws SQLException
148 {
149 assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
150
151 StubConnection.slowCreate = false;
152
153 HikariConfig config = newHikariConfig();
154 config.setMinimumIdle(5);
155 config.setMaximumPoolSize(5);
156 config.setInitializationFailTimeout(0);
157 config.setConnectionTestQuery("VALUES 1");
158 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
159
160 try (HikariDataSource ds = new HikariDataSource(config)) {
161 HikariPool pool = getPool(ds);
162
163 quietlySleep(1200L);
164
165 assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
166
167 ds.close();
168
169 assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
170 assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
171 assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
172 }
173 }
174
175 @Test
176 public void testShutdown4() throws SQLException
177 {
178 StubConnection.slowCreate = true;
179
180 HikariConfig config = newHikariConfig();
181 config.setMinimumIdle(10);
182 config.setMaximumPoolSize(10);
183 config.setInitializationFailTimeout(Long.MAX_VALUE);
184 config.setConnectionTestQuery("VALUES 1");
185 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
186
187 try (HikariDataSource ds = new HikariDataSource(config)) {
188 quietlySleep(500L);
189
190 ds.close();
191
192 long startTime = currentTime();
193 while (elapsedMillis(startTime) < SECONDS.toMillis(5) && threadCount() > 0) {
194 quietlySleep(250);
195 }
196
197 assertSame("Unreleased connections after shutdown", 0, getPool(ds).getTotalConnections());
198 }
199 }
200
201 @Test
202 public void testShutdown5() throws SQLException
203 {
204 Assert.assertSame("StubConnection count not as expected", 0, StubConnection.count.get());
205
206 HikariConfig config = newHikariConfig();
207 config.setMinimumIdle(5);
208 config.setMaximumPoolSize(5);
209 config.setInitializationFailTimeout(0);
210 config.setConnectionTestQuery("VALUES 1");
211 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
212
213 try (HikariDataSource ds = new HikariDataSource(config)) {
214 HikariPool pool = getPool(ds);
215
216 Connection[] connections = new Connection[5];
217 for (int i = 0; i < 5; i++) {
218 connections[i] = ds.getConnection();
219 }
220
221 Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
222
223 ds.close();
224
225 Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
226 Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
227 Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
228 }
229 }
230
231 @Test
232 public void testAfterShutdown() throws Exception
233 {
234 HikariConfig config = newHikariConfig();
235 config.setMinimumIdle(0);
236 config.setMaximumPoolSize(5);
237 config.setInitializationFailTimeout(0);
238 config.setConnectionTestQuery("VALUES 1");
239 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
240
241 try (HikariDataSource ds = new HikariDataSource(config)) {
242 ds.close();
243 try {
244 ds.getConnection();
245 }
246 catch (SQLException e) {
247 Assert.assertTrue(e.getMessage().contains("has been closed."));
248 }
249 }
250 }
251
252 @Test
253 public void testShutdownDuringInit() throws Exception
254 {
255 final HikariConfig config = newHikariConfig();
256 config.setMinimumIdle(5);
257 config.setMaximumPoolSize(5);
258 config.setConnectionTimeout(1000);
259 config.setValidationTimeout(1000);
260 config.setInitializationFailTimeout(0);
261 config.setConnectionTestQuery("VALUES 1");
262 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
263
264 try (HikariDataSource ds = new HikariDataSource(config)) {
265 StubConnection.slowCreate = true;
266 UtilityElf.quietlySleep(3000L);
267 }
268 }
269
270 @Test
271 public void testThreadedShutdown() throws Exception
272 {
273 final HikariConfig config = newHikariConfig();
274 config.setMinimumIdle(5);
275 config.setMaximumPoolSize(5);
276 config.setConnectionTimeout(1000);
277 config.setValidationTimeout(1000);
278 config.setInitializationFailTimeout(0);
279 config.setConnectionTestQuery("VALUES 1");
280 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
281
282 for (int i = 0; i < 4; i++) {
283 try (final HikariDataSource ds = new HikariDataSource(config)) {
284 Thread t = new Thread() {
285 @Override
286 public void run()
287 {
288 try (Connection connection = ds.getConnection()) {
289 for (int i = 0; i < 10; i++) {
290 Connection connection2 = null;
291 try {
292 connection2 = ds.getConnection();
293 PreparedStatement stmt = connection2.prepareStatement("SOMETHING");
294 UtilityElf.quietlySleep(20);
295 stmt.getMaxFieldSize();
296 }
297 catch (SQLException e) {
298 try {
299 if (connection2 != null) {
300 connection2.close();
301 }
302 }
303 catch (SQLException e2) {
304 if (e2.getMessage().contains("shutdown") || e2.getMessage().contains("evicted")) {
305 break;
306 }
307 }
308 }
309 }
310 }
311 catch (Exception e) {
312 Assert.fail(e.getMessage());
313 }
314 finally {
315 ds.close();
316 }
317 }
318 };
319 t.start();
320
321 Thread t2 = new Thread() {
322 @Override
323 public void run()
324 {
325 UtilityElf.quietlySleep(100);
326 try {
327 ds.close();
328 }
329 catch (IllegalStateException e) {
330 Assert.fail(e.getMessage());
331 }
332 }
333 };
334 t2.start();
335
336 t.join();
337 t2.join();
338
339 ds.close();
340 }
341 }
342 }
343
344 private int threadCount()
345 {
346 Thread[] threads = new Thread[Thread.activeCount() * 2];
347 Thread.enumerate(threads);
348
349 int count = 0;
350 for (Thread thread : threads) {
351 count += (thread != null && thread.getName().startsWith("Hikari")) ? 1 : 0;
352 }
353
354 return count;
355 }
356 }