commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r774574 - /commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java
Date Wed, 13 May 2009 22:43:35 GMT
Author: markt
Date: Wed May 13 22:43:34 2009
New Revision: 774574

URL: http://svn.apache.org/viewvc?rev=774574&view=rev
Log:
Add test case for POOL-135. There is a timing element so this may not fail on all machines.

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=774574&r1=774573&r2=774574&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
Wed May 13 22:43:34 2009
@@ -516,6 +516,84 @@
         }
     }
 
+    public void testMaxActiveUnderLoad() {
+        // Config
+        int numThreads = 199; // And main thread makes a round 200.
+        int numIter = 20;
+        int delay = 25;
+        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++) {
+            // Factor of 2 on iterations so main thread does work whilst other
+            // threads are running. Factor of 2 on delay so average delay for
+            // other threads == actual delay for main thread
+            threads[i] = new TestThread(pool, numIter * 2, delay * 2);
+            Thread t = new Thread(threads[i]);
+            t.start();
+        }
+        // Give the threads a chance to start doing some work
+        try {
+            Thread.sleep(5000);
+        } catch(Exception e) {
+            // ignored
+        }
+        
+        for (int i = 0; i < numIter; i++) {
+            Object obj = null;
+            try {
+                try {
+                    Thread.sleep(delay);
+                } catch(Exception e) {
+                    // ignored
+                }
+                obj = pool.borrowObject();
+                // Under load, observed _numActive > _maxActive 
+                if (pool.getNumActive() > pool.getMaxActive()) {
+                    throw new IllegalStateException("Too many active objects");
+                }
+                try {
+                    Thread.sleep(delay);
+                } catch(Exception e) {
+                    // ignored
+                }
+            } catch (Exception e) {
+                // Shouldn't happen
+                e.printStackTrace();
+                fail("Exception on borrow");
+            } finally {
+                if (obj != null) {
+                    try {
+                        pool.returnObject(obj);
+                    } catch (Exception e) {
+                        // Ignore
+                    }
+                }
+            }
+        }
+
+        for(int i=0;i<numThreads;i++) {
+            while(!(threads[i]).complete()) {
+                try {
+                    Thread.sleep(500L);
+                } catch(Exception e) {
+                    // ignored
+                }
+            }
+            if(threads[i].failed()) {
+                fail();
+            }
+        }
+    }
+
     public void testInvalidWhenExhaustedAction() throws Exception {
         try {
             pool.setWhenExhaustedAction(Byte.MAX_VALUE);
@@ -1297,6 +1375,7 @@
     protected boolean isFifo() {
         return false;
     }
+    
 }
 
 



Mime
View raw message