Source:
public void testIdleExpiration() throws Exception { long idleTimeoutMillis = 300; manager = new ConnectionManagerImpl("pool", factory, endpointManager, 5, 2, idleTimeoutMillis, -1, logger, 60 * 1000, cancelCriterion, poolStats); manager.start(background); await().untilAsserted(() -> { assertThat(factory.creates.get()).isEqualTo(2); assertThat(factory.destroys.get()).isEqualTo(0); assertThat(factory.closes.get()).isEqualTo(0); assertThat(poolStats.getIdleExpire()).isEqualTo(0); }); Connection connection1 = manager.borrowConnection(500); Connection connection2 = manager.borrowConnection(500); Connection connection3 = manager.borrowConnection(500); Connection connection4 = manager.borrowConnection(500); Connection connection5 = manager.borrowConnection(500); Thread.sleep(idleTimeoutMillis * 2); assertThat(factory.creates.get()).isEqualTo(5); assertThat(factory.destroys.get()).isEqualTo(0); assertThat(factory.closes.get()).isEqualTo(0); assertThat(poolStats.getIdleExpire()).isEqualTo(0); connection1.passivate(true); long elapsedMillis = Timer.measure(() -> { await().untilAsserted(() -> { assertThat(factory.creates.get()).isEqualTo(5); assertThat(factory.destroys.get()).isEqualTo(1); assertThat(factory.closes.get()).isEqualTo(1); assertThat(poolStats.getIdleExpire()).isEqualTo(1); }); }); checkIdleTimeout(idleTimeoutMillis, elapsedMillis); manager.returnConnection(connection2); manager.returnConnection(connection3); manager.returnConnection(connection4); manager.returnConnection(connection5); elapsedMillis = Timer.measure(() -> { await().untilAsserted(() -> { assertThat(factory.creates.get()).isEqualTo(5); assertThat(factory.destroys.get()).isEqualTo(3); assertThat(factory.closes.get()).isEqualTo(3); assertThat(poolStats.getIdleExpire()).isEqualTo(3); }); }); checkIdleTimeout(idleTimeoutMillis, elapsedMillis); Thread.sleep(idleTimeoutMillis * 2); assertThat(factory.creates.get()).isEqualTo(5); assertThat(factory.destroys.get()).isEqualTo(3); assertThat(factory.closes.get()).isEqualTo(3); assertThat(poolStats.getIdleExpire()).isEqualTo(3); }

Target:
public void testIdleExpiration() throws InterruptedException, AllConnectionsInUseException, NoAvailableServersException { final long idleTimeoutMillis = 300; manager = new ConnectionManagerImpl("pool", factory, endpointManager, 5, 2, idleTimeoutMillis, -1, logger, 60 * 1000, cancelCriterion, poolStats); manager.start(background); { factory.waitWhile(() -> factory.creates < 2); Assert.assertEquals(2, factory.creates); Assert.assertEquals(0, factory.destroys); Assert.assertEquals(0, factory.closes); Assert.assertEquals(0, poolStats.getIdleExpire()); } Connection conn1 = manager.borrowConnection(500); Connection conn2 = manager.borrowConnection(500); Connection conn3 = manager.borrowConnection(500); Connection conn4 = manager.borrowConnection(500); Connection conn5 = manager.borrowConnection(500); Thread.sleep(idleTimeoutMillis * 2); Assert.assertEquals(5, factory.creates); Assert.assertEquals(0, factory.destroys); Assert.assertEquals(0, factory.closes); Assert.assertEquals(0, poolStats.getIdleExpire()); { conn1.passivate(true); long elapsedMillis = factory.waitWhile(() -> factory.destroys < 1); Assert.assertEquals(5, factory.creates); Assert.assertEquals(1, factory.destroys); Assert.assertEquals(1, factory.closes); Assert.assertEquals(1, poolStats.getIdleExpire()); checkIdleTimeout(idleTimeoutMillis, elapsedMillis); } manager.returnConnection(conn2); manager.returnConnection(conn3); manager.returnConnection(conn4); manager.returnConnection(conn5); { long elapsedMillis = factory.waitWhile(() -> factory.destroys < 3); Assert.assertEquals(5, factory.creates); Assert.assertEquals(3, factory.destroys); Assert.assertEquals(3, factory.closes); Assert.assertEquals(3, poolStats.getIdleExpire()); checkIdleTimeout(idleTimeoutMillis, elapsedMillis); } Thread.sleep(idleTimeoutMillis * 2); Assert.assertEquals(5, factory.creates); Assert.assertEquals(3, factory.destroys); Assert.assertEquals(3, factory.closes); Assert.assertEquals(3, poolStats.getIdleExpire()); }