| 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 |
package com.zaxxer.hikari.pool; |
| 17 |
|
| 18 |
import com.zaxxer.hikari.HikariConfig; |
| 19 |
import com.zaxxer.hikari.HikariConfigMXBean; |
| 20 |
import com.zaxxer.hikari.HikariDataSource; |
| 21 |
import com.zaxxer.hikari.HikariPoolMXBean; |
| 22 |
import com.zaxxer.hikari.mocks.StubDataSource; |
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import javax.management.JMX; |
| 26 |
import javax.management.MBeanServer; |
| 27 |
import javax.management.MalformedObjectNameException; |
| 28 |
import javax.management.ObjectName; |
| 29 |
import java.lang.management.ManagementFactory; |
| 30 |
import java.sql.Connection; |
| 31 |
import java.sql.SQLException; |
| 32 |
import java.util.concurrent.TimeUnit; |
| 33 |
|
| 34 |
import static com.zaxxer.hikari.pool.TestElf.getUnsealedConfig; |
| 35 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 36 |
import static com.zaxxer.hikari.util.UtilityElf.quietlySleep; |
| 37 |
import static org.junit.Assert.assertEquals; |
| 38 |
import static org.junit.Assert.fail; |
| 39 |
|
| 40 |
public class TestMBean |
| 41 |
{ |
| 42 |
@Test |
| 43 |
public void testMBeanRegistration() { |
| 44 |
HikariConfig config = newHikariConfig(); |
| 45 |
config.setMinimumIdle(0); |
| 46 |
config.setMaximumPoolSize(1); |
| 47 |
config.setRegisterMbeans(true); |
| 48 |
config.setConnectionTimeout(2800); |
| 49 |
config.setConnectionTestQuery("VALUES 1"); |
| 50 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 51 |
|
| 52 |
new HikariDataSource(config).close(); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void testMBeanReporting() throws SQLException, InterruptedException, MalformedObjectNameException { |
| 57 |
HikariConfig config = newHikariConfig(); |
| 58 |
config.setMinimumIdle(3); |
| 59 |
config.setMaximumPoolSize(5); |
| 60 |
config.setRegisterMbeans(true); |
| 61 |
config.setConnectionTimeout(2800); |
| 62 |
config.setConnectionTestQuery("VALUES 1"); |
| 63 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 64 |
|
| 65 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100"); |
| 66 |
|
| 67 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 68 |
|
| 69 |
getUnsealedConfig(ds).setIdleTimeout(3000); |
| 70 |
|
| 71 |
TimeUnit.SECONDS.sleep(1); |
| 72 |
|
| 73 |
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); |
| 74 |
ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (testMBeanReporting)"); |
| 75 |
HikariPoolMXBean hikariPoolMXBean = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class); |
| 76 |
|
| 77 |
assertEquals(0, hikariPoolMXBean.getActiveConnections()); |
| 78 |
assertEquals(3, hikariPoolMXBean.getIdleConnections()); |
| 79 |
|
| 80 |
try (Connection ignored = ds.getConnection()) { |
| 81 |
assertEquals(1, hikariPoolMXBean.getActiveConnections()); |
| 82 |
|
| 83 |
TimeUnit.SECONDS.sleep(1); |
| 84 |
|
| 85 |
assertEquals(3, hikariPoolMXBean.getIdleConnections()); |
| 86 |
assertEquals(4, hikariPoolMXBean.getTotalConnections()); |
| 87 |
} |
| 88 |
|
| 89 |
TimeUnit.SECONDS.sleep(2); |
| 90 |
|
| 91 |
assertEquals(0, hikariPoolMXBean.getActiveConnections()); |
| 92 |
assertEquals(3, hikariPoolMXBean.getIdleConnections()); |
| 93 |
assertEquals(3, hikariPoolMXBean.getTotalConnections()); |
| 94 |
|
| 95 |
} |
| 96 |
finally { |
| 97 |
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs"); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void testMBeanChange() { |
| 103 |
HikariConfig config = newHikariConfig(); |
| 104 |
config.setMinimumIdle(3); |
| 105 |
config.setMaximumPoolSize(5); |
| 106 |
config.setRegisterMbeans(true); |
| 107 |
config.setConnectionTimeout(2800); |
| 108 |
config.setConnectionTestQuery("VALUES 1"); |
| 109 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 110 |
|
| 111 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 112 |
HikariConfigMXBean hikariConfigMXBean = ds.getHikariConfigMXBean(); |
| 113 |
hikariConfigMXBean.setIdleTimeout(3000); |
| 114 |
|
| 115 |
assertEquals(3000, ds.getIdleTimeout()); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void testMBeanConnectionTimeoutChange() throws SQLException { |
| 121 |
HikariConfig config = newHikariConfig(); |
| 122 |
config.setMinimumIdle(1); |
| 123 |
config.setMaximumPoolSize(2); |
| 124 |
config.setRegisterMbeans(true); |
| 125 |
config.setConnectionTimeout(2800); |
| 126 |
config.setConnectionTestQuery("VALUES 1"); |
| 127 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 128 |
|
| 129 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "250"); |
| 130 |
|
| 131 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 132 |
HikariConfigMXBean hikariConfigMXBean = ds.getHikariConfigMXBean(); |
| 133 |
assertEquals(2800, hikariConfigMXBean.getConnectionTimeout()); |
| 134 |
|
| 135 |
final StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); |
| 136 |
// connection acquisition takes more than 0 ms in a real system |
| 137 |
stubDataSource.setConnectionAcquistionTime(1200); |
| 138 |
|
| 139 |
hikariConfigMXBean.setConnectionTimeout(1000); |
| 140 |
|
| 141 |
quietlySleep(500); |
| 142 |
|
| 143 |
try (Connection conn1 = ds.getConnection(); |
| 144 |
Connection conn2 = ds.getConnection()) { |
| 145 |
fail("Connection should have timed out."); |
| 146 |
conn1.close(); |
| 147 |
conn2.close(); |
| 148 |
} |
| 149 |
catch (SQLException e) { |
| 150 |
assertEquals(1000, ds.getConnectionTimeout()); |
| 151 |
} |
| 152 |
} |
| 153 |
finally { |
| 154 |
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs"); |
| 155 |
} |
| 156 |
} |
| 157 |
} |