final UnreliableLifecycle lifecycle = new UnreliableLifecycle();
final AsyncPool<AtomicBoolean> pool = new AsyncPoolImpl<AtomicBoolean>(
"object pool", lifecycle, POOL_SIZE, TIMEOUT, _executor
);
PoolStats stats;
final List<AtomicBoolean> objects = new ArrayList<AtomicBoolean>();
pool.start();
// do a few gets
for(int i = 0; i < GET; i++)
{
FutureCallback<AtomicBoolean> cb = new FutureCallback<AtomicBoolean>();
pool.get(cb);
AtomicBoolean obj = cb.get();
objects.add(obj);
}
// put and destroy some, with errors
lifecycle.setFail(true);
for(int i = 0; i < PUT_BAD; i++)
{
AtomicBoolean obj = objects.remove(objects.size() - 1);
obj.set(false);
pool.put(obj);
}
for(int i = 0; i < DISPOSE; i++)
{
AtomicBoolean obj = objects.remove(objects.size() - 1);
pool.dispose(obj);
}
stats = pool.getStats();
Assert.assertEquals(stats.getTotalDestroyed(), 0);
Assert.assertEquals(stats.getTotalCreateErrors(), 0);
Assert.assertEquals(stats.getTotalDestroyErrors(), PUT_BAD + DISPOSE);
Assert.assertEquals(stats.getTotalBadDestroyed(), PUT_BAD + DISPOSE);
// create some with errors
for(int i = 0; i < CREATE_BAD; i++)
{
FutureCallback<AtomicBoolean> cb = new FutureCallback<AtomicBoolean>();
try
{
pool.get(cb);
}
catch (Exception e)
{
// this error is expected
}
}
stats = pool.getStats();
Assert.assertEquals(stats.getCheckedOut(), GET - PUT_BAD - DISPOSE);
// When the each create fails, it will retry and cancel the waiter,
// resulting in a second create error.
Assert.assertEquals(stats.getTotalCreateErrors(), 2*CREATE_BAD);
}