1 24 25 package org.objectweb.cjdbc.scenario.standalone.connection; 26 27 import java.sql.DriverManager ; 28 import java.sql.SQLException ; 29 import java.util.Stack ; 30 import java.util.Vector ; 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 51 public class RandomWaitPoolConnectionManagerTest extends NoTemplate 52 { 53 54 private MockDriver mockDriver; 55 56 57 private RandomWaitPoolConnectionManager pool; 58 59 64 public static Test suite() 65 { 66 return new TestSuite(RandomWaitPoolConnectionManagerTest.class); 67 } 68 69 74 public static void main(String [] args) 75 { 76 TestRunner.run(suite()); 77 } 78 79 82 protected void setUp() 83 { 84 try 86 { 87 mockDriver = new MockDriver(); 88 DriverManager.registerDriver(mockDriver); 89 } 90 catch (SQLException e) 91 { 92 fail("Failed to register driver: " + e); 93 } 94 } 95 96 99 protected void tearDown() 100 { 101 try 103 { 104 DriverManager.deregisterDriver(mockDriver); 105 } 106 catch (SQLException e) 107 { 108 fail("Failed to deregister driver: " + e); 109 } 110 } 111 112 118 private void initializePool(int poolSize, int timeout) 119 { 120 mockDriver.setExpectedConnectCalls(poolSize); 122 for (int i = 0; i < poolSize; i++) 123 { 124 mockDriver.setupConnect(new MockConnection2()); 125 } 126 127 try 129 { 130 pool = new RandomWaitPoolConnectionManager("", "", "", "", null, null, 131 poolSize, timeout); 132 } 133 catch (Exception e) 134 { 135 fail("Failed to create pool connection manager: " + e); 136 } 137 138 try 140 { 141 pool.initializeConnections(); 142 } 143 catch (SQLException e) 144 { 145 fail("Failed to initialize pool connection manager: " + e); 146 } 147 } 148 149 155 public void testGetAndReleaseConnection() throws Exception 156 { 157 initializePool(3, 10); 159 160 166 Thread t1 = new GetConnectionThread("thread1", pool, 5000); 169 170 Thread t2 = new GetConnectionThread("thread2", pool, 20000); 173 174 Thread t3 = new GetConnectionThread("thread3", pool, 7500); 177 178 assertEquals(3, ((Stack ) PrivilegedAccessor.getValue(pool, 179 "freeConnections")).size()); 180 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 181 "activeConnections")).size()); 182 183 t1.start(); 184 t2.start(); 185 t3.start(); 186 187 try 189 { 190 Thread.sleep(1000); 191 } 192 catch (InterruptedException e) 193 { 194 fail("Exception thrown: " + e); 195 } 196 197 try 200 { 201 assertNotNull(pool.getConnection()); 202 } 203 catch (UnreachableBackendException e1) 204 { 205 fail("Backend unreachable during test."); 206 } 207 208 assertEquals(0, ((Stack ) PrivilegedAccessor.getValue(pool, 209 "freeConnections")).size()); 210 assertEquals(3, ((Vector ) PrivilegedAccessor.getValue(pool, 211 "activeConnections")).size()); 212 213 try 216 { 217 assertNotNull(pool.getConnection()); 218 } 219 catch (UnreachableBackendException e2) 220 { 221 fail("Backend unreachable during test."); 222 } 223 224 assertEquals(0, ((Stack ) PrivilegedAccessor.getValue(pool, 225 "freeConnections")).size()); 226 assertEquals(3, ((Vector ) PrivilegedAccessor.getValue(pool, 227 "activeConnections")).size()); 228 229 try 232 { 233 assertNull(pool.getConnection()); 234 } 235 catch (UnreachableBackendException e3) 236 { 237 fail("Backend unreachable during test."); 238 } 239 240 assertEquals(0, ((Stack ) PrivilegedAccessor.getValue(pool, 241 "freeConnections")).size()); 242 assertEquals(3, ((Vector ) PrivilegedAccessor.getValue(pool, 243 "activeConnections")).size()); 244 245 try 247 { 248 t2.join(); 249 } 250 catch (InterruptedException e) 251 { 252 fail("Exception thrown: " + e); 253 } 254 255 assertEquals(1, ((Stack ) PrivilegedAccessor.getValue(pool, 256 "freeConnections")).size()); 257 assertEquals(2, ((Vector ) PrivilegedAccessor.getValue(pool, 258 "activeConnections")).size()); 259 260 } 261 } | Popular Tags |