use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class TestConnectionTimeoutRetry method testConnectionRetries2.
@Test
public void testConnectionRetries2() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setConnectionTimeout(2800);
config.setValidationTimeout(2800);
config.setInitializationFailTimeout(0);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
final StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused"));
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.schedule(new Runnable() {
@Override
public void run() {
stubDataSource.setThrowException(null);
}
}, 300, TimeUnit.MILLISECONDS);
long start = currentTime();
try {
try (Connection connection = ds.getConnection()) {
// close immediately
}
long elapsed = elapsedMillis(start);
assertTrue("Connection returned too quickly, something is wrong.", elapsed > 250);
assertTrue("Waited too long to get a connection.", elapsed < config.getConnectionTimeout());
} catch (SQLException e) {
fail("Should not have timed out: " + e.getMessage());
} finally {
scheduler.shutdownNow();
}
}
}
use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class TestConnectionTimeoutRetry method testConnectionRetries.
@Test
public void testConnectionRetries() throws SQLException {
HikariConfig config = newHikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(1);
config.setConnectionTimeout(2800);
config.setValidationTimeout(2800);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused"));
long start = currentTime();
try (Connection connection = ds.getConnection()) {
connection.close();
fail("Should not have been able to get a connection.");
} catch (SQLException e) {
long elapsed = elapsedMillis(start);
long timeout = config.getConnectionTimeout();
assertTrue("Didn't wait long enough for timeout", (elapsed >= timeout));
}
}
}
use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class MetricsTrackerTest method connectionTimeoutIsRecorded.
@Test(expected = SQLTransientConnectionException.class)
public void connectionTimeoutIsRecorded() throws Exception {
int timeoutMillis = 1000;
int timeToCreateNewConnectionMillis = timeoutMillis * 2;
StubDataSource stubDataSource = new StubDataSource();
stubDataSource.setConnectionAcquistionTime(timeToCreateNewConnectionMillis);
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 (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));
}
}
}
use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class TestConnections method testInitializationFailure2.
@Test
public void testInitializationFailure2() throws SQLException {
StubDataSource stubDataSource = new StubDataSource();
stubDataSource.setThrowException(new SQLException("Connection refused"));
HikariConfig config = newHikariConfig();
config.setMinimumIdle(1);
config.setConnectionTestQuery("VALUES 1");
config.setDataSource(stubDataSource);
try (HikariDataSource ds = new HikariDataSource(config);
Connection ignored = ds.getConnection()) {
fail("Initialization should have failed");
} catch (PoolInitializationException e) {
// passed
}
}
use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class TestConnections method testInitializationFailure1.
@Test
public void testInitializationFailure1() {
StubDataSource stubDataSource = new StubDataSource();
stubDataSource.setThrowException(new SQLException("Connection refused"));
try (HikariDataSource ds = newHikariDataSource()) {
ds.setMinimumIdle(1);
ds.setMaximumPoolSize(1);
ds.setConnectionTimeout(2500);
ds.setConnectionTestQuery("VALUES 1");
ds.setDataSource(stubDataSource);
try (Connection ignored = ds.getConnection()) {
fail("Initialization should have failed");
} catch (SQLException e) {
// passed
}
}
}
Aggregations