public PoolEntry<T, C> createEntry(final TimeValue timeToLive) { final PoolEntry<T, C> entry = new PoolEntry<>(this.route, timeToLive); this.leased.add(entry); return entry; }
public PoolEntry<T, C> createEntry(final TimeValue timeToLive) { final PoolEntry<T, C> entry = new PoolEntry<>(this.route, timeToLive); this.leased.add(entry); return entry; }
@Test(expected = IllegalArgumentException.class) public void testInvalidConstruction() throws Exception { new PoolEntry<String, HttpConnection>(null); }
public Future<PoolEntry<T, C>> lease( final Object state, final Timeout requestTimeout, final FutureCallback<PoolEntry<T, C>> callback) { Asserts.check(!terminated.get(), "Connection pool shut down"); final BasicFuture<PoolEntry<T, C>> future = new BasicFuture<>(callback); final PoolEntry<T, C> availableEntry = getAvailableEntry(state); if (availableEntry != null) { addLeased(availableEntry); future.completed(availableEntry); } else { if (pending.isEmpty() && leased.size() < max) { final PoolEntry<T, C> entry = new PoolEntry<>(route, timeToLive); addLeased(entry); future.completed(entry); } else { pending.add(new LeaseRequest<>(state, requestTimeout, future)); } } return future; }
public Future<PoolEntry<T, C>> lease( final Object state, final Timeout requestTimeout, final FutureCallback<PoolEntry<T, C>> callback) { Asserts.check(!terminated.get(), "Connection pool shut down"); final BasicFuture<PoolEntry<T, C>> future = new BasicFuture<>(callback); final PoolEntry<T, C> availableEntry = getAvailableEntry(state); if (availableEntry != null) { addLeased(availableEntry); future.completed(availableEntry); } else { if (pending.isEmpty() && leased.size() < max) { final PoolEntry<T, C> entry = new PoolEntry<>(route, timeToLive); addLeased(entry); future.completed(entry); } else { pending.add(new LeaseRequest<>(state, requestTimeout, future)); } } return future; }
@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); }
@Test(expected = IllegalStateException.class) public void testReleaseUnknownEntry() throws Exception { final LaxConnPool<String, HttpConnection> pool = new LaxConnPool<>(2); pool.release(new PoolEntry<String, HttpConnection>("somehost"), true); }
@Test(expected = IllegalStateException.class) public void testReleaseUnknownEntry() throws Exception { final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 2); pool.release(new PoolEntry<String, HttpConnection>("somehost"), true); }
@Test public void testExpiryDoesNotOverflow() { final PoolEntry<String, HttpConnection> entry = new PoolEntry<>( "route1", TimeValue.of(Long.MAX_VALUE, TimeUnit.MILLISECONDS), currentTimeSupplier); entry.assignConnection(Mockito.mock(HttpConnection.class)); Assert.assertEquals(Deadline.MAX_VALUE, entry.getValidityDeadline()); }
leaseRequest.completed(availableEntry); } else if (leased.size() < max) { final PoolEntry<T, C> newEntry = new PoolEntry<>(route, timeToLive); addLeased(newEntry); leaseRequest.completed(newEntry);
leaseRequest.completed(availableEntry); } else if (leased.size() < max) { final PoolEntry<T, C> newEntry = new PoolEntry<>(route, timeToLive); addLeased(newEntry); leaseRequest.completed(newEntry);
@Test public void testValidInfinitely() 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.getValidityDeadline()); Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiryDeadline()); }
@Test public void testShutdown() throws Exception { final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 2); pool.close(CloseMode.GRACEFUL); try { pool.lease("somehost", null); Assert.fail("IllegalStateException should have been thrown"); } catch (final IllegalStateException expected) { } // Ignored if shut down pool.release(new PoolEntry<String, HttpConnection>("somehost"), true); }
@Test public void testShutdown() throws Exception { final LaxConnPool<String, HttpConnection> pool = new LaxConnPool<>(2); pool.close(CloseMode.GRACEFUL); try { pool.lease("somehost", null); Assert.fail("IllegalStateException should have been thrown"); } catch (final IllegalStateException expected) { } // Ignored if shut down pool.release(new PoolEntry<String, HttpConnection>("somehost"), true); }
@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()); }
@Test public void testBasics() throws Exception { final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>( "route1", TimeValue.of(10L, TimeUnit.MILLISECONDS), currentTimeSupplier); Assert.assertEquals("route1", entry1.getRoute()); Assert.assertEquals(0, entry1.getUpdated()); Assert.assertEquals(Deadline.MIN_VALUE, entry1.getExpiryDeadline()); entry1.assignConnection(Mockito.mock(HttpConnection.class)); final long now = System.currentTimeMillis(); Assert.assertEquals("route1", entry1.getRoute()); Assert.assertTrue(now >= entry1.getUpdated()); Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiryDeadline()); Assert.assertEquals(entry1.getUpdated() + 10L, entry1.getValidityDeadline().getValue()); entry1.discardConnection(CloseMode.IMMEDIATE); Assert.assertEquals(0, entry1.getUpdated()); Assert.assertEquals(Deadline.MIN_VALUE, entry1.getExpiryDeadline()); }