Debian Codegraph @

src/test/java/com/zaxxer/hikari/pool/PostgresTest.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.getPool;
20 import static com.zaxxer.hikari.pool.TestElf.newHikariConfig;
21 import static com.zaxxer.hikari.util.ClockSource.currentTime;
22 import static com.zaxxer.hikari.util.ClockSource.elapsedMillis;
23 import static com.zaxxer.hikari.util.UtilityElf.quietlySleep;
24 import static java.util.concurrent.TimeUnit.MINUTES;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.sql.Connection;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.Statement;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.concurrent.TimeUnit;
35
36 import org.junit.Before;
37
38 import com.zaxxer.hikari.HikariConfig;
39 import com.zaxxer.hikari.HikariDataSource;
40 import com.zaxxer.hikari.util.UtilityElf;
41
42 /**
43 * This test is meant to be run manually and interactively and was
44 * build for issue #159.
45 *
46 * @author Brett Wooldridge
47 */
48 public class PostgresTest
49 {
50 //@Test
51 public void testCase1() throws Exception
52 {
53 HikariConfig config = newHikariConfig();
54 config.setMinimumIdle(3);
55 config.setMaximumPoolSize(10);
56 config.setConnectionTimeout(3000);
57 config.setIdleTimeout(TimeUnit.SECONDS.toMillis(10));
58 config.setValidationTimeout(TimeUnit.SECONDS.toMillis(2));
59
60 config.setJdbcUrl("jdbc:pgsql://localhost:5432/test");
61 config.setUsername("brettw");
62
63 try (final HikariDataSource ds = new HikariDataSource(config)) {
64 final long start = currentTime();
65 do {
66 Thread t = new Thread() {
67 public void run() {
68 try (Connection connection = ds.getConnection()) {
69 System.err.println("Obtained connection " + connection);
70 quietlySleep(TimeUnit.SECONDS.toMillis((long)(10 + (Math.random() * 20))));
71 }
72 catch (SQLException e) {
73 e.printStackTrace();
74 }
75 }
76 };
77 t.setDaemon(true);
78 t.start();
79
80 quietlySleep(TimeUnit.SECONDS.toMillis((long)((Math.random() * 20))));
81 } while (elapsedMillis(start) < MINUTES.toMillis(15));
82 }
83 }
84
85 //@Test
86 public void testCase2() throws Exception
87 {
88 HikariConfig config = newHikariConfig();
89 config.setMinimumIdle(3);
90 config.setMaximumPoolSize(10);
91 config.setConnectionTimeout(1000);
92 config.setIdleTimeout(TimeUnit.SECONDS.toMillis(60));
93
94 config.setJdbcUrl("jdbc:pgsql://localhost:5432/test");
95 config.setUsername("brettw");
96
97 try (HikariDataSource ds = new HikariDataSource(config)) {
98
99 try (Connection conn = ds.getConnection()) {
100 System.err.println("\nGot a connection, and released it. Now, enable the firewall.");
101 }
102
103 getPool(ds).logPoolState();
104 quietlySleep(5000L);
105
106 System.err.println("\nNow attempting another getConnection(), expecting a timeout...");
107
108 long start = currentTime();
109 try (Connection conn = ds.getConnection()) {
110 System.err.println("\nOpps, got a connection. Did you enable the firewall? " + conn);
111 fail("Opps, got a connection. Did you enable the firewall?");
112 }
113 catch (SQLException e)
114 {
115 assertTrue("Timeout less than expected " + elapsedMillis(start) + "ms", elapsedMillis(start) > 5000);
116 }
117
118 System.err.println("\nOk, so far so good. Now, disable the firewall again. Attempting connection in 5 seconds...");
119 quietlySleep(5000L);
120 getPool(ds).logPoolState();
121
122 try (Connection conn = ds.getConnection()) {
123 System.err.println("\nGot a connection, and released it.");
124 }
125 }
126
127 System.err.println("\nPassed.");
128 }
129
130 //@Test
131 public void testCase3() throws Exception
132 {
133 HikariConfig config = newHikariConfig();
134 config.setMinimumIdle(3);
135 config.setMaximumPoolSize(10);
136 config.setConnectionTimeout(1000);
137 config.setIdleTimeout(TimeUnit.SECONDS.toMillis(60));
138
139 config.setJdbcUrl("jdbc:pgsql://localhost:5432/test");
140 config.setUsername("brettw");
141
142 try (final HikariDataSource ds = new HikariDataSource(config)) {
143 for (int i = 0; i < 10; i++) {
144 new Thread() {
145 public void run() {
146 try (Connection conn = ds.getConnection()) {
147 System.err.println("ERROR: should not have acquired connection.");
148 }
149 catch (SQLException e) {
150 // expected
151 }
152 }
153 }.start();
154 }
155
156 quietlySleep(5000L);
157
158 System.err.println("Now, bring the DB online. Checking pool in 15 seconds.");
159 quietlySleep(15000L);
160
161 getPool(ds).logPoolState();
162 }
163 }
164
165 // @Test
166 public void testCase4() throws Exception
167 {
168 HikariConfig config = newHikariConfig();
169 config.setMinimumIdle(0);
170 config.setMaximumPoolSize(15);
171 config.setConnectionTimeout(10000);
172 config.setIdleTimeout(TimeUnit.MINUTES.toMillis(1));
173 config.setMaxLifetime(TimeUnit.MINUTES.toMillis(2));
174 config.setRegisterMbeans(true);
175
176 config.setJdbcUrl("jdbc:postgresql://localhost:5432/netld");
177 config.setUsername("brettw");
178
179 try (final HikariDataSource ds = new HikariDataSource(config)) {
180
181 countdown(20);
182 List<Thread> threads = new ArrayList<>();
183 for (int i = 0; i < 20; i++) {
184 threads.add(new Thread() {
185 public void run() {
186 UtilityElf.quietlySleep((long)(Math.random() * 2500L));
187 final long start = currentTime();
188 do {
189 try (Connection conn = ds.getConnection(); Statement stmt = conn.createStatement()) {
190 try (ResultSet rs = stmt.executeQuery("SELECT * FROM device WHERE device_id=0 ORDER BY device_id LIMIT 1 OFFSET 0")) {
191 rs.next();
192 }
193 UtilityElf.quietlySleep(100L); //Math.max(50L, (long)(Math.random() * 250L)));
194 }
195 catch (SQLException e) {
196 e.printStackTrace();
197 // throw new RuntimeException(e);
198 }
199
200 // UtilityElf.quietlySleep(10L); //Math.max(50L, (long)(Math.random() * 250L)));
201 } while (elapsedMillis(start) < TimeUnit.MINUTES.toMillis(5));
202 }
203 });
204 }
205
206 // threads.forEach(t -> t.start());
207 // threads.forEach(t -> { try { t.join(); } catch (InterruptedException e) {} });
208 }
209 }
210
211 @Before
212 public void before()
213 {
214 System.err.println("\n");
215 }
216
217 private void countdown(int seconds)
218 {
219 do {
220 System.out.printf("Starting in %d seconds...\n", seconds);
221 if (seconds > 10) {
222 UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(10));
223 seconds -= 10;
224 }
225 else {
226 UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(1));
227 seconds -= 1;
228 }
229 } while (seconds > 0);
230 }
231 }