KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > pool > impl > TestGenericObjectPool


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.pool.impl;
18
19 import java.util.NoSuchElementException JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestSuite;
23
24 import org.apache.commons.pool.BasePoolableObjectFactory;
25 import org.apache.commons.pool.ObjectPool;
26 import org.apache.commons.pool.PoolableObjectFactory;
27 import org.apache.commons.pool.TestObjectPool;
28
29 /**
30  * @author Rodney Waldhoff
31  * @author Dirk Verbeeck
32  * @version $Revision$ $Date: 2005-02-26 05:13:28 -0800 (Sat, 26 Feb 2005) $
33  */

34 public class TestGenericObjectPool extends TestObjectPool {
35     public TestGenericObjectPool(String JavaDoc testName) {
36         super(testName);
37     }
38
39     public static Test suite() {
40         return new TestSuite(TestGenericObjectPool.class);
41     }
42
43     protected ObjectPool makeEmptyPool(int mincap) {
44        GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
45        pool.setMaxActive(mincap);
46        pool.setMaxIdle(mincap);
47        return pool;
48     }
49     
50     protected Object JavaDoc getNthObject(int n) {
51         return String.valueOf(n);
52     }
53
54     public void setUp() throws Exception JavaDoc {
55         super.setUp();
56         pool = new GenericObjectPool(new SimpleFactory());
57     }
58
59     public void tearDown() throws Exception JavaDoc {
60         super.tearDown();
61         pool.close();
62         pool = null;
63     }
64
65     /**
66      * Activation failure on existing object doesn't fail the borrow
67      */

68     public void testActivationException() throws Exception JavaDoc {
69         SimpleFactory factory = new SimpleFactory(true, false);
70         factory.setThrowExceptionOnActivate(true);
71         factory.setValidationEnabled(false);
72         GenericObjectPool pool = new GenericObjectPool(factory);
73         
74         Object JavaDoc obj1 = pool.borrowObject();
75         pool.returnObject(obj1);
76
77         // obj1 was returned to the pool but failed to activate the second borrow
78
// a new object obj2 needs te be created
79
Object JavaDoc obj2 = pool.borrowObject();
80         assertTrue(obj1 != obj2);
81     }
82
83     /**
84      * Activation failure on new object fails the borrow
85      */

86     public void testActivationExceptionOnNewObject() throws Exception JavaDoc {
87         SimpleFactory factory = new SimpleFactory(true, false);
88         factory.setThrowExceptionOnActivate(true);
89         factory.setValidationEnabled(false);
90         GenericObjectPool pool = new GenericObjectPool(factory);
91         
92         Object JavaDoc obj1 = pool.borrowObject();
93         try {
94             Object JavaDoc obj2 = pool.borrowObject();
95             System.out.println("obj1: " + obj1);
96             System.out.println("obj2: " + obj2);
97             fail("a second object should have been created and failed to activate");
98         }
99         catch (Exception JavaDoc e) {}
100     }
101
102     public void testWhenExhaustedGrow() throws Exception JavaDoc {
103         GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
104         pool.setMaxActive(1);
105         pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
106         Object JavaDoc obj1 = pool.borrowObject();
107         assertNotNull(obj1);
108         Object JavaDoc obj2 = pool.borrowObject();
109         assertNotNull(obj2);
110         pool.returnObject(obj2);
111         pool.returnObject(obj1);
112         pool.close();
113     }
114
115     public void testWhenExhaustedFail() throws Exception JavaDoc {
116         GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
117         pool.setMaxActive(1);
118         pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
119         Object JavaDoc obj1 = pool.borrowObject();
120         assertNotNull(obj1);
121         try {
122             pool.borrowObject();
123             fail("Expected NoSuchElementException");
124         } catch(NoSuchElementException JavaDoc e) {
125             // expected
126
}
127         pool.returnObject(obj1);
128         pool.close();
129     }
130
131     public void testWhenExhaustedBlock() throws Exception JavaDoc {
132         GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
133         pool.setMaxActive(1);
134         pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
135         pool.setMaxWait(10L);
136         Object JavaDoc obj1 = pool.borrowObject();
137         assertNotNull(obj1);
138         try {
139             pool.borrowObject();
140             fail("Expected NoSuchElementException");
141         } catch(NoSuchElementException JavaDoc e) {
142             // expected
143
}
144         pool.returnObject(obj1);
145         pool.close();
146     }
147
148     public void testEvictWhileEmpty() throws Exception JavaDoc {
149         GenericObjectPool pool = new GenericObjectPool(new SimpleFactory(true,false));
150         pool.evict();
151         pool.evict();
152         pool.close();
153     }
154
155     public void testExceptionOnPassivateDuringReturn() throws Exception JavaDoc {
156         SimpleFactory factory = new SimpleFactory();
157         GenericObjectPool pool = new GenericObjectPool(factory);
158         Object JavaDoc obj = pool.borrowObject();
159         factory.setThrowExceptionOnPassivate(true);
160         pool.returnObject(obj);
161         assertEquals(0,pool.getNumIdle());
162         pool.close();
163     }
164
165     public void testWithInitiallyInvalid() throws Exception JavaDoc {
166         GenericObjectPool pool = new GenericObjectPool(new SimpleFactory(false));
167         pool.setTestOnBorrow(true);
168         try {
169             pool.borrowObject();
170             fail("Expected NoSuchElementException");
171         } catch(NoSuchElementException JavaDoc e) {
172             // expected
173
}
174     }
175
176     public void testWithSometimesInvalid() throws Exception JavaDoc {
177         GenericObjectPool pool = new GenericObjectPool(new SimpleFactory(true,false));
178         pool.setMaxIdle(10);
179         pool.setTestOnBorrow(true);
180         pool.setTestOnReturn(true);
181         pool.returnObject(pool.borrowObject());
182         assertEquals(0,pool.getNumIdle());
183     }
184
185     public void testSetFactoryWithActiveObjects() throws Exception JavaDoc {
186         GenericObjectPool pool = new GenericObjectPool();
187         pool.setMaxIdle(10);
188         pool.setFactory(new SimpleFactory());
189         Object JavaDoc obj = pool.borrowObject();
190         assertNotNull(obj);
191         try {
192             pool.setFactory(null);
193             fail("Expected IllegalStateException");
194         } catch(IllegalStateException JavaDoc e) {
195             // expected
196
}
197         try {
198             pool.setFactory(new SimpleFactory());
199             fail("Expected IllegalStateException");
200         } catch(IllegalStateException JavaDoc e) {
201             // expected
202
}
203     }
204
205     public void testSetFactoryWithNoActiveObjects() throws Exception JavaDoc {
206         GenericObjectPool pool = new GenericObjectPool();
207         pool.setMaxIdle(10);
208         pool.setFactory(new SimpleFactory());
209         Object JavaDoc obj = pool.borrowObject();
210         pool.returnObject(obj);
211         assertEquals(1,pool.getNumIdle());
212         pool.setFactory(new SimpleFactory());
213         assertEquals(0,pool.getNumIdle());
214     }
215     
216     public void testNegativeMaxActive() throws Exception JavaDoc {
217         pool.setMaxActive(-1);
218         pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
219         Object JavaDoc obj = pool.borrowObject();
220         assertEquals(getNthObject(0),obj);
221         pool.returnObject(obj);
222     }
223
224     public void testMaxIdle() throws Exception JavaDoc {
225         pool.setMaxActive(100);
226         pool.setMaxIdle(8);
227         Object JavaDoc[] active = new Object JavaDoc[100];
228         for(int i=0;i<100;i++) {
229             active[i] = pool.borrowObject();
230         }
231         assertEquals(100,pool.getNumActive());
232         assertEquals(0,pool.getNumIdle());
233         for(int i=0;i<100;i++) {
234             pool.returnObject(active[i]);
235             assertEquals(99 - i,pool.getNumActive());
236             assertEquals((i < 8 ? i+1 : 8),pool.getNumIdle());
237         }
238     }
239
240     public void testMaxIdleZero() throws Exception JavaDoc {
241         pool.setMaxActive(100);
242         pool.setMaxIdle(0);
243         Object JavaDoc[] active = new Object JavaDoc[100];
244         for(int i=0;i<100;i++) {
245             active[i] = pool.borrowObject();
246         }
247         assertEquals(100,pool.getNumActive());
248         assertEquals(0,pool.getNumIdle());
249         for(int i=0;i<100;i++) {
250             pool.returnObject(active[i]);
251             assertEquals(99 - i,pool.getNumActive());
252             assertEquals(0, pool.getNumIdle());
253         }
254     }
255
256     public void testMaxActive() throws Exception JavaDoc {
257         pool.setMaxActive(3);
258         pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
259
260         pool.borrowObject();
261         pool.borrowObject();
262         pool.borrowObject();
263         try {
264             pool.borrowObject();
265             fail("Expected NoSuchElementException");
266         } catch(NoSuchElementException JavaDoc e) {
267             // expected
268
}
269     }
270
271     public void testMaxActiveZero() throws Exception JavaDoc {
272         pool.setMaxActive(0);
273         pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
274
275         try {
276             pool.borrowObject();
277             fail("Expected NoSuchElementException");
278         } catch(NoSuchElementException JavaDoc e) {
279             // expected
280
}
281     }
282
283     public void testInvalidWhenExhaustedAction() throws Exception JavaDoc {
284         try {
285             pool.setWhenExhaustedAction(Byte.MAX_VALUE);
286             fail("Expected IllegalArgumentException");
287         } catch(IllegalArgumentException JavaDoc e) {
288             // expected
289
}
290
291         try {
292             ObjectPool pool = new GenericObjectPool(
293                 new SimpleFactory(),
294                 GenericObjectPool.DEFAULT_MAX_ACTIVE,
295                 Byte.MAX_VALUE,
296                 GenericObjectPool.DEFAULT_MAX_WAIT,
297                 GenericObjectPool.DEFAULT_MAX_IDLE,
298                 false,
299                 false,
300                 GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
301                 GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
302                 GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
303                 false
304             );
305             assertNotNull(pool);
306             fail("Expected IllegalArgumentException");
307         } catch(IllegalArgumentException JavaDoc e) {
308             // expected
309
}
310     }
311
312     public void testSettersAndGetters() throws Exception JavaDoc {
313         GenericObjectPool pool = new GenericObjectPool();
314         {
315             pool.setFactory(new SimpleFactory());
316         }
317         {
318             pool.setMaxActive(123);
319             assertEquals(123,pool.getMaxActive());
320         }
321         {
322             pool.setMaxIdle(12);
323             assertEquals(12,pool.getMaxIdle());
324         }
325         {
326             pool.setMaxWait(1234L);
327             assertEquals(1234L,pool.getMaxWait());
328         }
329         {
330             pool.setMinEvictableIdleTimeMillis(12345L);
331             assertEquals(12345L,pool.getMinEvictableIdleTimeMillis());
332         }
333         {
334             pool.setNumTestsPerEvictionRun(11);
335             assertEquals(11,pool.getNumTestsPerEvictionRun());
336         }
337         {
338             pool.setTestOnBorrow(true);
339             assertTrue(pool.getTestOnBorrow());
340             pool.setTestOnBorrow(false);
341             assertTrue(!pool.getTestOnBorrow());
342         }
343         {
344             pool.setTestOnReturn(true);
345             assertTrue(pool.getTestOnReturn());
346             pool.setTestOnReturn(false);
347             assertTrue(!pool.getTestOnReturn());
348         }
349         {
350             pool.setTestWhileIdle(true);
351             assertTrue(pool.getTestWhileIdle());
352             pool.setTestWhileIdle(false);
353             assertTrue(!pool.getTestWhileIdle());
354         }
355         {
356             pool.setTimeBetweenEvictionRunsMillis(11235L);
357             assertEquals(11235L,pool.getTimeBetweenEvictionRunsMillis());
358         }
359         {
360             pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
361             assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK,pool.getWhenExhaustedAction());
362             pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
363             assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL,pool.getWhenExhaustedAction());
364             pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
365             assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW,pool.getWhenExhaustedAction());
366         }
367     }
368     
369     public void testDefaultConfiguration() throws Exception JavaDoc {
370         GenericObjectPool pool = new GenericObjectPool();
371         assertConfiguration(new GenericObjectPool.Config(),pool);
372     }
373
374     public void testConstructors() throws Exception JavaDoc {
375         {
376             GenericObjectPool pool = new GenericObjectPool();
377             assertConfiguration(new GenericObjectPool.Config(),pool);
378         }
379         {
380             GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
381             assertConfiguration(new GenericObjectPool.Config(),pool);
382         }
383         {
384             GenericObjectPool.Config expected = new GenericObjectPool.Config();
385             expected.maxActive = 2;
386             expected.maxIdle = 3;
387             expected.maxWait = 5L;
388             expected.minEvictableIdleTimeMillis = 7L;
389             expected.numTestsPerEvictionRun = 9;
390             expected.testOnBorrow = true;
391             expected.testOnReturn = true;
392             expected.testWhileIdle = true;
393             expected.timeBetweenEvictionRunsMillis = 11L;
394             expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
395             GenericObjectPool pool = new GenericObjectPool(null,expected);
396             assertConfiguration(expected,pool);
397         }
398         {
399             GenericObjectPool.Config expected = new GenericObjectPool.Config();
400             expected.maxActive = 2;
401             GenericObjectPool pool = new GenericObjectPool(null,expected.maxActive);
402             assertConfiguration(expected,pool);
403         }
404         {
405             GenericObjectPool.Config expected = new GenericObjectPool.Config();
406             expected.maxActive = 2;
407             expected.maxWait = 5L;
408             expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
409             GenericObjectPool pool = new GenericObjectPool(null,expected.maxActive,expected.whenExhaustedAction,expected.maxWait);
410             assertConfiguration(expected,pool);
411         }
412         {
413             GenericObjectPool.Config expected = new GenericObjectPool.Config();
414             expected.maxActive = 2;
415             expected.maxWait = 5L;
416             expected.testOnBorrow = true;
417             expected.testOnReturn = true;
418             expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
419             GenericObjectPool pool = new GenericObjectPool(null,expected.maxActive,expected.whenExhaustedAction,expected.maxWait,expected.testOnBorrow,expected.testOnReturn);
420             assertConfiguration(expected,pool);
421         }
422         {
423             GenericObjectPool.Config expected = new GenericObjectPool.Config();
424             expected.maxActive = 2;
425             expected.maxIdle = 3;
426             expected.maxWait = 5L;
427             expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
428             GenericObjectPool pool = new GenericObjectPool(null,expected.maxActive,expected.whenExhaustedAction,expected.maxWait,expected.maxIdle);
429             assertConfiguration(expected,pool);
430         }
431         {
432             GenericObjectPool.Config expected = new GenericObjectPool.Config();
433             expected.maxActive = 2;
434             expected.maxIdle = 3;
435             expected.maxWait = 5L;
436             expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
437             expected.testOnBorrow = true;
438             expected.testOnReturn = true;
439             GenericObjectPool pool = new GenericObjectPool(null,expected.maxActive,expected.whenExhaustedAction,expected.maxWait,expected.maxIdle,expected.testOnBorrow,expected.testOnReturn);
440             assertConfiguration(expected,pool);
441         }
442         {
443             GenericObjectPool.Config expected = new GenericObjectPool.Config();
444             expected.maxActive = 2;
445             expected.maxIdle = 3;
446             expected.maxWait = 5L;
447             expected.minEvictableIdleTimeMillis = 7L;
448             expected.numTestsPerEvictionRun = 9;
449             expected.testOnBorrow = true;
450             expected.testOnReturn = true;
451             expected.testWhileIdle = true;
452             expected.timeBetweenEvictionRunsMillis = 11L;
453             expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
454             GenericObjectPool pool = new GenericObjectPool(null,expected.maxActive, expected.whenExhaustedAction, expected.maxWait, expected.maxIdle, expected.testOnBorrow, expected.testOnReturn, expected.timeBetweenEvictionRunsMillis, expected.numTestsPerEvictionRun, expected.minEvictableIdleTimeMillis, expected.testWhileIdle);
455             assertConfiguration(expected,pool);
456         }
457         {
458             GenericObjectPool.Config expected = new GenericObjectPool.Config();
459             expected.maxActive = 2;
460             expected.maxIdle = 3;
461             expected.minIdle = 1;
462             expected.maxWait = 5L;
463             expected.minEvictableIdleTimeMillis = 7L;
464             expected.numTestsPerEvictionRun = 9;
465             expected.testOnBorrow = true;
466             expected.testOnReturn = true;
467             expected.testWhileIdle = true;
468             expected.timeBetweenEvictionRunsMillis = 11L;
469             expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
470             GenericObjectPool pool = new GenericObjectPool(null,expected.maxActive, expected.whenExhaustedAction, expected.maxWait, expected.maxIdle, expected.minIdle, expected.testOnBorrow, expected.testOnReturn, expected.timeBetweenEvictionRunsMillis, expected.numTestsPerEvictionRun, expected.minEvictableIdleTimeMillis, expected.testWhileIdle);
471             assertConfiguration(expected,pool);
472         }
473     }
474
475     public void testSetConfig() throws Exception JavaDoc {
476         GenericObjectPool.Config expected = new GenericObjectPool.Config();
477         GenericObjectPool pool = new GenericObjectPool();
478         assertConfiguration(expected,pool);
479         expected.maxActive = 2;
480         expected.maxIdle = 3;
481         expected.maxWait = 5L;
482         expected.minEvictableIdleTimeMillis = 7L;
483         expected.numTestsPerEvictionRun = 9;
484         expected.testOnBorrow = true;
485         expected.testOnReturn = true;
486         expected.testWhileIdle = true;
487         expected.timeBetweenEvictionRunsMillis = 11L;
488         expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
489         pool.setConfig(expected);
490         assertConfiguration(expected,pool);
491     }
492
493     public void testDebugInfo() throws Exception JavaDoc {
494         GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
495         pool.setMaxIdle(3);
496         assertNotNull(pool.debugInfo());
497         Object JavaDoc obj = pool.borrowObject();
498         assertNotNull(pool.debugInfo());
499         pool.returnObject(obj);
500         assertNotNull(pool.debugInfo());
501     }
502
503     public void testStartAndStopEvictor() throws Exception JavaDoc {
504         // set up pool without evictor
505
pool.setMaxIdle(6);
506         pool.setMaxActive(6);
507         pool.setNumTestsPerEvictionRun(6);
508         pool.setMinEvictableIdleTimeMillis(100L);
509
510         for(int j=0;j<2;j++) {
511             // populate the pool
512
{
513                 Object JavaDoc[] active = new Object JavaDoc[6];
514                 for(int i=0;i<6;i++) {
515                     active[i] = pool.borrowObject();
516                 }
517                 for(int i=0;i<6;i++) {
518                     pool.returnObject(active[i]);
519                 }
520             }
521     
522             // note that it stays populated
523
assertEquals("Should have 6 idle",6,pool.getNumIdle());
524     
525             // start the evictor
526
pool.setTimeBetweenEvictionRunsMillis(50L);
527             
528             // wait a second (well, .2 seconds)
529
try { Thread.sleep(200L); } catch(Exception JavaDoc e) { }
530             
531             // assert that the evictor has cleared out the pool
532
assertEquals("Should have 0 idle",0,pool.getNumIdle());
533     
534             // stop the evictor
535
pool.startEvictor(0L);
536         }
537     }
538
539     public void testEvictionWithNegativeNumTests() throws Exception JavaDoc {
540         // when numTestsPerEvictionRun is negative, it represents a fraction of the idle objects to test
541
pool.setMaxIdle(6);
542         pool.setMaxActive(6);
543         pool.setNumTestsPerEvictionRun(-2);
544         pool.setMinEvictableIdleTimeMillis(50L);
545         pool.setTimeBetweenEvictionRunsMillis(100L);
546
547         Object JavaDoc[] active = new Object JavaDoc[6];
548         for(int i=0;i<6;i++) {
549             active[i] = pool.borrowObject();
550         }
551         for(int i=0;i<6;i++) {
552             pool.returnObject(active[i]);
553         }
554
555         try { Thread.sleep(100L); } catch(Exception JavaDoc e) { }
556         assertTrue("Should at most 6 idle, found " + pool.getNumIdle(),pool.getNumIdle() <= 6);
557         try { Thread.sleep(100L); } catch(Exception JavaDoc e) { }
558         assertTrue("Should at most 3 idle, found " + pool.getNumIdle(),pool.getNumIdle() <= 3);
559         try { Thread.sleep(100L); } catch(Exception JavaDoc e) { }
560         assertTrue("Should be at most 2 idle, found " + pool.getNumIdle(),pool.getNumIdle() <= 2);
561         try { Thread.sleep(100L); } catch(Exception JavaDoc e) { }
562         assertEquals("Should be zero idle, found " + pool.getNumIdle(),0,pool.getNumIdle());
563     }
564
565     public void testEviction() throws Exception JavaDoc {
566         pool.setMaxIdle(500);
567         pool.setMaxActive(500);
568         pool.setNumTestsPerEvictionRun(100);
569         pool.setMinEvictableIdleTimeMillis(250L);
570         pool.setTimeBetweenEvictionRunsMillis(500L);
571         pool.setTestWhileIdle(true);
572
573         Object JavaDoc[] active = new Object JavaDoc[500];
574         for(int i=0;i<500;i++) {
575             active[i] = pool.borrowObject();
576         }
577         for(int i=0;i<500;i++) {
578             pool.returnObject(active[i]);
579         }
580
581         try { Thread.sleep(1000L); } catch(Exception JavaDoc e) { }
582         assertTrue("Should be less than 500 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 500);
583         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
584         assertTrue("Should be less than 400 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 400);
585         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
586         assertTrue("Should be less than 300 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 300);
587         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
588         assertTrue("Should be less than 200 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 200);
589         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
590         assertTrue("Should be less than 100 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 100);
591         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
592         assertEquals("Should be zero idle, found " + pool.getNumIdle(),0,pool.getNumIdle());
593
594         for(int i=0;i<500;i++) {
595             active[i] = pool.borrowObject();
596         }
597         for(int i=0;i<500;i++) {
598             pool.returnObject(active[i]);
599         }
600
601         try { Thread.sleep(1000L); } catch(Exception JavaDoc e) { }
602         assertTrue("Should be less than 500 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 500);
603         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
604         assertTrue("Should be less than 400 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 400);
605         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
606         assertTrue("Should be less than 300 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 300);
607         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
608         assertTrue("Should be less than 200 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 200);
609         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
610         assertTrue("Should be less than 100 idle, found " + pool.getNumIdle(),pool.getNumIdle() < 100);
611         try { Thread.sleep(600L); } catch(Exception JavaDoc e) { }
612         assertEquals("Should be zero idle, found " + pool.getNumIdle(),0,pool.getNumIdle());
613     }
614
615     public void testEvictionSoftMinIdle() throws Exception JavaDoc {
616         GenericObjectPool pool = null;
617         
618         class TimeTest extends BasePoolableObjectFactory {
619             private final long createTime;
620             public TimeTest() {
621                 createTime = System.currentTimeMillis();
622             }
623             public Object JavaDoc makeObject() throws Exception JavaDoc {
624                 return new TimeTest();
625             }
626             public long getCreateTime() {
627                 return createTime;
628             }
629         }
630         
631         pool = new GenericObjectPool(new TimeTest());
632         
633         pool.setMaxIdle(5);
634         pool.setMaxActive(5);
635         pool.setNumTestsPerEvictionRun(5);
636         pool.setMinEvictableIdleTimeMillis(3000L);
637         pool.setTimeBetweenEvictionRunsMillis(250L);
638         pool.setTestWhileIdle(true);
639         pool.setSoftMinEvictableIdleTimeMillis(1000L);
640         pool.setMinIdle(2);
641         
642         Object JavaDoc[] active = new Object JavaDoc[5];
643         Long JavaDoc[] creationTime = new Long JavaDoc[5] ;
644         for(int i=0;i<5;i++) {
645             active[i] = pool.borrowObject();
646             creationTime[i] = new Long JavaDoc(((TimeTest)active[i]).getCreateTime());
647         }
648         
649         for(int i=0;i<5;i++) {
650             pool.returnObject(active[i]);
651         }
652         
653         try { Thread.sleep(1500L); } catch(Exception JavaDoc e) { }
654         assertTrue("Should be 2 OLD idle, found " + pool.getNumIdle(),pool.getNumIdle() == 2 &&
655                 ((TimeTest)pool.borrowObject()).getCreateTime() == creationTime[3].longValue() &&
656                 ((TimeTest)pool.borrowObject()).getCreateTime() == creationTime[4].longValue());
657         
658         try { Thread.sleep(2000L); } catch(Exception JavaDoc e) { }
659         assertTrue("Should be 2 NEW idle , found " + pool.getNumIdle(),pool.getNumIdle() == 2 &&
660                 ((TimeTest)pool.borrowObject()).getCreateTime() != creationTime[0].longValue() &&
661                 ((TimeTest)pool.borrowObject()).getCreateTime() != creationTime[1].longValue());
662     }
663
664     public void testMinIdle() throws Exception JavaDoc {
665         pool.setMaxIdle(500);
666         pool.setMinIdle(5);
667         pool.setMaxActive(10);
668         pool.setNumTestsPerEvictionRun(0);
669         pool.setMinEvictableIdleTimeMillis(50L);
670         pool.setTimeBetweenEvictionRunsMillis(100L);
671         pool.setTestWhileIdle(true);
672
673         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
674         assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 5);
675
676         Object JavaDoc[] active = new Object JavaDoc[5];
677         active[0] = pool.borrowObject();
678
679         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
680         assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 5);
681
682         for(int i=1 ; i<5 ; i++) {
683             active[i] = pool.borrowObject();
684         }
685
686         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
687         assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 5);
688
689         for(int i=0 ; i<5 ; i++) {
690             pool.returnObject(active[i]);
691         }
692
693         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
694         assertTrue("Should be 10 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 10);
695     }
696
697     public void testMinIdleMaxActive() throws Exception JavaDoc {
698         pool.setMaxIdle(500);
699         pool.setMinIdle(5);
700         pool.setMaxActive(10);
701         pool.setNumTestsPerEvictionRun(0);
702         pool.setMinEvictableIdleTimeMillis(50L);
703         pool.setTimeBetweenEvictionRunsMillis(100L);
704         pool.setTestWhileIdle(true);
705
706         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
707         assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 5);
708
709         Object JavaDoc[] active = new Object JavaDoc[10];
710
711         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
712         assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 5);
713
714         for(int i=0 ; i<5 ; i++) {
715             active[i] = pool.borrowObject();
716         }
717
718         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
719         assertTrue("Should be 5 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 5);
720
721         for(int i=0 ; i<5 ; i++) {
722             pool.returnObject(active[i]);
723         }
724
725         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
726         assertTrue("Should be 10 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 10);
727
728         for(int i=0 ; i<10 ; i++) {
729             active[i] = pool.borrowObject();
730         }
731
732         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
733         assertTrue("Should be 0 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 0);
734
735         for(int i=0 ; i<10 ; i++) {
736             pool.returnObject(active[i]);
737         }
738
739         try { Thread.sleep(150L); } catch(Exception JavaDoc e) { }
740         assertTrue("Should be 10 idle, found " + pool.getNumIdle(),pool.getNumIdle() == 10);
741     }
742
743     public void testThreaded1() throws Exception JavaDoc {
744         pool.setMaxActive(15);
745         pool.setMaxIdle(15);
746         pool.setMaxWait(1000L);
747         TestThread[] threads = new TestThread[20];
748         for(int i=0;i<20;i++) {
749             threads[i] = new TestThread(pool,100,50);
750             Thread JavaDoc t = new Thread JavaDoc(threads[i]);
751             t.start();
752         }
753         for(int i=0;i<20;i++) {
754             while(!(threads[i]).complete()) {
755                 try {
756                     Thread.sleep(500L);
757                 } catch(Exception JavaDoc e) {
758                     // ignored
759
}
760             }
761             if(threads[i].failed()) {
762                 fail();
763             }
764         }
765     }
766
767     class TestThread implements Runnable JavaDoc {
768         java.util.Random JavaDoc _random = new java.util.Random JavaDoc();
769         ObjectPool _pool = null;
770         boolean _complete = false;
771         boolean _failed = false;
772         int _iter = 100;
773         int _delay = 50;
774
775         public TestThread(ObjectPool pool) {
776             _pool = pool;
777         }
778
779         public TestThread(ObjectPool pool, int iter) {
780             _pool = pool;
781             _iter = iter;
782         }
783
784         public TestThread(ObjectPool pool, int iter, int delay) {
785             _pool = pool;
786             _iter = iter;
787             _delay = delay;
788         }
789
790         public boolean complete() {
791             return _complete;
792         }
793
794         public boolean failed() {
795             return _failed;
796         }
797
798         public void run() {
799             for(int i=0;i<_iter;i++) {
800                 try {
801                     Thread.sleep((long)_random.nextInt(_delay));
802                 } catch(Exception JavaDoc e) {
803                     // ignored
804
}
805                 Object JavaDoc obj = null;
806                 try {
807                     obj = _pool.borrowObject();
808                 } catch(Exception JavaDoc e) {
809                     _failed = true;
810                     _complete = true;
811                     break;
812                 }
813
814                 try {
815                     Thread.sleep((long)_random.nextInt(_delay));
816                 } catch(Exception JavaDoc e) {
817                     // ignored
818
}
819                 try {
820                     _pool.returnObject(obj);
821                 } catch(Exception JavaDoc e) {
822                     _failed = true;
823                     _complete = true;
824                     break;
825                 }
826             }
827             _complete = true;
828         }
829     }
830     
831     public void testAddObject() throws Exception JavaDoc {
832         assertEquals("should be zero idle", 0, pool.getNumIdle());
833         pool.addObject();
834         assertEquals("should be one idle", 1, pool.getNumIdle());
835         assertEquals("should be zero active", 0, pool.getNumActive());
836         Object JavaDoc obj = pool.borrowObject();
837         assertEquals("should be zero idle", 0, pool.getNumIdle());
838         assertEquals("should be one active", 1, pool.getNumActive());
839         pool.returnObject(obj);
840         assertEquals("should be one idle", 1, pool.getNumIdle());
841         assertEquals("should be zero active", 0, pool.getNumActive());
842     }
843     
844     private GenericObjectPool pool = null;
845
846     private void assertConfiguration(GenericObjectPool.Config expected, GenericObjectPool actual) throws Exception JavaDoc {
847         assertEquals("testOnBorrow",expected.testOnBorrow,actual.getTestOnBorrow());
848         assertEquals("testOnReturn",expected.testOnReturn,actual.getTestOnReturn());
849         assertEquals("testWhileIdle",expected.testWhileIdle,actual.getTestWhileIdle());
850         assertEquals("whenExhaustedAction",expected.whenExhaustedAction,actual.getWhenExhaustedAction());
851         assertEquals("maxActive",expected.maxActive,actual.getMaxActive());
852         assertEquals("maxIdle",expected.maxIdle,actual.getMaxIdle());
853         assertEquals("maxWait",expected.maxWait,actual.getMaxWait());
854         assertEquals("minEvictableIdleTimeMillis",expected.minEvictableIdleTimeMillis,actual.getMinEvictableIdleTimeMillis());
855         assertEquals("numTestsPerEvictionRun",expected.numTestsPerEvictionRun,actual.getNumTestsPerEvictionRun());
856         assertEquals("timeBetweenEvictionRunsMillis",expected.timeBetweenEvictionRunsMillis,actual.getTimeBetweenEvictionRunsMillis());
857     }
858
859     public class SimpleFactory implements PoolableObjectFactory {
860         public SimpleFactory() {
861             this(true);
862         }
863         public SimpleFactory(boolean valid) {
864             this(valid,valid);
865         }
866         public SimpleFactory(boolean evalid, boolean ovalid) {
867             evenValid = evalid;
868             oddValid = ovalid;
869         }
870         void setValid(boolean valid) {
871             setEvenValid(valid);
872             setOddValid(valid);
873         }
874         void setEvenValid(boolean valid) {
875             evenValid = valid;
876         }
877         void setOddValid(boolean valid) {
878             oddValid = valid;
879         }
880         public void setThrowExceptionOnPassivate(boolean bool) {
881             exceptionOnPassivate = bool;
882         }
883     
884         public Object JavaDoc makeObject() { return String.valueOf(makeCounter++); }
885         public void destroyObject(Object JavaDoc obj) { }
886         public boolean validateObject(Object JavaDoc obj) {
887             if (enableValidation) {
888                 return validateCounter++%2 == 0 ? evenValid : oddValid;
889             }
890             else {
891                 return true;
892             }
893         }
894         public void activateObject(Object JavaDoc obj) throws Exception JavaDoc {
895             if (exceptionOnActivate) {
896                 if (!(validateCounter++%2 == 0 ? evenValid : oddValid)) {
897                     throw new Exception JavaDoc();
898                 }
899             }
900         }
901         public void passivateObject(Object JavaDoc obj) throws Exception JavaDoc {
902             if(exceptionOnPassivate) {
903                 throw new Exception JavaDoc();
904             }
905         }
906         int makeCounter = 0;
907         int validateCounter = 0;
908         boolean evenValid = true;
909         boolean oddValid = true;
910         boolean exceptionOnPassivate = false;
911         boolean exceptionOnActivate = false;
912         boolean enableValidation = true;
913
914         public boolean isThrowExceptionOnActivate() {
915             return exceptionOnActivate;
916         }
917
918         public void setThrowExceptionOnActivate(boolean b) {
919             exceptionOnActivate = b;
920         }
921
922         public boolean isValidationEnabled() {
923             return enableValidation;
924         }
925
926         public void setValidationEnabled(boolean b) {
927             enableValidation = b;
928         }
929     }
930 }
931
932
933
Popular Tags