001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.sql.DriverManager;
012: import java.sql.SQLException;
013: import java.sql.Connection;
014: import java.util.Properties;
015:
016: /**
017: * Test {@link ConnectionPool}
018: *
019: * @version $Revision: 1.3 $, $Date: 2006/01/18 14:40:06 $
020: * @author billhorsman
021: * @author $Author: billhorsman $ (current maintainer)
022: * @since Proxool 0.8
023: */
024: public class ConnectionPoolTest extends AbstractProxoolTest {
025:
026: private static final Log LOG = LogFactory
027: .getLog(ConnectionPoolTest.class);
028:
029: public ConnectionPoolTest(String name) {
030: super (name);
031: }
032:
033: /**
034: * If we ask for more simultaneous connections then we have allowed we should gracefully
035: * refuse them.
036: */
037: public void testMaximumConnectionCount() throws Exception {
038:
039: String testName = "maximumConnectionCount";
040: String alias = testName;
041:
042: String url = TestHelper.buildProxoolUrl(alias,
043: TestConstants.HYPERSONIC_DRIVER,
044: TestConstants.HYPERSONIC_TEST_URL);
045: Properties info = new Properties();
046: info.setProperty(ProxoolConstants.USER_PROPERTY,
047: TestConstants.HYPERSONIC_USER);
048: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
049: TestConstants.HYPERSONIC_PASSWORD);
050: info
051: .setProperty(
052: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
053: "2");
054: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
055: ProxoolFacade.registerConnectionPool(url, info);
056:
057: DriverManager.getConnection(url);
058: DriverManager.getConnection(url);
059:
060: try {
061: DriverManager.getConnection(url);
062: fail("Didn't expect to get third connection");
063: } catch (SQLException e) {
064: // Log message only so we don't get a worrying stack trace
065: LOG.debug("Ignoring expected exception: " + e.getMessage());
066: }
067:
068: assertEquals("activeConnectionCount", 2, ProxoolFacade
069: .getSnapshot(alias, true).getActiveConnectionCount());
070:
071: }
072:
073: /**
074: * test what happens if maximum = minimum = 1
075: */
076: public void testMaximumEqualsMinimumConnectionCount()
077: throws Exception {
078:
079: String testName = "maximumEqualsMinimumConnectionCount";
080: String alias = testName;
081:
082: String url = TestHelper.buildProxoolUrl(alias,
083: TestConstants.HYPERSONIC_DRIVER,
084: TestConstants.HYPERSONIC_TEST_URL);
085: Properties info = new Properties();
086: info.setProperty(ProxoolConstants.USER_PROPERTY,
087: TestConstants.HYPERSONIC_USER);
088: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
089: TestConstants.HYPERSONIC_PASSWORD);
090: info
091: .setProperty(
092: ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY,
093: "1");
094: info
095: .setProperty(
096: ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY,
097: "1");
098: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
099: ProxoolFacade.registerConnectionPool(url, info);
100:
101: // opening and closing a connection should have no effect
102: Connection c1 = DriverManager.getConnection(url);
103: c1.close();
104:
105: // Leave this one open for a while
106: Connection c2 = DriverManager.getConnection(url);
107:
108: try {
109: Connection c3 = DriverManager.getConnection(url);
110: c3.close();
111: fail("Didn't expect to get third connection");
112: } catch (SQLException e) {
113: // Log message only so we don't get a worrying stack trace
114: LOG.debug("Ignoring expected exception: " + e.getMessage());
115: }
116:
117: assertEquals("activeConnectionCount", 1, ProxoolFacade
118: .getSnapshot(alias, true).getActiveConnectionCount());
119: c2.close();
120:
121: // Just check it all still works
122: DriverManager.getConnection(url).close();
123:
124: assertEquals("activeConnectionCount", 0, ProxoolFacade
125: .getSnapshot(alias, true).getActiveConnectionCount());
126:
127: }
128:
129: /**
130: * Checks whether shutdown is patient enough to wait for active connections
131: */
132: public void testShutdownPatience() throws ProxoolException,
133: SQLException {
134:
135: String testName = "shutdownPatience";
136: String alias = testName;
137:
138: String url = TestHelper.buildProxoolUrl(alias,
139: TestConstants.HYPERSONIC_DRIVER,
140: TestConstants.HYPERSONIC_TEST_URL);
141: Properties info = new Properties();
142: info.setProperty(ProxoolConstants.USER_PROPERTY,
143: TestConstants.HYPERSONIC_USER);
144: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
145: TestConstants.HYPERSONIC_PASSWORD);
146: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
147: ProxoolFacade.registerConnectionPool(url, info);
148:
149: // Open a connection that will close in 5 seconds
150: new Thread(new Closer(DriverManager.getConnection(url), 5000))
151: .start();
152:
153: long startTime = System.currentTimeMillis();
154: ProxoolFacade.removeConnectionPool(alias, 100000);
155: long shutdownTime = System.currentTimeMillis() - startTime;
156: assertTrue("shutdown took too long", shutdownTime < 50000);
157: assertTrue("shutdown was too quick", shutdownTime > 2000);
158: }
159:
160: /**
161: * Checks whether shutdown is impatient enough to shutdown even if
162: * some connections are still active
163: */
164: public void testShutdownImpatience() throws ProxoolException,
165: SQLException {
166:
167: String testName = "shutdownImpatience";
168: String alias = testName;
169:
170: String url = TestHelper.buildProxoolUrl(alias,
171: TestConstants.HYPERSONIC_DRIVER,
172: TestConstants.HYPERSONIC_TEST_URL);
173: Properties info = new Properties();
174: info.setProperty(ProxoolConstants.USER_PROPERTY,
175: TestConstants.HYPERSONIC_USER);
176: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
177: TestConstants.HYPERSONIC_PASSWORD);
178: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
179: ProxoolFacade.registerConnectionPool(url, info);
180:
181: // Open a connection that will close in 100 seconds
182: new Thread(new Closer(DriverManager.getConnection(url), 100000))
183: .start();
184:
185: long startTime = System.currentTimeMillis();
186: ProxoolFacade.removeConnectionPool(alias, 3000);
187: long shutdownTime = System.currentTimeMillis() - startTime;
188: assertTrue("shutdown took too long", shutdownTime < 50000);
189: assertTrue("shutdown was too quick", shutdownTime > 1000);
190: }
191:
192: class Closer implements Runnable {
193:
194: private Connection connection;
195:
196: private long duration;
197:
198: public Closer(Connection connection, long duration) {
199: this .connection = connection;
200: this .duration = duration;
201: }
202:
203: public void run() {
204: long startTime = System.currentTimeMillis();
205: try {
206: Thread.sleep(duration);
207: } catch (InterruptedException e) {
208: LOG.error("Awoken", e);
209: }
210: try {
211: connection.close();
212: LOG.debug("Connection closed after "
213: + (System.currentTimeMillis() - startTime)
214: + " milliseconds.");
215: } catch (SQLException e) {
216: LOG.error("Problem closing connection", e);
217: }
218: }
219:
220: }
221:
222: }
223:
224: /*
225: Revision history:
226: $Log: ConnectionPoolTest.java,v $
227: Revision 1.3 2006/01/18 14:40:06 billhorsman
228: Unbundled Jakarta's Commons Logging.
229:
230: Revision 1.2 2004/06/02 21:05:19 billhorsman
231: Don't log worrying stack traces for expected exceptions.
232:
233: Revision 1.1 2003/10/26 16:10:46 billhorsman
234: renamed to be more consistent
235:
236: Revision 1.9 2003/09/11 23:14:57 billhorsman
237: New test for when maximum-connection-count = 1 and minimum-connection-count = 1
238:
239: Revision 1.8 2003/03/12 00:14:12 billhorsman
240: change thresholds
241:
242: Revision 1.7 2003/03/11 01:19:48 billhorsman
243: new tests
244:
245: Revision 1.6 2003/03/10 15:31:26 billhorsman
246: fixes
247:
248: Revision 1.5 2003/03/04 10:24:40 billhorsman
249: removed try blocks around each test
250:
251: Revision 1.4 2003/03/03 17:08:55 billhorsman
252: all tests now extend AbstractProxoolTest
253:
254: Revision 1.3 2003/03/03 11:12:04 billhorsman
255: fixed licence
256:
257: Revision 1.2 2003/03/01 15:27:24 billhorsman
258: checkstyle
259:
260: Revision 1.1 2003/02/27 18:01:47 billhorsman
261: completely rethought the test structure. it's now
262: more obvious. no new tests yet though.
263:
264:
265: */
|