protected <T> T execSsh(final Map<String, ?> props, final Function<ShellTool, T> task) { final LoadingCache<Map<String, ?>, Pool<SshTool>> sshPoolCache = getSshPoolCache(); Pool<SshTool> pool = sshPoolCache.getUnchecked(props); if (LOG.isTraceEnabled()) { LOG.trace("{} execSsh got pool: {}", this, pool); } if (groovyTruth(props.get(CLOSE_CONNECTION.getName()))) { Function<SshTool, T> close = new Function<SshTool, T>() { @Override public T apply(SshTool input) { T result = task.apply(input); if (LOG.isDebugEnabled()) { LOG.debug("{} invalidating all sshPoolCache entries: {}", SshMachineLocation.this, sshPoolCache.stats().toString()); } sshPoolCache.invalidateAll(); sshPoolCache.cleanUp(); return result; } }; return pool.exec(close); } else { return pool.exec(task); } }
@Override public void run() { leases.add(pool.leaseObject()); } }));
@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 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 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 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); }