@Test(expected=IllegalArgumentException.class) public void testInvalidExpiry() throws Exception { final PoolEntry<String, HttpConnection> entry = new PoolEntry<>( "route1", TimeValue.of(0L, TimeUnit.MILLISECONDS), currentTimeSupplier); entry.updateExpiry(null); }
if (reusable) { entry.updateState(state); entry.updateExpiry(keepAlive); connection.passivate(); if (log.isDebugEnabled()) {
if (reusable) { entry.updateState(state); entry.updateExpiry(keepAlive); conn.passivate(); if (this.log.isDebugEnabled()) {
@Test public void testExpiry() throws Exception { final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>( "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier); entry1.assignConnection(Mockito.mock(HttpConnection.class)); Assert.assertEquals(Deadline.MAX_VALUE, entry1.getExpiryDeadline()); entry1.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS)); Assert.assertEquals(entry1.getUpdated() + 50L, entry1.getExpiryDeadline().getValue()); entry1.updateExpiry(TimeValue.ZERO_MILLISECONDS); Assert.assertEquals(Deadline.MAX_VALUE, entry1.getExpiryDeadline()); final PoolEntry<String, HttpConnection> entry2 = new PoolEntry<>( "route1", TimeValue.of(100L, TimeUnit.MILLISECONDS), currentTimeSupplier); entry2.assignConnection(Mockito.mock(HttpConnection.class)); final Deadline validityDeadline = entry2.getValidityDeadline(); Assert.assertEquals(entry2.getUpdated() + 100L, entry2.getExpiryDeadline().getValue()); entry2.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS)); Assert.assertEquals(entry2.getUpdated() + 50L, entry2.getExpiryDeadline().getValue()); entry2.updateExpiry(TimeValue.of(150L, TimeUnit.MILLISECONDS)); Assert.assertEquals(validityDeadline, entry2.getExpiryDeadline()); }
entry2.assignConnection(conn2); entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS)); pool.release(entry1, true); entry2.updateExpiry(TimeValue.of(1000, TimeUnit.SECONDS)); pool.release(entry2, true);
entry2.assignConnection(conn2); entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS)); pool.release(entry1, true); entry2.updateExpiry(TimeValue.of(1000, TimeUnit.SECONDS)); pool.release(entry2, true);
@Test public void testCreateNewIfExpired() throws Exception { final HttpConnection conn1 = Mockito.mock(HttpConnection.class); final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 2); final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null); Assert.assertTrue(future1.isDone()); final PoolEntry<String, HttpConnection> entry1 = future1.get(); Assert.assertNotNull(entry1); entry1.assignConnection(conn1); entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS)); pool.release(entry1, true); Thread.sleep(200L); final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null); Assert.assertTrue(future2.isDone()); Mockito.verify(conn1).close(CloseMode.GRACEFUL); final PoolStats totals = pool.getTotalStats(); Assert.assertEquals(0, totals.getAvailable()); Assert.assertEquals(1, totals.getLeased()); Assert.assertEquals(Collections.singleton("somehost"), pool.getRoutes()); final PoolStats stats = pool.getStats("somehost"); Assert.assertEquals(0, stats.getAvailable()); Assert.assertEquals(1, stats.getLeased()); }
@Test public void testCreateNewIfExpired() throws Exception { final HttpConnection conn1 = Mockito.mock(HttpConnection.class); final LaxConnPool<String, HttpConnection> pool = new LaxConnPool<>(2); final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null); Assert.assertTrue(future1.isDone()); final PoolEntry<String, HttpConnection> entry1 = future1.get(); Assert.assertNotNull(entry1); entry1.assignConnection(conn1); entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS)); pool.release(entry1, true); Thread.sleep(200L); final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null); Assert.assertTrue(future2.isDone()); Mockito.verify(conn1).close(CloseMode.GRACEFUL); final PoolStats totals = pool.getTotalStats(); Assert.assertEquals(0, totals.getAvailable()); Assert.assertEquals(1, totals.getLeased()); Assert.assertEquals(Collections.singleton("somehost"), pool.getRoutes()); final PoolStats stats = pool.getStats("somehost"); Assert.assertEquals(0, stats.getAvailable()); Assert.assertEquals(1, stats.getLeased()); }