use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestPoolEntry method testExpiry.
@Test
public void testExpiry() throws Exception {
final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>("route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier);
entry1.assignConnection(Mockito.mock(HttpConnection.class));
Assertions.assertEquals(Deadline.MAX_VALUE, entry1.getExpiryDeadline());
entry1.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
Assertions.assertEquals(entry1.getUpdated() + 50L, entry1.getExpiryDeadline().getValue());
entry1.updateExpiry(TimeValue.ZERO_MILLISECONDS);
Assertions.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();
Assertions.assertEquals(entry2.getUpdated() + 100L, entry2.getExpiryDeadline().getValue());
entry2.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS));
Assertions.assertEquals(entry2.getUpdated() + 50L, entry2.getExpiryDeadline().getValue());
entry2.updateExpiry(TimeValue.of(150L, TimeUnit.MILLISECONDS));
Assertions.assertEquals(validityDeadline, entry2.getExpiryDeadline());
}
use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestStrictConnPool method testCloseExpired.
@Test
public void testCloseExpired() throws Exception {
final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 2);
final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null);
final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null);
Assertions.assertTrue(future1.isDone());
final PoolEntry<String, HttpConnection> entry1 = future1.get();
Assertions.assertNotNull(entry1);
entry1.assignConnection(conn1);
Assertions.assertTrue(future2.isDone());
final PoolEntry<String, HttpConnection> entry2 = future2.get();
Assertions.assertNotNull(entry2);
entry2.assignConnection(conn2);
entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS));
pool.release(entry1, true);
Thread.sleep(200);
entry2.updateExpiry(TimeValue.of(1000, TimeUnit.SECONDS));
pool.release(entry2, true);
pool.closeExpired();
Mockito.verify(conn1).close(CloseMode.GRACEFUL);
Mockito.verify(conn2, Mockito.never()).close(ArgumentMatchers.any());
final PoolStats totals = pool.getTotalStats();
Assertions.assertEquals(1, totals.getAvailable());
Assertions.assertEquals(0, totals.getLeased());
Assertions.assertEquals(0, totals.getPending());
final PoolStats stats = pool.getStats("somehost");
Assertions.assertEquals(1, stats.getAvailable());
Assertions.assertEquals(0, stats.getLeased());
Assertions.assertEquals(0, stats.getPending());
}
use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestStrictConnPool method testLeaseRelease.
@Test
public void testLeaseRelease() throws Exception {
final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
final HttpConnection conn3 = Mockito.mock(HttpConnection.class);
final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 10);
final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null);
final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null);
final Future<PoolEntry<String, HttpConnection>> future3 = pool.lease("otherhost", null);
final PoolEntry<String, HttpConnection> entry1 = future1.get();
Assertions.assertNotNull(entry1);
entry1.assignConnection(conn1);
final PoolEntry<String, HttpConnection> entry2 = future2.get();
Assertions.assertNotNull(entry2);
entry2.assignConnection(conn2);
final PoolEntry<String, HttpConnection> entry3 = future3.get();
Assertions.assertNotNull(entry3);
entry3.assignConnection(conn3);
pool.release(entry1, true);
pool.release(entry2, true);
pool.release(entry3, false);
Mockito.verify(conn1, Mockito.never()).close(ArgumentMatchers.any());
Mockito.verify(conn2, Mockito.never()).close(ArgumentMatchers.any());
Mockito.verify(conn3, Mockito.times(1)).close(CloseMode.GRACEFUL);
final PoolStats totals = pool.getTotalStats();
Assertions.assertEquals(2, totals.getAvailable());
Assertions.assertEquals(0, totals.getLeased());
Assertions.assertEquals(0, totals.getPending());
}
use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestStrictConnPool method testCreateNewIfExpired.
@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);
Assertions.assertTrue(future1.isDone());
final PoolEntry<String, HttpConnection> entry1 = future1.get();
Assertions.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);
Assertions.assertTrue(future2.isDone());
Mockito.verify(conn1).close(CloseMode.GRACEFUL);
final PoolStats totals = pool.getTotalStats();
Assertions.assertEquals(0, totals.getAvailable());
Assertions.assertEquals(1, totals.getLeased());
Assertions.assertEquals(Collections.singleton("somehost"), pool.getRoutes());
final PoolStats stats = pool.getStats("somehost");
Assertions.assertEquals(0, stats.getAvailable());
Assertions.assertEquals(1, stats.getLeased());
}
use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestStrictConnPool method testLeaseRequestLockTimeout.
@Test
public void testLeaseRequestLockTimeout() throws Exception {
final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(1, 1);
final CountDownLatch lockHeld = new CountDownLatch(1);
final Thread holdInternalLock = new HoldInternalLockThread(pool, lockHeld);
// Start a thread to grab the internal conn pool lock
holdInternalLock.start();
// Wait until we know the internal lock is held
lockHeld.await();
// Attempt to get a connection while lock is held
final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null, Timeout.ofMilliseconds(10), null);
final ExecutionException executionException = Assertions.assertThrows(ExecutionException.class, () -> future2.get());
Assertions.assertTrue(executionException.getCause() instanceof DeadlineTimeoutException);
// Cleanup
holdInternalLock.interrupt();
}
Aggregations