Misc. compiler warning cleanups. Still quite a few remaining in the tests...

pull/712/head
Brett Wooldridge 9 years ago
parent d16f4eee5b
commit 15ef1bd290

@ -202,11 +202,8 @@ public abstract class ProxyConnection implements Connection
final int size = openStatements.size();
if (size > 0) {
for (int i = 0; i < size && delegate != ClosedConnection.CLOSED_CONNECTION; i++) {
try {
final Statement statement = openStatements.get(i);
if (statement != null) {
statement.close();
}
try (Statement statement = openStatements.get(i)) {
// automatic resource cleanup
}
catch (SQLException e) {
checkException(e);

@ -104,6 +104,9 @@ public final class JavassistProxyFactory
case "getProxyResultSet":
method.setBody("{return new " + packageName + ".HikariProxyResultSet($$);}");
break;
default:
// unhandled method
break;
}
}

@ -80,24 +80,24 @@ public class BasicPoolTest
ds.setIdleTimeout(3000);
Assert.assertSame("Total connections not as expected", 5, pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 5, pool.getIdleConnections());
Assert.assertEquals("Total connections not as expected", 5, pool.getTotalConnections());
Assert.assertEquals("Idle connections not as expected", 5, pool.getIdleConnections());
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
try (Connection connection = ds.getConnection()) {
Assert.assertNotNull(connection);
TimeUnit.MILLISECONDS.sleep(1500);
Assert.assertEquals("Second total connections not as expected", 6, pool.getTotalConnections());
Assert.assertEquals("Second idle connections not as expected", 5, pool.getIdleConnections());
}
TimeUnit.MILLISECONDS.sleep(1500);
Assert.assertSame("Second total connections not as expected", 6, pool.getTotalConnections());
Assert.assertSame("Second idle connections not as expected", 5, pool.getIdleConnections());
connection.close();
Assert.assertSame("Idle connections not as expected", 6, pool.getIdleConnections());
Assert.assertEquals("Idle connections not as expected", 6, pool.getIdleConnections());
TimeUnit.SECONDS.sleep(2);
Assert.assertSame("Third total connections not as expected", 5, pool.getTotalConnections());
Assert.assertSame("Third idle connections not as expected", 5, pool.getIdleConnections());
Assert.assertEquals("Third total connections not as expected", 5, pool.getTotalConnections());
Assert.assertEquals("Third idle connections not as expected", 5, pool.getIdleConnections());
}
}
@ -121,24 +121,24 @@ public class BasicPoolTest
ds.setIdleTimeout(3000);
Assert.assertSame("Total connections not as expected", 50, pool.getTotalConnections());
Assert.assertSame("Idle connections not as expected", 50, pool.getIdleConnections());
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
TimeUnit.MILLISECONDS.sleep(1500);
Assert.assertEquals("Total connections not as expected", 50, pool.getTotalConnections());
Assert.assertEquals("Idle connections not as expected", 50, pool.getIdleConnections());
Assert.assertSame("Second total connections not as expected", 50, pool.getTotalConnections());
Assert.assertSame("Second idle connections not as expected", 49, pool.getIdleConnections());
connection.close();
try (Connection connection = ds.getConnection()) {
Assert.assertNotNull(connection);
TimeUnit.MILLISECONDS.sleep(1500);
Assert.assertEquals("Second total connections not as expected", 50, pool.getTotalConnections());
Assert.assertEquals("Second idle connections not as expected", 49, pool.getIdleConnections());
}
Assert.assertSame("Idle connections not as expected", 50, pool.getIdleConnections());
Assert.assertEquals("Idle connections not as expected", 50, pool.getIdleConnections());
TimeUnit.SECONDS.sleep(3);
Assert.assertSame("Third total connections not as expected", 50, pool.getTotalConnections());
Assert.assertSame("Third idle connections not as expected", 50, pool.getIdleConnections());
Assert.assertEquals("Third total connections not as expected", 50, pool.getTotalConnections());
Assert.assertEquals("Third idle connections not as expected", 50, pool.getIdleConnections());
}
}
}

@ -20,6 +20,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.After;
import org.junit.Assert;
@ -31,91 +32,82 @@ import com.zaxxer.hikari.HikariDataSource;
public class ExceptionTest
{
private HikariDataSource ds;
@Before
public void setup()
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(2);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
ds = new HikariDataSource(config);
}
@After
public void teardown()
{
ds.close();
}
@Test
public void testException1() throws SQLException
{
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?");
Assert.assertNotNull(statement);
ResultSet resultSet = statement.executeQuery();
Assert.assertNotNull(resultSet);
try
{
private HikariDataSource ds;
@Before
public void setup()
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(2);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
ds = new HikariDataSource(config);
}
@After
public void teardown()
{
ds.close();
}
@Test
public void testException1() throws SQLException
{
try (Connection connection = ds.getConnection()) {
Assert.assertNotNull(connection);
PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?");
Assert.assertNotNull(statement);
ResultSet resultSet = statement.executeQuery();
Assert.assertNotNull(resultSet);
try {
statement.getMaxFieldSize();
Assert.fail();
}
catch (Exception e)
{
}
catch (Exception e) {
Assert.assertSame(SQLException.class, e.getClass());
}
connection.close();
HikariPool pool = TestElf.getPool(ds);
Assert.assertTrue("Total (3) connections not as expected", pool.getTotalConnections() >= 0);
Assert.assertTrue("Idle (3) connections not as expected", pool.getIdleConnections() >= 0);
}
@Test
public void testUseAfterStatementClose() throws SQLException
{
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
try
{
PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?");
statement.close();
statement.getMoreResults();
}
}
HikariPool pool = TestElf.getPool(ds);
Assert.assertTrue("Total (3) connections not as expected", pool.getTotalConnections() >= 0);
Assert.assertTrue("Idle (3) connections not as expected", pool.getIdleConnections() >= 0);
}
@Test
public void testUseAfterStatementClose() throws SQLException
{
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
try (Statement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?")) {
statement.close();
statement.getMoreResults();
Assert.fail();
}
catch (SQLException e) {
Assert.assertSame("Connection is closed", e.getMessage());
}
}
@Test
public void testUseAfterClose() throws SQLException
{
try (Connection connection = ds.getConnection()) {
Assert.assertNotNull(connection);
connection.close();
try (Statement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?")) {
Assert.fail();
}
catch (SQLException e)
{
}
catch (SQLException e) {
Assert.assertSame("Connection is closed", e.getMessage());
}
}
@Test
public void testUseAfterClose() throws SQLException
{
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
connection.close();
try
{
connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?");
Assert.fail();
}
catch (SQLException e)
{
Assert.assertSame("Connection is closed", e.getMessage());
}
}
}
}
}
}

@ -35,14 +35,16 @@ public class IsolationTest
ds.setIsolateInternalQueries(true);
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
Connection connection = ds.getConnection();
connection.close();
try (Connection connection = ds.getConnection()) {
connection.close();
Connection connection2 = ds.getConnection();
connection2.close();
try (Connection connection2 = ds.getConnection()) {
connection2.close();
Assert.assertNotSame(connection, connection2);
Assert.assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
Assert.assertNotSame(connection, connection2);
Assert.assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
}
}
}
}
@ -55,13 +57,15 @@ public class IsolationTest
ds.setIsolateInternalQueries(false);
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
Connection connection = ds.getConnection();
connection.close();
try (Connection connection = ds.getConnection()) {
connection.close();
Connection connection2 = ds.getConnection();
connection2.close();
try (Connection connection2 = ds.getConnection()) {
connection2.close();
Assert.assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
Assert.assertSame(connection.unwrap(Connection.class), connection2.unwrap(Connection.class));
}
}
}
}
}

@ -57,8 +57,9 @@ public class JdbcDriverTest
DriverDataSource unwrap = ds.unwrap(DriverDataSource.class);
Assert.assertNotNull(unwrap);
Connection connection = ds.getConnection();
connection.close();
try (Connection connection = ds.getConnection()) {
// test that getConnection() succeeds
}
}
@Test

@ -87,35 +87,37 @@ public class MiscTest
public void testLeakDetection() throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(Class.forName("com.zaxxer.hikari.pool.ProxyLeakTask"), ps);
TestElf.setConfigUnitTest(true);
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(4);
config.setPoolName("test");
config.setThreadFactory(Executors.defaultThreadFactory());
config.setMetricRegistry(null);
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(1));
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
TestElf.setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
TestElf.getPool(ds).logPoolState();
Connection connection = ds.getConnection();
UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(4));
connection.close();
UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(1));
ps.close();
String s = new String(baos.toByteArray());
Assert.assertNotNull("Exception string was null", s);
Assert.assertTrue("Expected exception to contain 'Connection leak detection' but contains *" + s + "*", s.contains("Connection leak detection"));
}
finally
{
TestElf.setConfigUnitTest(false);
try (PrintStream ps = new PrintStream(baos, true)) {
TestElf.setSlf4jTargetStream(Class.forName("com.zaxxer.hikari.pool.ProxyLeakTask"), ps);
TestElf.setConfigUnitTest(true);
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(4);
config.setPoolName("test");
config.setThreadFactory(Executors.defaultThreadFactory());
config.setMetricRegistry(null);
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(1));
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
TestElf.setSlf4jLogLevel(HikariPool.class, Level.DEBUG);
TestElf.getPool(ds).logPoolState();
try (Connection connection = ds.getConnection()) {
UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(4));
connection.close();
UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(1));
ps.close();
String s = new String(baos.toByteArray());
Assert.assertNotNull("Exception string was null", s);
Assert.assertTrue("Expected exception to contain 'Connection leak detection' but contains *" + s + "*", s.contains("Connection leak detection"));
}
}
finally
{
TestElf.setConfigUnitTest(false);
}
}
}
}

@ -68,40 +68,41 @@ public class ShutdownTest
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
final HikariDataSource ds = new HikariDataSource(config);
HikariPool pool = TestElf.getPool(ds);
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread() {
@Override
public void run()
{
try {
if (ds.getConnection() != null) {
UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(1));
try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = TestElf.getPool(ds);
Thread[] threads = new Thread[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread() {
@Override
public void run()
{
try {
if (ds.getConnection() != null) {
UtilityElf.quietlySleep(TimeUnit.SECONDS.toMillis(1));
}
}
catch (SQLException e) {
}
}
catch (SQLException e) {
}
}
};
threads[i].setDaemon(true);
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
};
threads[i].setDaemon(true);
}
for (int i = 0; i < 10; i++) {
threads[i].start();
}
UtilityElf.quietlySleep(1800L);
UtilityElf.quietlySleep(1800L);
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
ds.close();
ds.close();
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
Assert.assertTrue(ds.isClosed());
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
Assert.assertTrue(ds.isClosed());
}
}
@Test
@ -118,19 +119,20 @@ public class ShutdownTest
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
HikariDataSource ds = new HikariDataSource(config);
HikariPool pool = TestElf.getPool(ds);
try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = TestElf.getPool(ds);
UtilityElf.quietlySleep(1200L);
UtilityElf.quietlySleep(1200L);
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() > 0);
ds.close();
ds.close();
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
Assert.assertTrue(ds.toString().startsWith("HikariDataSource (") && ds.toString().endsWith(")"));
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
Assert.assertTrue(ds.toString().startsWith("HikariDataSource (") && ds.toString().endsWith(")"));
}
}
@Test
@ -147,18 +149,19 @@ public class ShutdownTest
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
HikariDataSource ds = new HikariDataSource(config);
HikariPool pool = TestElf.getPool(ds);
try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = TestElf.getPool(ds);
UtilityElf.quietlySleep(1200L);
UtilityElf.quietlySleep(1200L);
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
ds.close();
ds.close();
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
}
}
@Test
@ -173,18 +176,18 @@ public class ShutdownTest
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
HikariDataSource ds = new HikariDataSource(config);
try (HikariDataSource ds = new HikariDataSource(config)) {
UtilityElf.quietlySleep(500L);
UtilityElf.quietlySleep(500L);
ds.close();
ds.close();
long startTime = ClockSource.INSTANCE.currentTime();
while (ClockSource.INSTANCE.elapsedMillis(startTime) < TimeUnit.SECONDS.toMillis(5) && threadCount() > 0) {
UtilityElf.quietlySleep(250);
}
long startTime = ClockSource.INSTANCE.currentTime();
while (ClockSource.INSTANCE.elapsedMillis(startTime) < TimeUnit.SECONDS.toMillis(5) && threadCount() > 0) {
UtilityElf.quietlySleep(250);
Assert.assertSame("Unreleased connections after shutdown", 0, TestElf.getPool(ds).getTotalConnections());
}
Assert.assertSame("Unreleased connections after shutdown", 0, TestElf.getPool(ds).getTotalConnections());
}
@Test
@ -199,42 +202,43 @@ public class ShutdownTest
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
HikariDataSource ds = new HikariDataSource(config);
HikariPool pool = TestElf.getPool(ds);
try (HikariDataSource ds = new HikariDataSource(config)) {
HikariPool pool = TestElf.getPool(ds);
Connection[] connections = new Connection[5];
for (int i = 0; i < 5; i++) {
connections[i] = ds.getConnection();
}
Connection[] connections = new Connection[5];
for (int i = 0; i < 5; i++) {
connections[i] = ds.getConnection();
}
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
Assert.assertTrue("Total connection count not as expected, ", pool.getTotalConnections() == 5);
ds.close();
ds.close();
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
Assert.assertSame("Active connection count not as expected, ", 0, pool.getActiveConnections());
Assert.assertSame("Idle connection count not as expected, ", 0, pool.getIdleConnections());
Assert.assertSame("Total connection count not as expected, ", 0, pool.getTotalConnections());
}
}
@Test
public void testAfterShutdown() throws Exception
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(5);
config.setInitializationFailFast(true);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
HikariDataSource ds = new HikariDataSource(config);
ds.close();
try
{
ds.getConnection();
}
catch (SQLException e) {
Assert.assertTrue(e.getMessage().contains("has been closed."));
}
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(5);
config.setInitializationFailFast(true);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
ds.close();
try {
ds.getConnection();
}
catch (SQLException e) {
Assert.assertTrue(e.getMessage().contains("has been closed."));
}
}
}
@Test
@ -250,7 +254,7 @@ public class ShutdownTest
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
try (HikariDataSource ds = new HikariDataSource(config)) {
StubConnection.slowCreate = true;
StubConnection.slowCreate = true;
UtilityElf.quietlySleep(3000L);
}
}
@ -268,64 +272,64 @@ public class ShutdownTest
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
for (int i = 0; i < 4; i++) {
final HikariDataSource ds = new HikariDataSource(config);
Thread t = new Thread() {
@Override
public void run() {
Connection connection = null;
try {
connection = ds.getConnection();
for (int i = 0; i < 10; i++) {
Connection connection2 = null;
try {
connection2 = ds.getConnection();
PreparedStatement stmt = connection2.prepareStatement("SOMETHING");
UtilityElf.quietlySleep(20);
stmt.getMaxFieldSize();
}
catch (SQLException e) {
try (final HikariDataSource ds = new HikariDataSource(config)) {
Thread t = new Thread() {
@Override
public void run()
{
try (Connection connection = ds.getConnection()) {
for (int i = 0; i < 10; i++) {
Connection connection2 = null;
try {
if (connection2 != null) {
connection2.close();
}
connection2 = ds.getConnection();
PreparedStatement stmt = connection2.prepareStatement("SOMETHING");
UtilityElf.quietlySleep(20);
stmt.getMaxFieldSize();
}
catch (SQLException e2) {
if (e2.getMessage().contains("shutdown") || e2.getMessage().contains("evicted")) {
break;
catch (SQLException e) {
try {
if (connection2 != null) {
connection2.close();
}
}
catch (SQLException e2) {
if (e2.getMessage().contains("shutdown") || e2.getMessage().contains("evicted")) {
break;
}
}
}
}
}
catch (Exception e) {
Assert.fail(e.getMessage());
}
finally {
ds.close();
}
}
catch (Exception e) {
Assert.fail(e.getMessage());
}
finally {
try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }
ds.close();
}
}
};
t.start();
Thread t2 = new Thread() {
@Override
public void run() {
UtilityElf.quietlySleep(100);
try {
ds.close();
}
catch (IllegalStateException e) {
Assert.fail(e.getMessage());
};
t.start();
Thread t2 = new Thread() {
@Override
public void run()
{
UtilityElf.quietlySleep(100);
try {
ds.close();
}
catch (IllegalStateException e) {
Assert.fail(e.getMessage());
}
}
}
};
t2.start();
};
t2.start();
t.join();
t2.join();
t.join();
t2.join();
ds.close();
ds.close();
}
}
}

@ -30,90 +30,86 @@ import com.zaxxer.hikari.HikariDataSource;
public class StatementTest
{
private HikariDataSource ds;
@Before
public void setup()
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(2);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
ds = new HikariDataSource(config);
}
@After
public void teardown()
{
ds.close();
}
@Test
public void testStatementClose() throws SQLException
{
ds.getConnection().close();
HikariPool pool = TestElf.getPool(ds);
Assert.assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
Assert.assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 1);
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
Assert.assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
Assert.assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 0);
Statement statement = connection.createStatement();
Assert.assertNotNull(statement);
connection.close();
Assert.assertTrue(statement.isClosed());
}
@Test
public void testAutoStatementClose() throws SQLException
{
Connection connection = ds.getConnection();
Assert.assertNotNull(connection);
Statement statement1 = connection.createStatement();
Assert.assertNotNull(statement1);
Statement statement2 = connection.createStatement();
Assert.assertNotNull(statement2);
connection.close();
Assert.assertTrue(statement1.isClosed());
Assert.assertTrue(statement2.isClosed());
}
@Test
public void testDoubleStatementClose() throws SQLException
{
Connection connection = ds.getConnection();
Statement statement1 = connection.createStatement();
statement1.close();
statement1.close();
connection.close();
}
@Test
public void testOutOfOrderStatementClose() throws SQLException
{
Connection connection = ds.getConnection();
Statement statement1 = connection.createStatement();
Statement statement2 = connection.createStatement();
statement1.close();
statement2.close();
connection.close();
}
private HikariDataSource ds;
@Before
public void setup()
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(1);
config.setMaximumPoolSize(2);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");
ds = new HikariDataSource(config);
}
@After
public void teardown()
{
ds.close();
}
@Test
public void testStatementClose() throws SQLException
{
ds.getConnection().close();
HikariPool pool = TestElf.getPool(ds);
Assert.assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
Assert.assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 1);
try (Connection connection = ds.getConnection()) {
Assert.assertNotNull(connection);
Assert.assertTrue("Total connections not as expected", pool.getTotalConnections() >= 1);
Assert.assertTrue("Idle connections not as expected", pool.getIdleConnections() >= 0);
Statement statement = connection.createStatement();
Assert.assertNotNull(statement);
connection.close();
Assert.assertTrue(statement.isClosed());
}
}
@Test
public void testAutoStatementClose() throws SQLException
{
try (Connection connection = ds.getConnection()) {
Assert.assertNotNull(connection);
Statement statement1 = connection.createStatement();
Assert.assertNotNull(statement1);
Statement statement2 = connection.createStatement();
Assert.assertNotNull(statement2);
connection.close();
Assert.assertTrue(statement1.isClosed());
Assert.assertTrue(statement2.isClosed());
}
}
@Test
public void testDoubleStatementClose() throws SQLException
{
try (Connection connection = ds.getConnection();
Statement statement1 = connection.createStatement()) {
statement1.close();
statement1.close();
}
}
@Test
public void testOutOfOrderStatementClose() throws SQLException
{
try (Connection connection = ds.getConnection();
Statement statement1 = connection.createStatement();
Statement statement2 = connection.createStatement()) {
statement1.close();
statement2.close();
}
}
}

@ -63,54 +63,56 @@ public class TestConcurrentBag
@Test
public void testConcurrentBag() throws Exception
{
ConcurrentBag<PoolEntry> bag = new ConcurrentBag<>(new IBagStateListener() {
@Override
public Future<Boolean> addBagItem()
{
return null;
try (ConcurrentBag<PoolEntry> bag = new ConcurrentBag<>(new IBagStateListener() {
@Override
public Future<Boolean> addBagItem()
{
return null;
}
})
) {
Assert.assertEquals(0, bag.values(8).size());
PoolEntry reserved = pool.newPoolEntry();
bag.add(reserved);
bag.reserve(reserved); // reserved
PoolEntry inuse = pool.newPoolEntry();
bag.add(inuse);
bag.borrow(2, TimeUnit.MILLISECONDS); // in use
PoolEntry notinuse = pool.newPoolEntry();
bag.add(notinuse); // not in use
bag.dumpState();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(ConcurrentBag.class, ps);
bag.requite(reserved);
bag.remove(notinuse);
Assert.assertTrue(new String(baos.toByteArray()).contains("not borrowed or reserved"));
bag.unreserve(notinuse);
Assert.assertTrue(new String(baos.toByteArray()).contains("was not reserved"));
bag.remove(inuse);
bag.remove(inuse);
Assert.assertTrue(new String(baos.toByteArray()).contains("not borrowed or reserved"));
bag.close();
try {
PoolEntry bagEntry = pool.newPoolEntry();
bag.add(bagEntry);
Assert.assertNotEquals(bagEntry, bag.borrow(100, TimeUnit.MILLISECONDS));
}
});
Assert.assertEquals(0, bag.values(8).size());
PoolEntry reserved = pool.newPoolEntry();
bag.add(reserved);
bag.reserve(reserved); // reserved
PoolEntry inuse = pool.newPoolEntry();
bag.add(inuse);
bag.borrow(2, TimeUnit.MILLISECONDS); // in use
PoolEntry notinuse = pool.newPoolEntry();
bag.add(notinuse); // not in use
bag.dumpState();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos, true);
TestElf.setSlf4jTargetStream(ConcurrentBag.class, ps);
bag.requite(reserved);
bag.remove(notinuse);
Assert.assertTrue(new String(baos.toByteArray()).contains("not borrowed or reserved"));
bag.unreserve(notinuse);
Assert.assertTrue(new String(baos.toByteArray()).contains("was not reserved"));
bag.remove(inuse);
bag.remove(inuse);
Assert.assertTrue(new String(baos.toByteArray()).contains("not borrowed or reserved"));
bag.close();
try {
PoolEntry bagEntry = pool.newPoolEntry();
bag.add(bagEntry);
Assert.assertNotEquals(bagEntry, bag.borrow(100, TimeUnit.MILLISECONDS));
}
catch (IllegalStateException e) {
Assert.assertTrue(new String(baos.toByteArray()).contains("ignoring add()"));
catch (IllegalStateException e) {
Assert.assertTrue(new String(baos.toByteArray()).contains("ignoring add()"));
}
Assert.assertNotNull(notinuse.toString());
}
Assert.assertNotNull(notinuse.toString());
}
}

@ -55,20 +55,21 @@ public class TestConnectionCloseBlocking {
config.setDataSource(new CustomMockDataSource());
long start = ClockSource.INSTANCE.currentTime();
try (HikariDataSource ds = new HikariDataSource(config)) {
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
UtilityElf.quietlySleep(1100L);
shouldFail = true;
// on physical connection close we sleep 2 seconds
connection = ds.getConnection();
Assert.assertTrue("Waited longer than timeout", (ClockSource.INSTANCE.elapsedMillis(start) < config.getConnectionTimeout()));
try (HikariDataSource ds = new HikariDataSource(config);
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
UtilityElf.quietlySleep(1100L);
shouldFail = true;
// on physical connection close we sleep 2 seconds
try (Connection connection2 = ds.getConnection()) {
Assert.assertTrue("Waited longer than timeout", (ClockSource.INSTANCE.elapsedMillis(start) < config.getConnectionTimeout()));
}
} catch (SQLException e) {
Assert.assertTrue("getConnection failed because close connection took longer than timeout", (ClockSource.INSTANCE.elapsedMillis(start) < config.getConnectionTimeout()));
}

Loading…
Cancel
Save