Search in sources :

Example 11 with StubDataSource

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();
        }
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 12 with StubDataSource

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
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) Test(org.junit.Test)

Example 13 with StubDataSource

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"));
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) HikariConfig(com.zaxxer.hikari.HikariConfig) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test)

Aggregations

HikariDataSource (com.zaxxer.hikari.HikariDataSource)13 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)13 Test (org.junit.Test)12 HikariConfig (com.zaxxer.hikari.HikariConfig)10 Connection (java.sql.Connection)10 StubConnection (com.zaxxer.hikari.mocks.StubConnection)8 SQLException (java.sql.SQLException)8 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)3 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 HikariConfigMXBean (com.zaxxer.hikari.HikariConfigMXBean)1 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 AbstractContext (org.osjava.sj.jndi.AbstractContext)1