commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1175276 - in /commons/proper/pool/trunk/src: java/org/apache/commons/pool2/ java/org/apache/commons/pool2/impl/ test/org/apache/commons/pool2/ test/org/apache/commons/pool2/impl/
Date Sat, 24 Sep 2011 22:32:46 GMT
Author: psteitz
Date: Sat Sep 24 22:32:46 2011
New Revision: 1175276

URL: http://svn.apache.org/viewvc?rev=1175276&view=rev
Log:
Dropped BaseKeyedObjectPool.

Removed:
    commons/proper/pool/trunk/src/java/org/apache/commons/pool2/BaseKeyedObjectPool.java
    commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestBaseKeyedObjectPool.java
Modified:
    commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
    commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPool.java
    commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java

Modified: commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java?rev=1175276&r1=1175275&r2=1175276&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
Sat Sep 24 22:32:46 2011
@@ -43,7 +43,7 @@ import javax.management.MalformedObjectN
 import javax.management.NotCompliantMBeanException;
 import javax.management.ObjectName;
 
-import org.apache.commons.pool2.BaseKeyedObjectPool;
+import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.KeyedPoolableObjectFactory;
 import org.apache.commons.pool2.PoolUtils;
 
@@ -210,8 +210,8 @@ import org.apache.commons.pool2.PoolUtil
  * @version $Revision$ $Date$
  * @since Pool 1.0
  */
-public class GenericKeyedObjectPool<K,T> extends BaseKeyedObjectPool<K,T>
-        implements GenericKeyedObjectPoolMBean<K> {
+public class GenericKeyedObjectPool<K,T> implements KeyedObjectPool<K,T>,
+    GenericKeyedObjectPoolMBean<K> {
 
     //--- constructors -----------------------------------------------
 
@@ -712,7 +712,6 @@ public class GenericKeyedObjectPool<K,T>
      * @return object instance from the keyed pool
      * @throws NoSuchElementException if a keyed object instance cannot be returned.
      */
-    @Override
     public T borrowObject(K key) throws Exception {
         return borrowObject(key, _maxWait);
     }
@@ -868,7 +867,6 @@ public class GenericKeyedObjectPool<K,T>
       * @param t instance to return to the keyed pool
       * @throws Exception
       */
-     @Override
      public void returnObject(K key, T t) throws Exception {
          
          ObjectDeque<T> objectDeque = poolMap.get(key);
@@ -955,7 +953,6 @@ public class GenericKeyedObjectPool<K,T>
       * @param obj instance to invalidate
       * @throws Exception if an exception occurs destroying the object
       */
-     @Override
      public void invalidateObject(K key, T obj) throws Exception {
          
          ObjectDeque<T> objectDeque = poolMap.get(key);
@@ -984,7 +981,6 @@ public class GenericKeyedObjectPool<K,T>
       * while removed items are being destroyed.</li>
       * <li>Exceptions encountered destroying idle instances are swallowed.</li></ul></p>
       */
-     @Override
      public void clear() {
          Iterator<K> iter = poolMap.keySet().iterator();
          
@@ -1000,7 +996,6 @@ public class GenericKeyedObjectPool<K,T>
       *
       * @param key the key to clear
       */
-     @Override
      public void clear(K key) {
          
          register(key);
@@ -1034,7 +1029,6 @@ public class GenericKeyedObjectPool<K,T>
       *
       * @return the total number of instances currently borrowed from this pool
       */
-     @Override
      public int getNumActive() {
          return numTotal.get() - getNumIdle();
      }
@@ -1044,7 +1038,6 @@ public class GenericKeyedObjectPool<K,T>
       *
       * @return the total number of instances currently idle in this pool
       */
-     @Override
      public int getNumIdle() {
          Iterator<ObjectDeque<T>> iter = poolMap.values().iterator();
          int result = 0;
@@ -1063,7 +1056,6 @@ public class GenericKeyedObjectPool<K,T>
       * @param key the key to query
       * @return the number of instances corresponding to the given <code>key</code>
currently borrowed in this pool
       */
-     @Override
      public int getNumActive(K key) {
          final ObjectDeque<T> objectDeque = poolMap.get(key);
          if (objectDeque != null) {
@@ -1080,7 +1072,6 @@ public class GenericKeyedObjectPool<K,T>
       * @param key the key to query
       * @return the number of instances corresponding to the given <code>key</code>
currently idle in this pool
       */
-     @Override
      public int getNumIdle(K key) {
          final ObjectDeque<T> objectDeque = poolMap.get(key);
          return objectDeque != null ? objectDeque.getIdleObjects().size() : 0;
@@ -1097,7 +1088,6 @@ public class GenericKeyedObjectPool<K,T>
       * 
       * @throws Exception
       */
-     @Override
      public void close() throws Exception {
          if (isClosed()) {
              return;
@@ -1108,7 +1098,7 @@ public class GenericKeyedObjectPool<K,T>
                  return;
              }
 
-             super.close();
+             closed = true;
              clear();
              evictionIterator = null;
              evictionKeyIterator = null;
@@ -1121,6 +1111,30 @@ public class GenericKeyedObjectPool<K,T>
 
      }
 
+     /**
+      * Has this pool instance been closed.
+      * @return <code>true</code> when this pool has been closed.
+      * @since Pool 1.4
+      */
+     public boolean isClosed() {
+         return closed;
+     }
+
+     /**
+      * Throws an <code>IllegalStateException</code> when this pool has been
closed.
+      * @throws IllegalStateException when this pool has been closed.
+      * @see #isClosed()
+      * @since Pool 1.4
+      */
+     protected void assertOpen() throws IllegalStateException {
+         if(isClosed()) {
+             throw new IllegalStateException("Pool not open");
+         }
+     }
+
+     /** Whether or not the pool is closed */
+     private volatile boolean closed = false;
+
      
      /**
       * Clears oldest 15% of objects in pool.  The method sorts the
@@ -1589,7 +1603,6 @@ public class GenericKeyedObjectPool<K,T>
      * @throws IllegalStateException when no factory has been set or after {@link #close}
has been
      * called on this pool.
      */
-    @Override
     public void addObject(K key) throws Exception {
         assertOpen();
         if (_factory == null) {

Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPool.java?rev=1175276&r1=1175275&r2=1175276&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPool.java (original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool2/TestKeyedObjectPool.java Sat
Sep 24 22:32:46 2011
@@ -26,6 +26,7 @@ import java.util.NoSuchElementException;
 import junit.framework.TestCase;
 
 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
+import org.junit.After;
 import org.junit.Test;
 
 /**
@@ -45,6 +46,38 @@ public abstract class TestKeyedObjectPoo
     protected abstract KeyedObjectPool<Object,Object> makeEmptyPool(KeyedPoolableObjectFactory<Object,Object>
factory);
 
     protected final String KEY = "key";
+    
+    private KeyedObjectPool<Object,Object> _pool = null;
+    
+    protected abstract Object makeKey(int n);
+    
+    protected abstract boolean isFifo();
+    
+    protected abstract boolean isLifo();
+    
+    @After
+    public void tearDown() throws Exception {
+        _pool = null;
+    }
+    
+    /**
+     * Create an {@link KeyedObjectPool} instance
+     * that can contain at least <i>mincapacity</i>
+     * idle and active objects, or
+     * throw {@link IllegalArgumentException}
+     * if such a pool cannot be created.
+     * @param mincapacity 
+     */
+    protected abstract KeyedObjectPool<Object,Object> makeEmptyPool(int mincapacity);
 
+    
+    /**
+     * Return what we expect to be the n<sup>th</sup>
+     * object (zero indexed) created by the pool
+     * for the given key.
+     * @param key 
+     * @param n 
+     */
+    protected abstract Object getNthObject(Object key, int n);  
 
     @Test
     public void testClosedPoolBehavior() throws Exception {
@@ -380,6 +413,227 @@ public abstract class TestKeyedObjectPoo
             return; // test not supported
         }
     }
+    
+    @Test
+    public void testBaseBorrowReturn() throws Exception {
+        try {
+            _pool = makeEmptyPool(3);
+        } catch(UnsupportedOperationException uoe) {
+            return; // skip this test if unsupported
+        }
+        Object keya = makeKey(0);
+        Object obj0 = _pool.borrowObject(keya);
+        assertEquals(getNthObject(keya,0),obj0);
+        Object obj1 = _pool.borrowObject(keya);
+        assertEquals(getNthObject(keya,1),obj1);
+        Object obj2 = _pool.borrowObject(keya);
+        assertEquals(getNthObject(keya,2),obj2);
+        _pool.returnObject(keya,obj2);
+        obj2 = _pool.borrowObject(keya);
+        assertEquals(getNthObject(keya,2),obj2);
+        _pool.returnObject(keya,obj1);
+        obj1 = _pool.borrowObject(keya);
+        assertEquals(getNthObject(keya,1),obj1);
+        _pool.returnObject(keya,obj0);
+        _pool.returnObject(keya,obj2);
+        obj2 = _pool.borrowObject(keya);
+        if (isLifo()) {
+            assertEquals(getNthObject(keya,2),obj2);
+        }
+        if (isFifo()) {
+            assertEquals(getNthObject(keya,0),obj2);
+        }
+        obj0 = _pool.borrowObject(keya);
+        if (isLifo()) {
+            assertEquals(getNthObject(keya,0),obj0);
+        }
+        if (isFifo()) {
+            assertEquals(getNthObject(keya,2),obj0);
+        }
+    }
+
+    @Test
+    public void testBaseBorrow() throws Exception {
+        try {
+            _pool = makeEmptyPool(3);
+        } catch(UnsupportedOperationException uoe) {
+            return; // skip this test if unsupported
+        }
+        Object keya = makeKey(0);
+        Object keyb = makeKey(1);
+        assertEquals("1",getNthObject(keya,0),_pool.borrowObject(keya));
+        assertEquals("2",getNthObject(keyb,0),_pool.borrowObject(keyb));
+        assertEquals("3",getNthObject(keyb,1),_pool.borrowObject(keyb));
+        assertEquals("4",getNthObject(keya,1),_pool.borrowObject(keya));
+        assertEquals("5",getNthObject(keyb,2),_pool.borrowObject(keyb));
+        assertEquals("6",getNthObject(keya,2),_pool.borrowObject(keya));
+    }
+
+    @Test
+    public void testBaseNumActiveNumIdle() throws Exception {
+        try {
+            _pool = makeEmptyPool(3);
+        } catch(UnsupportedOperationException uoe) {
+            return; // skip this test if unsupported
+        }
+        Object keya = makeKey(0);
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        Object obj0 = _pool.borrowObject(keya);
+        assertEquals(1,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        Object obj1 = _pool.borrowObject(keya);
+        assertEquals(2,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        _pool.returnObject(keya,obj1);
+        assertEquals(1,_pool.getNumActive(keya));
+        assertEquals(1,_pool.getNumIdle(keya));
+        _pool.returnObject(keya,obj0);
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(2,_pool.getNumIdle(keya));
+
+        assertEquals(0,_pool.getNumActive("xyzzy12345"));
+        assertEquals(0,_pool.getNumIdle("xyzzy12345"));
+    }
+
+    @Test
+    public void testBaseNumActiveNumIdle2() throws Exception {
+        try {
+            _pool = makeEmptyPool(6);
+        } catch(UnsupportedOperationException uoe) {
+            return; // skip this test if unsupported
+        }
+        Object keya = makeKey(0);
+        Object keyb = makeKey(1);
+        assertEquals(0,_pool.getNumActive());
+        assertEquals(0,_pool.getNumIdle());
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        assertEquals(0,_pool.getNumActive(keyb));
+        assertEquals(0,_pool.getNumIdle(keyb));
+
+        Object objA0 = _pool.borrowObject(keya);
+        Object objB0 = _pool.borrowObject(keyb);
+
+        assertEquals(2,_pool.getNumActive());
+        assertEquals(0,_pool.getNumIdle());
+        assertEquals(1,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        assertEquals(1,_pool.getNumActive(keyb));
+        assertEquals(0,_pool.getNumIdle(keyb));
+
+        Object objA1 = _pool.borrowObject(keya);
+        Object objB1 = _pool.borrowObject(keyb);
+
+        assertEquals(4,_pool.getNumActive());
+        assertEquals(0,_pool.getNumIdle());
+        assertEquals(2,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        assertEquals(2,_pool.getNumActive(keyb));
+        assertEquals(0,_pool.getNumIdle(keyb));
+
+        _pool.returnObject(keya,objA0);
+        _pool.returnObject(keyb,objB0);
+
+        assertEquals(2,_pool.getNumActive());
+        assertEquals(2,_pool.getNumIdle());
+        assertEquals(1,_pool.getNumActive(keya));
+        assertEquals(1,_pool.getNumIdle(keya));
+        assertEquals(1,_pool.getNumActive(keyb));
+        assertEquals(1,_pool.getNumIdle(keyb));
+
+        _pool.returnObject(keya,objA1);
+        _pool.returnObject(keyb,objB1);
+
+        assertEquals(0,_pool.getNumActive());
+        assertEquals(4,_pool.getNumIdle());
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(2,_pool.getNumIdle(keya));
+        assertEquals(0,_pool.getNumActive(keyb));
+        assertEquals(2,_pool.getNumIdle(keyb));
+    }
+
+    @Test
+    public void testBaseClear() throws Exception {
+        try {
+            _pool = makeEmptyPool(3);
+        } catch(UnsupportedOperationException uoe) {
+            return; // skip this test if unsupported
+        }
+        Object keya = makeKey(0);
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        Object obj0 = _pool.borrowObject(keya);
+        Object obj1 = _pool.borrowObject(keya);
+        assertEquals(2,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        _pool.returnObject(keya,obj1);
+        _pool.returnObject(keya,obj0);
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(2,_pool.getNumIdle(keya));
+        _pool.clear(keya);
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        Object obj2 = _pool.borrowObject(keya);
+        assertEquals(getNthObject(keya,2),obj2);
+    }
+
+    @Test
+    public void testBaseInvalidateObject() throws Exception {
+        try {
+            _pool = makeEmptyPool(3);
+        } catch(UnsupportedOperationException uoe) {
+            return; // skip this test if unsupported
+        }
+        Object keya = makeKey(0);
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        Object obj0 = _pool.borrowObject(keya);
+        Object obj1 = _pool.borrowObject(keya);
+        assertEquals(2,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        _pool.invalidateObject(keya,obj0);
+        assertEquals(1,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+        _pool.invalidateObject(keya,obj1);
+        assertEquals(0,_pool.getNumActive(keya));
+        assertEquals(0,_pool.getNumIdle(keya));
+    }
+
+    @Test
+    public void testBaseAddObject() throws Exception {
+        try {
+            _pool = makeEmptyPool(3);
+        } catch(UnsupportedOperationException uoe) {
+            return; // skip this test if unsupported
+        }
+        Object key = makeKey(0);
+        try {
+            assertEquals(0,_pool.getNumIdle());
+            assertEquals(0,_pool.getNumActive());
+            assertEquals(0,_pool.getNumIdle(key));
+            assertEquals(0,_pool.getNumActive(key));
+            _pool.addObject(key);
+            assertEquals(1,_pool.getNumIdle());
+            assertEquals(0,_pool.getNumActive());
+            assertEquals(1,_pool.getNumIdle(key));
+            assertEquals(0,_pool.getNumActive(key));
+            Object obj = _pool.borrowObject(key);
+            assertEquals(getNthObject(key,0),obj);
+            assertEquals(0,_pool.getNumIdle());
+            assertEquals(1,_pool.getNumActive());
+            assertEquals(0,_pool.getNumIdle(key));
+            assertEquals(1,_pool.getNumActive(key));
+            _pool.returnObject(key,obj);
+            assertEquals(1,_pool.getNumIdle());
+            assertEquals(0,_pool.getNumActive());
+            assertEquals(1,_pool.getNumIdle(key));
+            assertEquals(0,_pool.getNumActive(key));
+        } catch(UnsupportedOperationException e) {
+            return; // skip this test if one of those calls is unsupported
+        }
+    }
+
 
     private void reset(final KeyedObjectPool<Object,Object> pool, final FailingKeyedPoolableObjectFactory
factory, final List<MethodCall> expectedMethods) throws Exception {
         pool.clear();

Modified: commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java?rev=1175276&r1=1175275&r2=1175276&view=diff
==============================================================================
--- commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
(original)
+++ commons/proper/pool/trunk/src/test/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
Sat Sep 24 22:32:46 2011
@@ -34,7 +34,7 @@ import java.util.concurrent.atomic.Atomi
 
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.KeyedPoolableObjectFactory;
-import org.apache.commons.pool2.TestBaseKeyedObjectPool;
+import org.apache.commons.pool2.TestKeyedObjectPool;
 import org.apache.commons.pool2.VisitTracker;
 import org.apache.commons.pool2.VisitTrackerFactory;
 import org.apache.commons.pool2.Waiter;
@@ -47,9 +47,8 @@ import org.junit.Test;
  * @author Rodney Waldhoff
  * @version $Revision$ $Date$
  */
-public class TestGenericKeyedObjectPool extends TestBaseKeyedObjectPool {
+public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
 
-    @Override
     protected KeyedObjectPool<Object,Object> makeEmptyPool(int mincapacity) {
 
         KeyedPoolableObjectFactory<Object,Object> factory =
@@ -79,19 +78,16 @@ public class TestGenericKeyedObjectPool 
         return pool;
     }
 
-    @Override
     protected KeyedObjectPool<Object,Object> makeEmptyPool(KeyedPoolableObjectFactory<Object,Object>
factory) {
         GenericKeyedObjectPool<Object,Object> pool =
             new GenericKeyedObjectPool<Object,Object>(factory);
         return pool;
     }
 
-    @Override
     protected Object getNthObject(Object key, int n) {
         return String.valueOf(key) + String.valueOf(n);
     }
 
-    @Override
     protected Object makeKey(int n) {
         return String.valueOf(n);
     }



Mime
View raw message