use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class TestConnectionTimeoutRetry method testConnectionRetries5.
@Test
public void testConnectionRetries5() throws SQLException {
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(2);
config.setConnectionTimeout(1000);
config.setValidationTimeout(1000);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
final Connection connection1 = ds.getConnection();
long start = ClockSource.INSTANCE.currentTime();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
scheduler.schedule(new Runnable() {
@Override
public void run() {
try {
connection1.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}, 250, TimeUnit.MILLISECONDS);
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused"));
try {
Connection connection2 = ds.getConnection();
connection2.close();
long elapsed = ClockSource.INSTANCE.elapsedMillis(start);
Assert.assertTrue("Waited too long to get a connection.", (elapsed >= 250) && (elapsed < config.getConnectionTimeout()));
} catch (SQLException e) {
Assert.fail("Should not have timed out.");
} finally {
scheduler.shutdownNow();
}
}
}
use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class TestConnections method testInitializationFailure.
@Test
public void testInitializationFailure() throws SQLException {
StubDataSource stubDataSource = new StubDataSource();
stubDataSource.setThrowException(new SQLException("Connection refused"));
try (HikariDataSource ds = new HikariDataSource()) {
ds.setMinimumIdle(3);
ds.setMaximumPoolSize(3);
ds.setConnectionTimeout(2500);
ds.setAllowPoolSuspension(true);
ds.setConnectionTestQuery("VALUES 1");
ds.setDataSource(stubDataSource);
try (Connection c = ds.getConnection()) {
Assert.fail("Initialization should have failed");
} catch (PoolInitializationException e) {
// passed
}
}
}
use of com.zaxxer.hikari.mocks.StubDataSource in project HikariCP by brettwooldridge.
the class UnwrapTest method testUnwrapDataSource.
@Test
public void testUnwrapDataSource() throws SQLException {
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(1);
config.setInitializationFailFast(true);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
StubDataSource unwrap = ds.unwrap(StubDataSource.class);
Assert.assertNotNull(unwrap);
Assert.assertTrue(unwrap instanceof StubDataSource);
Assert.assertTrue(ds.isWrapperFor(HikariDataSource.class));
Assert.assertTrue(ds.unwrap(HikariDataSource.class) instanceof HikariDataSource);
Assert.assertFalse(ds.isWrapperFor(getClass()));
try {
ds.unwrap(getClass());
} catch (SQLException e) {
Assert.assertTrue(e.getMessage().contains("Wrapped DataSource"));
}
}
}
Aggregations