KickJava   Java API By Example, From Geeks To Geeks.

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


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

51 public class RandomWaitPoolConnectionManagerTest extends NoTemplate
52 {
53   /** Fake driver. */
54   private MockDriver mockDriver;
55
56   /** Random wait connection manager pool to test. */
57   private RandomWaitPoolConnectionManager pool;
58
59   /**
60    * Builds a new TestSuite
61    *
62    * @return the TestSuite
63    */

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

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

82   protected void setUp()
83   {
84     // Create and register the mock driver
85
try
86     {
87       mockDriver = new MockDriver();
88       DriverManager.registerDriver(mockDriver);
89     }
90     catch (SQLException JavaDoc e)
91     {
92       fail("Failed to register driver: " + e);
93     }
94   }
95
96   /**
97    * @see junit.framework.TestCase#tearDown()
98    */

99   protected void tearDown()
100   {
101     // Deregister the driver
102
try
103     {
104       DriverManager.deregisterDriver(mockDriver);
105     }
106     catch (SQLException JavaDoc e)
107     {
108       fail("Failed to deregister driver: " + e);
109     }
110   }
111
112   /**
113    * Creates and initializes a <code>RandomWaitoolConnectionManager</code>.
114    *
115    * @param poolSize pool size.
116    * @param timeout timeout.
117    */

118   private void initializePool(int poolSize, int timeout)
119   {
120     // Initialize driver
121
mockDriver.setExpectedConnectCalls(poolSize);
122     for (int i = 0; i < poolSize; i++)
123     {
124       mockDriver.setupConnect(new MockConnection2());
125     }
126
127     // Create the pool
128
try
129     {
130       pool = new RandomWaitPoolConnectionManager("", "", "", "", null, null,
131           poolSize, timeout);
132     }
133     catch (Exception JavaDoc e)
134     {
135       fail("Failed to create pool connection manager: " + e);
136     }
137
138     // Initialize the pool
139
try
140     {
141       pool.initializeConnections();
142     }
143     catch (SQLException JavaDoc e)
144     {
145       fail("Failed to initialize pool connection manager: " + e);
146     }
147   }
148
149   /**
150    * @see org.objectweb.cjdbc.controller.connection.RandomWaitPoolConnectionManager#releaseConnection(Connection)
151    * @see org.objectweb.cjdbc.controller.connection.RandomWaitPoolConnectionManager#getConnection()
152    *
153    * @throws Exception if an error occurs
154    */

155   public void testGetAndReleaseConnection() throws Exception JavaDoc
156   {
157     // Create a random wait pool connection manager of 3 connections
158
initializePool(3, 10);
159
160     // <- TIMEOUT ->
161
// ----------------
162
// t1 ........
163
// t2 ................................
164
// t3 ............
165

166     // Create 1 thread that gets a connection from the pool and sleep during
167
// 1/ 2 * TIMEOUT seconds and then release the connection
168
Thread JavaDoc t1 = new GetConnectionThread("thread1", pool, 5000);
169
170     // Create 1 thread that get a connection from the pool and sleep during
171
// 2 * TIMEOUT seconds and then release the connection
172
Thread JavaDoc t2 = new GetConnectionThread("thread2", pool, 20000);
173
174     // Create 1 thread that get a connection from the pool and sleep during
175
// 3 / 4 * TIMEOUT seconds and then release the connection
176
Thread JavaDoc t3 = new GetConnectionThread("thread3", pool, 7500);
177
178     assertEquals(3, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
179         "freeConnections")).size());
180     assertEquals(0, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
181         "activeConnections")).size());
182
183     t1.start();
184     t2.start();
185     t3.start();
186
187     // make sure that the threads have got their connection
188
try
189     {
190       Thread.sleep(1000);
191     }
192     catch (InterruptedException JavaDoc e)
193     {
194       fail("Exception thrown: " + e);
195     }
196
197     // Get another connection: t1 should release his connection before the
198
// timeout
199
try
200     {
201       assertNotNull(pool.getConnection());
202     }
203     catch (UnreachableBackendException e1)
204     {
205       fail("Backend unreachable during test.");
206     }
207
208     assertEquals(0, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
209         "freeConnections")).size());
210     assertEquals(3, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
211         "activeConnections")).size());
212
213     // Get another connection: t3 should release his connection before the
214
// timeout
215
try
216     {
217       assertNotNull(pool.getConnection());
218     }
219     catch (UnreachableBackendException e2)
220     {
221       fail("Backend unreachable during test.");
222     }
223
224     assertEquals(0, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
225         "freeConnections")).size());
226     assertEquals(3, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
227         "activeConnections")).size());
228
229     // Try to get a second connection: t2 should not release his connection
230
// before the timeout
231
try
232     {
233       assertNull(pool.getConnection());
234     }
235     catch (UnreachableBackendException e3)
236     {
237       fail("Backend unreachable during test.");
238     }
239
240     assertEquals(0, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
241         "freeConnections")).size());
242     assertEquals(3, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
243         "activeConnections")).size());
244
245     // make sure to wait for the end of the thread 2
246
try
247     {
248       t2.join();
249     }
250     catch (InterruptedException JavaDoc e)
251     {
252       fail("Exception thrown: " + e);
253     }
254
255     assertEquals(1, ((Stack JavaDoc) PrivilegedAccessor.getValue(pool,
256         "freeConnections")).size());
257     assertEquals(2, ((Vector JavaDoc) PrivilegedAccessor.getValue(pool,
258         "activeConnections")).size());
259
260   }
261 }
Popular Tags