KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ConnectionPoolTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.sql.DriverManager JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import java.sql.Connection JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 /**
17  * Test {@link ConnectionPool}
18  *
19  * @version $Revision: 1.3 $, $Date: 2006/01/18 14:40:06 $
20  * @author billhorsman
21  * @author $Author: billhorsman $ (current maintainer)
22  * @since Proxool 0.8
23  */

24 public class ConnectionPoolTest extends AbstractProxoolTest {
25
26     private static final Log LOG = LogFactory.getLog(ConnectionPoolTest.class);
27
28     public ConnectionPoolTest(String JavaDoc name) {
29         super(name);
30     }
31
32     /**
33      * If we ask for more simultaneous connections then we have allowed we should gracefully
34      * refuse them.
35      */

36     public void testMaximumConnectionCount() throws Exception JavaDoc {
37
38         String JavaDoc testName = "maximumConnectionCount";
39         String JavaDoc alias = testName;
40
41         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
42                 TestConstants.HYPERSONIC_DRIVER,
43                 TestConstants.HYPERSONIC_TEST_URL);
44         Properties JavaDoc info = new Properties JavaDoc();
45         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
46         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
47         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "2");
48         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
49         ProxoolFacade.registerConnectionPool(url, info);
50
51         DriverManager.getConnection(url);
52         DriverManager.getConnection(url);
53
54         try {
55             DriverManager.getConnection(url);
56             fail("Didn't expect to get third connection");
57         } catch (SQLException JavaDoc e) {
58             // Log message only so we don't get a worrying stack trace
59
LOG.debug("Ignoring expected exception: " + e.getMessage());
60         }
61
62         assertEquals("activeConnectionCount", 2, ProxoolFacade.getSnapshot(alias, true).getActiveConnectionCount());
63
64     }
65
66     /**
67      * test what happens if maximum = minimum = 1
68      */

69     public void testMaximumEqualsMinimumConnectionCount() throws Exception JavaDoc {
70
71         String JavaDoc testName = "maximumEqualsMinimumConnectionCount";
72         String JavaDoc alias = testName;
73
74         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
75                 TestConstants.HYPERSONIC_DRIVER,
76                 TestConstants.HYPERSONIC_TEST_URL);
77         Properties JavaDoc info = new Properties JavaDoc();
78         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
79         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
80         info.setProperty(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, "1");
81         info.setProperty(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, "1");
82         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
83         ProxoolFacade.registerConnectionPool(url, info);
84
85         // opening and closing a connection should have no effect
86
Connection JavaDoc c1 = DriverManager.getConnection(url);
87         c1.close();
88
89         // Leave this one open for a while
90
Connection JavaDoc c2 = DriverManager.getConnection(url);
91
92         try {
93             Connection JavaDoc c3 = DriverManager.getConnection(url);
94             c3.close();
95             fail("Didn't expect to get third connection");
96         } catch (SQLException JavaDoc e) {
97             // Log message only so we don't get a worrying stack trace
98
LOG.debug("Ignoring expected exception: " + e.getMessage());
99         }
100
101         assertEquals("activeConnectionCount", 1, ProxoolFacade.getSnapshot(alias, true).getActiveConnectionCount());
102         c2.close();
103
104         // Just check it all still works
105
DriverManager.getConnection(url).close();
106
107         assertEquals("activeConnectionCount", 0, ProxoolFacade.getSnapshot(alias, true).getActiveConnectionCount());
108
109     }
110
111     /**
112      * Checks whether shutdown is patient enough to wait for active connections
113      */

114     public void testShutdownPatience() throws ProxoolException, SQLException JavaDoc {
115
116         String JavaDoc testName = "shutdownPatience";
117         String JavaDoc alias = testName;
118
119         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
120                 TestConstants.HYPERSONIC_DRIVER,
121                 TestConstants.HYPERSONIC_TEST_URL);
122         Properties JavaDoc info = new Properties JavaDoc();
123         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
124         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD); info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
125         ProxoolFacade.registerConnectionPool(url, info);
126
127         // Open a connection that will close in 5 seconds
128
new Thread JavaDoc(new Closer(DriverManager.getConnection(url), 5000)).start();
129
130         long startTime = System.currentTimeMillis();
131         ProxoolFacade.removeConnectionPool(alias, 100000);
132         long shutdownTime = System.currentTimeMillis() - startTime;
133         assertTrue("shutdown took too long", shutdownTime < 50000);
134         assertTrue("shutdown was too quick", shutdownTime > 2000);
135     }
136
137     /**
138      * Checks whether shutdown is impatient enough to shutdown even if
139      * some connections are still active
140      */

141     public void testShutdownImpatience() throws ProxoolException, SQLException JavaDoc {
142
143         String JavaDoc testName = "shutdownImpatience";
144         String JavaDoc alias = testName;
145
146         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
147                 TestConstants.HYPERSONIC_DRIVER,
148                 TestConstants.HYPERSONIC_TEST_URL);
149         Properties JavaDoc info = new Properties JavaDoc();
150         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
151         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
152         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
153         ProxoolFacade.registerConnectionPool(url, info);
154
155         // Open a connection that will close in 100 seconds
156
new Thread JavaDoc(new Closer(DriverManager.getConnection(url), 100000)).start();
157
158         long startTime = System.currentTimeMillis();
159         ProxoolFacade.removeConnectionPool(alias, 3000);
160         long shutdownTime = System.currentTimeMillis() - startTime;
161         assertTrue("shutdown took too long", shutdownTime < 50000);
162         assertTrue("shutdown was too quick", shutdownTime > 1000);
163     }
164
165     class Closer implements Runnable JavaDoc {
166
167         private Connection JavaDoc connection;
168
169         private long duration;
170
171         public Closer(Connection JavaDoc connection, long duration) {
172             this.connection = connection;
173             this.duration = duration;
174         }
175
176         public void run() {
177             long startTime = System.currentTimeMillis();
178             try {
179                 Thread.sleep(duration);
180             } catch (InterruptedException JavaDoc e) {
181                 LOG.error("Awoken", e);
182             }
183             try {
184                 connection.close();
185                 LOG.debug("Connection closed after " + (System.currentTimeMillis() - startTime)
186                         + " milliseconds.");
187             } catch (SQLException JavaDoc e) {
188                 LOG.error("Problem closing connection", e);
189             }
190         }
191
192     }
193
194 }
195
196 /*
197  Revision history:
198  $Log: ConnectionPoolTest.java,v $
199  Revision 1.3 2006/01/18 14:40:06 billhorsman
200  Unbundled Jakarta's Commons Logging.
201
202  Revision 1.2 2004/06/02 21:05:19 billhorsman
203  Don't log worrying stack traces for expected exceptions.
204
205  Revision 1.1 2003/10/26 16:10:46 billhorsman
206  renamed to be more consistent
207
208  Revision 1.9 2003/09/11 23:14:57 billhorsman
209  New test for when maximum-connection-count = 1 and minimum-connection-count = 1
210
211  Revision 1.8 2003/03/12 00:14:12 billhorsman
212  change thresholds
213
214  Revision 1.7 2003/03/11 01:19:48 billhorsman
215  new tests
216
217  Revision 1.6 2003/03/10 15:31:26 billhorsman
218  fixes
219
220  Revision 1.5 2003/03/04 10:24:40 billhorsman
221  removed try blocks around each test
222
223  Revision 1.4 2003/03/03 17:08:55 billhorsman
224  all tests now extend AbstractProxoolTest
225
226  Revision 1.3 2003/03/03 11:12:04 billhorsman
227  fixed licence
228
229  Revision 1.2 2003/03/01 15:27:24 billhorsman
230  checkstyle
231
232  Revision 1.1 2003/02/27 18:01:47 billhorsman
233  completely rethought the test structure. it's now
234  more obvious. no new tests yet though.
235
236
237 */

238
Popular Tags