AsyncPoolResourceManagerAdapter implements Lifecycle<T> { @Override public void destroy(final T obj, final boolean error, final Callback<T> callback) { _executor.submit(new Runnable() { @Override public void run() { LOGGER.info("Running teardown for the client connection " + obj + " Error is : " + error); boolean success = _resourceManager.destroy(_key, error, obj); if (success) { callback.onSuccess(obj); } else { callback.onError(new Exception("Unable to destroy resource for key " + _key)); } } }); } AsyncPoolResourceManagerAdapter(K key, PooledResourceManager<K, T> resourceManager,
      ExecutorService executorService, MetricsRegistry registry); @Override void create(final Callback<T> callback); @Override boolean validateGet(T obj); @Override boolean validatePut(T obj); @Override void destroy(final T obj, final boolean error, final Callback<T> callback); @Override LifecycleStats<Histogram> getStats();  }
@Test public void testDestroy() { { String key = "localhost:8080"; String value = "dummy"; MyPooledResourceManager rm = new MyPooledResourceManager(true, null); AsyncPoolResourceManagerAdapter<String, String> adapter = new AsyncPoolResourceManagerAdapter<String, String>(key, rm, MoreExecutors.sameThreadExecutor(), null); MyCallback callback = new MyCallback(); adapter.destroy(value, true, callback); Assert.assertEquals(callback.getResource(), value, "Callback Resource"); Assert.assertEquals(callback.isOnSuccessCalled(), true, "Callback onSuccess"); Assert.assertEquals(callback.isOnErrorCalled(), false, "Callback onError"); Assert.assertNull(callback.getThrowable(), "Callback Error Null"); Assert.assertEquals(rm.getKeyForDestroy(), key, "Resource Manager create Key"); Assert.assertEquals(rm.getResourceForDestroy(), value, "Resource Manager create Resource"); } { String key = "localhost:8080"; String value = "dummy"; MyPooledResourceManager rm = new MyPooledResourceManager(false, null); AsyncPoolResourceManagerAdapter<String, String> adapter = new AsyncPoolResourceManagerAdapter<String, String>(key, rm, MoreExecutors.sameThreadExecutor(), null); MyCallback callback = new MyCallback(); adapter.destroy(value, true, callback); Assert.assertNull(callback.getResource(), "Callback Resource"); Assert.assertEquals(callback.isOnSuccessCalled(), false, "Callback onSuccess"); Assert.assertEquals(callback.isOnErrorCalled(), true, "Callback onError"); Assert.assertNotNull(callback.getThrowable(), "Callback Error Null"); Assert.assertEquals(rm.getKeyForDestroy(), key, "Resource Manager create Key"); Assert.assertEquals(rm.getResourceForDestroy(), value, "Resource Manager create Resource"); } }