public void testConcurrentInvalidation() throws Exception {
final int POOL_SIZE = 30;
pool.setMaxActive(POOL_SIZE);
pool.setMaxIdle(POOL_SIZE);
pool.setWhenExhaustedAction(AbandonedObjectPool.WHEN_EXHAUSTED_FAIL);
// Exhaust the connection pool
Vector vec = new Vector();
for (int i = 0; i < POOL_SIZE; i++) {
vec.add(pool.borrowObject());
}
// Abandon all borrowed objects
for (int i = 0; i < vec.size(); i++) {
((PooledTestObject)vec.elementAt(i)).setAbandoned(true);
}
// Try launching a bunch of borrows concurrently. Abandoned sweep will be triggered for each.
final int CONCURRENT_BORROWS = 5;
Thread[] threads = new Thread[CONCURRENT_BORROWS];
for (int i = 0; i < CONCURRENT_BORROWS; i++) {
threads[i] = new ConcurrentBorrower(vec);
threads[i].start();
}
// Wait for all the threads to finish
for (int i = 0; i < CONCURRENT_BORROWS; i++) {
threads[i].join();
}
// Return all objects that have not been destroyed
for (int i = 0; i < vec.size(); i++) {
PooledTestObject pto = (PooledTestObject)vec.elementAt(i);
if (pto.isActive()) {
pool.returnObject(pto);
}
}
// Now, the number of open connections should be 0
assertTrue("numActive should have been 0, was " + pool.getNumActive(), pool.getNumActive() == 0);
}
Tests fix for Bug 28579, a bug in AbandonedObjectPool that causes numActive to go negative
in GenericObjectPool |