You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ma...@apache.org on 2009/05/17 18:35:24 UTC

svn commit: r775700 - /commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java

Author: markt
Date: Sun May 17 16:35:24 2009
New Revision: 775700

URL: http://svn.apache.org/viewvc?rev=775700&view=rev
Log:
Add test case for POOL-75

Modified:
    commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java

Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java?rev=775700&r1=775699&r2=775700&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java (original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java Sun May 17 16:35:24 2009
@@ -1117,6 +1117,7 @@
         int _iter = 100;
         int _delay = 50;
         boolean _randomDelay = true;
+        Object _expectedObject = null;
 
         public TestThread(ObjectPool pool) {
             _pool = pool;
@@ -1141,6 +1142,10 @@
             _randomDelay = randomDelay;
         }
 
+        public void setExpectedObject(Object obj) {
+            _expectedObject = obj;
+        }
+
         public boolean complete() {
             return _complete;
         }
@@ -1168,6 +1173,12 @@
                     break;
                 }
 
+                if (_expectedObject != null && !_expectedObject.equals(obj)) {
+                    _failed = true;
+                    _complete = true;
+                    break;
+                }
+                
                 try {
                     Thread.sleep(delay);
                 } catch(Exception e) {
@@ -1375,7 +1386,54 @@
     protected boolean isFifo() {
         return false;
     }
-    
+
+    /*
+     * Note: This test relies on timing for correct execution. There *should* be
+     * enough margin for this to work correctly on most (all?) systems but be
+     * aware of this if you see a failure of this test.
+     */
+    public void testBorrowObjectFairness() {
+        // Config
+        int numThreads = 30;
+        int maxActive = 10;
+
+        SimpleFactory factory = new SimpleFactory();
+        factory.setMaxActive(maxActive);
+        pool.setFactory(factory);
+        pool.setMaxActive(maxActive);
+        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
+        pool.setTimeBetweenEvictionRunsMillis(-1);
+
+        // Start threads to borrow objects
+        TestThread[] threads = new TestThread[numThreads];
+        for(int i=0;i<numThreads;i++) {
+            threads[i] = new TestThread(pool, 1, 500, false);
+            threads[i].setExpectedObject(String.valueOf(i % maxActive));
+            Thread t = new Thread(threads[i]);
+            t.start();
+            // Short delay to ensure threads start in correct order
+            try {
+                Thread.sleep(10);
+            } catch (InterruptedException e) {
+                fail();
+                e.printStackTrace();
+            }
+        }
+
+        // Wait for threads to finish
+        for(int i=0;i<numThreads;i++) {
+            while(!(threads[i]).complete()) {
+                try {
+                    Thread.sleep(500L);
+                } catch(Exception e) {
+                    // ignored
+                }
+            }
+            if(threads[i].failed()) {
+                fail();
+            }
+        }
+    }
 }