@Override public <R> R exec(Function<? super T,R> receiver) { Lease<T> lease = leaseObject(); try { if (LOG.isTraceEnabled()) LOG.trace("{} executing {} with leasee {}", new Object[] {this, receiver, lease.leasedObject()}); return receiver.apply(lease.leasedObject()); } finally { Streams.closeQuietly(lease); } }
@Test public void testConcurrentCallsNeverHaveSameVal() throws Exception { final Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).build(); final Set<Lease<Integer>> leases = Collections.newSetFromMap(new ConcurrentHashMap<Lease<Integer>, Boolean>()); List<ListenableFuture<?>> futures = Lists.newArrayList(); for (int i = 0; i < 1000; i++) { futures.add(executor.submit(new Runnable() { @Override public void run() { leases.add(pool.leaseObject()); } })); } Futures.allAsList(futures).get(); Set<Integer> currentlyLeased = Sets.newLinkedHashSet(); for (Lease<Integer> lease : leases) { boolean val = currentlyLeased.add(lease.leasedObject()); if (!val) fail("duplicate="+lease.leasedObject()+"; vals="+leases); } } }
@Test public void testGeneratesNewValuesWhenRequired() throws Exception { Pool<Integer> pool = BasicPool.<Integer>builder().supplier(supplier).build(); Lease<Integer> lease1 = pool.leaseObject(); assertEquals(lease1.leasedObject(), (Integer)0); Lease<Integer> lease2 = pool.leaseObject(); assertEquals(lease2.leasedObject(), (Integer)1); }
@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 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 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 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); }
@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); }