@Test public void testCloseIdle() throws Exception { final LocalConnFactory connFactory = Mockito.mock(LocalConnFactory.class); final HttpConnection conn1 = Mockito.mock(HttpConnection.class); Mockito.when(conn1.isOpen()).thenReturn(true); final HttpConnection conn2 = Mockito.mock(HttpConnection.class); Mockito.when(conn2.isOpen()).thenReturn(true); Mockito.when(connFactory.create(Mockito.eq("somehost"))).thenReturn(conn1, conn2); final LocalConnPool pool = new LocalConnPool(connFactory, 2, 2); final Future<LocalPoolEntry> future1 = pool.lease("somehost", null); final LocalPoolEntry entry1 = future1.get(1, TimeUnit.SECONDS); Assert.assertNotNull(entry1); final Future<LocalPoolEntry> future2 = pool.lease("somehost", null); final LocalPoolEntry entry2 = future2.get(1, TimeUnit.SECONDS); Assert.assertNotNull(entry2); entry1.updateExpiry(0, TimeUnit.MILLISECONDS); pool.release(entry1, true); Thread.sleep(200L); entry2.updateExpiry(0, TimeUnit.MILLISECONDS); pool.release(entry2, true); pool.closeIdle(50, TimeUnit.MILLISECONDS); Mockito.verify(conn1).close(); Mockito.verify(conn2, Mockito.never()).close(); PoolStats totals = pool.getTotalStats(); Assert.assertEquals(1, totals.getAvailable()); Assert.assertEquals(0, totals.getLeased()); PoolStats stats = pool.getStats("somehost"); Assert.assertEquals(1, stats.getAvailable()); Assert.assertEquals(0, stats.getLeased()); pool.closeIdle(-1, TimeUnit.MILLISECONDS); Mockito.verify(conn2).close(); totals = pool.getTotalStats(); Assert.assertEquals(0, totals.getAvailable()); Assert.assertEquals(0, totals.getLeased()); stats = pool.getStats("somehost"); Assert.assertEquals(0, stats.getAvailable()); Assert.assertEquals(0, stats.getLeased()); }
@Test(expected = IllegalArgumentException.class) public void testCloseIdleInvalid() throws Exception { final LocalConnFactory connFactory = Mockito.mock(LocalConnFactory.class); final LocalConnPool pool = new LocalConnPool(connFactory, 2, 2); pool.closeIdle(50, null); }