1 22 package org.jboss.test.deadlock.test; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.ejb.DuplicateKeyException ; 27 import javax.ejb.ObjectNotFoundException ; 28 29 import java.util.ArrayList ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 import java.util.Date ; 33 import java.util.Random ; 34 35 import junit.framework.Test; 36 37 import org.jboss.test.deadlock.interfaces.BeanOrder; 38 import org.jboss.test.deadlock.interfaces.EnterpriseEntityHome; 39 import org.jboss.test.deadlock.interfaces.EnterpriseEntity; 40 import org.jboss.test.deadlock.interfaces.StatelessSessionHome; 41 import org.jboss.test.deadlock.interfaces.StatelessSession; 42 import org.jboss.test.JBossTestCase; 43 import org.jboss.ejb.plugins.TxInterceptorCMT; 44 45 51 public class BeanStressTestCase 52 extends JBossTestCase 53 { 54 org.jboss.logging.Logger log = getLog(); 55 56 static boolean deployed = false; 57 static int test = 0; 58 static Date startDate = new Date (); 59 60 protected final String namingFactory = 61 System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 62 63 protected final String providerURL = 64 System.getProperty(Context.PROVIDER_URL); 65 66 public BeanStressTestCase(String name) 67 { 68 super(name); 69 } 70 71 boolean failed = false; 72 73 private StatelessSession getSession() throws Exception 74 { 75 76 StatelessSessionHome home = (StatelessSessionHome) new InitialContext ().lookup("nextgen.StatelessSession"); 77 return home.create(); 78 } 79 80 public class RunTest implements Runnable 81 { 82 public String test; 83 84 public RunTest(String test) 85 { 86 this.test = test; 87 } 88 89 public void run() 90 { 91 if (test.equals("AB")) 92 runAB(); 93 else 94 runBA(); 95 } 96 97 private void runAB() 98 { 99 log.debug("running AB"); 100 try 101 { 102 getSession().callAB(); 103 } 104 catch (Exception ex) 105 { 106 failed = true; 107 } 108 } 109 110 private void runBA() 111 { 112 log.debug("running BA"); 113 try 114 { 115 getSession().callBA(); 116 } 117 catch (Exception ex) 118 { 119 failed = true; 120 } 121 } 122 } 123 124 public void testDeadLock() 125 throws Exception 126 { 127 EnterpriseEntityHome home = (EnterpriseEntityHome) new InitialContext ().lookup("nextgenEnterpriseEntity"); 128 try 129 { 130 EnterpriseEntity A = home.findByPrimaryKey("A"); 131 } 132 catch (ObjectNotFoundException ex) 133 { 134 home.create("A"); 135 } 136 try 137 { 138 EnterpriseEntity B = home.findByPrimaryKey("B"); 139 } 140 catch (ObjectNotFoundException ex) 141 { 142 home.create("B"); 143 } 144 Thread one = new Thread (new RunTest("AB")); 145 Thread two = new Thread (new RunTest("BA")); 146 one.start(); 147 two.start(); 148 one.join(); 149 two.join(); 150 if (failed) 151 { 152 fail("testing of deadlock AB BA scenario failed"); 153 } 154 } 155 156 Random random = new Random (); 157 158 int target; 159 int iterations; 160 161 Object lock = new Object (); 162 int completed = 0; 163 164 Exception unexpected; 165 166 public class OrderTest 167 implements Runnable 168 { 169 BeanOrder beanOrder; 170 EnterpriseEntityHome home; 171 172 String toStringCached; 173 174 public OrderTest(EnterpriseEntityHome home, int beanCount, int depth) 175 { 176 ArrayList list = new ArrayList (); 178 for (int i = 0; i < depth; i++) 179 list.add(new Integer (i % beanCount).toString()); 180 181 Collections.shuffle(list, random); 183 184 beanOrder = new BeanOrder((String []) list.toArray(new String [beanCount])); 185 this.home = home; 186 } 187 188 public void run() 189 { 190 try 191 { 192 EnterpriseEntity bean = home.findByPrimaryKey(beanOrder.order[0]); 193 home = null; 194 for (int i = 0; i < iterations; i++) 195 { 196 log.debug("Before: iter=" + i + " " + this); 197 bean.callAnotherBean(beanOrder); 198 log.debug("After : iter=" + i + " " + this); 199 } 200 } 201 catch (Exception e) 202 { 203 if (TxInterceptorCMT.isADE(e) == null) 204 { 205 log.debug("Saw exception for " + this, e); 206 unexpected = e; 207 } 208 } 209 } 210 211 public String toString() 212 { 213 if (toStringCached != null) 214 return toStringCached; 215 216 StringBuffer buffer = new StringBuffer (); 217 buffer.append(" hash=").append(hashCode()); 218 buffer.append(" order=").append(Arrays.asList(beanOrder.order)); 219 220 toStringCached = buffer.toString(); 221 return toStringCached; 222 } 223 } 224 225 public class TestThread 226 extends Thread 227 { 228 OrderTest test; 229 230 public TestThread(OrderTest test) 231 { 232 super(test); 233 this.test = test; 234 } 235 236 public void run() 237 { 238 super.run(); 239 synchronized (lock) 240 { 241 completed++; 242 log.debug("Completed " + completed + " of " + target); 243 lock.notifyAll(); 244 } 245 } 246 } 247 248 public void waitForCompletion() 249 throws Exception 250 { 251 log.debug("Waiting for completion"); 252 synchronized (lock) 253 { 254 while (completed < target) 255 { 256 lock.wait(); 257 } 258 } 259 if (unexpected != null) 260 { 261 log.error("Unexpected exception", unexpected); 262 fail("Unexpected exception"); 263 } 264 } 265 266 271 public void testAllCompleteOrFail() 272 throws Exception 273 { 274 doAllCompleteOrFail("nextgenEnterpriseEntity" ,2); 275 } 276 277 282 public void testAllCompleteOrFailReentrant() 283 throws Exception 284 { 285 doAllCompleteOrFail("nextgenEnterpriseEntityReentrant", 4); 286 } 287 288 293 public void testAllCompleteOrFailNotSupported() 294 throws Exception 295 { 296 doAllCompleteOrFail("nextgenEnterpriseEntityNotSupported", 2); 297 } 298 299 304 public void testAllCompleteOrFailNotSupportedReentrant() 305 throws Exception 306 { 307 doAllCompleteOrFail("nextgenEnterpriseEntityNotSupportedReentrant", 4); 308 } 309 310 315 public void doAllCompleteOrFail(String jndiName, int depth) 316 throws Exception 317 { 318 log.debug("========= Starting " + getName()); 319 320 target = 40; 325 int beanCount = 2; 326 completed = 0; 327 unexpected = null; 328 329 EnterpriseEntityHome home = (EnterpriseEntityHome) new InitialContext ().lookup(jndiName); 331 for (int i = 0; i < beanCount; i++) 332 { 333 try 334 { 335 home.create(new Integer (i).toString()); 336 } 337 catch (DuplicateKeyException weDontCare) 338 { 339 } 340 } 341 342 TestThread[] threads = new TestThread[target]; 344 for (int i = 0; i < target; i++) 345 threads[i] = new TestThread(new OrderTest(home, beanCount, depth)); 346 347 for (int i = 0; i < target; i++) 349 { 350 log.debug("Starting " + threads[i].test); 351 threads[i].start(); 352 } 353 354 waitForCompletion(); 355 356 log.debug("========= Completed " + getName()); 357 } 358 359 public class CMRTest 360 implements Runnable 361 { 362 StatelessSession session; 363 String jndiName; 364 String start; 365 366 public CMRTest(StatelessSession session, String jndiName, String start) 367 { 368 this.session = session; 369 this.jndiName = jndiName; 370 this.start = start; 371 } 372 373 public void run() 374 { 375 try 376 { 377 session.cmrTest(jndiName, start); 378 } 379 catch (Exception e) 380 { 381 if (TxInterceptorCMT.isADE(e) == null) 382 { 383 log.debug("Saw exception for " + this, e); 384 unexpected = e; 385 } 386 } 387 } 388 389 public String toString() 390 { 391 return hashCode() + " " + start; 392 } 393 } 394 395 public class CMRTestThread 396 extends Thread 397 { 398 CMRTest test; 399 400 public CMRTestThread(CMRTest test) 401 { 402 super(test); 403 this.test = test; 404 } 405 406 public void run() 407 { 408 super.run(); 409 synchronized (lock) 410 { 411 completed++; 412 log.debug("Completed " + completed + " of " + target); 413 lock.notifyAll(); 414 } 415 } 416 } 417 418 422 public void testAllCompleteOrFailCMR() 423 throws Exception 424 { 425 doAllCompleteOrFailCMR("local/nextgenEnterpriseEntity"); 426 } 427 428 432 public void doAllCompleteOrFailCMR(String jndiName) 433 throws Exception 434 { 435 log.debug("========= Starting " + getName()); 436 437 target = 40; 442 completed = 0; 443 unexpected = null; 444 445 StatelessSessionHome home = (StatelessSessionHome) new InitialContext ().lookup("nextgen.StatelessSession"); 447 StatelessSession session = home.create(); 448 session.createCMRTestData(jndiName); 449 450 CMRTestThread[] threads = new CMRTestThread[target]; 452 for (int i = 0; i < target; i++) 453 threads[i] = new CMRTestThread(new CMRTest(session, jndiName, i % 2 == 0 ? "First" : "Second")); 454 455 for (int i = 0; i < target; i++) 457 { 458 log.debug("Starting " + threads[i].test); 459 threads[i].start(); 460 } 461 462 waitForCompletion(); 463 464 log.debug("========= Completed " + getName()); 465 } 466 467 497 498 public void testCleanup() throws Exception 499 { 500 super.restartDBPool(); 502 } 503 504 public static Test suite() throws Exception 505 { 506 return getDeploySetup(BeanStressTestCase.class, "deadlock.jar"); 507 } 508 } 509 | Popular Tags |