Search in sources :

Example 36 with HttpConnection

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

Example 37 with HttpConnection

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

Example 38 with HttpConnection

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

Example 39 with HttpConnection

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

Example 40 with HttpConnection

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();
}
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)

Aggregations

HttpConnection (org.apache.hc.core5.http.HttpConnection)40 HttpResponse (org.apache.hc.core5.http.HttpResponse)19 Test (org.junit.jupiter.api.Test)17 HttpRequest (org.apache.hc.core5.http.HttpRequest)15 Header (org.apache.hc.core5.http.Header)14 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)13 HttpAsyncRequester (org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 HttpHost (org.apache.hc.core5.http.HttpHost)11 RequestLine (org.apache.hc.core5.http.message.RequestLine)11 StatusLine (org.apache.hc.core5.http.message.StatusLine)11 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)11 H2StreamListener (org.apache.hc.core5.http2.impl.nio.H2StreamListener)11 List (java.util.List)10 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)10 IOException (java.io.IOException)9 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)9 Message (org.apache.hc.core5.http.Message)8 HttpException (org.apache.hc.core5.http.HttpException)7 AsyncClientEndpoint (org.apache.hc.core5.http.nio.AsyncClientEndpoint)7