commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwaldh...@apache.org
Subject cvs commit: jakarta-commons/pool/src/test/org/apache/commons/pool/impl TestGenericObjectPool.java TestGenericKeyedObjectPool.java
Date Fri, 18 Apr 2003 20:58:40 GMT
rwaldhoff    2003/04/18 13:58:40

  Modified:    pool/src/java/org/apache/commons/pool/impl
                        GenericObjectPool.java GenericKeyedObjectPool.java
               pool/src/test/org/apache/commons/pool/impl
                        TestGenericObjectPool.java
                        TestGenericKeyedObjectPool.java
  Log:
  fix issue 18062 by throwing NoSuchElementException when an newly created object is invalid.
 This prevents an endless loop caused by invalid factory configuration.
  
  Revision  Changes    Path
  1.19      +9 -4      jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java
  
  Index: GenericObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericObjectPool.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- GenericObjectPool.java	14 Mar 2003 01:04:50 -0000	1.18
  +++ GenericObjectPool.java	18 Apr 2003 20:58:39 -0000	1.19
  @@ -708,6 +708,7 @@
       public synchronized Object borrowObject() throws Exception {
           assertOpen();
           long starttime = System.currentTimeMillis();
  +        boolean newlyCreated = false;
           for(;;) {
               ObjectTimestampPair pair = null;
               // if there are any sleeping, just grab one of those
  @@ -723,6 +724,7 @@
                   if(_maxActive <= 0 || _numActive < _maxActive) {
                       Object obj = _factory.makeObject();
                       pair = new ObjectTimestampPair(obj);
  +                    newlyCreated = true;
                   } else {
                       // the pool is exhausted
                       switch(_whenExhaustedAction) {
  @@ -755,6 +757,9 @@
               _factory.activateObject(pair.value);
               if(_testOnBorrow && !_factory.validateObject(pair.value)) {
                   _factory.destroyObject(pair.value);
  +                if(newlyCreated) {
  +                    throw new NoSuchElementException("Could not create a validated object");
  +                } // else keep looping
               } else {
                   _numActive++;
                   return pair.value;
  
  
  
  1.15      +9 -4      jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java
  
  Index: GenericKeyedObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/java/org/apache/commons/pool/impl/GenericKeyedObjectPool.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- GenericKeyedObjectPool.java	14 Mar 2003 01:04:50 -0000	1.14
  +++ GenericKeyedObjectPool.java	18 Apr 2003 20:58:40 -0000	1.15
  @@ -729,6 +729,7 @@
   
       public synchronized Object borrowObject(Object key) throws Exception {
           long starttime = System.currentTimeMillis();
  +        boolean newlyCreated = false;
           for(;;) {
               CursorableLinkedList pool = (CursorableLinkedList)(_poolMap.get(key));
               if(null == pool) {
  @@ -757,6 +758,7 @@
                   if(_maxActive <= 0 || active < _maxActive) {
                       Object obj = _factory.makeObject(key);
                       pair = new ObjectTimestampPair(obj);
  +                    newlyCreated = true;
                   } else {
                       // the pool is exhausted
                       switch(_whenExhaustedAction) {
  @@ -789,6 +791,9 @@
               _factory.activateObject(key,pair.value);
               if(_testOnBorrow && !_factory.validateObject(key,pair.value)) {
                   _factory.destroyObject(key,pair.value);
  +                if(newlyCreated) {
  +                    throw new NoSuchElementException("Could not create a validated object");
  +                } // else keep looping
               } else {
                   Integer active = (Integer)(_activeMap.get(key));
                   if(null == active) {
  
  
  
  1.13      +24 -6     jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java
  
  Index: TestGenericObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestGenericObjectPool.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- TestGenericObjectPool.java	14 Mar 2003 00:58:59 -0000	1.12
  +++ TestGenericObjectPool.java	18 Apr 2003 20:58:40 -0000	1.13
  @@ -104,6 +104,17 @@
           pool = null;
       }
   
  +    public void testWithInitiallyInvalid() throws Exception {
  +        GenericObjectPool pool = new GenericObjectPool(new SimpleFactory(false));
  +        pool.setTestOnBorrow(true);
  +        try {
  +            pool.borrowObject();
  +            fail("Expected NoSuchElementException");
  +        } catch(NoSuchElementException e) {
  +            // expected 
  +        }
  +    }
  +
       public void testZeroMaxActive() throws Exception {
           pool.setMaxActive(0);
           pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
  @@ -389,12 +400,19 @@
       private GenericObjectPool pool = null;
   
       static class SimpleFactory implements PoolableObjectFactory {
  -        int counter = 0;
  +        public SimpleFactory() {
  +            this(true);
  +        }
  +        public SimpleFactory(boolean valid) {
  +            this.valid = valid;
  +        }
           public Object makeObject() { return String.valueOf(counter++); }
           public void destroyObject(Object obj) { }
  -        public boolean validateObject(Object obj) { return true; }
  +        public boolean validateObject(Object obj) { return valid; }
           public void activateObject(Object obj) { }
           public void passivateObject(Object obj) { }
  +        int counter = 0;
  +        boolean valid = true;
       }
   }
   
  
  
  
  1.12      +24 -6     jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java
  
  Index: TestGenericKeyedObjectPool.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/pool/src/test/org/apache/commons/pool/impl/TestGenericKeyedObjectPool.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- TestGenericKeyedObjectPool.java	7 Mar 2003 20:28:36 -0000	1.11
  +++ TestGenericKeyedObjectPool.java	18 Apr 2003 20:58:40 -0000	1.12
  @@ -128,6 +128,17 @@
           pool = null;
       }
   
  +    public void testWithInitiallyInvalid() throws Exception {
  +        GenericKeyedObjectPool pool = new GenericKeyedObjectPool(new SimpleFactory(false));
  +        pool.setTestOnBorrow(true);
  +        try {
  +            pool.borrowObject("xyzzy");
  +            fail("Expected NoSuchElementException");
  +        } catch(NoSuchElementException e) {
  +            // expected 
  +        }
  +    }
  +
       public void testZeroMaxActive() throws Exception {
           pool.setMaxActive(0);
           pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
  @@ -462,12 +473,19 @@
       }
   
       static class SimpleFactory implements KeyedPoolableObjectFactory {
  -        int counter = 0;
  +        public SimpleFactory() {
  +            this(true);
  +        }
  +        public SimpleFactory(boolean valid) {
  +            this.valid = valid;
  +        }
           public Object makeObject(Object key) { return String.valueOf(key) + String.valueOf(counter++);
}
           public void destroyObject(Object key, Object obj) { }
  -        public boolean validateObject(Object key, Object obj) { return true; }
  +        public boolean validateObject(Object key, Object obj) { return valid; }
           public void activateObject(Object key, Object obj) { }
           public void passivateObject(Object key, Object obj) { }
  +        int counter = 0;
  +        boolean valid;
       }
   
   }
  
  
  

---------------------------------------------------------------------
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