use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestLaxConnPool 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 LaxConnPool<String, HttpConnection> pool = new LaxConnPool<>(2);
pool.setMaxPerRoute("somehost", 2);
pool.setMaxPerRoute("otherhost", 1);
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());
}
use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestLaxConnPool 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 LaxConnPool<String, HttpConnection> pool = new LaxConnPool<>(2);
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 TestLaxConnPool method testLeaseRequestTimeout.
@Test
public void testLeaseRequestTimeout() throws Exception {
final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
final LaxConnPool<String, HttpConnection> pool = new LaxConnPool<>(1);
final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null, Timeout.ofMilliseconds(0), null);
final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null, Timeout.ofMilliseconds(0), null);
final Future<PoolEntry<String, HttpConnection>> future3 = pool.lease("somehost", null, Timeout.ofMilliseconds(10), null);
Assertions.assertTrue(future1.isDone());
final PoolEntry<String, HttpConnection> entry1 = future1.get();
Assertions.assertNotNull(entry1);
entry1.assignConnection(conn1);
Assertions.assertFalse(future2.isDone());
Assertions.assertFalse(future3.isDone());
Thread.sleep(100);
pool.validatePendingRequests();
Assertions.assertFalse(future2.isDone());
Assertions.assertTrue(future3.isDone());
}
use of org.apache.hc.core5.http.HttpConnection in project httpcomponents-core by apache.
the class TestStrictConnPool method testConnectionRedistributionOnTotalMaxLimit.
@Test
public void testConnectionRedistributionOnTotalMaxLimit() throws Exception {
final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
final HttpConnection conn3 = Mockito.mock(HttpConnection.class);
final HttpConnection conn4 = Mockito.mock(HttpConnection.class);
final HttpConnection conn5 = Mockito.mock(HttpConnection.class);
final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 10);
pool.setMaxPerRoute("somehost", 2);
pool.setMaxPerRoute("otherhost", 2);
pool.setMaxTotal(2);
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 Future<PoolEntry<String, HttpConnection>> future4 = pool.lease("otherhost", null);
Assertions.assertTrue(future1.isDone());
final PoolEntry<String, HttpConnection> entry1 = future1.get();
Assertions.assertNotNull(entry1);
Assertions.assertFalse(entry1.hasConnection());
entry1.assignConnection(conn1);
Assertions.assertTrue(future2.isDone());
final PoolEntry<String, HttpConnection> entry2 = future2.get();
Assertions.assertNotNull(entry2);
Assertions.assertFalse(entry2.hasConnection());
entry2.assignConnection(conn2);
Assertions.assertFalse(future3.isDone());
Assertions.assertFalse(future4.isDone());
PoolStats totals = pool.getTotalStats();
Assertions.assertEquals(0, totals.getAvailable());
Assertions.assertEquals(2, totals.getLeased());
Assertions.assertEquals(2, totals.getPending());
pool.release(entry1, true);
pool.release(entry2, true);
Assertions.assertTrue(future3.isDone());
final PoolEntry<String, HttpConnection> entry3 = future3.get();
Assertions.assertNotNull(entry3);
Assertions.assertFalse(entry3.hasConnection());
entry3.assignConnection(conn3);
Assertions.assertTrue(future4.isDone());
final PoolEntry<String, HttpConnection> entry4 = future4.get();
Assertions.assertNotNull(entry4);
Assertions.assertFalse(entry4.hasConnection());
entry4.assignConnection(conn4);
totals = pool.getTotalStats();
Assertions.assertEquals(0, totals.getAvailable());
Assertions.assertEquals(2, totals.getLeased());
Assertions.assertEquals(0, totals.getPending());
final Future<PoolEntry<String, HttpConnection>> future5 = pool.lease("somehost", null);
final Future<PoolEntry<String, HttpConnection>> future6 = pool.lease("otherhost", null);
pool.release(entry3, true);
pool.release(entry4, true);
Assertions.assertTrue(future5.isDone());
final PoolEntry<String, HttpConnection> entry5 = future5.get();
Assertions.assertNotNull(entry5);
Assertions.assertFalse(entry5.hasConnection());
entry5.assignConnection(conn5);
Assertions.assertTrue(future6.isDone());
final PoolEntry<String, HttpConnection> entry6 = future6.get();
Assertions.assertNotNull(entry6);
Assertions.assertTrue(entry6.hasConnection());
Assertions.assertSame(conn4, entry6.getConnection());
totals = pool.getTotalStats();
Assertions.assertEquals(0, totals.getAvailable());
Assertions.assertEquals(2, totals.getLeased());
Assertions.assertEquals(0, totals.getPending());
pool.release(entry5, true);
pool.release(entry6, true);
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 testStatefulConnectionRedistributionOnPerRouteMaxLimit.
@Test
public void testStatefulConnectionRedistributionOnPerRouteMaxLimit() throws Exception {
final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 10);
pool.setMaxPerRoute("somehost", 2);
pool.setMaxTotal(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();
entry1.assignConnection(conn1);
Assertions.assertNotNull(entry1);
Assertions.assertTrue(future2.isDone());
final PoolEntry<String, HttpConnection> entry2 = future2.get();
Assertions.assertNotNull(entry2);
entry2.assignConnection(conn2);
PoolStats totals = pool.getTotalStats();
Assertions.assertEquals(0, totals.getAvailable());
Assertions.assertEquals(2, totals.getLeased());
Assertions.assertEquals(0, totals.getPending());
entry1.updateState("some-stuff");
pool.release(entry1, true);
entry2.updateState("some-stuff");
pool.release(entry2, true);
final Future<PoolEntry<String, HttpConnection>> future3 = pool.lease("somehost", "some-stuff");
final Future<PoolEntry<String, HttpConnection>> future4 = pool.lease("somehost", "some-stuff");
Assertions.assertTrue(future1.isDone());
final PoolEntry<String, HttpConnection> entry3 = future3.get();
Assertions.assertNotNull(entry3);
Assertions.assertSame(conn2, entry3.getConnection());
Assertions.assertTrue(future4.isDone());
final PoolEntry<String, HttpConnection> entry4 = future4.get();
Assertions.assertNotNull(entry4);
Assertions.assertSame(conn1, entry4.getConnection());
pool.release(entry3, true);
pool.release(entry4, true);
totals = pool.getTotalStats();
Assertions.assertEquals(2, totals.getAvailable());
Assertions.assertEquals(0, totals.getLeased());
Assertions.assertEquals(0, totals.getPending());
final Future<PoolEntry<String, HttpConnection>> future5 = pool.lease("somehost", "some-other-stuff");
Assertions.assertTrue(future5.isDone());
Mockito.verify(conn2).close(CloseMode.GRACEFUL);
Mockito.verify(conn1, Mockito.never()).close(ArgumentMatchers.any());
totals = pool.getTotalStats();
Assertions.assertEquals(1, totals.getAvailable());
Assertions.assertEquals(1, totals.getLeased());
}
Aggregations