commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r755728 - in /commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl: TestGenericKeyedObjectPool.java TestGenericObjectPool.java
Date Wed, 18 Mar 2009 20:52:41 GMT
Author: psteitz
Date: Wed Mar 18 20:52:41 2009
New Revision: 755728

URL: http://svn.apache.org/viewvc?rev=755728&view=rev
Log:
Added tests to ensure pool counters are properly maintained when factory exceptions occur.

Modified:
    commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java
    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/TestGenericKeyedObjectPool.java
URL: http://svn.apache.org/viewvc/commons/proper/pool/trunk/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java?rev=755728&r1=755727&r2=755728&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
Wed Mar 18 20:52:41 2009
@@ -1205,6 +1205,85 @@
         assertEquals(whenExhaustedAction,pool.getWhenExhaustedAction());
         assertEquals(lifo, pool.getLifo());  
     }
+    
+    public void testExceptionOnPassivateDuringReturn() throws Exception {
+        SimpleFactory factory = new SimpleFactory();        
+        GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory);
+        Object obj = pool.borrowObject("one");
+        factory.setThrowExceptionOnPassivate(true);
+        pool.returnObject("one", obj);
+        assertEquals(0,pool.getNumIdle());
+        pool.close();
+    }
+    
+    public void testExceptionOnDestroyDuringBorrow() throws Exception {
+        SimpleFactory factory = new SimpleFactory(); 
+        factory.setThrowExceptionOnDestroy(true);
+        factory.setValidationEnabled(true);
+        GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory);
+        pool.setTestOnBorrow(true);
+        Object obj1 = pool.borrowObject("one");
+        factory.setValid(false); // Make validation fail on next borrow attempt
+        try {
+            Object obj2 = pool.borrowObject("one");
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException ex) {
+            // expected
+        }
+        assertEquals(1, pool.getNumActive("one"));
+        assertEquals(0, pool.getNumIdle("one"));
+        assertEquals(1, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+    }
+    
+    public void testExceptionOnDestroyDuringReturn() throws Exception {
+        SimpleFactory factory = new SimpleFactory(); 
+        factory.setThrowExceptionOnDestroy(true);
+        factory.setValidationEnabled(true);
+        GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory);
+        pool.setTestOnReturn(true);
+        Object obj1 = pool.borrowObject("one");
+        Object obj2 = pool.borrowObject("one");
+        factory.setValid(false); // Make validation fail
+        pool.returnObject("one", obj1);
+        assertEquals(1, pool.getNumActive("one"));
+        assertEquals(0, pool.getNumIdle("one"));
+        assertEquals(1, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+    }
+    
+    public void testExceptionOnActivateDuringBorrow() throws Exception {
+        SimpleFactory factory = new SimpleFactory(); 
+        GenericKeyedObjectPool pool = new GenericKeyedObjectPool(factory);
+        Object obj1 = pool.borrowObject("one");
+        Object obj2 = pool.borrowObject("one");
+        pool.returnObject("one", obj1);
+        pool.returnObject("one", obj2);
+        factory.setThrowExceptionOnActivate(true);
+        factory.setEvenValid(false);  
+        // Activation will now throw every other time
+        // First attempt throws, but loop continues and second succeeds
+        Object obj = pool.borrowObject("one");
+        assertEquals(1, pool.getNumActive("one"));
+        assertEquals(0, pool.getNumIdle("one"));
+        assertEquals(1, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+        
+        pool.returnObject("one", obj);
+        factory.setValid(false);
+        // Validation will now fail on activation when borrowObject returns
+        // an idle instance, and then when attempting to create a new instance
+        try {
+            obj1 = pool.borrowObject("one");
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException ex) {
+            // expected
+        }
+        assertEquals(0, pool.getNumActive("one"));
+        assertEquals(0, pool.getNumIdle("one"));
+        assertEquals(0, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+    }
 
     class TestThread implements Runnable {
         java.util.Random _random = new java.util.Random();
@@ -1290,11 +1369,14 @@
             }
             return String.valueOf(key) + String.valueOf(counter++);
         }
-        public void destroyObject(Object key, Object obj) {
+        public void destroyObject(Object key, Object obj) throws Exception {
             doWait(destroyLatency);
             synchronized(this) {
                 activeCount--;
             }
+            if (exceptionOnDestroy) {
+                throw new Exception();
+            }
         }
         public boolean validateObject(Object key, Object obj) {
             if (enableValidation) { 
@@ -1303,8 +1385,18 @@
                 return valid;
             }
         }
-        public void activateObject(Object key, Object obj) { }
-        public void passivateObject(Object key, Object obj) { }
+        public void activateObject(Object key, Object obj) throws Exception {
+            if (exceptionOnActivate) {
+                if (!(validateCounter++%2 == 0 ? evenValid : oddValid)) {
+                    throw new Exception();
+                }
+            }
+        }
+        public void passivateObject(Object key, Object obj) throws Exception {
+            if (exceptionOnPassivate) {
+                throw new Exception();
+            }
+        }
         
         public void setMaxActive(int maxActive) {
             this.maxActive = maxActive;
@@ -1318,6 +1410,22 @@
         void setEvenValid(boolean valid) {
             evenValid = valid;
         }
+        void setValid(boolean valid) {
+            evenValid = valid;
+            oddValid = valid;
+        }
+        
+        public void setThrowExceptionOnActivate(boolean b) {
+            exceptionOnActivate = b;
+        }
+        
+        public void setThrowExceptionOnDestroy(boolean b) {
+            exceptionOnDestroy = b;
+        }
+        
+        public void setThrowExceptionOnPassivate(boolean b) {
+            exceptionOnPassivate = b;
+        }
         
         int counter = 0;
         boolean valid;
@@ -1329,6 +1437,9 @@
         boolean enableValidation = false;
         long destroyLatency = 0;
         int maxActive = Integer.MAX_VALUE;
+        boolean exceptionOnPassivate = false;
+        boolean exceptionOnActivate = false;
+        boolean exceptionOnDestroy = false;
         
         private void doWait(long latency) {
             try {

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=755728&r1=755727&r2=755728&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 Mar 18 20:52:41 2009
@@ -358,6 +358,65 @@
         assertEquals(0,pool.getNumIdle());
         pool.close();
     }
+    
+    public void testExceptionOnDestroyDuringBorrow() throws Exception {
+        SimpleFactory factory = new SimpleFactory(); 
+        factory.setThrowExceptionOnDestroy(true);
+        GenericObjectPool pool = new GenericObjectPool(factory);
+        pool.setTestOnBorrow(true);
+        Object obj1 = pool.borrowObject();
+        factory.setValid(false); // Make validation fail on next borrow attempt
+        try {
+            Object obj2 = pool.borrowObject();
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException ex) {
+            // expected
+        }
+        assertEquals(1, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+    }
+    
+    public void testExceptionOnDestroyDuringReturn() throws Exception {
+        SimpleFactory factory = new SimpleFactory(); 
+        factory.setThrowExceptionOnDestroy(true);
+        GenericObjectPool pool = new GenericObjectPool(factory);
+        pool.setTestOnReturn(true);
+        Object obj1 = pool.borrowObject();
+        Object obj2 = pool.borrowObject();
+        factory.setValid(false); // Make validation fail
+        pool.returnObject(obj1);
+        assertEquals(1, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+    }
+    
+    public void testExceptionOnActivateDuringBorrow() throws Exception {
+        SimpleFactory factory = new SimpleFactory(); 
+        GenericObjectPool pool = new GenericObjectPool(factory);
+        Object obj1 = pool.borrowObject();
+        Object obj2 = pool.borrowObject();
+        pool.returnObject(obj1);
+        pool.returnObject(obj2);
+        factory.setThrowExceptionOnActivate(true);
+        factory.setEvenValid(false);  
+        // Activation will now throw every other time
+        // First attempt throws, but loop continues and second succeeds
+        Object obj = pool.borrowObject();
+        assertEquals(1, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+        
+        pool.returnObject(obj);
+        factory.setValid(false);
+        // Validation will now fail on activation when borrowObject returns
+        // an idle instance, and then when attempting to create a new instance
+        try {
+            obj1 = pool.borrowObject();
+            fail("Expecting NoSuchElementException");
+        } catch (NoSuchElementException ex) {
+            // expected
+        }
+        assertEquals(0, pool.getNumActive());
+        assertEquals(0, pool.getNumIdle());
+    }
 
     public void testSetFactoryWithActiveObjects() throws Exception {
         GenericObjectPool pool = new GenericObjectPool();
@@ -1173,13 +1232,16 @@
             }
             return String.valueOf(makeCounter++);
         }
-        public void destroyObject(Object obj) {
+        public void destroyObject(Object obj) throws Exception {
             if (destroyLatency > 0) {
                 doWait(destroyLatency);
             }
             synchronized(this) {
                 activeCount--;
             }
+            if (exceptionOnDestroy) {
+                throw new Exception();
+            }
         }
         public boolean validateObject(Object obj) {
             if (enableValidation) { 
@@ -1208,6 +1270,7 @@
         boolean oddValid = true;
         boolean exceptionOnPassivate = false;
         boolean exceptionOnActivate = false;
+        boolean exceptionOnDestroy = false;
         boolean enableValidation = true;
         long destroyLatency = 0;
         long makeLatency = 0;
@@ -1220,6 +1283,10 @@
         public void setThrowExceptionOnActivate(boolean b) {
             exceptionOnActivate = b;
         }
+        
+        public void setThrowExceptionOnDestroy(boolean b) {
+            exceptionOnDestroy = b;
+        }
 
         public boolean isValidationEnabled() {
             return enableValidation;



Mime
View raw message