dirkv 2004/09/19 10:24:28
Modified: pool/src/test/org/apache/commons/pool/impl
TestGenericKeyedObjectPool.java
Log:
Bugzilla Bug 31298: setMinIdle feature implemented for GenericKeyedObjectPool
- original patch from Bowler Simon
Revision Changes Path
1.21 +145 -12
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.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- TestGenericKeyedObjectPool.java 4 Jul 2004 17:41:36 -0000 1.20
+++ TestGenericKeyedObjectPool.java 19 Sep 2004 17:24:28 -0000 1.21
@@ -1,12 +1,12 @@
/*
* Copyright 1999-2004 The Apache Software Foundation.
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -43,14 +43,14 @@
GenericKeyedObjectPool pool = new GenericKeyedObjectPool(
new KeyedPoolableObjectFactory() {
HashMap map = new HashMap();
- public Object makeObject(Object key) {
+ public Object makeObject(Object key) {
int counter = 0;
Integer Counter = (Integer)(map.get(key));
if(null != Counter) {
counter = Counter.intValue();
}
- map.put(key,new Integer(counter + 1));
- return String.valueOf(key) + String.valueOf(counter);
+ map.put(key,new Integer(counter + 1));
+ return String.valueOf(key) + String.valueOf(counter);
}
public void destroyObject(Object key, Object obj) { }
public boolean validateObject(Object key, Object obj) { return
true; }
@@ -62,11 +62,11 @@
pool.setMaxIdle(mincapacity);
return pool;
}
-
+
protected Object getNthObject(Object key, int n) {
return String.valueOf(key) + String.valueOf(n);
}
-
+
protected Object makeKey(int n) {
return String.valueOf(n);
}
@@ -77,7 +77,7 @@
super.setUp();
pool = new GenericKeyedObjectPool(new SimpleFactory());
}
-
+
public void tearDown() throws Exception {
super.tearDown();
pool.close();
@@ -91,7 +91,7 @@
pool.borrowObject("xyzzy");
fail("Expected NoSuchElementException");
} catch(NoSuchElementException e) {
- // expected
+ // expected
}
}
@@ -212,7 +212,7 @@
} catch(NoSuchElementException e) {
// expected
}
-
+
assertEquals(0, pool.getNumIdle());
pool.returnObject("b", o3);
@@ -458,6 +458,139 @@
}
}
+ public void testMinIdle() throws Exception {
+ pool.setMaxIdle(500);
+ pool.setMinIdle(5);
+ pool.setMaxActive(10);
+ pool.setNumTestsPerEvictionRun(0);
+ pool.setMinEvictableIdleTimeMillis(50L);
+ pool.setTimeBetweenEvictionRunsMillis(100L);
+ pool.setTestWhileIdle(true);
+
+
+ //Generate a random key
+ String key = "A";
+
+ pool.preparePool(key, true);
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+
+ Object[] active = new Object[5];
+ active[0] = pool.borrowObject(key);
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+
+ for(int i=1 ; i<5 ; i++) {
+ active[i] = pool.borrowObject(key);
+ }
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+
+ for(int i=0 ; i<5 ; i++) {
+ pool.returnObject(key, active[i]);
+ }
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 10 idle, found " +
pool.getNumIdle(),pool.getNumIdle() == 10);
+ }
+
+ public void testMinIdleMaxActive() throws Exception {
+ pool.setMaxIdle(500);
+ pool.setMinIdle(5);
+ pool.setMaxActive(10);
+ pool.setNumTestsPerEvictionRun(0);
+ pool.setMinEvictableIdleTimeMillis(50L);
+ pool.setTimeBetweenEvictionRunsMillis(100L);
+ pool.setTestWhileIdle(true);
+
+ String key = "A";
+
+ pool.preparePool(key, true);
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+
+ Object[] active = new Object[10];
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+
+ for(int i=0 ; i<5 ; i++) {
+ active[i] = pool.borrowObject(key);
+ }
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+
+ for(int i=0 ; i<5 ; i++) {
+ pool.returnObject(key, active[i]);
+ }
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 10 idle, found " +
pool.getNumIdle(),pool.getNumIdle() == 10);
+
+ for(int i=0 ; i<10 ; i++) {
+ active[i] = pool.borrowObject(key);
+ }
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 0 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 0);
+
+ for(int i=0 ; i<10 ; i++) {
+ pool.returnObject(key, active[i]);
+ }
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 10 idle, found " +
pool.getNumIdle(),pool.getNumIdle() == 10);
+ }
+
+ public void testMinIdleNoPopulateImmediately() throws Exception {
+ pool.setMaxIdle(500);
+ pool.setMinIdle(5);
+ pool.setMaxActive(10);
+ pool.setNumTestsPerEvictionRun(0);
+ pool.setMinEvictableIdleTimeMillis(50L);
+ pool.setTimeBetweenEvictionRunsMillis(1000L);
+ pool.setTestWhileIdle(true);
+
+
+ //Generate a random key
+ String key = "A";
+
+ pool.preparePool(key, false);
+
+ assertTrue("Should be 0 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 0);
+
+ try { Thread.sleep(1500L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+ }
+
+ public void testMinIdleNoPreparePool() throws Exception {
+ pool.setMaxIdle(500);
+ pool.setMinIdle(5);
+ pool.setMaxActive(10);
+ pool.setNumTestsPerEvictionRun(0);
+ pool.setMinEvictableIdleTimeMillis(50L);
+ pool.setTimeBetweenEvictionRunsMillis(100L);
+ pool.setTestWhileIdle(true);
+
+
+ //Generate a random key
+ String key = "A";
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 0 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 0);
+
+ Object active = pool.borrowObject(key);
+ assertNotNull(active);
+
+ try { Thread.sleep(150L); } catch(Exception e) { }
+ assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle()
== 5);
+ }
+
class TestThread implements Runnable {
java.util.Random _random = new java.util.Random();
KeyedObjectPool _pool = null;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]