0001: /*
0002: * Copyright 1999-2004 The Apache Software Foundation.
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016:
0017: package org.apache.commons.pool.impl;
0018:
0019: import java.util.NoSuchElementException;
0020: import java.util.List;
0021: import java.util.ArrayList;
0022: import java.util.Arrays;
0023:
0024: import junit.framework.Test;
0025: import junit.framework.TestSuite;
0026:
0027: import org.apache.commons.pool.BasePoolableObjectFactory;
0028: import org.apache.commons.pool.ObjectPool;
0029: import org.apache.commons.pool.PoolableObjectFactory;
0030: import org.apache.commons.pool.TestObjectPool;
0031:
0032: /**
0033: * @author Rodney Waldhoff
0034: * @author Dirk Verbeeck
0035: * @version $Revision: 387103 $ $Date: 2006-03-19 20:48:49 -0500 (Sun, 19 Mar 2006) $
0036: */
0037: public class TestGenericObjectPool extends TestObjectPool {
0038: public TestGenericObjectPool(String testName) {
0039: super (testName);
0040: }
0041:
0042: public static Test suite() {
0043: return new TestSuite(TestGenericObjectPool.class);
0044: }
0045:
0046: protected ObjectPool makeEmptyPool(int mincap) {
0047: GenericObjectPool pool = new GenericObjectPool(
0048: new SimpleFactory());
0049: pool.setMaxActive(mincap);
0050: pool.setMaxIdle(mincap);
0051: return pool;
0052: }
0053:
0054: protected Object getNthObject(int n) {
0055: return String.valueOf(n);
0056: }
0057:
0058: public void setUp() throws Exception {
0059: super .setUp();
0060: pool = new GenericObjectPool(new SimpleFactory());
0061: }
0062:
0063: public void tearDown() throws Exception {
0064: super .tearDown();
0065: pool.close();
0066: pool = null;
0067: }
0068:
0069: /**
0070: * Activation failure on existing object doesn't fail the borrow
0071: */
0072: public void testActivationException() throws Exception {
0073: SimpleFactory factory = new SimpleFactory(true, false);
0074: factory.setThrowExceptionOnActivate(true);
0075: factory.setValidationEnabled(false);
0076: GenericObjectPool pool = new GenericObjectPool(factory);
0077:
0078: Object obj1 = pool.borrowObject();
0079: pool.returnObject(obj1);
0080:
0081: // obj1 was returned to the pool but failed to activate the second borrow
0082: // a new object obj2 needs te be created
0083: Object obj2 = pool.borrowObject();
0084: assertTrue(obj1 != obj2);
0085: }
0086:
0087: /**
0088: * Activation failure on new object fails the borrow
0089: */
0090: public void testActivationExceptionOnNewObject() throws Exception {
0091: SimpleFactory factory = new SimpleFactory(true, false);
0092: factory.setThrowExceptionOnActivate(true);
0093: factory.setValidationEnabled(false);
0094: GenericObjectPool pool = new GenericObjectPool(factory);
0095:
0096: Object obj1 = pool.borrowObject();
0097: try {
0098: Object obj2 = pool.borrowObject();
0099: System.out.println("obj1: " + obj1);
0100: System.out.println("obj2: " + obj2);
0101: fail("a second object should have been created and failed to activate");
0102: } catch (Exception e) {
0103: }
0104: }
0105:
0106: public void testWhenExhaustedGrow() throws Exception {
0107: GenericObjectPool pool = new GenericObjectPool(
0108: new SimpleFactory());
0109: pool.setMaxActive(1);
0110: pool
0111: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
0112: Object obj1 = pool.borrowObject();
0113: assertNotNull(obj1);
0114: Object obj2 = pool.borrowObject();
0115: assertNotNull(obj2);
0116: pool.returnObject(obj2);
0117: pool.returnObject(obj1);
0118: pool.close();
0119: }
0120:
0121: public void testWhenExhaustedFail() throws Exception {
0122: GenericObjectPool pool = new GenericObjectPool(
0123: new SimpleFactory());
0124: pool.setMaxActive(1);
0125: pool
0126: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
0127: Object obj1 = pool.borrowObject();
0128: assertNotNull(obj1);
0129: try {
0130: pool.borrowObject();
0131: fail("Expected NoSuchElementException");
0132: } catch (NoSuchElementException e) {
0133: // expected
0134: }
0135: pool.returnObject(obj1);
0136: pool.close();
0137: }
0138:
0139: public void testWhenExhaustedBlock() throws Exception {
0140: GenericObjectPool pool = new GenericObjectPool(
0141: new SimpleFactory());
0142: pool.setMaxActive(1);
0143: pool
0144: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
0145: pool.setMaxWait(10L);
0146: Object obj1 = pool.borrowObject();
0147: assertNotNull(obj1);
0148: try {
0149: pool.borrowObject();
0150: fail("Expected NoSuchElementException");
0151: } catch (NoSuchElementException e) {
0152: // expected
0153: }
0154: pool.returnObject(obj1);
0155: pool.close();
0156: }
0157:
0158: public void testEvictWhileEmpty() throws Exception {
0159: GenericObjectPool pool = new GenericObjectPool(
0160: new SimpleFactory(true, false));
0161: pool.evict();
0162: pool.evict();
0163: pool.close();
0164: }
0165:
0166: public void testExceptionOnPassivateDuringReturn() throws Exception {
0167: SimpleFactory factory = new SimpleFactory();
0168: GenericObjectPool pool = new GenericObjectPool(factory);
0169: Object obj = pool.borrowObject();
0170: factory.setThrowExceptionOnPassivate(true);
0171: pool.returnObject(obj);
0172: assertEquals(0, pool.getNumIdle());
0173: pool.close();
0174: }
0175:
0176: public void testWithInitiallyInvalid() throws Exception {
0177: GenericObjectPool pool = new GenericObjectPool(
0178: new SimpleFactory(false));
0179: pool.setTestOnBorrow(true);
0180: try {
0181: pool.borrowObject();
0182: fail("Expected NoSuchElementException");
0183: } catch (NoSuchElementException e) {
0184: // expected
0185: }
0186: }
0187:
0188: public void testWithSometimesInvalid() throws Exception {
0189: GenericObjectPool pool = new GenericObjectPool(
0190: new SimpleFactory(true, false));
0191: pool.setMaxIdle(10);
0192: pool.setTestOnBorrow(true);
0193: pool.setTestOnReturn(true);
0194: pool.returnObject(pool.borrowObject());
0195: assertEquals(0, pool.getNumIdle());
0196: }
0197:
0198: public void testSetFactoryWithActiveObjects() throws Exception {
0199: GenericObjectPool pool = new GenericObjectPool();
0200: pool.setMaxIdle(10);
0201: pool.setFactory(new SimpleFactory());
0202: Object obj = pool.borrowObject();
0203: assertNotNull(obj);
0204: try {
0205: pool.setFactory(null);
0206: fail("Expected IllegalStateException");
0207: } catch (IllegalStateException e) {
0208: // expected
0209: }
0210: try {
0211: pool.setFactory(new SimpleFactory());
0212: fail("Expected IllegalStateException");
0213: } catch (IllegalStateException e) {
0214: // expected
0215: }
0216: }
0217:
0218: public void testSetFactoryWithNoActiveObjects() throws Exception {
0219: GenericObjectPool pool = new GenericObjectPool();
0220: pool.setMaxIdle(10);
0221: pool.setFactory(new SimpleFactory());
0222: Object obj = pool.borrowObject();
0223: pool.returnObject(obj);
0224: assertEquals(1, pool.getNumIdle());
0225: pool.setFactory(new SimpleFactory());
0226: assertEquals(0, pool.getNumIdle());
0227: }
0228:
0229: public void testNegativeMaxActive() throws Exception {
0230: pool.setMaxActive(-1);
0231: pool
0232: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
0233: Object obj = pool.borrowObject();
0234: assertEquals(getNthObject(0), obj);
0235: pool.returnObject(obj);
0236: }
0237:
0238: public void testMaxIdle() throws Exception {
0239: pool.setMaxActive(100);
0240: pool.setMaxIdle(8);
0241: Object[] active = new Object[100];
0242: for (int i = 0; i < 100; i++) {
0243: active[i] = pool.borrowObject();
0244: }
0245: assertEquals(100, pool.getNumActive());
0246: assertEquals(0, pool.getNumIdle());
0247: for (int i = 0; i < 100; i++) {
0248: pool.returnObject(active[i]);
0249: assertEquals(99 - i, pool.getNumActive());
0250: assertEquals((i < 8 ? i + 1 : 8), pool.getNumIdle());
0251: }
0252: }
0253:
0254: public void testMaxIdleZero() throws Exception {
0255: pool.setMaxActive(100);
0256: pool.setMaxIdle(0);
0257: Object[] active = new Object[100];
0258: for (int i = 0; i < 100; i++) {
0259: active[i] = pool.borrowObject();
0260: }
0261: assertEquals(100, pool.getNumActive());
0262: assertEquals(0, pool.getNumIdle());
0263: for (int i = 0; i < 100; i++) {
0264: pool.returnObject(active[i]);
0265: assertEquals(99 - i, pool.getNumActive());
0266: assertEquals(0, pool.getNumIdle());
0267: }
0268: }
0269:
0270: public void testMaxActive() throws Exception {
0271: pool.setMaxActive(3);
0272: pool
0273: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
0274:
0275: pool.borrowObject();
0276: pool.borrowObject();
0277: pool.borrowObject();
0278: try {
0279: pool.borrowObject();
0280: fail("Expected NoSuchElementException");
0281: } catch (NoSuchElementException e) {
0282: // expected
0283: }
0284: }
0285:
0286: public void testMaxActiveZero() throws Exception {
0287: pool.setMaxActive(0);
0288: pool
0289: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
0290:
0291: try {
0292: pool.borrowObject();
0293: fail("Expected NoSuchElementException");
0294: } catch (NoSuchElementException e) {
0295: // expected
0296: }
0297: }
0298:
0299: public void testInvalidWhenExhaustedAction() throws Exception {
0300: try {
0301: pool.setWhenExhaustedAction(Byte.MAX_VALUE);
0302: fail("Expected IllegalArgumentException");
0303: } catch (IllegalArgumentException e) {
0304: // expected
0305: }
0306:
0307: try {
0308: ObjectPool pool = new GenericObjectPool(
0309: new SimpleFactory(),
0310: GenericObjectPool.DEFAULT_MAX_ACTIVE,
0311: Byte.MAX_VALUE,
0312: GenericObjectPool.DEFAULT_MAX_WAIT,
0313: GenericObjectPool.DEFAULT_MAX_IDLE,
0314: false,
0315: false,
0316: GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS,
0317: GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN,
0318: GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS,
0319: false);
0320: assertNotNull(pool);
0321: fail("Expected IllegalArgumentException");
0322: } catch (IllegalArgumentException e) {
0323: // expected
0324: }
0325: }
0326:
0327: public void testSettersAndGetters() throws Exception {
0328: GenericObjectPool pool = new GenericObjectPool();
0329: {
0330: pool.setFactory(new SimpleFactory());
0331: }
0332: {
0333: pool.setMaxActive(123);
0334: assertEquals(123, pool.getMaxActive());
0335: }
0336: {
0337: pool.setMaxIdle(12);
0338: assertEquals(12, pool.getMaxIdle());
0339: }
0340: {
0341: pool.setMaxWait(1234L);
0342: assertEquals(1234L, pool.getMaxWait());
0343: }
0344: {
0345: pool.setMinEvictableIdleTimeMillis(12345L);
0346: assertEquals(12345L, pool.getMinEvictableIdleTimeMillis());
0347: }
0348: {
0349: pool.setNumTestsPerEvictionRun(11);
0350: assertEquals(11, pool.getNumTestsPerEvictionRun());
0351: }
0352: {
0353: pool.setTestOnBorrow(true);
0354: assertTrue(pool.getTestOnBorrow());
0355: pool.setTestOnBorrow(false);
0356: assertTrue(!pool.getTestOnBorrow());
0357: }
0358: {
0359: pool.setTestOnReturn(true);
0360: assertTrue(pool.getTestOnReturn());
0361: pool.setTestOnReturn(false);
0362: assertTrue(!pool.getTestOnReturn());
0363: }
0364: {
0365: pool.setTestWhileIdle(true);
0366: assertTrue(pool.getTestWhileIdle());
0367: pool.setTestWhileIdle(false);
0368: assertTrue(!pool.getTestWhileIdle());
0369: }
0370: {
0371: pool.setTimeBetweenEvictionRunsMillis(11235L);
0372: assertEquals(11235L, pool
0373: .getTimeBetweenEvictionRunsMillis());
0374: }
0375: {
0376: pool
0377: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
0378: assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool
0379: .getWhenExhaustedAction());
0380: pool
0381: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
0382: assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool
0383: .getWhenExhaustedAction());
0384: pool
0385: .setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
0386: assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool
0387: .getWhenExhaustedAction());
0388: }
0389: }
0390:
0391: public void testDefaultConfiguration() throws Exception {
0392: GenericObjectPool pool = new GenericObjectPool();
0393: assertConfiguration(new GenericObjectPool.Config(), pool);
0394: }
0395:
0396: public void testConstructors() throws Exception {
0397: {
0398: GenericObjectPool pool = new GenericObjectPool();
0399: assertConfiguration(new GenericObjectPool.Config(), pool);
0400: }
0401: {
0402: GenericObjectPool pool = new GenericObjectPool(
0403: new SimpleFactory());
0404: assertConfiguration(new GenericObjectPool.Config(), pool);
0405: }
0406: {
0407: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0408: expected.maxActive = 2;
0409: expected.maxIdle = 3;
0410: expected.maxWait = 5L;
0411: expected.minEvictableIdleTimeMillis = 7L;
0412: expected.numTestsPerEvictionRun = 9;
0413: expected.testOnBorrow = true;
0414: expected.testOnReturn = true;
0415: expected.testWhileIdle = true;
0416: expected.timeBetweenEvictionRunsMillis = 11L;
0417: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0418: GenericObjectPool pool = new GenericObjectPool(null,
0419: expected);
0420: assertConfiguration(expected, pool);
0421: }
0422: {
0423: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0424: expected.maxActive = 2;
0425: GenericObjectPool pool = new GenericObjectPool(null,
0426: expected.maxActive);
0427: assertConfiguration(expected, pool);
0428: }
0429: {
0430: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0431: expected.maxActive = 2;
0432: expected.maxWait = 5L;
0433: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0434: GenericObjectPool pool = new GenericObjectPool(null,
0435: expected.maxActive, expected.whenExhaustedAction,
0436: expected.maxWait);
0437: assertConfiguration(expected, pool);
0438: }
0439: {
0440: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0441: expected.maxActive = 2;
0442: expected.maxWait = 5L;
0443: expected.testOnBorrow = true;
0444: expected.testOnReturn = true;
0445: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0446: GenericObjectPool pool = new GenericObjectPool(null,
0447: expected.maxActive, expected.whenExhaustedAction,
0448: expected.maxWait, expected.testOnBorrow,
0449: expected.testOnReturn);
0450: assertConfiguration(expected, pool);
0451: }
0452: {
0453: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0454: expected.maxActive = 2;
0455: expected.maxIdle = 3;
0456: expected.maxWait = 5L;
0457: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0458: GenericObjectPool pool = new GenericObjectPool(null,
0459: expected.maxActive, expected.whenExhaustedAction,
0460: expected.maxWait, expected.maxIdle);
0461: assertConfiguration(expected, pool);
0462: }
0463: {
0464: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0465: expected.maxActive = 2;
0466: expected.maxIdle = 3;
0467: expected.maxWait = 5L;
0468: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0469: expected.testOnBorrow = true;
0470: expected.testOnReturn = true;
0471: GenericObjectPool pool = new GenericObjectPool(null,
0472: expected.maxActive, expected.whenExhaustedAction,
0473: expected.maxWait, expected.maxIdle,
0474: expected.testOnBorrow, expected.testOnReturn);
0475: assertConfiguration(expected, pool);
0476: }
0477: {
0478: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0479: expected.maxActive = 2;
0480: expected.maxIdle = 3;
0481: expected.maxWait = 5L;
0482: expected.minEvictableIdleTimeMillis = 7L;
0483: expected.numTestsPerEvictionRun = 9;
0484: expected.testOnBorrow = true;
0485: expected.testOnReturn = true;
0486: expected.testWhileIdle = true;
0487: expected.timeBetweenEvictionRunsMillis = 11L;
0488: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0489: GenericObjectPool pool = new GenericObjectPool(null,
0490: expected.maxActive, expected.whenExhaustedAction,
0491: expected.maxWait, expected.maxIdle,
0492: expected.testOnBorrow, expected.testOnReturn,
0493: expected.timeBetweenEvictionRunsMillis,
0494: expected.numTestsPerEvictionRun,
0495: expected.minEvictableIdleTimeMillis,
0496: expected.testWhileIdle);
0497: assertConfiguration(expected, pool);
0498: }
0499: {
0500: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0501: expected.maxActive = 2;
0502: expected.maxIdle = 3;
0503: expected.minIdle = 1;
0504: expected.maxWait = 5L;
0505: expected.minEvictableIdleTimeMillis = 7L;
0506: expected.numTestsPerEvictionRun = 9;
0507: expected.testOnBorrow = true;
0508: expected.testOnReturn = true;
0509: expected.testWhileIdle = true;
0510: expected.timeBetweenEvictionRunsMillis = 11L;
0511: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0512: GenericObjectPool pool = new GenericObjectPool(null,
0513: expected.maxActive, expected.whenExhaustedAction,
0514: expected.maxWait, expected.maxIdle,
0515: expected.minIdle, expected.testOnBorrow,
0516: expected.testOnReturn,
0517: expected.timeBetweenEvictionRunsMillis,
0518: expected.numTestsPerEvictionRun,
0519: expected.minEvictableIdleTimeMillis,
0520: expected.testWhileIdle);
0521: assertConfiguration(expected, pool);
0522: }
0523: }
0524:
0525: public void testSetConfig() throws Exception {
0526: GenericObjectPool.Config expected = new GenericObjectPool.Config();
0527: GenericObjectPool pool = new GenericObjectPool();
0528: assertConfiguration(expected, pool);
0529: expected.maxActive = 2;
0530: expected.maxIdle = 3;
0531: expected.maxWait = 5L;
0532: expected.minEvictableIdleTimeMillis = 7L;
0533: expected.numTestsPerEvictionRun = 9;
0534: expected.testOnBorrow = true;
0535: expected.testOnReturn = true;
0536: expected.testWhileIdle = true;
0537: expected.timeBetweenEvictionRunsMillis = 11L;
0538: expected.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW;
0539: pool.setConfig(expected);
0540: assertConfiguration(expected, pool);
0541: }
0542:
0543: public void testDebugInfo() throws Exception {
0544: GenericObjectPool pool = new GenericObjectPool(
0545: new SimpleFactory());
0546: pool.setMaxIdle(3);
0547: assertNotNull(pool.debugInfo());
0548: Object obj = pool.borrowObject();
0549: assertNotNull(pool.debugInfo());
0550: pool.returnObject(obj);
0551: assertNotNull(pool.debugInfo());
0552: }
0553:
0554: public void testStartAndStopEvictor() throws Exception {
0555: // set up pool without evictor
0556: pool.setMaxIdle(6);
0557: pool.setMaxActive(6);
0558: pool.setNumTestsPerEvictionRun(6);
0559: pool.setMinEvictableIdleTimeMillis(100L);
0560:
0561: for (int j = 0; j < 2; j++) {
0562: // populate the pool
0563: {
0564: Object[] active = new Object[6];
0565: for (int i = 0; i < 6; i++) {
0566: active[i] = pool.borrowObject();
0567: }
0568: for (int i = 0; i < 6; i++) {
0569: pool.returnObject(active[i]);
0570: }
0571: }
0572:
0573: // note that it stays populated
0574: assertEquals("Should have 6 idle", 6, pool.getNumIdle());
0575:
0576: // start the evictor
0577: pool.setTimeBetweenEvictionRunsMillis(50L);
0578:
0579: // wait a second (well, .2 seconds)
0580: try {
0581: Thread.sleep(200L);
0582: } catch (Exception e) {
0583: }
0584:
0585: // assert that the evictor has cleared out the pool
0586: assertEquals("Should have 0 idle", 0, pool.getNumIdle());
0587:
0588: // stop the evictor
0589: pool.startEvictor(0L);
0590: }
0591: }
0592:
0593: public void testEvictionWithNegativeNumTests() throws Exception {
0594: // when numTestsPerEvictionRun is negative, it represents a fraction of the idle objects to test
0595: pool.setMaxIdle(6);
0596: pool.setMaxActive(6);
0597: pool.setNumTestsPerEvictionRun(-2);
0598: pool.setMinEvictableIdleTimeMillis(50L);
0599: pool.setTimeBetweenEvictionRunsMillis(100L);
0600:
0601: Object[] active = new Object[6];
0602: for (int i = 0; i < 6; i++) {
0603: active[i] = pool.borrowObject();
0604: }
0605: for (int i = 0; i < 6; i++) {
0606: pool.returnObject(active[i]);
0607: }
0608:
0609: try {
0610: Thread.sleep(100L);
0611: } catch (Exception e) {
0612: }
0613: assertTrue("Should at most 6 idle, found " + pool.getNumIdle(),
0614: pool.getNumIdle() <= 6);
0615: try {
0616: Thread.sleep(100L);
0617: } catch (Exception e) {
0618: }
0619: assertTrue("Should at most 3 idle, found " + pool.getNumIdle(),
0620: pool.getNumIdle() <= 3);
0621: try {
0622: Thread.sleep(100L);
0623: } catch (Exception e) {
0624: }
0625: assertTrue("Should be at most 2 idle, found "
0626: + pool.getNumIdle(), pool.getNumIdle() <= 2);
0627: try {
0628: Thread.sleep(100L);
0629: } catch (Exception e) {
0630: }
0631: assertEquals("Should be zero idle, found " + pool.getNumIdle(),
0632: 0, pool.getNumIdle());
0633: }
0634:
0635: public void testEviction() throws Exception {
0636: pool.setMaxIdle(500);
0637: pool.setMaxActive(500);
0638: pool.setNumTestsPerEvictionRun(100);
0639: pool.setMinEvictableIdleTimeMillis(250L);
0640: pool.setTimeBetweenEvictionRunsMillis(500L);
0641: pool.setTestWhileIdle(true);
0642:
0643: Object[] active = new Object[500];
0644: for (int i = 0; i < 500; i++) {
0645: active[i] = pool.borrowObject();
0646: }
0647: for (int i = 0; i < 500; i++) {
0648: pool.returnObject(active[i]);
0649: }
0650:
0651: try {
0652: Thread.sleep(1000L);
0653: } catch (Exception e) {
0654: }
0655: assertTrue("Should be less than 500 idle, found "
0656: + pool.getNumIdle(), pool.getNumIdle() < 500);
0657: try {
0658: Thread.sleep(600L);
0659: } catch (Exception e) {
0660: }
0661: assertTrue("Should be less than 400 idle, found "
0662: + pool.getNumIdle(), pool.getNumIdle() < 400);
0663: try {
0664: Thread.sleep(600L);
0665: } catch (Exception e) {
0666: }
0667: assertTrue("Should be less than 300 idle, found "
0668: + pool.getNumIdle(), pool.getNumIdle() < 300);
0669: try {
0670: Thread.sleep(600L);
0671: } catch (Exception e) {
0672: }
0673: assertTrue("Should be less than 200 idle, found "
0674: + pool.getNumIdle(), pool.getNumIdle() < 200);
0675: try {
0676: Thread.sleep(600L);
0677: } catch (Exception e) {
0678: }
0679: assertTrue("Should be less than 100 idle, found "
0680: + pool.getNumIdle(), pool.getNumIdle() < 100);
0681: try {
0682: Thread.sleep(600L);
0683: } catch (Exception e) {
0684: }
0685: assertEquals("Should be zero idle, found " + pool.getNumIdle(),
0686: 0, pool.getNumIdle());
0687:
0688: for (int i = 0; i < 500; i++) {
0689: active[i] = pool.borrowObject();
0690: }
0691: for (int i = 0; i < 500; i++) {
0692: pool.returnObject(active[i]);
0693: }
0694:
0695: try {
0696: Thread.sleep(1000L);
0697: } catch (Exception e) {
0698: }
0699: assertTrue("Should be less than 500 idle, found "
0700: + pool.getNumIdle(), pool.getNumIdle() < 500);
0701: try {
0702: Thread.sleep(600L);
0703: } catch (Exception e) {
0704: }
0705: assertTrue("Should be less than 400 idle, found "
0706: + pool.getNumIdle(), pool.getNumIdle() < 400);
0707: try {
0708: Thread.sleep(600L);
0709: } catch (Exception e) {
0710: }
0711: assertTrue("Should be less than 300 idle, found "
0712: + pool.getNumIdle(), pool.getNumIdle() < 300);
0713: try {
0714: Thread.sleep(600L);
0715: } catch (Exception e) {
0716: }
0717: assertTrue("Should be less than 200 idle, found "
0718: + pool.getNumIdle(), pool.getNumIdle() < 200);
0719: try {
0720: Thread.sleep(600L);
0721: } catch (Exception e) {
0722: }
0723: assertTrue("Should be less than 100 idle, found "
0724: + pool.getNumIdle(), pool.getNumIdle() < 100);
0725: try {
0726: Thread.sleep(600L);
0727: } catch (Exception e) {
0728: }
0729: assertEquals("Should be zero idle, found " + pool.getNumIdle(),
0730: 0, pool.getNumIdle());
0731: }
0732:
0733: public void testEvictionSoftMinIdle() throws Exception {
0734: GenericObjectPool pool = null;
0735:
0736: class TimeTest extends BasePoolableObjectFactory {
0737: private final long createTime;
0738:
0739: public TimeTest() {
0740: createTime = System.currentTimeMillis();
0741: }
0742:
0743: public Object makeObject() throws Exception {
0744: return new TimeTest();
0745: }
0746:
0747: public long getCreateTime() {
0748: return createTime;
0749: }
0750: }
0751:
0752: pool = new GenericObjectPool(new TimeTest());
0753:
0754: pool.setMaxIdle(5);
0755: pool.setMaxActive(5);
0756: pool.setNumTestsPerEvictionRun(5);
0757: pool.setMinEvictableIdleTimeMillis(3000L);
0758: pool.setSoftMinEvictableIdleTimeMillis(1000L);
0759: pool.setMinIdle(2);
0760:
0761: Object[] active = new Object[5];
0762: Long[] creationTime = new Long[5];
0763: for (int i = 0; i < 5; i++) {
0764: active[i] = pool.borrowObject();
0765: creationTime[i] = new Long(((TimeTest) active[i])
0766: .getCreateTime());
0767: }
0768:
0769: for (int i = 0; i < 5; i++) {
0770: pool.returnObject(active[i]);
0771: }
0772:
0773: // Soft evict all but minIdle(2)
0774: Thread.sleep(1500L);
0775: pool.evict();
0776: assertEquals("Idle count different than expected.", 2, pool
0777: .getNumIdle());
0778:
0779: // Hard evict the rest.
0780: Thread.sleep(2000L);
0781: pool.evict();
0782: assertEquals("Idle count different than expected.", 0, pool
0783: .getNumIdle());
0784: }
0785:
0786: public void testMinIdle() throws Exception {
0787: pool.setMaxIdle(500);
0788: pool.setMinIdle(5);
0789: pool.setMaxActive(10);
0790: pool.setNumTestsPerEvictionRun(0);
0791: pool.setMinEvictableIdleTimeMillis(50L);
0792: pool.setTimeBetweenEvictionRunsMillis(100L);
0793: pool.setTestWhileIdle(true);
0794:
0795: try {
0796: Thread.sleep(150L);
0797: } catch (Exception e) {
0798: }
0799: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
0800: .getNumIdle() == 5);
0801:
0802: Object[] active = new Object[5];
0803: active[0] = pool.borrowObject();
0804:
0805: try {
0806: Thread.sleep(150L);
0807: } catch (Exception e) {
0808: }
0809: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
0810: .getNumIdle() == 5);
0811:
0812: for (int i = 1; i < 5; i++) {
0813: active[i] = pool.borrowObject();
0814: }
0815:
0816: try {
0817: Thread.sleep(150L);
0818: } catch (Exception e) {
0819: }
0820: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
0821: .getNumIdle() == 5);
0822:
0823: for (int i = 0; i < 5; i++) {
0824: pool.returnObject(active[i]);
0825: }
0826:
0827: try {
0828: Thread.sleep(150L);
0829: } catch (Exception e) {
0830: }
0831: assertTrue("Should be 10 idle, found " + pool.getNumIdle(),
0832: pool.getNumIdle() == 10);
0833: }
0834:
0835: public void testMinIdleMaxActive() throws Exception {
0836: pool.setMaxIdle(500);
0837: pool.setMinIdle(5);
0838: pool.setMaxActive(10);
0839: pool.setNumTestsPerEvictionRun(0);
0840: pool.setMinEvictableIdleTimeMillis(50L);
0841: pool.setTimeBetweenEvictionRunsMillis(100L);
0842: pool.setTestWhileIdle(true);
0843:
0844: try {
0845: Thread.sleep(150L);
0846: } catch (Exception e) {
0847: }
0848: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
0849: .getNumIdle() == 5);
0850:
0851: Object[] active = new Object[10];
0852:
0853: try {
0854: Thread.sleep(150L);
0855: } catch (Exception e) {
0856: }
0857: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
0858: .getNumIdle() == 5);
0859:
0860: for (int i = 0; i < 5; i++) {
0861: active[i] = pool.borrowObject();
0862: }
0863:
0864: try {
0865: Thread.sleep(150L);
0866: } catch (Exception e) {
0867: }
0868: assertTrue("Should be 5 idle, found " + pool.getNumIdle(), pool
0869: .getNumIdle() == 5);
0870:
0871: for (int i = 0; i < 5; i++) {
0872: pool.returnObject(active[i]);
0873: }
0874:
0875: try {
0876: Thread.sleep(150L);
0877: } catch (Exception e) {
0878: }
0879: assertTrue("Should be 10 idle, found " + pool.getNumIdle(),
0880: pool.getNumIdle() == 10);
0881:
0882: for (int i = 0; i < 10; i++) {
0883: active[i] = pool.borrowObject();
0884: }
0885:
0886: try {
0887: Thread.sleep(150L);
0888: } catch (Exception e) {
0889: }
0890: assertTrue("Should be 0 idle, found " + pool.getNumIdle(), pool
0891: .getNumIdle() == 0);
0892:
0893: for (int i = 0; i < 10; i++) {
0894: pool.returnObject(active[i]);
0895: }
0896:
0897: try {
0898: Thread.sleep(150L);
0899: } catch (Exception e) {
0900: }
0901: assertTrue("Should be 10 idle, found " + pool.getNumIdle(),
0902: pool.getNumIdle() == 10);
0903: }
0904:
0905: public void testThreaded1() throws Exception {
0906: pool.setMaxActive(15);
0907: pool.setMaxIdle(15);
0908: pool.setMaxWait(1000L);
0909: TestThread[] threads = new TestThread[20];
0910: for (int i = 0; i < 20; i++) {
0911: threads[i] = new TestThread(pool, 100, 50);
0912: Thread t = new Thread(threads[i]);
0913: t.start();
0914: }
0915: for (int i = 0; i < 20; i++) {
0916: while (!(threads[i]).complete()) {
0917: try {
0918: Thread.sleep(500L);
0919: } catch (Exception e) {
0920: // ignored
0921: }
0922: }
0923: if (threads[i].failed()) {
0924: fail();
0925: }
0926: }
0927: }
0928:
0929: class TestThread implements Runnable {
0930: java.util.Random _random = new java.util.Random();
0931: ObjectPool _pool = null;
0932: boolean _complete = false;
0933: boolean _failed = false;
0934: int _iter = 100;
0935: int _delay = 50;
0936:
0937: public TestThread(ObjectPool pool) {
0938: _pool = pool;
0939: }
0940:
0941: public TestThread(ObjectPool pool, int iter) {
0942: _pool = pool;
0943: _iter = iter;
0944: }
0945:
0946: public TestThread(ObjectPool pool, int iter, int delay) {
0947: _pool = pool;
0948: _iter = iter;
0949: _delay = delay;
0950: }
0951:
0952: public boolean complete() {
0953: return _complete;
0954: }
0955:
0956: public boolean failed() {
0957: return _failed;
0958: }
0959:
0960: public void run() {
0961: for (int i = 0; i < _iter; i++) {
0962: try {
0963: Thread.sleep((long) _random.nextInt(_delay));
0964: } catch (Exception e) {
0965: // ignored
0966: }
0967: Object obj = null;
0968: try {
0969: obj = _pool.borrowObject();
0970: } catch (Exception e) {
0971: _failed = true;
0972: _complete = true;
0973: break;
0974: }
0975:
0976: try {
0977: Thread.sleep((long) _random.nextInt(_delay));
0978: } catch (Exception e) {
0979: // ignored
0980: }
0981: try {
0982: _pool.returnObject(obj);
0983: } catch (Exception e) {
0984: _failed = true;
0985: _complete = true;
0986: break;
0987: }
0988: }
0989: _complete = true;
0990: }
0991: }
0992:
0993: public void testFIFO() throws Exception {
0994: pool.addObject(); // "0"
0995: pool.addObject(); // "1"
0996: pool.addObject(); // "2"
0997: assertEquals("Oldest", "0", pool.borrowObject());
0998: assertEquals("Middle", "1", pool.borrowObject());
0999: assertEquals("Youngest", "2", pool.borrowObject());
1000: assertEquals("new-3", "3", pool.borrowObject());
1001: pool.returnObject("r");
1002: assertEquals("returned", "r", pool.borrowObject());
1003: assertEquals("new-4", "4", pool.borrowObject());
1004: }
1005:
1006: public void testAddObject() throws Exception {
1007: assertEquals("should be zero idle", 0, pool.getNumIdle());
1008: pool.addObject();
1009: assertEquals("should be one idle", 1, pool.getNumIdle());
1010: assertEquals("should be zero active", 0, pool.getNumActive());
1011: Object obj = pool.borrowObject();
1012: assertEquals("should be zero idle", 0, pool.getNumIdle());
1013: assertEquals("should be one active", 1, pool.getNumActive());
1014: pool.returnObject(obj);
1015: assertEquals("should be one idle", 1, pool.getNumIdle());
1016: assertEquals("should be zero active", 0, pool.getNumActive());
1017: }
1018:
1019: private List testFactorySequenceStates = new ArrayList(5);
1020:
1021: public void testFactorySequence() throws Exception {
1022: // setup
1023: // We need a factory that tracks method call sequence.
1024: PoolableObjectFactory pof = new PoolableObjectFactory() {
1025: public Object makeObject() throws Exception {
1026: testFactorySequenceStates.add("makeObject");
1027: return new Object();
1028: }
1029:
1030: public void activateObject(Object obj) throws Exception {
1031: testFactorySequenceStates.add("activateObject");
1032: }
1033:
1034: public boolean validateObject(Object obj) {
1035: testFactorySequenceStates.add("validateObject");
1036: return true;
1037: }
1038:
1039: public void passivateObject(Object obj) throws Exception {
1040: testFactorySequenceStates.add("passivateObject");
1041: }
1042:
1043: public void destroyObject(Object obj) throws Exception {
1044: testFactorySequenceStates.add("destroyObject");
1045: }
1046: };
1047:
1048: GenericObjectPool pool = new GenericObjectPool(pof);
1049: pool.setTestOnBorrow(true);
1050: pool.setTestOnReturn(true);
1051:
1052: // check the order in which the factory is called during borrow
1053: testFactorySequenceStates.clear();
1054: Object o = pool.borrowObject();
1055: List desiredSequence = Arrays.asList(new String[] {
1056: "makeObject", "activateObject", "validateObject" });
1057: assertEquals("Wrong sequence", desiredSequence,
1058: testFactorySequenceStates);
1059:
1060: // check the order in which the factory is called when returning an object
1061: testFactorySequenceStates.clear();
1062: pool.returnObject(o);
1063: desiredSequence = Arrays.asList(new String[] {
1064: "validateObject", "passivateObject" });
1065: assertEquals("Wrong sequence", desiredSequence,
1066: testFactorySequenceStates);
1067:
1068: // check the order in which the factory is called during borrow again
1069: testFactorySequenceStates.clear();
1070: o = pool.borrowObject();
1071: desiredSequence = Arrays.asList(new String[] {
1072: "activateObject", "validateObject" });
1073: assertEquals("Wrong sequence", desiredSequence,
1074: testFactorySequenceStates);
1075:
1076: // check the order in which the factory is called when invalidating an object
1077: testFactorySequenceStates.clear();
1078: pool.invalidateObject(o);
1079: desiredSequence = Arrays
1080: .asList(new String[] { "destroyObject" });
1081: assertEquals("Wrong sequence", desiredSequence,
1082: testFactorySequenceStates);
1083: }
1084:
1085: private GenericObjectPool pool = null;
1086:
1087: private void assertConfiguration(GenericObjectPool.Config expected,
1088: GenericObjectPool actual) throws Exception {
1089: assertEquals("testOnBorrow", expected.testOnBorrow, actual
1090: .getTestOnBorrow());
1091: assertEquals("testOnReturn", expected.testOnReturn, actual
1092: .getTestOnReturn());
1093: assertEquals("testWhileIdle", expected.testWhileIdle, actual
1094: .getTestWhileIdle());
1095: assertEquals("whenExhaustedAction",
1096: expected.whenExhaustedAction, actual
1097: .getWhenExhaustedAction());
1098: assertEquals("maxActive", expected.maxActive, actual
1099: .getMaxActive());
1100: assertEquals("maxIdle", expected.maxIdle, actual.getMaxIdle());
1101: assertEquals("maxWait", expected.maxWait, actual.getMaxWait());
1102: assertEquals("minEvictableIdleTimeMillis",
1103: expected.minEvictableIdleTimeMillis, actual
1104: .getMinEvictableIdleTimeMillis());
1105: assertEquals("numTestsPerEvictionRun",
1106: expected.numTestsPerEvictionRun, actual
1107: .getNumTestsPerEvictionRun());
1108: assertEquals("timeBetweenEvictionRunsMillis",
1109: expected.timeBetweenEvictionRunsMillis, actual
1110: .getTimeBetweenEvictionRunsMillis());
1111: }
1112:
1113: public class SimpleFactory implements PoolableObjectFactory {
1114: public SimpleFactory() {
1115: this (true);
1116: }
1117:
1118: public SimpleFactory(boolean valid) {
1119: this (valid, valid);
1120: }
1121:
1122: public SimpleFactory(boolean evalid, boolean ovalid) {
1123: evenValid = evalid;
1124: oddValid = ovalid;
1125: }
1126:
1127: void setValid(boolean valid) {
1128: setEvenValid(valid);
1129: setOddValid(valid);
1130: }
1131:
1132: void setEvenValid(boolean valid) {
1133: evenValid = valid;
1134: }
1135:
1136: void setOddValid(boolean valid) {
1137: oddValid = valid;
1138: }
1139:
1140: public void setThrowExceptionOnPassivate(boolean bool) {
1141: exceptionOnPassivate = bool;
1142: }
1143:
1144: public Object makeObject() {
1145: return String.valueOf(makeCounter++);
1146: }
1147:
1148: public void destroyObject(Object obj) {
1149: }
1150:
1151: public boolean validateObject(Object obj) {
1152: if (enableValidation) {
1153: return validateCounter++ % 2 == 0 ? evenValid
1154: : oddValid;
1155: } else {
1156: return true;
1157: }
1158: }
1159:
1160: public void activateObject(Object obj) throws Exception {
1161: if (exceptionOnActivate) {
1162: if (!(validateCounter++ % 2 == 0 ? evenValid : oddValid)) {
1163: throw new Exception();
1164: }
1165: }
1166: }
1167:
1168: public void passivateObject(Object obj) throws Exception {
1169: if (exceptionOnPassivate) {
1170: throw new Exception();
1171: }
1172: }
1173:
1174: int makeCounter = 0;
1175: int validateCounter = 0;
1176: boolean evenValid = true;
1177: boolean oddValid = true;
1178: boolean exceptionOnPassivate = false;
1179: boolean exceptionOnActivate = false;
1180: boolean enableValidation = true;
1181:
1182: public boolean isThrowExceptionOnActivate() {
1183: return exceptionOnActivate;
1184: }
1185:
1186: public void setThrowExceptionOnActivate(boolean b) {
1187: exceptionOnActivate = b;
1188: }
1189:
1190: public boolean isValidationEnabled() {
1191: return enableValidation;
1192: }
1193:
1194: public void setValidationEnabled(boolean b) {
1195: enableValidation = b;
1196: }
1197: }
1198:
1199: protected boolean isLifo() {
1200:
1201: return false;
1202: }
1203:
1204: protected boolean isFifo() {
1205: return true;
1206: }
1207: }
|