Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2013, 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.newHikariConfig;
20 import static com.zaxxer.hikari.pool.TestElf.setSlf4jLogLevel;
21 import static org.junit.Assert.fail;
22
23 import java.sql.Connection;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicReference;
29
30 import org.apache.logging.log4j.Level;
31 import org.junit.After;
32 import org.junit.Test;
33 import org.slf4j.LoggerFactory;
34
35 import com.zaxxer.hikari.HikariConfig;
36 import com.zaxxer.hikari.HikariDataSource;
37 import com.zaxxer.hikari.util.ConcurrentBag;
38
39 /**
40 * @author Matthew Tambara ([email protected])
41 */
42 public class ConnectionRaceConditionTest
43 {
44
45 public static final int ITERATIONS = 10_000;
46
47 @Test
48 public void testRaceCondition() throws Exception
49 {
50 HikariConfig config = newHikariConfig();
51 config.setMinimumIdle(0);
52 config.setMaximumPoolSize(10);
53 config.setInitializationFailTimeout(Long.MAX_VALUE);
54 config.setConnectionTimeout(5000);
55 config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
56
57 setSlf4jLogLevel(ConcurrentBag.class, Level.INFO);
58
59 final AtomicReference<Exception> ref = new AtomicReference<>(null);
60
61 // Initialize HikariPool with no initial connections and room to grow
62 try (final HikariDataSource ds = new HikariDataSource(config)) {
63 ExecutorService threadPool = Executors.newFixedThreadPool(2);
64 for (int i = 0; i < ITERATIONS; i++) {
65 threadPool.submit(new Callable<Exception>() {
66 /** {@inheritDoc} */
67 @Override
68 public Exception call() throws Exception
69 {
70 if (ref.get() == null) {
71 Connection c2;
72 try {
73 c2 = ds.getConnection();
74 ds.evictConnection(c2);
75 }
76 catch (Exception e) {
77 ref.set(e);
78 }
79 }
80 return null;
81 }
82 });
83 }
84
85 threadPool.shutdown();
86 threadPool.awaitTermination(30, TimeUnit.SECONDS);
87
88 if (ref.get() != null) {
89 LoggerFactory.getLogger(ConnectionRaceConditionTest.class).error("Task failed", ref.get());
90 fail("Task failed");
91 }
92 }
93 catch (Exception e) {
94 throw e;
95 }
96 }
97
98 @After
99 public void after()
100 {
101 System.getProperties().remove("com.zaxxer.hikari.housekeeping.periodMs");
102
103 setSlf4jLogLevel(HikariPool.class, Level.WARN);
104 setSlf4jLogLevel(ConcurrentBag.class, Level.WARN);
105 }
106 }