KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > connection > VariablePoolConnectionManagerTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.connection;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.Stack JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36 import junit.textui.TestRunner;
37
38 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
39 import org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager;
40 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
41 import org.objectweb.cjdbc.scenario.tools.mock.MockDriver;
42 import org.objectweb.cjdbc.scenario.tools.util.GetConnectionThread;
43 import org.objectweb.cjdbc.scenario.tools.util.PrivilegedAccessor;
44
45 import com.mockobjects.sql.MockConnection2;
46
47 /**
48  * <code>VariablePoolConnectionManager</code> test class.
49  *
50  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
51  * @see org.objectweb.cjdbc.controller.connection.RandomWaitPoolConnectionManager
52  */

53 public class VariablePoolConnectionManagerTest extends NoTemplate
54 {
55   /** Fake driver. */
56   private MockDriver mockDriver;
57
58   /** Variable pool connection manager pool to test. */
59   private VariablePoolConnectionManager pool;
60
61   /**
62    * Builds a new TestSuite
63    *
64    * @return the TestSuite
65    */

66   public static Test suite()
67   {
68     return new TestSuite(VariablePoolConnectionManagerTest.class);
69   }
70
71   /**
72    * Starts a new test
73    *
74    * @param args test parameters
75    */

76   public static void main(String JavaDoc[] args)
77   {
78     TestRunner.run(suite());
79   }
80
81   /**
82    * @see junit.framework.TestCase#setUp()
83    */

84   protected void setUp()
85   {
86     // Create aand register the mock driver
87
try
88     {
89       mockDriver = new MockDriver()
90       {
91         public Connection JavaDoc connect(String JavaDoc arg0, Properties JavaDoc arg1)
92             throws SQLException JavaDoc
93         {
94           return new MockConnection2();
95         }
96       };
97       DriverManager.registerDriver(mockDriver);
98     }
99     catch (SQLException JavaDoc e)
100     {
101       fail("Failed to register driver: " + e);
102     }
103
104   }
105
106   /**
107    * @see junit.framework.TestCase#tearDown()
108    */

109   protected void tearDown()
110   {
111     // Deregister the driver
112
try
113     {
114       DriverManager.deregisterDriver(mockDriver);
115     }
116     catch (SQLException JavaDoc e)
117     {
118       fail("Failed to deregister driver: " + e);
119     }
120
121     pool = null;
122     System.gc();
123   }
124
125   /**
126    * Creates and initializes a <code>VariablePoolConnectionManager</code>.
127    *
128    * @param initPoolSize init pool size.
129    * @param minPoolSize min pool size.
130    * @param maxPoolSize max pool size.
131    * @param idleTimeout idle timeout
132    * @param waitTimeout wait timeout.
133    */

134   private void initializePool(int initPoolSize, int minPoolSize,
135       int maxPoolSize, int idleTimeout, int waitTimeout)
136   {
137     // Create a variable pool connection manager
138
try
139     {
140       pool = new VariablePoolConnectionManager("", "", "", "", null, null,
141           initPoolSize, minPoolSize, maxPoolSize, idleTimeout, waitTimeout);
142     }
143     catch (Exception JavaDoc e)
144     {
145       fail("Failed to create pool connection manager: " + e);
146     }
147
148     // Initialize the pool
149
try
150     {
151       pool.initializeConnections();
152     }
153     catch (SQLException JavaDoc e)
154     {
155       fail("Failed to initialize pool connection manager: " + e);
156     }
157   }
158
159   /**
160    * Test of the wait timeout feature.
161    *
162    * @throws Exception if an error occurs
163    */

164   public void testWaitTimeoutFeature() throws Exception JavaDoc
165   {
166     initializePool(3, 1, 3, 15, 10);
167
168     // Create 1 thread that gets a connection from the pool, sleep during
169
// 2.5 s and then release the connection
170
Thread JavaDoc t1 = new GetConnectionThread("thread1", pool, 2500);
171
172     // Create 1 thread that get a connection from the pool, sleep during
173
// 20 s and then release the connection
174
Thread JavaDoc t2 = new GetConnectionThread("thread2", pool, 20000);
175
176     // Create 1 thread that get a connection from the pool, sleep during
177
// 7.5 s and then release the connection
178
Thread JavaDoc t3 = new GetConnectionThread("thread3", pool, 7500);
179
180     // <- IDLE_TIMEOUT ->
181
// ---------------------------
182
// <- WAIT_TIMEOUT ->
183
// ------------------
184
// t1 .....
185
// t2 ....................................
186
// t3 .............
187
//
188
// with WAIT_TIMEOUT = 10 s
189
// 0 2.5 7.5 20
190

191     assertEquals(3, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
192         "freeConnections")).size());
193     assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
194         "activeConnections")).size());
195
196     t1.start();
197     t2.start();
198     t3.start();
199
200     // Make sure the thread have got their connection
201
try
202     {
203       Thread.sleep(500);
204     }
205     catch (InterruptedException JavaDoc e)
206     {
207       fail("Exception thrown: " + e);
208     }
209
210     // Get another connection: t1 should release his connection before the
211
// timeout
212
try
213     {
214       assertNotNull(pool.getConnection());
215     }
216     catch (UnreachableBackendException e1)
217     {
218       fail("Backend unreachable during test.");
219     }
220
221     assertEquals(0, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
222         "freeConnections")).size());
223     assertEquals(3, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
224         "activeConnections")).size());
225     assertEquals(3, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
226         .intValue());
227
228     // Get another connection: t3 should release his connection before the
229
// timeout
230
try
231     {
232       assertNotNull(pool.getConnection());
233     }
234     catch (UnreachableBackendException e1)
235     {
236       fail("Backend unreachable during test.");
237     }
238
239     assertEquals(0, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
240         "freeConnections")).size());
241     assertEquals(3, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
242         "activeConnections")).size());
243     assertEquals(3, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
244         .intValue());
245
246     // Try to get a second connection: t2 should not release his connection
247
// before the timeout
248
try
249     {
250       assertNull(pool.getConnection());
251     }
252     catch (UnreachableBackendException e1)
253     {
254       fail("Backend unreachable during test.");
255     }
256
257     assertEquals(0, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
258         "freeConnections")).size());
259     assertEquals(3, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
260         "activeConnections")).size());
261     assertEquals(3, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
262         .intValue());
263
264     try
265     {
266       t1.join();
267       t2.join();
268       t3.join();
269     }
270     catch (InterruptedException JavaDoc e)
271     {
272       fail("Unexpected exception");
273     }
274   }
275
276   /**
277    * @see org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager#releaseConnection(Connection)
278    * @see org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager#getConnection()
279    * @throws Exception if an error occurs
280    */

281   public void testGetAndReleaseConnection() throws Exception JavaDoc
282   {
283     initializePool(3, 1, 3, 15, 10);
284
285     // Create 1 thread that gets a connection from the pool, sleep during
286
// 2.5 s and then release the connection
287
Thread JavaDoc t1 = new GetConnectionThread("thread1", pool, 2500);
288
289     // Create 1 thread that get a connection from the pool, sleep during
290
// 20 s and then release the connection
291
Thread JavaDoc t2 = new GetConnectionThread("thread2", pool, 20000);
292
293     // Create 1 thread that get a connection from the pool, sleep during
294
// 7.5 s and then release the connection
295
Thread JavaDoc t3 = new GetConnectionThread("thread3", pool, 7500);
296
297     // <- IDLE_TIMEOUT ->
298
// ---------------------------
299
// <- WAIT_TIMEOUT ->
300
// ------------------
301
// t1 .....
302
// t2 ....................................
303
// t3 .............
304
//
305
// c1 CAAAAAFFFFFFFFFFFFFFFFFFFFFFFFFFFD
306
// c2 CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
307
// c3 CAAAAAAAAAAAAAFFFFFFFFFFFFFFFFFFFFFFFFFFFD
308
//
309
// C = creation
310
// A = active
311
// F = idle
312
//
313
// 0 2.5 7.5 17.5 20 22.5 35
314

315     assertEquals(3, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
316         "freeConnections")).size());
317     assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
318         "activeConnections")).size());
319
320     t1.start();
321     t2.start();
322     t3.start();
323
324     // Make sure the thread have got their connection
325
try
326     {
327       Thread.sleep(500);
328     }
329     catch (InterruptedException JavaDoc e)
330     {
331       fail("Exception thrown: " + e);
332     }
333
334     try
335     {
336       Thread.sleep(2500);
337       // t = 3 s
338
assertEquals(1, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
339           "freeConnections")).size());
340       assertEquals(2, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
341           "activeConnections")).size());
342       assertEquals(3, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
343           .intValue());
344
345       Thread.sleep(5500);
346       // t = 8.5 s
347
assertEquals(2, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
348           "freeConnections")).size());
349       assertEquals(1, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
350           "activeConnections")).size());
351       assertEquals(3, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
352           .intValue());
353
354       Thread.sleep(9500);
355       // t = 18 s
356
assertEquals(1, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
357           "freeConnections")).size());
358       assertEquals(1, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
359           "activeConnections")).size());
360       assertEquals(2, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
361           .intValue());
362
363       Thread.sleep(2500);
364       // t = 20.5
365
assertEquals(2, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
366           "freeConnections")).size());
367       assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
368           "activeConnections")).size());
369       assertEquals(2, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
370           .intValue());
371
372       Thread.sleep(2500);
373       // t = 23 s
374
assertEquals(1, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
375           "freeConnections")).size());
376       assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
377           "activeConnections")).size());
378       assertEquals(1, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
379           .intValue());
380
381       Thread.sleep(12500);
382       // t = 35.5 s
383
assertEquals(1, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
384           "freeConnections")).size());
385       assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
386           "activeConnections")).size());
387       assertEquals(1, ((Integer JavaDoc) PrivilegedAccessor.getValue(pool, "poolSize"))
388           .intValue());
389
390       t1.join();
391       t2.join();
392       t3.join();
393     }
394     catch (InterruptedException JavaDoc e)
395     {
396       fail("Exception thrown: " + e);
397     }
398   }
399
400   /**
401    * @see org.objectweb.cjdbc.controller.connection.VariablePoolConnectionManager.RemoveIdleConnectionsThread
402    */

403   public void testRemoveIdleConnectionsThread() throws Exception JavaDoc
404   {
405     // Create and initialize pool connection manager
406
// initializePool(init,min,max,idle,wait)
407
initializePool(20, 10, 30, 10, 5);
408
409     assertEquals(20, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
410         "freeConnections")).size());
411     assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
412         "activeConnections")).size());
413     assertEquals(30, pool.getMaxPoolSize());
414     assertEquals(10, pool.getMinPoolSize());
415     // pool is converting wait/idle timeout to seconds ...
416
assertEquals(5000, pool.getWaitTimeout());
417     assertEquals(10000, pool.getIdleTimeout());
418
419     // Wait until the idle timeout has been reached
420
try
421     {
422       Thread.sleep(11000);
423     }
424     catch (InterruptedException JavaDoc e)
425     {
426       fail("Exception thrown: " + e);
427     }
428
429     assertEquals(10, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
430         "freeConnections")).size());
431     assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
432         "activeConnections")).size());
433     assertEquals(30, pool.getMaxPoolSize());
434     assertEquals(10, pool.getMinPoolSize());
435   }
436 }
Popular Tags