| 1 |
package com.zaxxer.hikari.pool; |
| 2 |
|
| 3 |
import com.zaxxer.hikari.HikariDataSource; |
| 4 |
import com.zaxxer.hikari.metrics.IMetricsTracker; |
| 5 |
import com.zaxxer.hikari.mocks.StubDataSource; |
| 6 |
import org.junit.Test; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.SQLTransientConnectionException; |
| 10 |
import java.util.concurrent.TimeUnit; |
| 11 |
|
| 12 |
import static com.zaxxer.hikari.pool.TestElf.newHikariDataSource; |
| 13 |
import static junit.framework.TestCase.fail; |
| 14 |
import static org.hamcrest.CoreMatchers.is; |
| 15 |
import static org.junit.Assert.assertThat; |
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
/** |
| 19 |
* @author [email protected] on 2/16/17. |
| 20 |
*/ |
| 21 |
public class MetricsTrackerTest |
| 22 |
{ |
| 23 |
|
| 24 |
@Test(expected = SQLTransientConnectionException.class) |
| 25 |
public void connectionTimeoutIsRecorded() throws Exception |
| 26 |
{ |
| 27 |
int timeoutMillis = 1000; |
| 28 |
int timeToCreateNewConnectionMillis = timeoutMillis * 2; |
| 29 |
|
| 30 |
StubDataSource stubDataSource = new StubDataSource(); |
| 31 |
stubDataSource.setConnectionAcquistionTime(timeToCreateNewConnectionMillis); |
| 32 |
|
| 33 |
StubMetricsTracker metricsTracker = new StubMetricsTracker(); |
| 34 |
|
| 35 |
try (HikariDataSource ds = newHikariDataSource()) { |
| 36 |
ds.setMinimumIdle(0); |
| 37 |
ds.setMaximumPoolSize(1); |
| 38 |
ds.setConnectionTimeout(timeoutMillis); |
| 39 |
ds.setDataSource(stubDataSource); |
| 40 |
ds.setMetricsTrackerFactory((poolName, poolStats) -> metricsTracker); |
| 41 |
|
| 42 |
try (Connection c = ds.getConnection()) { |
| 43 |
fail("Connection shouldn't have been successfully created due to configured connection timeout"); |
| 44 |
|
| 45 |
} finally { |
| 46 |
// assert that connection timeout was measured |
| 47 |
assertThat(metricsTracker.connectionTimeoutRecorded, is(true)); |
| 48 |
// assert that measured time to acquire connection should be roughly equal or greater than the configured connection timeout time |
| 49 |
assertTrue(metricsTracker.connectionAcquiredNanos >= TimeUnit.NANOSECONDS.convert(timeoutMillis, TimeUnit.MILLISECONDS)); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
private static class StubMetricsTracker implements IMetricsTracker |
| 55 |
{ |
| 56 |
|
| 57 |
private Long connectionCreatedMillis; |
| 58 |
private Long connectionAcquiredNanos; |
| 59 |
private Long connectionBorrowedMillis; |
| 60 |
private boolean connectionTimeoutRecorded; |
| 61 |
|
| 62 |
@Override |
| 63 |
public void recordConnectionCreatedMillis(long connectionCreatedMillis) |
| 64 |
{ |
| 65 |
this.connectionCreatedMillis = connectionCreatedMillis; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void recordConnectionAcquiredNanos(long elapsedAcquiredNanos) |
| 70 |
{ |
| 71 |
this.connectionAcquiredNanos = elapsedAcquiredNanos; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void recordConnectionUsageMillis(long elapsedBorrowedMillis) |
| 76 |
{ |
| 77 |
this.connectionBorrowedMillis = elapsedBorrowedMillis; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void recordConnectionTimeout() |
| 82 |
{ |
| 83 |
this.connectionTimeoutRecorded = true; |
| 84 |
} |
| 85 |
} |
| 86 |
} |