@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 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); }
private BasicPool<SshTool> buildPool(final Map<String, ?> properties) { return BasicPool.<SshTool>builder() .name(getDisplayName()+"@"+address+":"+getPort()+ (config().getRaw(SSH_HOST).isPresent() ? "("+getConfig(SSH_HOST)+":"+getPort()+")" : "")+ ":hash"+System.identityHashCode(this)) .supplier(new Supplier<SshTool>() { @Override public SshTool get() { return connectSsh(properties); }}) .viabilityChecker(new Predicate<SshTool>() { @Override public boolean apply(SshTool input) { return input != null && input.isConnected(); }}) .closer(new Function<SshTool,Void>() { @Override public Void apply(SshTool input) { if (LOG.isDebugEnabled()) { LOG.debug("{} closing pool for {}", this, input); } try { input.disconnect(); } catch (Exception e) { if (logSsh.isDebugEnabled()) logSsh.debug("On machine "+SshMachineLocation.this+", ssh-disconnect failed", e); } return null; }}) .build(); }