Skip to content

Instantly share code, notes, and snippets.

@brettwooldridge
Created October 10, 2014 03:28
Show Gist options
  • Save brettwooldridge/a451da5db02039b47de6 to your computer and use it in GitHub Desktop.
Save brettwooldridge/a451da5db02039b47de6 to your computer and use it in GitHub Desktop.
HikariCP test for issue #159
/*
* Copyright (C) 2013, 2014 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.sql.Connection;
import java.sql.SQLException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.zaxxer.hikari.util.PoolUtilities;
/**
*
* @author Brett Wooldridge
*/
public class PostgresTest
{
@Test
public void testCase1() throws Exception
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(10);
config.setInitializationFailFast(true);
config.setJdbc4ConnectionTest(false);
config.setConnectionTimeout(5000);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
config.addDataSourceProperty("serverName", "192.168.0.114");
config.addDataSourceProperty("portNumber", "5432");
config.addDataSourceProperty("databaseName", "test");
config.addDataSourceProperty("user", "brettw");
config.addDataSourceProperty("tcpKeepAlive", true);
try (HikariDataSource ds = new HikariDataSource(config)) {
System.err.println("\nMake sure the firewall is enabled. Attempting connection in 5 seconds...");
PoolUtilities.quietlySleep(5000L);
TestElf.getPool(ds).logPoolState();
System.err.println("\nNow attempting getConnection(), expecting a timeout...");
long start = System.currentTimeMillis();
try (Connection conn = ds.getConnection()) {
System.err.println("\nOpps, got a connection. Are you sure the firewall is enabled?");
}
catch (SQLException e)
{
Assert.assertTrue("Timeout less than expected " + (System.currentTimeMillis() - start) + "ms", (System.currentTimeMillis() - start) > 5000);
}
}
System.err.println("\nPassed.");
}
@Test
public void testCase2() throws Exception
{
HikariConfig config = new HikariConfig();
config.setMinimumIdle(0);
config.setMaximumPoolSize(10);
config.setInitializationFailFast(true);
config.setJdbc4ConnectionTest(false);
config.setConnectionTimeout(5000);
config.setConnectionTestQuery("VALUES 1");
config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
config.addDataSourceProperty("serverName", "192.168.0.114");
config.addDataSourceProperty("portNumber", "5432");
config.addDataSourceProperty("databaseName", "test");
config.addDataSourceProperty("user", "brettw");
config.addDataSourceProperty("tcpKeepAlive", true);
try (HikariDataSource ds = new HikariDataSource(config)) {
System.err.println("\nDisable the firewall, please. Starting test in 5 seconds...");
PoolUtilities.quietlySleep(5000L);
try (Connection conn = ds.getConnection()) {
System.err.println("\nGot a connection, and released it. Now, enable the firewall.");
}
TestElf.getPool(ds).logPoolState();
PoolUtilities.quietlySleep(5000L);
System.err.println("\nNow attempting another getConnection(), expecting a timeout...");
long start = System.currentTimeMillis();
try (Connection conn = ds.getConnection()) {
System.err.println("\nOpps, got a connection. Did you enable the firewall? " + conn);
Assert.fail("Opps, got a connection. Did you enable the firewall?");
}
catch (SQLException e)
{
Assert.assertTrue("Timeout less than expected " + (System.currentTimeMillis() - start) + "ms", (System.currentTimeMillis() - start) > 5000);
}
System.err.println("\nOk, so far so good. Now, disable the firewall again. Attempting connection in 5 seconds...");
PoolUtilities.quietlySleep(5000L);
TestElf.getPool(ds).logPoolState();
try (Connection conn = ds.getConnection()) {
System.err.println("\nGot a connection, and released it.");
}
}
System.err.println("\nPassed.");
}
@Before
public void before()
{
System.err.println("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment