commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r899485 - /commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java
Date Fri, 15 Jan 2010 00:04:46 GMT
Author: sebb
Date: Fri Jan 15 00:04:45 2010
New Revision: 899485

URL: http://svn.apache.org/viewvc?rev=899485&view=rev
Log:
Add multi-threaded borrow/wait/return test
Prompted by problems with testing DBCP on Continuum

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

Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java?rev=899485&r1=899484&r2=899485&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java
Fri Jan 15 00:04:45 2010
@@ -1340,6 +1340,55 @@
     }
 
     /*
+     * Test multi-threaded pool access.
+     * Multiple keys, multiple threads, but maxActive only allows half the threads to succeed.
+     * 
+     * This test was prompted by Continuum build failures in the Commons DBCP test case:
+     * TestSharedPoolDataSource.testMultipleThreads2()
+     * Let's see if the this fails on Continuum too!
+     */
+    public void testMaxWaitMultiThreaded() throws Exception {
+        final long maxWait = 200; // wait for connection
+        final long holdTime = 2 * maxWait; // how long to hold connection
+        final int keyCount = 4; // number of different keys
+        final int threadsPerKey = 5; // number of threads to grab the key initiallu
+        SimpleFactory factory = new SimpleFactory();
+        GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory);
+        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
+        pool.setMaxWait(maxWait);
+        pool.setMaxActive(threadsPerKey);
+        // Create enough threads so half the threads will have to wait
+        WaitingTestThread wtt[] = new WaitingTestThread[keyCount * threadsPerKey * 2];
+        for(int i=0; i < wtt.length; i++){
+            wtt[i] = new WaitingTestThread(pool,Integer.toString(i % keyCount),holdTime);
+        }
+        long origin = System.currentTimeMillis()-1000;
+        for(int i=0; i < wtt.length; i++){
+            wtt[i].start();
+        }
+        int failed = 0;
+        for(int i=0; i < wtt.length; i++){
+            wtt[i].join();
+            if (wtt[i]._thrown != null){
+                failed++;
+            }
+        }
+        if (wtt.length/2 != failed){
+            for(int i=0; i < wtt.length; i++){
+                WaitingTestThread wt = wtt[i];
+                System.out.println(""
+                        + " Preborrow: "+(wt.preborrow-origin)
+                        + " Postborrow: "+(wt.postborrow != 0 ? wt.postborrow-origin : -1)
+                        + " BorrowTime: "+(wt.postborrow != 0 ? wt.postborrow-wt.preborrow
: -1)
+                        + " PostReturn: "+(wt.postreturn != 0 ? wt.postreturn-origin : -1)
+                        + " Ended: "+(wt.ended-origin)
+                        + " Key: "+(wt._key)
+                        );
+            }            
+        }
+        assertEquals("Expected half the threads to fail",wtt.length/2,failed);
+    }
+    /*
      * Very simple test thread that just tries to borrow an object from
      * the provided pool with the specified key and returns it
      */
@@ -1362,6 +1411,44 @@
         }
     }
     
+    /*
+     * Very simple test thread that just tries to borrow an object from
+     * the provided pool with the specified key and returns it after a wait
+     */
+    static class WaitingTestThread extends Thread {
+        private final KeyedObjectPool _pool;
+        private final String _key;
+        private final long _pause;
+        private Throwable _thrown;
+        
+        private long preborrow; // just before borrow
+        private long postborrow; //  borrow returned
+        private long postreturn; // after object was returned
+        private long ended;
+
+        public WaitingTestThread(KeyedObjectPool pool, String key, long pause) {
+            _pool = pool;
+            _key = key;
+            _pause = pause;
+            _thrown = null;
+        }
+
+        public void run() {
+            try {
+                preborrow = System.currentTimeMillis();
+                Object obj = _pool.borrowObject(_key);
+                postborrow = System.currentTimeMillis();
+                Thread.sleep(_pause);
+                _pool.returnObject(_key, obj);
+                postreturn = System.currentTimeMillis();
+            } catch (Exception e) {
+                _thrown = e;
+            } finally{
+                ended = System.currentTimeMillis();
+            }
+        }
+    }
+    
     static class TestThread implements Runnable {
         private final java.util.Random _random = new java.util.Random();
         



Mime
View raw message