@Test public void testReusesOnlyViableVals() throws Exception { Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).viabilityChecker(Predicates.equalTo(1)).build(); Lease<Integer> lease1 = pool.leaseObject(); Lease<Integer> lease2 = pool.leaseObject(); Lease<Integer> lease3 = pool.leaseObject(); lease1.close(); lease2.close(); lease3.close(); Lease<Integer> lease4 = pool.leaseObject(); assertEquals(lease4.leasedObject(), (Integer)1); }
@Test public void testClosesResourcesInPool() throws Exception { Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).closer(closer).build(); Lease<Integer> lease1 = pool.leaseObject(); Lease<Integer> lease2 = pool.leaseObject(); lease1.close(); lease2.close(); pool.close(); assertEquals(closedVals, ImmutableList.of(0, 1)); }
@Test public void testClosesResourceReturnedAfterPoolIsClosed() throws Exception { Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).closer(closer).build(); Lease<Integer> lease1 = pool.leaseObject(); pool.close(); assertEquals(closedVals, ImmutableList.of()); lease1.close(); assertEquals(closedVals, ImmutableList.of(lease1.leasedObject())); }
@Test public void testGeneratesNewIfOnlyUnviableValsInPool() throws Exception { Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).viabilityChecker(Predicates.alwaysFalse()).closer(closer).build(); Lease<Integer> lease1 = pool.leaseObject(); assertEquals(lease1.leasedObject(), (Integer)0); lease1.close(); Lease<Integer> lease2 = pool.leaseObject(); assertEquals(lease2.leasedObject(), (Integer)1); // Expect the "unviable" resource to have been closed assertEquals(closedVals, ImmutableList.of(0)); }
@Test public void testReusesReturnedVals() throws Exception { Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).build(); Lease<Integer> lease1 = pool.leaseObject(); assertEquals(lease1.leasedObject(), (Integer)0); lease1.close(); Lease<Integer> lease2 = pool.leaseObject(); assertEquals(lease2.leasedObject(), (Integer)0); }
@Test public void testDoesNotReuseUnviableVals() throws Exception { Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).viabilityChecker(Predicates.alwaysFalse()).build(); Lease<Integer> lease1 = pool.leaseObject(); assertEquals(lease1.leasedObject(), (Integer)0); lease1.close(); Lease<Integer> lease2 = pool.leaseObject(); assertEquals(lease2.leasedObject(), (Integer)1); }