001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.pool.impl;
018:
019: import java.util.HashMap;
020: import java.util.NoSuchElementException;
021:
022: import junit.framework.Test;
023: import junit.framework.TestSuite;
024:
025: import org.apache.commons.pool.KeyedObjectPool;
026: import org.apache.commons.pool.KeyedPoolableObjectFactory;
027: import org.apache.commons.pool.TestKeyedObjectPool;
028:
029: /**
030: * @author Rodney Waldhoff
031: * @version $Revision: 383290 $ $Date: 2006-03-05 02:00:15 -0500 (Sun, 05 Mar 2006) $
032: */
033: public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
034: public TestGenericKeyedObjectPool(String testName) {
035: super (testName);
036: }
037:
038: public static Test suite() {
039: return new TestSuite(TestGenericKeyedObjectPool.class);
040: }
041:
042: protected KeyedObjectPool makeEmptyPool(int mincapacity) {
043: GenericKeyedObjectPool pool = new GenericKeyedObjectPool(
044: new KeyedPoolableObjectFactory() {
045: HashMap map = new HashMap();
046:
047: public Object makeObject(Object key) {
048: int counter = 0;
049: Integer Counter = (Integer) (map.get(key));
050: if (null != Counter) {
051: counter = Counter.intValue();
052: }
053: map.put(key, new Integer(counter + 1));
054: return String.valueOf(key)
055: + String.valueOf(counter);
056: }
057:
058: public void destroyObject(Object key, Object obj) {
059: }
060:
061: public boolean validateObject(Object key, Object obj) {
062: return true;
063: }
064:
065: public void activateObject(Object key, Object obj) {
066: }
067:
068: public void passivateObject(Object key, Object obj) {
069: }
070: });
071: pool.setMaxActive(mincapacity);
072: pool.setMaxIdle(mincapacity);
073: return pool;
074: }
075:
076: protected Object getNthObject(Object key, int n) {
077: return String.valueOf(key) + String.valueOf(n);
078: }
079:
080: protected Object makeKey(int n) {
081: return String.valueOf(n);
082: }
083:
084: private GenericKeyedObjectPool pool = null;
085:
086: public void setUp() throws Exception {
087: super .setUp();
088: pool = new GenericKeyedObjectPool(new SimpleFactory());
089: }
090:
091: public void tearDown() throws Exception {
092: super .tearDown();
093: pool.close();
094: pool = null;
095: }
096:
097: public void testWithInitiallyInvalid() throws Exception {
098: GenericKeyedObjectPool pool = new GenericKeyedObjectPool(
099: new SimpleFactory(false));
100: pool.setTestOnBorrow(true);
101: try {
102: pool.borrowObject("xyzzy");
103: fail("Expected NoSuchElementException");
104: } catch (NoSuchElementException e) {
105: // expected
106: }
107: }
108:
109: public void testNegativeMaxActive() throws Exception {
110: pool.setMaxActive(-1);
111: pool
112: .setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
113: Object obj = pool.borrowObject("");
114: assertEquals("0", obj);
115: pool.returnObject("", obj);
116: }
117:
118: public void testNumActiveNumIdle2() throws Exception {
119: assertEquals(0, pool.getNumActive());
120: assertEquals(0, pool.getNumIdle());
121: assertEquals(0, pool.getNumActive("A"));
122: assertEquals(0, pool.getNumIdle("A"));
123: assertEquals(0, pool.getNumActive("B"));
124: assertEquals(0, pool.getNumIdle("B"));
125:
126: Object objA0 = pool.borrowObject("A");
127: Object objB0 = pool.borrowObject("B");
128:
129: assertEquals(2, pool.getNumActive());
130: assertEquals(0, pool.getNumIdle());
131: assertEquals(1, pool.getNumActive("A"));
132: assertEquals(0, pool.getNumIdle("A"));
133: assertEquals(1, pool.getNumActive("B"));
134: assertEquals(0, pool.getNumIdle("B"));
135:
136: Object objA1 = pool.borrowObject("A");
137: Object objB1 = pool.borrowObject("B");
138:
139: assertEquals(4, pool.getNumActive());
140: assertEquals(0, pool.getNumIdle());
141: assertEquals(2, pool.getNumActive("A"));
142: assertEquals(0, pool.getNumIdle("A"));
143: assertEquals(2, pool.getNumActive("B"));
144: assertEquals(0, pool.getNumIdle("B"));
145:
146: pool.returnObject("A", objA0);
147: pool.returnObject("B", objB0);
148:
149: assertEquals(2, pool.getNumActive());
150: assertEquals(2, pool.getNumIdle());
151: assertEquals(1, pool.getNumActive("A"));
152: assertEquals(1, pool.getNumIdle("A"));
153: assertEquals(1, pool.getNumActive("B"));
154: assertEquals(1, pool.getNumIdle("B"));
155:
156: pool.returnObject("A", objA1);
157: pool.returnObject("B", objB1);
158:
159: assertEquals(0, pool.getNumActive());
160: assertEquals(4, pool.getNumIdle());
161: assertEquals(0, pool.getNumActive("A"));
162: assertEquals(2, pool.getNumIdle("A"));
163: assertEquals(0, pool.getNumActive("B"));
164: assertEquals(2, pool.getNumIdle("B"));
165: }
166:
167: public void testMaxIdle() throws Exception {
168: pool.setMaxActive(100);
169: pool.setMaxIdle(8);
170: Object[] active = new Object[100];
171: for (int i = 0; i < 100; i++) {
172: active[i] = pool.borrowObject("");
173: }
174: assertEquals(100, pool.getNumActive(""));
175: assertEquals(0, pool.getNumIdle(""));
176: for (int i = 0; i < 100; i++) {
177: pool.returnObject("", active[i]);
178: assertEquals(99 - i, pool.getNumActive(""));
179: assertEquals((i < 8 ? i + 1 : 8), pool.getNumIdle(""));
180: }
181: }
182:
183: public void testMaxActive() throws Exception {
184: pool.setMaxActive(3);
185: pool
186: .setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
187:
188: pool.borrowObject("");
189: pool.borrowObject("");
190: pool.borrowObject("");
191: try {
192: pool.borrowObject("");
193: fail("Expected NoSuchElementException");
194: } catch (NoSuchElementException e) {
195: // expected
196: }
197: }
198:
199: public void testMaxActiveZero() throws Exception {
200: pool.setMaxActive(0);
201: pool
202: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
203:
204: try {
205: pool.borrowObject("a");
206: fail("Expected NoSuchElementException");
207: } catch (NoSuchElementException e) {
208: // expected
209: }
210: }
211:
212: public void testMaxTotal() throws Exception {
213: pool.setMaxActive(2);
214: pool.setMaxTotal(3);
215: pool
216: .setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
217:
218: Object o1 = pool.borrowObject("a");
219: assertNotNull(o1);
220: Object o2 = pool.borrowObject("a");
221: assertNotNull(o2);
222: Object o3 = pool.borrowObject("b");
223: assertNotNull(o3);
224: try {
225: pool.borrowObject("c");
226: fail("Expected NoSuchElementException");
227: } catch (NoSuchElementException e) {
228: // expected
229: }
230:
231: assertEquals(0, pool.getNumIdle());
232:
233: pool.returnObject("b", o3);
234: assertEquals(1, pool.getNumIdle());
235: assertEquals(1, pool.getNumIdle("b"));
236:
237: Object o4 = pool.borrowObject("b");
238: assertNotNull(o4);
239: assertEquals(0, pool.getNumIdle());
240: assertEquals(0, pool.getNumIdle("b"));
241: }
242:
243: public void testMaxTotalZero() throws Exception {
244: pool.setMaxTotal(0);
245: pool
246: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
247:
248: try {
249: pool.borrowObject("a");
250: fail("Expected NoSuchElementException");
251: } catch (NoSuchElementException e) {
252: // expected
253: }
254: }
255:
256: public void testMaxTotalLRU() throws Exception {
257: pool.setMaxActive(2);
258: pool.setMaxTotal(3);
259: // pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW);
260:
261: Object o1 = pool.borrowObject("a");
262: assertNotNull(o1);
263: pool.returnObject("a", o1);
264: Thread.sleep(10);
265:
266: Object o2 = pool.borrowObject("b");
267: assertNotNull(o2);
268: pool.returnObject("b", o2);
269: Thread.sleep(10);
270:
271: Object o3 = pool.borrowObject("c");
272: assertNotNull(o3);
273: pool.returnObject("c", o3);
274: Thread.sleep(10);
275:
276: Object o4 = pool.borrowObject("a");
277: assertNotNull(o4);
278: pool.returnObject("a", o4);
279: Thread.sleep(10);
280:
281: assertSame(o1, o4);
282:
283: // this should cause b to be bumped out of the pool
284: Object o5 = pool.borrowObject("d");
285: assertNotNull(o5);
286: pool.returnObject("d", o5);
287: Thread.sleep(10);
288:
289: // now re-request b, we should get a different object because it should
290: // have been expelled from pool (was oldest because a was requested after b)
291: Object o6 = pool.borrowObject("b");
292: assertNotNull(o6);
293: pool.returnObject("b", o6);
294:
295: assertNotSame(o1, o6);
296:
297: // second a is still in there
298: Object o7 = pool.borrowObject("a");
299: assertNotNull(o7);
300: pool.returnObject("a", o7);
301:
302: assertSame(o4, o7);
303: }
304:
305: public void testSettersAndGetters() throws Exception {
306: GenericKeyedObjectPool pool = new GenericKeyedObjectPool();
307: {
308: pool.setFactory(new SimpleFactory());
309: }
310: {
311: pool.setMaxActive(123);
312: assertEquals(123, pool.getMaxActive());
313: }
314: {
315: pool.setMaxIdle(12);
316: assertEquals(12, pool.getMaxIdle());
317: }
318: {
319: pool.setMaxWait(1234L);
320: assertEquals(1234L, pool.getMaxWait());
321: }
322: {
323: pool.setMinEvictableIdleTimeMillis(12345L);
324: assertEquals(12345L, pool.getMinEvictableIdleTimeMillis());
325: }
326: {
327: pool.setNumTestsPerEvictionRun(11);
328: assertEquals(11, pool.getNumTestsPerEvictionRun());
329: }
330: {
331: pool.setTestOnBorrow(true);
332: assertTrue(pool.getTestOnBorrow());
333: pool.setTestOnBorrow(false);
334: assertTrue(!pool.getTestOnBorrow());
335: }
336: {
337: pool.setTestOnReturn(true);
338: assertTrue(pool.getTestOnReturn());
339: pool.setTestOnReturn(false);
340: assertTrue(!pool.getTestOnReturn());
341: }
342: {
343: pool.setTestWhileIdle(true);
344: assertTrue(pool.getTestWhileIdle());
345: pool.setTestWhileIdle(false);
346: assertTrue(!pool.getTestWhileIdle());
347: }
348: {
349: pool.setTimeBetweenEvictionRunsMillis(11235L);
350: assertEquals(11235L, pool
351: .getTimeBetweenEvictionRunsMillis());
352: }
353: {
354: pool
355: .setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK);
356: assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool
357: .getWhenExhaustedAction());
358: pool
359: .setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL);
360: assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool
361: .getWhenExhaustedAction());
362: pool
363: .setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW);
364: assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool
365: .getWhenExhaustedAction());
366: }
367: }
368:
369: public void testEviction() throws Exception {
370: pool.setMaxIdle(500);
371: pool.setMaxActive(500);
372: pool.setNumTestsPerEvictionRun(100);
373: pool.setMinEvictableIdleTimeMillis(250L);
374: pool.setTimeBetweenEvictionRunsMillis(500L);
375:
376: Object[] active = new Object[500];
377: for (int i = 0; i < 500; i++) {
378: active[i] = pool.borrowObject("");
379: }
380: for (int i = 0; i < 500; i++) {
381: pool.returnObject("", active[i]);
382: }
383:
384: try {
385: Thread.sleep(1000L);
386: } catch (Exception e) {
387: }
388: assertTrue("Should be less than 500 idle, found "
389: + pool.getNumIdle(""), pool.getNumIdle("") < 500);
390: try {
391: Thread.sleep(600L);
392: } catch (Exception e) {
393: }
394: assertTrue("Should be less than 400 idle, found "
395: + pool.getNumIdle(""), pool.getNumIdle("") < 400);
396: try {
397: Thread.sleep(600L);
398: } catch (Exception e) {
399: }
400: assertTrue("Should be less than 300 idle, found "
401: + pool.getNumIdle(""), pool.getNumIdle("") < 300);
402: try {
403: Thread.sleep(600L);
404: } catch (Exception e) {
405: }
406: assertTrue("Should be less than 200 idle, found "
407: + pool.getNumIdle(""), pool.getNumIdle("") < 200);
408: try {
409: Thread.sleep(600L);
410: } catch (Exception e) {
411: }
412: assertTrue("Should be less than 100 idle, found "
413: + pool.getNumIdle(""), pool.getNumIdle("") < 100);
414: try {
415: Thread.sleep(600L);
416: } catch (Exception e) {
417: }
418: assertEquals("Should be zero idle, found "
419: + pool.getNumIdle(""), 0, pool.getNumIdle(""));
420:
421: for (int i = 0; i < 500; i++) {
422: active[i] = pool.borrowObject("");
423: }
424: for (int i = 0; i < 500; i++) {
425: pool.returnObject("", active[i]);
426: }
427:
428: try {
429: Thread.sleep(1000L);
430: } catch (Exception e) {
431: }
432: assertTrue("Should be less than 500 idle, found "
433: + pool.getNumIdle(""), pool.getNumIdle("") < 500);
434: try {
435: Thread.sleep(600L);
436: } catch (Exception e) {
437: }
438: assertTrue("Should be less than 400 idle, found "
439: + pool.getNumIdle(""), pool.getNumIdle("") < 400);
440: try {
441: Thread.sleep(600L);
442: } catch (Exception e) {
443: }
444: assertTrue("Should be less than 300 idle, found "
445: + pool.getNumIdle(""), pool.getNumIdle("") < 300);
446: try {
447: Thread.sleep(600L);
448: } catch (Exception e) {
449: }
450: assertTrue("Should be less than 200 idle, found "
451: + pool.getNumIdle(""), pool.getNumIdle("") < 200);
452: try {
453: Thread.sleep(600L);
454: } catch (Exception e) {
455: }
456: assertTrue("Should be less than 100 idle, found "
457: + pool.getNumIdle(""), pool.getNumIdle("") < 100);
458: try {
459: Thread.sleep(600L);
460: } catch (Exception e) {
461: }
462: assertEquals("Should be zero idle, found "
463: + pool.getNumIdle(""), 0, pool.getNumIdle(""));
464: }
465:
466: public void testEviction2() throws Exception {
467: pool.setMaxIdle(500);
468: pool.setMaxActive(500);
469: pool.setNumTestsPerEvictionRun(100);
470: pool.setMinEvictableIdleTimeMillis(500L);
471: pool.setTimeBetweenEvictionRunsMillis(500L);
472:
473: Object[] active = new Object[500];
474: Object[] active2 = new Object[500];
475: for (int i = 0; i < 500; i++) {
476: active[i] = pool.borrowObject("");
477: active2[i] = pool.borrowObject("2");
478: }
479: for (int i = 0; i < 500; i++) {
480: pool.returnObject("", active[i]);
481: pool.returnObject("2", active2[i]);
482: }
483:
484: try {
485: Thread.sleep(1000L);
486: } catch (Exception e) {
487: }
488: assertTrue("Should be less than 1000 idle, found "
489: + pool.getNumIdle(), pool.getNumIdle() < 1000);
490: try {
491: Thread.sleep(600L);
492: } catch (Exception e) {
493: }
494: assertTrue("Should be less than 900 idle, found "
495: + pool.getNumIdle(), pool.getNumIdle() < 900);
496: try {
497: Thread.sleep(600L);
498: } catch (Exception e) {
499: }
500: assertTrue("Should be less than 800 idle, found "
501: + pool.getNumIdle(), pool.getNumIdle() < 800);
502: try {
503: Thread.sleep(600L);
504: } catch (Exception e) {
505: }
506: assertTrue("Should be less than 700 idle, found "
507: + pool.getNumIdle(), pool.getNumIdle() < 700);
508: try {
509: Thread.sleep(600L);
510: } catch (Exception e) {
511: }
512: assertTrue("Should be less than 600 idle, found "
513: + pool.getNumIdle(), pool.getNumIdle() < 600);
514: try {
515: Thread.sleep(600L);
516: } catch (Exception e) {
517: }
518: assertTrue("Should be less than 500 idle, found "
519: + pool.getNumIdle(), pool.getNumIdle() < 500);
520: try {
521: Thread.sleep(600L);
522: } catch (Exception e) {
523: }
524: assertTrue("Should be less than 400 idle, found "
525: + pool.getNumIdle(), pool.getNumIdle() < 400);
526: try {
527: Thread.sleep(600L);
528: } catch (Exception e) {
529: }
530: assertTrue("Should be less than 300 idle, found "
531: + pool.getNumIdle(), pool.getNumIdle() < 300);
532: try {
533: Thread.sleep(600L);
534: } catch (Exception e) {
535: }
536: assertTrue("Should be less than 200 idle, found "
537: + pool.getNumIdle(), pool.getNumIdle() < 200);
538: try {
539: Thread.sleep(600L);
540: } catch (Exception e) {
541: }
542: assertTrue("Should be less than 100 idle, found "
543: + pool.getNumIdle(), pool.getNumIdle() < 100);
544: try {
545: Thread.sleep(600L);
546: } catch (Exception e) {
547: }
548: assertEquals("Should be zero idle, found " + pool.getNumIdle(),
549: 0, pool.getNumIdle());
550: }
551:
552: public void testThreaded1() throws Exception {
553: pool.setMaxActive(15);
554: pool.setMaxIdle(15);
555: pool.setMaxWait(1000L);
556: TestThread[] threads = new TestThread[20];
557: for (int i = 0; i < 20; i++) {
558: threads[i] = new TestThread(pool, 100, 50);
559: Thread t = new Thread(threads[i]);
560: t.start();
561: }
562: for (int i = 0; i < 20; i++) {
563: while (!(threads[i]).complete()) {
564: try {
565: Thread.sleep(500L);
566: } catch (Exception e) {
567: // ignored
568: }
569: }
570: if (threads[i].failed()) {
571: fail();
572: }
573: }
574: }
575:
576: public void testMinIdle() throws Exception {
577: pool.setMaxIdle(500);
578: pool.setMinIdle(5);
579: pool.setMaxActive(10);
580: pool.setNumTestsPerEvictionRun(0);
581: pool.setMinEvictableIdleTimeMillis(50L);
582: pool.setTimeBetweenEvictionRunsMillis(100L);
583: pool.setTestWhileIdle(true);
584:
585: //Generate a random key
586: String key = "A";
587:
588: pool.preparePool(key, true);
589:
590: try {
591: Thread.sleep(150L);
592: } catch (Exception e) {
593: }
594: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
595: .getNumIdle() == 5);
596:
597: Object[] active = new Object[5];
598: active[0] = pool.borrowObject(key);
599:
600: try {
601: Thread.sleep(150L);
602: } catch (Exception e) {
603: }
604: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
605: .getNumIdle() == 5);
606:
607: for (int i = 1; i < 5; i++) {
608: active[i] = pool.borrowObject(key);
609: }
610:
611: try {
612: Thread.sleep(150L);
613: } catch (Exception e) {
614: }
615: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
616: .getNumIdle() == 5);
617:
618: for (int i = 0; i < 5; i++) {
619: pool.returnObject(key, active[i]);
620: }
621:
622: try {
623: Thread.sleep(150L);
624: } catch (Exception e) {
625: }
626: assertTrue("Should be 10 idle, found " + pool.getNumIdle(),
627: pool.getNumIdle() == 10);
628: }
629:
630: public void testMinIdleMaxActive() throws Exception {
631: pool.setMaxIdle(500);
632: pool.setMinIdle(5);
633: pool.setMaxActive(10);
634: pool.setNumTestsPerEvictionRun(0);
635: pool.setMinEvictableIdleTimeMillis(50L);
636: pool.setTimeBetweenEvictionRunsMillis(100L);
637: pool.setTestWhileIdle(true);
638:
639: String key = "A";
640:
641: pool.preparePool(key, true);
642:
643: try {
644: Thread.sleep(150L);
645: } catch (Exception e) {
646: }
647: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
648: .getNumIdle() == 5);
649:
650: Object[] active = new Object[10];
651:
652: try {
653: Thread.sleep(150L);
654: } catch (Exception e) {
655: }
656: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
657: .getNumIdle() == 5);
658:
659: for (int i = 0; i < 5; i++) {
660: active[i] = pool.borrowObject(key);
661: }
662:
663: try {
664: Thread.sleep(150L);
665: } catch (Exception e) {
666: }
667: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
668: .getNumIdle() == 5);
669:
670: for (int i = 0; i < 5; i++) {
671: pool.returnObject(key, active[i]);
672: }
673:
674: try {
675: Thread.sleep(150L);
676: } catch (Exception e) {
677: }
678: assertTrue("Should be 10 idle, found " + pool.getNumIdle(),
679: pool.getNumIdle() == 10);
680:
681: for (int i = 0; i < 10; i++) {
682: active[i] = pool.borrowObject(key);
683: }
684:
685: try {
686: Thread.sleep(150L);
687: } catch (Exception e) {
688: }
689: assertTrue("Should be 0 idle, found " + pool.getNumIdle(), pool
690: .getNumIdle() == 0);
691:
692: for (int i = 0; i < 10; i++) {
693: pool.returnObject(key, active[i]);
694: }
695:
696: try {
697: Thread.sleep(150L);
698: } catch (Exception e) {
699: }
700: assertTrue("Should be 10 idle, found " + pool.getNumIdle(),
701: pool.getNumIdle() == 10);
702: }
703:
704: public void testMinIdleNoPopulateImmediately() throws Exception {
705: pool.setMaxIdle(500);
706: pool.setMinIdle(5);
707: pool.setMaxActive(10);
708: pool.setNumTestsPerEvictionRun(0);
709: pool.setMinEvictableIdleTimeMillis(50L);
710: pool.setTimeBetweenEvictionRunsMillis(1000L);
711: pool.setTestWhileIdle(true);
712:
713: //Generate a random key
714: String key = "A";
715:
716: pool.preparePool(key, false);
717:
718: assertTrue("Should be 0 idle, found " + pool.getNumIdle(), pool
719: .getNumIdle() == 0);
720:
721: try {
722: Thread.sleep(1500L);
723: } catch (Exception e) {
724: }
725: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
726: .getNumIdle() == 5);
727: }
728:
729: public void testMinIdleNoPreparePool() throws Exception {
730: pool.setMaxIdle(500);
731: pool.setMinIdle(5);
732: pool.setMaxActive(10);
733: pool.setNumTestsPerEvictionRun(0);
734: pool.setMinEvictableIdleTimeMillis(50L);
735: pool.setTimeBetweenEvictionRunsMillis(100L);
736: pool.setTestWhileIdle(true);
737:
738: //Generate a random key
739: String key = "A";
740:
741: try {
742: Thread.sleep(150L);
743: } catch (Exception e) {
744: }
745: assertTrue("Should be 0 idle, found " + pool.getNumIdle(), pool
746: .getNumIdle() == 0);
747:
748: Object active = pool.borrowObject(key);
749: assertNotNull(active);
750:
751: try {
752: Thread.sleep(150L);
753: } catch (Exception e) {
754: }
755: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
756: .getNumIdle() == 5);
757: }
758:
759: public void testFIFO() throws Exception {
760: final Object key = "key";
761: pool.addObject(key); // "key0"
762: pool.addObject(key); // "key1"
763: pool.addObject(key); // "key2"
764: assertEquals("Oldest", "key0", pool.borrowObject(key));
765: assertEquals("Middle", "key1", pool.borrowObject(key));
766: assertEquals("Youngest", "key2", pool.borrowObject(key));
767: assertEquals("new-3", "key3", pool.borrowObject(key));
768: pool.returnObject(key, "r");
769: assertEquals("returned", "r", pool.borrowObject(key));
770: assertEquals("new-4", "key4", pool.borrowObject(key));
771: }
772:
773: class TestThread implements Runnable {
774: java.util.Random _random = new java.util.Random();
775: KeyedObjectPool _pool = null;
776: boolean _complete = false;
777: boolean _failed = false;
778: int _iter = 100;
779: int _delay = 50;
780:
781: public TestThread(KeyedObjectPool pool) {
782: _pool = pool;
783: }
784:
785: public TestThread(KeyedObjectPool pool, int iter) {
786: _pool = pool;
787: _iter = iter;
788: }
789:
790: public TestThread(KeyedObjectPool pool, int iter, int delay) {
791: _pool = pool;
792: _iter = iter;
793: _delay = delay;
794: }
795:
796: public boolean complete() {
797: return _complete;
798: }
799:
800: public boolean failed() {
801: return _failed;
802: }
803:
804: public void run() {
805: for (int i = 0; i < _iter; i++) {
806: String key = String.valueOf(_random.nextInt(3));
807: try {
808: Thread.sleep((long) _random.nextInt(_delay));
809: } catch (Exception e) {
810: // ignored
811: }
812: Object obj = null;
813: try {
814: obj = _pool.borrowObject(key);
815: } catch (Exception e) {
816: _failed = true;
817: _complete = true;
818: break;
819: }
820:
821: try {
822: Thread.sleep((long) _random.nextInt(_delay));
823: } catch (Exception e) {
824: // ignored
825: }
826: try {
827: _pool.returnObject(key, obj);
828: } catch (Exception e) {
829: _failed = true;
830: _complete = true;
831: break;
832: }
833: }
834: _complete = true;
835: }
836: }
837:
838: static class SimpleFactory implements KeyedPoolableObjectFactory {
839: public SimpleFactory() {
840: this (true);
841: }
842:
843: public SimpleFactory(boolean valid) {
844: this .valid = valid;
845: }
846:
847: public Object makeObject(Object key) {
848: return String.valueOf(key) + String.valueOf(counter++);
849: }
850:
851: public void destroyObject(Object key, Object obj) {
852: }
853:
854: public boolean validateObject(Object key, Object obj) {
855: return valid;
856: }
857:
858: public void activateObject(Object key, Object obj) {
859: }
860:
861: public void passivateObject(Object key, Object obj) {
862: }
863:
864: int counter = 0;
865: boolean valid;
866: }
867:
868: protected boolean isLifo() {
869: return false;
870: }
871:
872: protected boolean isFifo() {
873: return true;
874: }
875:
876: }
|