Search in sources :

Example 1 with StubDataSource

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();
        }
    }
}
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) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test)

Example 2 with StubDataSource

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

Example 3 with StubDataSource

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

Example 4 with StubDataSource

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

Example 5 with StubDataSource

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
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) StubDataSource(com.zaxxer.hikari.mocks.StubDataSource) Test(org.junit.Test)

Aggregations

HikariDataSource (com.zaxxer.hikari.HikariDataSource)11 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)11 Test (org.junit.Test)10 HikariConfig (com.zaxxer.hikari.HikariConfig)9 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)7 Connection (java.sql.Connection)7 StubConnection (com.zaxxer.hikari.mocks.StubConnection)6 SQLException (java.sql.SQLException)5 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