Search in sources :

Example 11 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool 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());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 12 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool 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();
}
Also used : DeadlineTimeoutException(org.apache.hc.core5.util.DeadlineTimeoutException) HttpConnection(org.apache.hc.core5.http.HttpConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 13 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool in project httpcomponents-core by apache.

the class TestStrictConnPool method testLeaseRequestInterrupted.

@Test
public void testLeaseRequestInterrupted() 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();
    Thread.currentThread().interrupt();
    // Attempt to get a connection while lock is held and thread is interrupted
    final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null, Timeout.ofMilliseconds(10), null);
    Assertions.assertTrue(Thread.interrupted());
    Assertions.assertThrows(CancellationException.class, () -> future2.get());
    // Cleanup
    holdInternalLock.interrupt();
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 14 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool in project httpcomponents-core by apache.

the class TestStrictConnPool method testMaxLimits.

@Test
public void testMaxLimits() 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);
    pool.setMaxPerRoute("somehost", 2);
    pool.setMaxPerRoute("otherhost", 1);
    pool.setMaxTotal(3);
    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, true);
    final PoolStats totals = pool.getTotalStats();
    Assertions.assertEquals(3, totals.getAvailable());
    Assertions.assertEquals(0, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    final Future<PoolEntry<String, HttpConnection>> future4 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future5 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future6 = pool.lease("otherhost", null);
    final Future<PoolEntry<String, HttpConnection>> future7 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future8 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future9 = pool.lease("otherhost", null);
    Assertions.assertTrue(future4.isDone());
    final PoolEntry<String, HttpConnection> entry4 = future4.get();
    Assertions.assertNotNull(entry4);
    Assertions.assertSame(conn2, entry4.getConnection());
    Assertions.assertTrue(future5.isDone());
    final PoolEntry<String, HttpConnection> entry5 = future5.get();
    Assertions.assertNotNull(entry5);
    Assertions.assertSame(conn1, entry5.getConnection());
    Assertions.assertTrue(future6.isDone());
    final PoolEntry<String, HttpConnection> entry6 = future6.get();
    Assertions.assertNotNull(entry6);
    Assertions.assertSame(conn3, entry6.getConnection());
    Assertions.assertFalse(future7.isDone());
    Assertions.assertFalse(future8.isDone());
    Assertions.assertFalse(future9.isDone());
    pool.release(entry4, true);
    pool.release(entry5, false);
    pool.release(entry6, true);
    Assertions.assertTrue(future7.isDone());
    final PoolEntry<String, HttpConnection> entry7 = future7.get();
    Assertions.assertNotNull(entry7);
    Assertions.assertSame(conn2, entry7.getConnection());
    Assertions.assertTrue(future8.isDone());
    final PoolEntry<String, HttpConnection> entry8 = future8.get();
    Assertions.assertNotNull(entry8);
    Assertions.assertNull(entry8.getConnection());
    Assertions.assertTrue(future9.isDone());
    final PoolEntry<String, HttpConnection> entry9 = future9.get();
    Assertions.assertNotNull(entry9);
    Assertions.assertSame(conn3, entry9.getConnection());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Aggregations

HttpConnection (org.apache.hc.core5.http.HttpConnection)10 Test (org.junit.jupiter.api.Test)10 HttpHost (org.apache.hc.core5.http.HttpHost)4 DefaultDisposalCallback (org.apache.hc.core5.pool.DefaultDisposalCallback)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpRequestExecutor (org.apache.hc.core5.http.impl.io.HttpRequestExecutor)2 ClientHttp1StreamDuplexerFactory (org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory)2 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)2 IOEventHandlerFactory (org.apache.hc.core5.reactor.IOEventHandlerFactory)2 IOSession (org.apache.hc.core5.reactor.IOSession)2 ExecutionException (java.util.concurrent.ExecutionException)1 Supplier (org.apache.hc.core5.function.Supplier)1 HttpRequester (org.apache.hc.core5.http.impl.bootstrap.HttpRequester)1 DefaultBHttpClientConnectionFactory (org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory)1 ClientHttp1IOEventHandlerFactory (org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandlerFactory)1 DefaultHttpResponseParserFactory (org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory)1 DefaultTlsSetupHandler (org.apache.hc.core5.http.io.ssl.DefaultTlsSetupHandler)1 BasicClientTlsStrategy (org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy)1 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)1 RequestHandlerRegistry (org.apache.hc.core5.http.protocol.RequestHandlerRegistry)1