commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From di...@apache.org
Subject svn commit: r170359 - in /jakarta/commons/proper/pool/trunk/src: java/org/apache/commons/pool/impl/StackObjectPool.java test/org/apache/commons/pool/impl/TestStackObjectPool.java
Date Mon, 16 May 2005 11:08:10 GMT
Author: dirkv
Date: Mon May 16 04:08:09 2005
New Revision: 170359

URL: http://svn.apache.org/viewcvs?rev=170359&view=rev
Log:
ASF Bugzilla Bug 34902 [pool] StackObjectPool.releaseObject(...) has silly behavior when the
pool is full
- report & patch by Sandy McArthur

Modified:
    jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java
    jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestStackObjectPool.java

Modified: jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java?rev=170359&r1=170358&r2=170359&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/java/org/apache/commons/pool/impl/StackObjectPool.java
Mon May 16 04:08:09 2005
@@ -38,6 +38,7 @@
  * artificial limits.
  *
  * @author Rodney Waldhoff
+ * @author Dirk Verbeeck
  * @version $Revision$ $Date$
  */
 public class StackObjectPool extends BaseObjectPool implements ObjectPool {
@@ -157,10 +158,14 @@
 
         synchronized(this) {
             _numActive--;
-            if(_pool.size() >= _maxSleeping) {
-                shouldDestroy = true;
-            } else if(success) {
+            if (success) {
+                Object toBeDestroyed = null;
+                if(_pool.size() >= _maxSleeping) {
+                    shouldDestroy = true;
+                    toBeDestroyed = _pool.remove(0); // remove the stalest object
+                }
                 _pool.push(obj);
+                obj = toBeDestroyed; // swap returned obj with the stalest one so it can
be destroyed
             }
             notifyAll(); // _numActive has changed
         }

Modified: jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestStackObjectPool.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestStackObjectPool.java?rev=170359&r1=170358&r2=170359&view=diff
==============================================================================
--- jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestStackObjectPool.java
(original)
+++ jakarta/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestStackObjectPool.java
Mon May 16 04:08:09 2005
@@ -18,6 +18,8 @@
 
 import java.util.BitSet;
 import java.util.NoSuchElementException;
+import java.util.List;
+import java.util.ArrayList;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
@@ -28,6 +30,7 @@
 
 /**
  * @author Rodney Waldhoff
+ * @author Dirk Verbeeck
  * @version $Revision$ $Date$
  */
 public class TestStackObjectPool extends TestObjectPool {
@@ -203,7 +206,56 @@
         }
     }
 
-        
+    private final List destroyed = new ArrayList();
+    public void testReturnObjectDiscardOrder() throws Exception {
+        // setup
+        // We need a factory that tracks what was discarded.
+        PoolableObjectFactory pof = new PoolableObjectFactory() {
+            int i = 0;
+            public Object makeObject() throws Exception {
+                return new Integer(i++);
+            }
+
+            public void destroyObject(Object obj) throws Exception {
+                destroyed.add(obj);
+            }
+
+            public boolean validateObject(Object obj) {
+                return obj instanceof Integer;
+            }
+
+            public void activateObject(Object obj) throws Exception {
+            }
+
+            public void passivateObject(Object obj) throws Exception {
+            }
+        };
+        ObjectPool pool = new StackObjectPool(pof, 3);
+
+        // borrow more objects than the pool can hold
+        Integer i0 = (Integer)pool.borrowObject();
+        Integer i1 = (Integer)pool.borrowObject();
+        Integer i2 = (Integer)pool.borrowObject();
+        Integer i3 = (Integer)pool.borrowObject();
+
+        // tests
+        // return as many as the pool will hold.
+        pool.returnObject(i0);
+        pool.returnObject(i1);
+        pool.returnObject(i2);
+
+        // the pool should now be full.
+        assertEquals("No returned objects should have been destroyed yet.",0, destroyed.size());
+
+        // cause the pool to discard a returned object.
+        pool.returnObject(i3);
+        assertEquals("One object should have been destroyed.", 1, destroyed.size());
+
+        // check to see what object was destroyed
+        Integer d = (Integer)destroyed.get(0);
+        assertEquals("Destoryed objects should have the stalest object.", i0, d);
+    }
+
     static class SimpleFactory implements PoolableObjectFactory {
         int counter = 0;
         public Object makeObject() { return String.valueOf(counter++); }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message