1 22 package org.jboss.test.jca.test; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 28 import javax.resource.ResourceException ; 29 import javax.resource.spi.ConnectionRequestInfo ; 30 import javax.resource.spi.ManagedConnectionFactory ; 31 import javax.security.auth.Subject ; 32 33 import junit.framework.TestCase; 34 35 import org.jboss.logging.Logger; 36 import org.jboss.resource.connectionmanager.BaseConnectionManager2; 37 import org.jboss.resource.connectionmanager.CachedConnectionManager; 38 import org.jboss.resource.connectionmanager.ConnectionListener; 39 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 40 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 41 import org.jboss.resource.connectionmanager.ManagedConnectionPool; 42 import org.jboss.resource.connectionmanager.NoTxConnectionManager; 43 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool.BasePool; 44 import org.jboss.test.jca.adapter.TestConnectionRequestInfo; 45 import org.jboss.test.jca.adapter.TestManagedConnectionFactory; 46 47 56 public class BaseConnectionManagerUnitTestCase extends TestCase 57 { 58 59 Logger log = Logger.getLogger(getClass()); 60 61 62 Subject subject = new Subject (); 63 ConnectionRequestInfo cri = new TestConnectionRequestInfo(); 64 CachedConnectionManager ccm = new CachedConnectionManager(); 65 66 67 72 public BaseConnectionManagerUnitTestCase (String name) 73 { 74 super(name); 75 } 76 77 78 private BaseConnectionManager2 getCM( 79 InternalManagedConnectionPool.PoolParams pp) 80 throws Exception 81 { 82 ManagedConnectionFactory mcf = new TestManagedConnectionFactory(); 83 ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, log); 84 BaseConnectionManager2 cm = new NoTxConnectionManager(ccm, poolingStrategy); 85 poolingStrategy.setConnectionListenerFactory(cm); 86 87 88 if(pp.prefill){ 89 90 BasePool bp = (BasePool)poolingStrategy; 91 bp.prefill(null, null, false); 92 93 } 94 95 return cm; 96 } 97 98 private void shutdown(BaseConnectionManager2 cm) 99 { 100 JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy(); 101 pool.shutdown(); 102 } 103 104 protected void setUp() 105 { 106 log.debug("================> Start " + getName()); 107 } 108 109 protected void tearDown() 110 { 111 log.debug("================> End " + getName()); 112 } 113 114 public void testGetManagedConnections() throws Exception 115 { 116 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 117 pp.minSize = 0; 118 pp.maxSize = 5; 119 pp.blockingTimeout = 100; 120 pp.idleTimeout = 500; 121 BaseConnectionManager2 cm = getCM(pp); 122 try 123 { 124 ArrayList cs = new ArrayList (); 125 for (int i = 0; i < pp.maxSize; i++) 126 { 127 ConnectionListener cl = cm.getManagedConnection(null, null); 128 assertTrue("Got a null connection!", cl.getManagedConnection() != null); 129 cs.add(cl); 130 } assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize); 132 try 133 { 134 cm.getManagedConnection(null, null); 135 fail("Got a connection more than maxSize!"); 136 } 137 catch (ResourceException re) 138 { 139 } for (Iterator i = cs.iterator(); i.hasNext();) 142 { 143 cm.returnManagedConnection((ConnectionListener)i.next(), true); 144 } assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0); 146 } 147 finally 148 { 149 shutdown(cm); 150 } 151 } 152 153 public void testIdleTimeout() throws Exception 154 { 155 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 156 pp.minSize = 0; 157 pp.maxSize = 5; 158 pp.blockingTimeout = 10; 159 pp.idleTimeout = 1000; 160 BaseConnectionManager2 cm = getCM(pp); 161 try 162 { 163 Collection mcs = new ArrayList (pp.maxSize); 164 for (int i = 0 ; i < pp.maxSize; i++) 165 mcs.add(cm.getManagedConnection(subject, cri)); 166 for (Iterator i = mcs.iterator(); i.hasNext(); ) 167 cm.returnManagedConnection((ConnectionListener)i.next(), false); 168 169 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize); 170 Thread.sleep(2500); 172 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0); 173 } 174 finally 175 { 176 shutdown(cm); 177 } 178 } 179 180 public void testPartialIdleTimeout() throws Exception 181 { 182 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 183 pp.minSize = 0; 184 pp.maxSize = 5; 185 pp.blockingTimeout = 10; 186 pp.idleTimeout = 2000; 187 BaseConnectionManager2 cm = getCM(pp); 188 try 189 { 190 Collection mcs = new ArrayList (pp.maxSize); 191 for (int i = 0 ; i < pp.maxSize; i++) 192 mcs.add(cm.getManagedConnection(subject, cri)); 193 for (Iterator i = mcs.iterator(); i.hasNext(); ) 194 cm.returnManagedConnection((ConnectionListener)i.next(), false); 195 196 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize); 197 Thread.sleep(1500); 198 ConnectionListener cl = cm.getManagedConnection(subject, cri); 199 cm.returnManagedConnection(cl, false); 200 201 Thread.sleep(1500); 203 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 1); 204 205 Thread.sleep(1500); 207 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0); 208 } 209 finally 210 { 211 shutdown(cm); 212 } 213 } 214 215 public void testFillToMin() throws Exception 216 { 217 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 218 pp.minSize = 3; 219 pp.maxSize = 5; 220 pp.blockingTimeout = 10; 221 pp.idleTimeout = 2000; 222 BaseConnectionManager2 cm = getCM(pp); 223 try 224 { 225 ConnectionListener cl = cm.getManagedConnection(subject, cri); 226 cm.returnManagedConnection(cl, false); 227 Thread.sleep(1000); 229 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.minSize); 230 Thread.sleep(3000); 232 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.minSize); 233 } 234 finally 235 { 236 shutdown(cm); 237 } 238 } 239 240 public void testPrefillPool() throws Exception { 241 242 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 243 pp.minSize = 1; 244 pp.maxSize = 5; 245 pp.blockingTimeout = 10; 246 pp.idleTimeout = 2000; 247 pp.prefill = true; 248 249 BaseConnectionManager2 cm = getCM(pp); 250 Thread.sleep(3000); 252 assertTrue("Prefilled pool: " + cm.getConnectionCount(), pp.minSize == cm.getConnectionCount()); 253 254 } 255 256 public void testNonStrictMinPool() throws Exception 257 { 258 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 259 pp.minSize = 10; 260 pp.maxSize = 15; 261 pp.blockingTimeout = 10; 262 pp.idleTimeout = 1000; 263 pp.prefill = true; 264 265 BaseConnectionManager2 cm = getCM(pp); 266 JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy(); 267 Thread.sleep(5000); 268 assertTrue("Non StrictMin pool should allow destroyed connections below minimum connections", pool.getConnectionDestroyedCount() > 0); 269 270 } 271 272 public void testStrictMinPool() throws Exception 273 { 274 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 275 pp.minSize = 10; 276 pp.maxSize = 15; 277 pp.blockingTimeout = 10; 278 pp.idleTimeout = 1000; 279 pp.prefill = true; 280 pp.stictMin = true; 281 282 BaseConnectionManager2 cm = getCM(pp); 283 JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy(); 284 Thread.sleep(3500); 286 assertTrue("StrictMin pool should not destroy below minimum connections", pool.getConnectionDestroyedCount() == 0); 287 288 } 289 290 public void testMisConfiguredFillToMin() throws Exception 291 { 292 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 293 pp.minSize = 6; 294 pp.maxSize = 5; 295 pp.blockingTimeout = 10; 296 pp.idleTimeout = 2000; 297 BaseConnectionManager2 cm = getCM(pp); 298 try 299 { 300 ConnectionListener cl = cm.getManagedConnection(subject, cri); 301 cm.returnManagedConnection(cl, false); 302 Thread.sleep(1000); 304 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize); 305 Thread.sleep(3000); 307 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize); 308 } 309 finally 310 { 311 shutdown(cm); 312 } 313 } 314 315 public void testChangedMaximum() throws Exception 316 { 317 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 318 pp.minSize = 0; 319 pp.maxSize = 5; 320 pp.blockingTimeout = 100; 321 pp.idleTimeout = 0; 322 BaseConnectionManager2 cm = getCM(pp); 323 JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy(); 324 try 325 { 326 ArrayList cs = new ArrayList (); 328 for (int i = 0; i < pp.maxSize; i++) 329 { 330 ConnectionListener cl = cm.getManagedConnection(null, null); 331 assertTrue("Got a null connection!", cl.getManagedConnection() != null); 332 cs.add(cl); 333 } 334 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize); 335 336 pp.maxSize = 6; 338 pool.flush(); 339 340 for (Iterator i = cs.iterator(); i.hasNext();) 342 { 343 cm.returnManagedConnection((ConnectionListener)i.next(), true); 344 } 345 346 cs = new ArrayList (); 348 for (int i = 0; i < pp.maxSize; i++) 349 { 350 ConnectionListener cl = cm.getManagedConnection(null, null); 351 assertTrue("Got a null connection!", cl.getManagedConnection() != null); 352 cs.add(cl); 353 } 354 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == pp.maxSize); 355 356 try 357 { 358 cm.getManagedConnection(null, null); 359 fail("Got a connection more than maxSize!"); 360 } 361 catch (ResourceException expected) 362 { 363 } 364 365 for (Iterator i = cs.iterator(); i.hasNext();) 367 cm.returnManagedConnection((ConnectionListener)i.next(), true); 368 assertTrue("Wrong number of connections counted: " + cm.getConnectionCount(), cm.getConnectionCount() == 0); 369 } 370 finally 371 { 372 shutdown(cm); 373 } 374 } 375 376 377 } | Popular Tags |