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));
}
}
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();
}
}
}
}
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
}
}
}
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
}
}
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());
}
}
Aggregations