More test coverage.
parent
7a46803cf5
commit
e43c11f7ca
@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package com.zaxxer.hikari;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.mocks.MockDataSource;
|
||||||
|
import com.zaxxer.hikari.util.PoolUtilities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for cases when db network connectivity goes down and close is called on existing connections. By default Hikari
|
||||||
|
* blocks longer than getMaximumTimeout (it can hang for a lot of time depending on driver timeout settings). Closing
|
||||||
|
* async the connections fixes this issue.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestConnectionCloseBlocking {
|
||||||
|
@Test
|
||||||
|
public void testConnectionCloseBlocking() throws SQLException {
|
||||||
|
HikariConfig config = new HikariConfig();
|
||||||
|
config.setMinimumIdle(0);
|
||||||
|
config.setMaximumPoolSize(1);
|
||||||
|
config.setConnectionTimeout(1500);
|
||||||
|
config.setDataSource(new CustomMockDataSource());
|
||||||
|
|
||||||
|
HikariDataSource ds = new HikariDataSource(config);
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
Connection connection = ds.getConnection();
|
||||||
|
connection.close();
|
||||||
|
// Hikari only checks for validity for connections with lastAccess > 1000 ms so we sleep for 1001 ms to force
|
||||||
|
// Hikari to do a connection validation which will fail and will trigger the connection to be closed
|
||||||
|
PoolUtilities.quietlySleep(1001);
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
connection = ds.getConnection(); // on physical connection close we sleep 2 seconds
|
||||||
|
Assert.assertTrue("Waited longer than timeout",
|
||||||
|
(PoolUtilities.elapsedTimeMs(start) < config.getConnectionTimeout()));
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Assert.assertTrue("getConnection failed because close connection took longer than timeout",
|
||||||
|
(PoolUtilities.elapsedTimeMs(start) < config.getConnectionTimeout()));
|
||||||
|
} finally {
|
||||||
|
ds.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CustomMockDataSource extends MockDataSource {
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() throws SQLException {
|
||||||
|
Connection mockConnection = super.getConnection();
|
||||||
|
when(mockConnection.isValid(anyInt())).thenReturn(false);
|
||||||
|
doAnswer(new Answer<Void>() {
|
||||||
|
@Override
|
||||||
|
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||||
|
TimeUnit.SECONDS.sleep(2);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}).when(mockConnection).close();
|
||||||
|
return mockConnection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
package com.zaxxer.hikari;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.mocks.StubConnection;
|
||||||
|
import com.zaxxer.hikari.mocks.StubStatement;
|
||||||
|
|
||||||
|
public class TestProxies
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void testProxyCreation() throws SQLException
|
||||||
|
{
|
||||||
|
HikariConfig config = new HikariConfig();
|
||||||
|
config.setMinimumIdle(0);
|
||||||
|
config.setMaximumPoolSize(1);
|
||||||
|
config.setConnectionTestQuery("VALUES 1");
|
||||||
|
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
|
||||||
|
|
||||||
|
HikariDataSource ds = new HikariDataSource(config);
|
||||||
|
try {
|
||||||
|
Connection conn = ds.getConnection();
|
||||||
|
|
||||||
|
Assert.assertNotNull(conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE));
|
||||||
|
Assert.assertNotNull(conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT));
|
||||||
|
Assert.assertNotNull(conn.prepareCall("some sql"));
|
||||||
|
Assert.assertNotNull(conn.prepareCall("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE));
|
||||||
|
Assert.assertNotNull(conn.prepareCall("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", PreparedStatement.NO_GENERATED_KEYS));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", new int[3]));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", new String[3]));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT));
|
||||||
|
Assert.assertNotNull(conn.toString());
|
||||||
|
|
||||||
|
Assert.assertTrue(conn.isWrapperFor(Connection.class));
|
||||||
|
Assert.assertTrue(conn.isValid(10));
|
||||||
|
Assert.assertFalse(conn.isClosed());
|
||||||
|
Assert.assertTrue(conn.unwrap(StubConnection.class) instanceof StubConnection);
|
||||||
|
try {
|
||||||
|
conn.unwrap(TestProxies.class);
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ds.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStatementProxy() throws SQLException
|
||||||
|
{
|
||||||
|
HikariConfig config = new HikariConfig();
|
||||||
|
config.setMinimumIdle(0);
|
||||||
|
config.setMaximumPoolSize(1);
|
||||||
|
config.setConnectionTestQuery("VALUES 1");
|
||||||
|
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
|
||||||
|
|
||||||
|
HikariDataSource ds = new HikariDataSource(config);
|
||||||
|
try {
|
||||||
|
Connection conn = ds.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement stmt = conn.prepareStatement("some sql");
|
||||||
|
stmt.executeQuery();
|
||||||
|
stmt.executeQuery("some sql");
|
||||||
|
Assert.assertFalse(stmt.isClosed());
|
||||||
|
Assert.assertNotNull(stmt.getGeneratedKeys());
|
||||||
|
Assert.assertNotNull(stmt.getResultSet());
|
||||||
|
Assert.assertNotNull(stmt.getConnection());
|
||||||
|
Assert.assertTrue(stmt.unwrap(StubStatement.class) instanceof StubStatement);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ds.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Brett Wooldridge
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.zaxxer.hikari;
|
||||||
|
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.util.ConcurrentBag;
|
||||||
|
import com.zaxxer.hikari.util.ConcurrentBag.BagEntry;
|
||||||
|
import com.zaxxer.hikari.util.PoolUtilities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Brett Wooldridge
|
||||||
|
*/
|
||||||
|
public class MiscTest
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void testLogWriter() throws SQLException
|
||||||
|
{
|
||||||
|
HikariConfig config = new HikariConfig();
|
||||||
|
config.setMinimumIdle(1);
|
||||||
|
config.setMaximumPoolSize(4);
|
||||||
|
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
|
||||||
|
|
||||||
|
final HikariDataSource ds = new HikariDataSource(config);
|
||||||
|
try {
|
||||||
|
PrintWriter writer = new PrintWriter(System.out);
|
||||||
|
ds.setLogWriter(writer);
|
||||||
|
Assert.assertSame(writer, ds.getLogWriter());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ds.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalidIsolation()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
PoolUtilities.getTransactionIsolation("INVALID");
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Assert.assertTrue(e instanceof IllegalArgumentException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testCreateInstance()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
PoolUtilities.createInstance("invalid", null);
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
catch (RuntimeException e) {
|
||||||
|
Assert.assertTrue(e.getCause() instanceof ClassNotFoundException);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBagDump()
|
||||||
|
{
|
||||||
|
ConcurrentBag<BagEntry> bag = new ConcurrentBag<BagEntry>(null);
|
||||||
|
bag.dumpState();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
package com.zaxxer.hikari;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.mocks.StubConnection;
|
||||||
|
import com.zaxxer.hikari.mocks.StubStatement;
|
||||||
|
|
||||||
|
public class TestProxies
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void testProxyCreation() throws SQLException
|
||||||
|
{
|
||||||
|
HikariConfig config = new HikariConfig();
|
||||||
|
config.setMinimumIdle(0);
|
||||||
|
config.setMaximumPoolSize(1);
|
||||||
|
config.setConnectionTestQuery("VALUES 1");
|
||||||
|
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
|
||||||
|
|
||||||
|
HikariDataSource ds = new HikariDataSource(config);
|
||||||
|
try {
|
||||||
|
Connection conn = ds.getConnection();
|
||||||
|
|
||||||
|
Assert.assertNotNull(conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE));
|
||||||
|
Assert.assertNotNull(conn.createStatement(ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT));
|
||||||
|
Assert.assertNotNull(conn.prepareCall("some sql"));
|
||||||
|
Assert.assertNotNull(conn.prepareCall("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE));
|
||||||
|
Assert.assertNotNull(conn.prepareCall("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", PreparedStatement.NO_GENERATED_KEYS));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", new int[3]));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", new String[3]));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE));
|
||||||
|
Assert.assertNotNull(conn.prepareStatement("some sql", ResultSet.FETCH_FORWARD, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.HOLD_CURSORS_OVER_COMMIT));
|
||||||
|
Assert.assertNotNull(conn.toString());
|
||||||
|
|
||||||
|
Assert.assertTrue(conn.isWrapperFor(Connection.class));
|
||||||
|
Assert.assertTrue(conn.isValid(10));
|
||||||
|
Assert.assertFalse(conn.isClosed());
|
||||||
|
Assert.assertTrue(conn.unwrap(StubConnection.class) instanceof StubConnection);
|
||||||
|
try {
|
||||||
|
conn.unwrap(TestProxies.class);
|
||||||
|
Assert.fail();
|
||||||
|
}
|
||||||
|
catch (SQLException e) {
|
||||||
|
// pass
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ds.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStatementProxy() throws SQLException
|
||||||
|
{
|
||||||
|
HikariConfig config = new HikariConfig();
|
||||||
|
config.setMinimumIdle(0);
|
||||||
|
config.setMaximumPoolSize(1);
|
||||||
|
config.setConnectionTestQuery("VALUES 1");
|
||||||
|
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
|
||||||
|
|
||||||
|
HikariDataSource ds = new HikariDataSource(config);
|
||||||
|
try {
|
||||||
|
Connection conn = ds.getConnection();
|
||||||
|
|
||||||
|
PreparedStatement stmt = conn.prepareStatement("some sql");
|
||||||
|
stmt.executeQuery();
|
||||||
|
stmt.executeQuery("some sql");
|
||||||
|
Assert.assertFalse(stmt.isClosed());
|
||||||
|
Assert.assertNotNull(stmt.getGeneratedKeys());
|
||||||
|
Assert.assertNotNull(stmt.getResultSet());
|
||||||
|
Assert.assertNotNull(stmt.getConnection());
|
||||||
|
Assert.assertTrue(stmt.unwrap(StubStatement.class) instanceof StubStatement);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ds.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue