001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.performance.pool;
018
019 import java.io.InputStream;
020 import java.io.IOException;
021 import java.util.ArrayList;
022 import java.util.List;
023 import java.util.logging.Logger;
024 import org.apache.commons.dbcp.AbandonedConfig;
025 import org.apache.commons.dbcp.AbandonedObjectPool;
026 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
027 import org.apache.commons.pool.impl.GenericObjectPool;
028 import junit.framework.Test;
029 import junit.framework.TestCase;
030 import junit.framework.TestSuite;
031
032 public class PoolSoakTest extends TestCase {
033
034
035 public PoolSoakTest(String name) {
036 super(name);
037 }
038
039
040 public static Test suite() {
041 return new TestSuite(PoolSoakTest.class);
042 }
043
044 protected PoolSoak poolSoak = null;
045
046 public void setUp() throws Exception {
047 poolSoak = new PoolSoak();
048 poolSoak.configure();
049 }
050
051 public void testGenericObjectPoolConfig() throws Exception {
052 /** Contents of config file
053 <max-active>15</max-active>
054 <max-idle>15</max-idle>
055 <min-idle>0</min-idle>
056 <max-wait>-1</max-wait>
057 <!-- block, fail, or grow -->
058 <exhausted-action>block</exhausted-action>
059 <test-on-borrow>false</test-on-borrow>
060 <test-on-return>false</test-on-return>
061 <time-between-evictions>-1</time-between-evictions>
062 <tests-per-eviction>3</tests-per-eviction>
063 <idle-timeout>-1</idle-timeout>
064 <test-while-idle>false</test-while-idle>
065 **/
066 poolSoak.getDigester().parse(getInputStream("config-pool.xml"));
067 poolSoak.init();
068 GenericObjectPool pool = poolSoak.getGenericObjectPool();
069 assertEquals(15, pool.getMaxActive());
070 assertEquals(15, pool.getMaxIdle());
071 assertEquals(10, pool.getMinIdle());
072 assertEquals(-1, pool.getMaxWait());
073 assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK,
074 pool.getWhenExhaustedAction());
075 assertEquals(false, pool.getTestOnBorrow());
076 assertEquals(false, pool.getTestOnReturn());
077 assertEquals(-1, pool.getTimeBetweenEvictionRunsMillis());
078 assertEquals(3, pool.getNumTestsPerEvictionRun());
079 assertEquals(-1, pool.getMinEvictableIdleTimeMillis());
080 assertEquals(false, pool.getTestWhileIdle());
081 }
082
083 public void testAbandonedObjectPoolConfig() throws Exception {
084 /*
085 <pool>
086 <!-- GenericObjectPool or AbandonedObjectPool -->
087 <type>AbandonedObjectPool</type>
088 <max-active>15</max-active>
089 <max-idle>-1</max-idle>
090 <min-idle>0</min-idle>
091 <max-wait>-1</max-wait>
092 <!-- block, fail, or grow -->
093 <exhausted-action>grow</exhausted-action>
094 <test-on-borrow>true</test-on-borrow>
095 <test-on-return>false</test-on-return>
096 <time-between-evictions>-1</time-between-evictions>
097 <tests-per-eviction>3</tests-per-eviction>
098 <idle-timeout>-1</idle-timeout>
099 <test-while-idle>true</test-while-idle>
100 </pool>
101
102 <!-- Ignored unless pool type is AbandonedObjectPool -->
103 <abandoned-config>
104 <log-abandoned>true</log-abandoned>
105 <remove-abandoned>false</remove-abandoned>
106 <abandoned-timeout>50000</abandoned-timeout>
107 </abandoned-config>
108 */
109 poolSoak.getDigester().parse(getInputStream("config-abandoned.xml"));
110 poolSoak.init();
111 AbandonedObjectPool pool = (AbandonedObjectPool) poolSoak.getGenericObjectPool();
112 assertEquals(15, pool.getMaxActive());
113 assertEquals(-1, pool.getMaxIdle());
114 assertEquals(0, pool.getMinIdle());
115 assertEquals(-1, pool.getMaxWait());
116 assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW,
117 pool.getWhenExhaustedAction());
118 assertEquals(true, pool.getTestOnBorrow());
119 assertEquals(false, pool.getTestOnReturn());
120 assertEquals(-1, pool.getTimeBetweenEvictionRunsMillis());
121 assertEquals(3, pool.getNumTestsPerEvictionRun());
122 assertEquals(-1, pool.getMinEvictableIdleTimeMillis());
123 assertEquals(true, pool.getTestWhileIdle());
124 }
125
126 /**
127 * Return an appropriate InputStream for the specified test file (which
128 * must be inside our current package).
129 *
130 * Borrowed from Commons Digester RuleTestCase.
131 *
132 * @param name Name of the test file we want
133 * @exception IOException if an input/output error occurs
134 */
135 protected InputStream getInputStream(String name) throws IOException {
136
137 return (this.getClass().getResourceAsStream
138 ("/org/apache/commons/performance/pool/" + name));
139 }
140
141 }