Search in sources :

Example 1 with StubConnection

use of com.zaxxer.hikari.mocks.StubConnection in project HikariCP by brettwooldridge.

the class UnwrapTest method testUnwrapConnection.

@Test
public void testUnwrapConnection() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(1);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        ds.getConnection().close();
        assertSame("Idle connections not as expected", 1, getPool(ds).getIdleConnections());
        Connection connection = ds.getConnection();
        assertNotNull(connection);
        StubConnection unwrapped = connection.unwrap(StubConnection.class);
        assertTrue("unwrapped connection is not instance of StubConnection: " + unwrapped, (unwrapped != null && unwrapped instanceof StubConnection));
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) StubConnection(com.zaxxer.hikari.mocks.StubConnection) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 2 with StubConnection

use of com.zaxxer.hikari.mocks.StubConnection in project HikariCP by brettwooldridge.

the class TestProxies method testOtherExceptions.

@Test
public void testOtherExceptions() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection conn = ds.getConnection()) {
            StubConnection stubConnection = conn.unwrap(StubConnection.class);
            stubConnection.throwException = true;
            try {
                conn.setTransactionIsolation(Connection.TRANSACTION_NONE);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.isReadOnly();
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.setReadOnly(false);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.setCatalog("");
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.setAutoCommit(false);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.clearWarnings();
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.isValid(0);
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.isWrapperFor(getClass());
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.unwrap(getClass());
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                conn.close();
                fail();
            } catch (SQLException e) {
            // pass
            }
            try {
                assertFalse(conn.isValid(0));
            } catch (SQLException e) {
                fail();
            }
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) StubConnection(com.zaxxer.hikari.mocks.StubConnection) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 3 with StubConnection

use of com.zaxxer.hikari.mocks.StubConnection in project HikariCP by brettwooldridge.

the class TestProxies method testStatementExceptions.

@Test
public void testStatementExceptions() throws SQLException {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(0);
    config.setMaximumPoolSize(1);
    config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(1));
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    try (HikariDataSource ds = new HikariDataSource(config)) {
        Connection conn = ds.getConnection();
        StubConnection stubConnection = conn.unwrap(StubConnection.class);
        stubConnection.throwException = true;
        try {
            conn.createStatement();
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.createStatement(0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.createStatement(0, 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareCall("");
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareCall("", 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareCall("", 0, 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("");
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", new int[0]);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", new String[0]);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
        try {
            conn.prepareStatement("", 0, 0, 0);
            fail();
        } catch (SQLException e) {
        // pass
        }
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) StubConnection(com.zaxxer.hikari.mocks.StubConnection) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) Test(org.junit.Test)

Example 4 with StubConnection

use of com.zaxxer.hikari.mocks.StubConnection in project HikariCP by brettwooldridge.

the class TestConnections method testInvalidConnectionTestQuery.

@Test
public void testInvalidConnectionTestQuery() {
    class BadConnection extends StubConnection {

        /** {@inheritDoc} */
        @Override
        public Statement createStatement() throws SQLException {
            throw new SQLException("Simulated exception in createStatement()");
        }
    }
    StubDataSource stubDataSource = new StubDataSource() {

        /** {@inheritDoc} */
        @Override
        public Connection getConnection() throws SQLException {
            return new BadConnection();
        }
    };
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(2);
    config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(3));
    config.setConnectionTestQuery("VALUES 1");
    config.setInitializationFailTimeout(TimeUnit.SECONDS.toMillis(2));
    config.setDataSource(stubDataSource);
    try (HikariDataSource ds = new HikariDataSource(config)) {
        try (Connection c = ds.getConnection()) {
            fail("getConnection() should have failed");
        } catch (SQLException e) {
            assertSame("Simulated exception in createStatement()", e.getNextException().getMessage());
        }
    } catch (PoolInitializationException e) {
        assertSame("Simulated exception in createStatement()", e.getCause().getMessage());
    }
    config.setInitializationFailTimeout(0);
    try (HikariDataSource ds = new HikariDataSource(config)) {
        fail("Initialization should have failed");
    } catch (PoolInitializationException e) {
    // passed
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) StubConnection(com.zaxxer.hikari.mocks.StubConnection) 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) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) Test(org.junit.Test) TestElf.setConfigUnitTest(com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)

Example 5 with StubConnection

use of com.zaxxer.hikari.mocks.StubConnection in project HikariCP by brettwooldridge.

the class TestConnections method testMaximumPoolLimit.

@Test
public void testMaximumPoolLimit() throws Exception {
    HikariConfig config = newHikariConfig();
    config.setMinimumIdle(1);
    config.setMaximumPoolSize(4);
    config.setConnectionTimeout(20000);
    config.setInitializationFailTimeout(0);
    config.setConnectionTestQuery("VALUES 1");
    config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
    final AtomicReference<Exception> ref = new AtomicReference<>();
    // reset counter
    StubConnection.count.set(0);
    try (final HikariDataSource ds = new HikariDataSource(config)) {
        final HikariPool pool = getPool(ds);
        Thread[] threads = new Thread[20];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        pool.logPoolState("Before acquire ");
                        try (Connection connection = ds.getConnection()) {
                            pool.logPoolState("After  acquire ");
                            quietlySleep(500);
                        }
                    } catch (Exception e) {
                        ref.set(e);
                    }
                }
            });
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        pool.logPoolState("before check ");
        assertNull((ref.get() != null ? ref.get().toString() : ""), ref.get());
        assertSame("StubConnection count not as expected", 4, StubConnection.count.get());
    }
}
Also used : TestElf.newHikariDataSource(com.zaxxer.hikari.pool.TestElf.newHikariDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Connection(java.sql.Connection) StubConnection(com.zaxxer.hikari.mocks.StubConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) HikariConfig(com.zaxxer.hikari.HikariConfig) TestElf.newHikariConfig(com.zaxxer.hikari.pool.TestElf.newHikariConfig) PoolInitializationException(com.zaxxer.hikari.pool.HikariPool.PoolInitializationException) SQLException(java.sql.SQLException) SQLTransientConnectionException(java.sql.SQLTransientConnectionException) Test(org.junit.Test) TestElf.setConfigUnitTest(com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)

Aggregations

HikariConfig (com.zaxxer.hikari.HikariConfig)7 HikariDataSource (com.zaxxer.hikari.HikariDataSource)7 StubConnection (com.zaxxer.hikari.mocks.StubConnection)7 TestElf.newHikariConfig (com.zaxxer.hikari.pool.TestElf.newHikariConfig)7 Connection (java.sql.Connection)7 Test (org.junit.Test)7 SQLException (java.sql.SQLException)5 PoolInitializationException (com.zaxxer.hikari.pool.HikariPool.PoolInitializationException)2 TestElf.newHikariDataSource (com.zaxxer.hikari.pool.TestElf.newHikariDataSource)2 TestElf.setConfigUnitTest (com.zaxxer.hikari.pool.TestElf.setConfigUnitTest)2 StubDataSource (com.zaxxer.hikari.mocks.StubDataSource)1 SQLTransientConnectionException (java.sql.SQLTransientConnectionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1