Fixed broken test; added tests for POOL-240 (tests pass here).
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/pool/branches/1_5_RELEASE@1544942 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java b/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java
index a1f9d67..bc562a2 100644
--- a/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java
+++ b/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java
@@ -1782,4 +1782,59 @@
pool.returnObject(instance);
assertEquals(factory.getMakeCounter(), pool.getNumIdle());
}
+
+ /**
+ * Verify that threads waiting on a depleted pool get served when a checked out
+ * object is invalidated.
+ *
+ * JIRA: POOL-240
+ */
+ public void testInvalidateFreesCapacity() throws Exception {
+ SimpleFactory factory = new SimpleFactory();
+ GenericObjectPool pool = new GenericObjectPool(factory);
+ pool.setMaxActive(2);
+ pool.setMaxWait(500);
+ // Borrow an instance and hold if for 5 seconds
+ WaitingTestThread thread1 = new WaitingTestThread(pool, 5000);
+ thread1.start();
+ // Borrow another instance
+ Object obj = pool.borrowObject();
+ // Launch another thread - will block, but fail in 500 ms
+ WaitingTestThread thread2 = new WaitingTestThread(pool, 100);
+ thread2.start();
+ // Invalidate the object borrowed by this thread - should allow thread2 to create
+ Thread.sleep(20);
+ pool.invalidateObject(obj);
+ Thread.sleep(600); // Wait for thread2 to timeout
+ if (thread2._thrown != null) {
+ fail(thread2._thrown.toString());
+ }
+ }
+
+ /**
+ * Verify that threads waiting on a depleted pool get served when a returning
+ * object fails validation.
+ *
+ * JIRA: POOL-240
+ */
+ public void testValidationFailureOnReturnFreesCapacity() throws Exception {
+ SimpleFactory factory = new SimpleFactory();
+ factory.setValidationEnabled(true);
+ factory.setValid(false); // Validate will always fail
+ GenericObjectPool pool = new GenericObjectPool(factory);
+ pool.setMaxActive(2);
+ pool.setMaxWait(1500);
+ pool.setTestOnReturn(true);
+ pool.setTestOnBorrow(false);
+ // Borrow an instance and hold if for 5 seconds
+ WaitingTestThread thread1 = new WaitingTestThread(pool, 5000);
+ thread1.start();
+ // Borrow another instance and return it after 500 ms (validation will fail)
+ WaitingTestThread thread2 = new WaitingTestThread(pool, 500);
+ thread2.start();
+ Thread.sleep(50);
+ // Try to borrow an object
+ Object obj = pool.borrowObject();
+ pool.returnObject(obj);
+ }
}