Fix timing sensitive test that often fails on travis-ci

pull/437/head
Brett Wooldridge 10 years ago
parent e125d509fe
commit b2cfd09998

@ -31,7 +31,7 @@ public class TestConnectionTimeoutRetry
try (HikariDataSource ds = new HikariDataSource(config)) { try (HikariDataSource ds = new HikariDataSource(config)) {
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused")); stubDataSource.setThrowException(new SQLException("Connection refused"));
long start = ClockSource.INSTANCE.currentTime(); long start = ClockSource.INSTANCE.currentTime();
try (Connection connection = ds.getConnection()) { try (Connection connection = ds.getConnection()) {
connection.close(); connection.close();
@ -60,7 +60,7 @@ public class TestConnectionTimeoutRetry
try (HikariDataSource ds = new HikariDataSource(config)) { try (HikariDataSource ds = new HikariDataSource(config)) {
final StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); final StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused")); stubDataSource.setThrowException(new SQLException("Connection refused"));
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.schedule(new Runnable() { scheduler.schedule(new Runnable() {
public void run() public void run()
@ -68,12 +68,12 @@ public class TestConnectionTimeoutRetry
stubDataSource.setThrowException(null); stubDataSource.setThrowException(null);
} }
}, 300, TimeUnit.MILLISECONDS); }, 300, TimeUnit.MILLISECONDS);
long start = ClockSource.INSTANCE.currentTime(); long start = ClockSource.INSTANCE.currentTime();
try { try {
Connection connection = ds.getConnection(); Connection connection = ds.getConnection();
connection.close(); connection.close();
long elapsed = ClockSource.INSTANCE.elapsedMillis(start); long elapsed = ClockSource.INSTANCE.elapsedMillis(start);
Assert.assertTrue("Connection returned too quickly, something is wrong.", elapsed > 250); Assert.assertTrue("Connection returned too quickly, something is wrong.", elapsed > 250);
Assert.assertTrue("Waited too long to get a connection.", elapsed < config.getConnectionTimeout()); Assert.assertTrue("Waited too long to get a connection.", elapsed < config.getConnectionTimeout());
@ -103,7 +103,7 @@ public class TestConnectionTimeoutRetry
final Connection connection2 = ds.getConnection(); final Connection connection2 = ds.getConnection();
Assert.assertNotNull(connection1); Assert.assertNotNull(connection1);
Assert.assertNotNull(connection2); Assert.assertNotNull(connection2);
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
scheduler.schedule(new Runnable() { scheduler.schedule(new Runnable() {
public void run() public void run()
@ -116,12 +116,12 @@ public class TestConnectionTimeoutRetry
} }
} }
}, 800, TimeUnit.MILLISECONDS); }, 800, TimeUnit.MILLISECONDS);
long start = ClockSource.INSTANCE.currentTime(); long start = ClockSource.INSTANCE.currentTime();
try { try {
Connection connection3 = ds.getConnection(); Connection connection3 = ds.getConnection();
connection3.close(); connection3.close();
long elapsed = ClockSource.INSTANCE.elapsedMillis(start); long elapsed = ClockSource.INSTANCE.elapsedMillis(start);
Assert.assertTrue("Waited too long to get a connection.", (elapsed >= 700) && (elapsed < 950)); Assert.assertTrue("Waited too long to get a connection.", (elapsed >= 700) && (elapsed < 950));
} }
@ -148,7 +148,7 @@ public class TestConnectionTimeoutRetry
try (HikariDataSource ds = new HikariDataSource(config)) { try (HikariDataSource ds = new HikariDataSource(config)) {
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused")); stubDataSource.setThrowException(new SQLException("Connection refused"));
long start = ClockSource.INSTANCE.currentTime(); long start = ClockSource.INSTANCE.currentTime();
try { try {
Connection connection = ds.getConnection(); Connection connection = ds.getConnection();
@ -175,9 +175,9 @@ public class TestConnectionTimeoutRetry
try (HikariDataSource ds = new HikariDataSource(config)) { try (HikariDataSource ds = new HikariDataSource(config)) {
final Connection connection1 = ds.getConnection(); final Connection connection1 = ds.getConnection();
long start = ClockSource.INSTANCE.currentTime(); long start = ClockSource.INSTANCE.currentTime();
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
scheduler.schedule(new Runnable() { scheduler.schedule(new Runnable() {
public void run() public void run()
@ -190,14 +190,14 @@ public class TestConnectionTimeoutRetry
} }
} }
}, 250, TimeUnit.MILLISECONDS); }, 250, TimeUnit.MILLISECONDS);
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); StubDataSource stubDataSource = ds.unwrap(StubDataSource.class);
stubDataSource.setThrowException(new SQLException("Connection refused")); stubDataSource.setThrowException(new SQLException("Connection refused"));
try { try {
Connection connection2 = ds.getConnection(); Connection connection2 = ds.getConnection();
connection2.close(); connection2.close();
long elapsed = ClockSource.INSTANCE.elapsedMillis(start); long elapsed = ClockSource.INSTANCE.elapsedMillis(start);
Assert.assertTrue("Waited too long to get a connection.", (elapsed >= 250) && (elapsed < config.getConnectionTimeout())); Assert.assertTrue("Waited too long to get a connection.", (elapsed >= 250) && (elapsed < config.getConnectionTimeout()));
} }
@ -234,7 +234,7 @@ public class TestConnectionTimeoutRetry
Connection connection6 = ds.getConnection(); Connection connection6 = ds.getConnection();
Connection connection7 = ds.getConnection(); Connection connection7 = ds.getConnection();
Thread.sleep(1200); Thread.sleep(2000);
Assert.assertSame("Totals connections not as expected", 10, TestElf.getPool(ds).getTotalConnections()); Assert.assertSame("Totals connections not as expected", 10, TestElf.getPool(ds).getTotalConnections());
Assert.assertSame("Idle connections not as expected", 3, TestElf.getPool(ds).getIdleConnections()); Assert.assertSame("Idle connections not as expected", 3, TestElf.getPool(ds).getIdleConnections());

Loading…
Cancel
Save