| 1 |
/* |
| 2 |
* Copyright (C) 2016 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.db; |
| 18 |
|
| 19 |
import com.zaxxer.hikari.HikariConfig; |
| 20 |
import com.zaxxer.hikari.HikariDataSource; |
| 21 |
import com.zaxxer.hikari.pool.HikariPool; |
| 22 |
import org.junit.Assert; |
| 23 |
import org.junit.Before; |
| 24 |
import org.junit.Test; |
| 25 |
|
| 26 |
import java.sql.Connection; |
| 27 |
import java.sql.SQLException; |
| 28 |
import java.sql.Statement; |
| 29 |
|
| 30 |
import static com.zaxxer.hikari.pool.TestElf.getPool; |
| 31 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 32 |
import static com.zaxxer.hikari.pool.TestElf.getUnsealedConfig; |
| 33 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 34 |
import static java.util.concurrent.TimeUnit.SECONDS; |
| 35 |
import static org.junit.Assert.assertEquals; |
| 36 |
import static org.junit.Assert.assertNotNull; |
| 37 |
|
| 38 |
/** |
| 39 |
* @author brettw |
| 40 |
* |
| 41 |
*/ |
| 42 |
public class BasicPoolTest |
| 43 |
{ |
| 44 |
@Before |
| 45 |
public void setup() throws SQLException |
| 46 |
{ |
| 47 |
HikariConfig config = newHikariConfig(); |
| 48 |
config.setMinimumIdle(1); |
| 49 |
config.setMaximumPoolSize(2); |
| 50 |
config.setConnectionTestQuery("SELECT 1"); |
| 51 |
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); |
| 52 |
config.addDataSourceProperty("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); |
| 53 |
|
| 54 |
try (HikariDataSource ds = new HikariDataSource(config); |
| 55 |
Connection conn = ds.getConnection(); |
| 56 |
Statement stmt = conn.createStatement()) { |
| 57 |
stmt.executeUpdate("DROP TABLE IF EXISTS basic_pool_test"); |
| 58 |
stmt.executeUpdate("CREATE TABLE basic_pool_test (" |
| 59 |
+ "id INTEGER NOT NULL IDENTITY PRIMARY KEY, " |
| 60 |
+ "timestamp TIMESTAMP, " |
| 61 |
+ "string VARCHAR(128), " |
| 62 |
+ "string_from_number NUMERIC " |
| 63 |
+ ")"); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void testIdleTimeout() throws InterruptedException, SQLException |
| 69 |
{ |
| 70 |
HikariConfig config = newHikariConfig(); |
| 71 |
config.setMinimumIdle(5); |
| 72 |
config.setMaximumPoolSize(10); |
| 73 |
config.setConnectionTestQuery("SELECT 1"); |
| 74 |
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); |
| 75 |
config.addDataSourceProperty("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); |
| 76 |
|
| 77 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "1000"); |
| 78 |
|
| 79 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 80 |
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs"); |
| 81 |
|
| 82 |
SECONDS.sleep(1); |
| 83 |
|
| 84 |
HikariPool pool = getPool(ds); |
| 85 |
|
| 86 |
getUnsealedConfig(ds).setIdleTimeout(3000); |
| 87 |
|
| 88 |
assertEquals("Total connections not as expected", 5, pool.getTotalConnections()); |
| 89 |
assertEquals("Idle connections not as expected", 5, pool.getIdleConnections()); |
| 90 |
|
| 91 |
try (Connection connection = ds.getConnection()) { |
| 92 |
Assert.assertNotNull(connection); |
| 93 |
|
| 94 |
MILLISECONDS.sleep(1500); |
| 95 |
|
| 96 |
assertEquals("Second total connections not as expected", 6, pool.getTotalConnections()); |
| 97 |
assertEquals("Second idle connections not as expected", 5, pool.getIdleConnections()); |
| 98 |
} |
| 99 |
|
| 100 |
assertEquals("Idle connections not as expected", 6, pool.getIdleConnections()); |
| 101 |
|
| 102 |
SECONDS.sleep(2); |
| 103 |
|
| 104 |
assertEquals("Third total connections not as expected", 5, pool.getTotalConnections()); |
| 105 |
assertEquals("Third idle connections not as expected", 5, pool.getIdleConnections()); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void testIdleTimeout2() throws InterruptedException, SQLException |
| 111 |
{ |
| 112 |
HikariConfig config = newHikariConfig(); |
| 113 |
config.setMaximumPoolSize(50); |
| 114 |
config.setConnectionTestQuery("SELECT 1"); |
| 115 |
config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource"); |
| 116 |
config.addDataSourceProperty("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); |
| 117 |
|
| 118 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "1000"); |
| 119 |
|
| 120 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 121 |
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs"); |
| 122 |
|
| 123 |
SECONDS.sleep(1); |
| 124 |
|
| 125 |
HikariPool pool = getPool(ds); |
| 126 |
|
| 127 |
getUnsealedConfig(ds).setIdleTimeout(3000); |
| 128 |
|
| 129 |
assertEquals("Total connections not as expected", 50, pool.getTotalConnections()); |
| 130 |
assertEquals("Idle connections not as expected", 50, pool.getIdleConnections()); |
| 131 |
|
| 132 |
try (Connection connection = ds.getConnection()) { |
| 133 |
assertNotNull(connection); |
| 134 |
|
| 135 |
MILLISECONDS.sleep(1500); |
| 136 |
|
| 137 |
assertEquals("Second total connections not as expected", 50, pool.getTotalConnections()); |
| 138 |
assertEquals("Second idle connections not as expected", 49, pool.getIdleConnections()); |
| 139 |
} |
| 140 |
|
| 141 |
assertEquals("Idle connections not as expected", 50, pool.getIdleConnections()); |
| 142 |
|
| 143 |
SECONDS.sleep(3); |
| 144 |
|
| 145 |
assertEquals("Third total connections not as expected", 50, pool.getTotalConnections()); |
| 146 |
assertEquals("Third idle connections not as expected", 50, pool.getIdleConnections()); |
| 147 |
} |
| 148 |
} |
| 149 |
} |