1 24 25 package org.objectweb.cjdbc.scenario.standalone.connection; 26 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.sql.SQLException ; 30 import java.util.Properties ; 31 import java.util.Stack ; 32 import java.util.Vector ; 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 53 public class VariablePoolConnectionManagerTest extends NoTemplate 54 { 55 56 private MockDriver mockDriver; 57 58 59 private VariablePoolConnectionManager pool; 60 61 66 public static Test suite() 67 { 68 return new TestSuite(VariablePoolConnectionManagerTest.class); 69 } 70 71 76 public static void main(String [] args) 77 { 78 TestRunner.run(suite()); 79 } 80 81 84 protected void setUp() 85 { 86 try 88 { 89 mockDriver = new MockDriver() 90 { 91 public Connection connect(String arg0, Properties arg1) 92 throws SQLException 93 { 94 return new MockConnection2(); 95 } 96 }; 97 DriverManager.registerDriver(mockDriver); 98 } 99 catch (SQLException e) 100 { 101 fail("Failed to register driver: " + e); 102 } 103 104 } 105 106 109 protected void tearDown() 110 { 111 try 113 { 114 DriverManager.deregisterDriver(mockDriver); 115 } 116 catch (SQLException e) 117 { 118 fail("Failed to deregister driver: " + e); 119 } 120 121 pool = null; 122 System.gc(); 123 } 124 125 134 private void initializePool(int initPoolSize, int minPoolSize, 135 int maxPoolSize, int idleTimeout, int waitTimeout) 136 { 137 try 139 { 140 pool = new VariablePoolConnectionManager("", "", "", "", null, null, 141 initPoolSize, minPoolSize, maxPoolSize, idleTimeout, waitTimeout); 142 } 143 catch (Exception e) 144 { 145 fail("Failed to create pool connection manager: " + e); 146 } 147 148 try 150 { 151 pool.initializeConnections(); 152 } 153 catch (SQLException e) 154 { 155 fail("Failed to initialize pool connection manager: " + e); 156 } 157 } 158 159 164 public void testWaitTimeoutFeature() throws Exception 165 { 166 initializePool(3, 1, 3, 15, 10); 167 168 Thread t1 = new GetConnectionThread("thread1", pool, 2500); 171 172 Thread t2 = new GetConnectionThread("thread2", pool, 20000); 175 176 Thread t3 = new GetConnectionThread("thread3", pool, 7500); 179 180 191 assertEquals(3, ((Stack ) PrivilegedAccessor.getValue(pool, 192 "freeConnections")).size()); 193 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 194 "activeConnections")).size()); 195 196 t1.start(); 197 t2.start(); 198 t3.start(); 199 200 try 202 { 203 Thread.sleep(500); 204 } 205 catch (InterruptedException e) 206 { 207 fail("Exception thrown: " + e); 208 } 209 210 try 213 { 214 assertNotNull(pool.getConnection()); 215 } 216 catch (UnreachableBackendException e1) 217 { 218 fail("Backend unreachable during test."); 219 } 220 221 assertEquals(0, ((Stack ) PrivilegedAccessor.getValue(pool, 222 "freeConnections")).size()); 223 assertEquals(3, ((Vector ) PrivilegedAccessor.getValue(pool, 224 "activeConnections")).size()); 225 assertEquals(3, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 226 .intValue()); 227 228 try 231 { 232 assertNotNull(pool.getConnection()); 233 } 234 catch (UnreachableBackendException e1) 235 { 236 fail("Backend unreachable during test."); 237 } 238 239 assertEquals(0, ((Stack ) PrivilegedAccessor.getValue(pool, 240 "freeConnections")).size()); 241 assertEquals(3, ((Vector ) PrivilegedAccessor.getValue(pool, 242 "activeConnections")).size()); 243 assertEquals(3, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 244 .intValue()); 245 246 try 249 { 250 assertNull(pool.getConnection()); 251 } 252 catch (UnreachableBackendException e1) 253 { 254 fail("Backend unreachable during test."); 255 } 256 257 assertEquals(0, ((Stack ) PrivilegedAccessor.getValue(pool, 258 "freeConnections")).size()); 259 assertEquals(3, ((Vector ) PrivilegedAccessor.getValue(pool, 260 "activeConnections")).size()); 261 assertEquals(3, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 262 .intValue()); 263 264 try 265 { 266 t1.join(); 267 t2.join(); 268 t3.join(); 269 } 270 catch (InterruptedException e) 271 { 272 fail("Unexpected exception"); 273 } 274 } 275 276 281 public void testGetAndReleaseConnection() throws Exception 282 { 283 initializePool(3, 1, 3, 15, 10); 284 285 Thread t1 = new GetConnectionThread("thread1", pool, 2500); 288 289 Thread t2 = new GetConnectionThread("thread2", pool, 20000); 292 293 Thread t3 = new GetConnectionThread("thread3", pool, 7500); 296 297 315 assertEquals(3, ((Stack ) PrivilegedAccessor.getValue(pool, 316 "freeConnections")).size()); 317 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 318 "activeConnections")).size()); 319 320 t1.start(); 321 t2.start(); 322 t3.start(); 323 324 try 326 { 327 Thread.sleep(500); 328 } 329 catch (InterruptedException e) 330 { 331 fail("Exception thrown: " + e); 332 } 333 334 try 335 { 336 Thread.sleep(2500); 337 assertEquals(1, ((Stack ) PrivilegedAccessor.getValue(pool, 339 "freeConnections")).size()); 340 assertEquals(2, ((Vector ) PrivilegedAccessor.getValue(pool, 341 "activeConnections")).size()); 342 assertEquals(3, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 343 .intValue()); 344 345 Thread.sleep(5500); 346 assertEquals(2, ((Stack ) PrivilegedAccessor.getValue(pool, 348 "freeConnections")).size()); 349 assertEquals(1, ((Vector ) PrivilegedAccessor.getValue(pool, 350 "activeConnections")).size()); 351 assertEquals(3, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 352 .intValue()); 353 354 Thread.sleep(9500); 355 assertEquals(1, ((Stack ) PrivilegedAccessor.getValue(pool, 357 "freeConnections")).size()); 358 assertEquals(1, ((Vector ) PrivilegedAccessor.getValue(pool, 359 "activeConnections")).size()); 360 assertEquals(2, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 361 .intValue()); 362 363 Thread.sleep(2500); 364 assertEquals(2, ((Stack ) PrivilegedAccessor.getValue(pool, 366 "freeConnections")).size()); 367 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 368 "activeConnections")).size()); 369 assertEquals(2, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 370 .intValue()); 371 372 Thread.sleep(2500); 373 assertEquals(1, ((Stack ) PrivilegedAccessor.getValue(pool, 375 "freeConnections")).size()); 376 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 377 "activeConnections")).size()); 378 assertEquals(1, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 379 .intValue()); 380 381 Thread.sleep(12500); 382 assertEquals(1, ((Stack ) PrivilegedAccessor.getValue(pool, 384 "freeConnections")).size()); 385 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 386 "activeConnections")).size()); 387 assertEquals(1, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 388 .intValue()); 389 390 t1.join(); 391 t2.join(); 392 t3.join(); 393 } 394 catch (InterruptedException e) 395 { 396 fail("Exception thrown: " + e); 397 } 398 } 399 400 403 public void testRemoveIdleConnectionsThread() throws Exception 404 { 405 initializePool(20, 10, 30, 10, 5); 408 409 assertEquals(20, ((Stack ) PrivilegedAccessor.getValue(pool, 410 "freeConnections")).size()); 411 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 412 "activeConnections")).size()); 413 assertEquals(30, pool.getMaxPoolSize()); 414 assertEquals(10, pool.getMinPoolSize()); 415 assertEquals(5000, pool.getWaitTimeout()); 417 assertEquals(10000, pool.getIdleTimeout()); 418 419 try 421 { 422 Thread.sleep(11000); 423 } 424 catch (InterruptedException e) 425 { 426 fail("Exception thrown: " + e); 427 } 428 429 assertEquals(10, ((Stack ) PrivilegedAccessor.getValue(pool, 430 "freeConnections")).size()); 431 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 432 "activeConnections")).size()); 433 assertEquals(30, pool.getMaxPoolSize()); 434 assertEquals(10, pool.getMinPoolSize()); 435 } 436 } | Popular Tags |