@Test public void testMaxLimits() throws Exception {
  HttpConnectionFactory connFactory=Mockito.mock(HttpConnectionFactory.class);
  HttpConnection conn1=Mockito.mock(HttpConnection.class);
  Mockito.when(connFactory.create(Mockito.eq("somehost"))).thenReturn(conn1);
  HttpConnection conn2=Mockito.mock(HttpConnection.class);
  Mockito.when(connFactory.create(Mockito.eq("otherhost"))).thenReturn(conn2);
  LocalConnPool pool=new LocalConnPool(connFactory,2,10);
  pool.setMaxPerRoute("somehost",2);
  pool.setMaxPerRoute("otherhost",1);
  pool.setMaxTotal(3);
  Future<LocalPoolEntry> future1=pool.lease("somehost",null);
  GetPoolEntryThread t1=new GetPoolEntryThread(future1);
  t1.start();
  Future<LocalPoolEntry> future2=pool.lease("somehost",null);
  GetPoolEntryThread t2=new GetPoolEntryThread(future2);
  t2.start();
  Future<LocalPoolEntry> future3=pool.lease("otherhost",null);
  GetPoolEntryThread t3=new GetPoolEntryThread(future3);
  t3.start();
  t1.join(GRACE_PERIOD);
  Assert.assertTrue(future1.isDone());
  LocalPoolEntry entry1=t1.getEntry();
  Assert.assertNotNull(entry1);
  t2.join(GRACE_PERIOD);
  Assert.assertTrue(future2.isDone());
  LocalPoolEntry entry2=t2.getEntry();
  Assert.assertNotNull(entry2);
  t3.join(GRACE_PERIOD);
  Assert.assertTrue(future3.isDone());
  LocalPoolEntry entry3=t3.getEntry();
  Assert.assertNotNull(entry3);
  pool.release(entry1,true);
  pool.release(entry2,true);
  pool.release(entry3,true);
  PoolStats totals=pool.getTotalStats();
  Assert.assertEquals(3,totals.getAvailable());
  Assert.assertEquals(0,totals.getLeased());
  Future<LocalPoolEntry> future4=pool.lease("somehost",null);
  GetPoolEntryThread t4=new GetPoolEntryThread(future4);
  t4.start();
  Future<LocalPoolEntry> future5=pool.lease("somehost",null);
  GetPoolEntryThread t5=new GetPoolEntryThread(future5);
  t5.start();
  Future<LocalPoolEntry> future6=pool.lease("otherhost",null);
  GetPoolEntryThread t6=new GetPoolEntryThread(future6);
  t6.start();
  t4.join(GRACE_PERIOD);
  Assert.assertTrue(future4.isDone());
  LocalPoolEntry entry4=t4.getEntry();
  Assert.assertNotNull(entry4);
  t5.join(GRACE_PERIOD);
  Assert.assertTrue(future5.isDone());
  LocalPoolEntry entry5=t5.getEntry();
  Assert.assertNotNull(entry5);
  t6.join(GRACE_PERIOD);
  Assert.assertTrue(future6.isDone());
  LocalPoolEntry entry6=t6.getEntry();
  Assert.assertNotNull(entry6);
  Future<LocalPoolEntry> future7=pool.lease("somehost",null);
  GetPoolEntryThread t7=new GetPoolEntryThread(future7);
  t7.start();
  Future<LocalPoolEntry> future8=pool.lease("somehost",null);
  GetPoolEntryThread t8=new GetPoolEntryThread(future8);
  t8.start();
  Future<LocalPoolEntry> future9=pool.lease("otherhost",null);
  GetPoolEntryThread t9=new GetPoolEntryThread(future9);
  t9.start();
  Assert.assertFalse(t7.isDone());
  Assert.assertFalse(t8.isDone());
  Assert.assertFalse(t9.isDone());
  Mockito.verify(connFactory,Mockito.times(3)).create(Mockito.any(String.class));
  pool.release(entry4,true);
  pool.release(entry5,false);
  pool.release(entry6,true);
  t7.join();
  Assert.assertTrue(future7.isDone());
  t8.join();
  Assert.assertTrue(future8.isDone());
  t9.join();
  Assert.assertTrue(future9.isDone());
  Mockito.verify(connFactory,Mockito.times(4)).create(Mockito.any(String.class));
}
