|
|
|
@ -12,7 +12,8 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
import static com.zaxxer.hikari.pool.TestElf.newHikariDataSource;
|
|
|
|
|
import static junit.framework.TestCase.fail;
|
|
|
|
|
import static org.hamcrest.CoreMatchers.is;
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
|
import static org.hamcrest.MatcherAssert.assertThat;
|
|
|
|
|
import static org.junit.Assert.assertThrows;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -20,40 +21,43 @@ import static org.junit.Assert.assertTrue;
|
|
|
|
|
*/
|
|
|
|
|
public class MetricsTrackerTest
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
@Test(expected = SQLTransientConnectionException.class)
|
|
|
|
|
@Test
|
|
|
|
|
public void connectionTimeoutIsRecorded() throws Exception
|
|
|
|
|
{
|
|
|
|
|
int timeoutMillis = 1000;
|
|
|
|
|
int timeToCreateNewConnectionMillis = timeoutMillis * 2;
|
|
|
|
|
assertThrows(SQLTransientConnectionException.class, () -> {
|
|
|
|
|
int timeoutMillis = 1000;
|
|
|
|
|
int timeToCreateNewConnectionMillis = timeoutMillis * 2;
|
|
|
|
|
|
|
|
|
|
StubDataSource stubDataSource = new StubDataSource();
|
|
|
|
|
stubDataSource.setConnectionAcquistionTime(timeToCreateNewConnectionMillis);
|
|
|
|
|
|
|
|
|
|
StubDataSource stubDataSource = new StubDataSource();
|
|
|
|
|
stubDataSource.setConnectionAcquistionTime(timeToCreateNewConnectionMillis);
|
|
|
|
|
StubMetricsTracker metricsTracker = new StubMetricsTracker();
|
|
|
|
|
|
|
|
|
|
StubMetricsTracker metricsTracker = new StubMetricsTracker();
|
|
|
|
|
try (HikariDataSource ds = newHikariDataSource()) {
|
|
|
|
|
ds.setMinimumIdle(0);
|
|
|
|
|
ds.setMaximumPoolSize(1);
|
|
|
|
|
ds.setConnectionTimeout(timeoutMillis);
|
|
|
|
|
ds.setDataSource(stubDataSource);
|
|
|
|
|
ds.setMetricsTrackerFactory((poolName, poolStats) -> metricsTracker);
|
|
|
|
|
|
|
|
|
|
try (HikariDataSource ds = newHikariDataSource()) {
|
|
|
|
|
ds.setMinimumIdle(0);
|
|
|
|
|
ds.setMaximumPoolSize(1);
|
|
|
|
|
ds.setConnectionTimeout(timeoutMillis);
|
|
|
|
|
ds.setDataSource(stubDataSource);
|
|
|
|
|
ds.setMetricsTrackerFactory((poolName, poolStats) -> metricsTracker);
|
|
|
|
|
try (Connection c = ds.getConnection()) {
|
|
|
|
|
fail("Connection shouldn't have been successfully created due to configured connection timeout");
|
|
|
|
|
|
|
|
|
|
try (Connection c = ds.getConnection()) {
|
|
|
|
|
fail("Connection shouldn't have been successfully created due to configured connection timeout");
|
|
|
|
|
} finally {
|
|
|
|
|
// assert that connection timeout was measured
|
|
|
|
|
assertThat(metricsTracker.connectionTimeoutRecorded, is(true));
|
|
|
|
|
// assert that measured time to acquire connection should be roughly equal or greater than the configured connection timeout time
|
|
|
|
|
assertTrue(metricsTracker.connectionAcquiredNanos >= TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS));
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
// assert that connection timeout was measured
|
|
|
|
|
assertThat(metricsTracker.connectionTimeoutRecorded, is(true));
|
|
|
|
|
// assert that measured time to acquire connection should be roughly equal or greater than the configured connection timeout time
|
|
|
|
|
assertTrue(metricsTracker.connectionAcquiredNanos >= TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS));
|
|
|
|
|
metricsTracker.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
|
|
|
|
private static class StubMetricsTracker implements IMetricsTracker
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private Long connectionCreatedMillis;
|
|
|
|
|
private Long connectionAcquiredNanos;
|
|
|
|
|
private Long connectionBorrowedMillis;
|
|
|
|
|