1 7 package org.jboss.test.cache.test.generic; 8 9 import EDU.oswego.cs.dl.util.concurrent.Sync; 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.jboss.cache.lock.ReadWriteLockWithUpgrade; 14 import java.io.*; 15 import java.util.*; 16 17 32 public class ReadWriteLockWithUpgradeUnitTestCase extends TestCase 33 { 34 static final ReadWriteLockWithUpgrade lock_ = new ReadWriteLockWithUpgrade(); 35 static long SLEEP_MSECS = 500; 36 Vector lockResult = new Vector(); 37 int NO_MORE_OP = 0; 38 int INVOKE_READ = 1; 39 int INVOKE_WRITE = 2; 40 int INVOKE_UPGRADE = 3; 41 42 public ReadWriteLockWithUpgradeUnitTestCase(String name) 43 { 44 super(name); 45 } 46 47 48 public static void main(String [] args) throws Exception 49 { 50 log("\nBeginning ReadWriteLockWithUpgrade automated testing ...\n"); 51 52 junit.textui.TestRunner.run(suite()); 53 } 54 55 public static Test suite() 57 { 58 TestSuite suite = new TestSuite(); 59 suite.addTestSuite(ReadWriteLockWithUpgradeUnitTestCase.class); 61 return suite; 62 } 63 64 public void setUp() 65 { 66 logX("\n"); 67 log("Setting up test case ..."); 68 } 69 70 public void tearDown() 71 { 72 log("Tearing down test case ..."); 73 } 74 75 public static void _sleep(long msecs) 76 { 77 try { 78 Thread.sleep(msecs); 79 } catch (InterruptedException ex) { 80 } 81 } 82 83 public static void log(String str) 84 { 85 System.out.println(Thread.currentThread() + ": " 86 + java.util.Calendar.getInstance().getTime() + " : " + str); 87 } 88 89 public static void logX(String str) 91 { 92 log(str); 93 } 98 99 public static void logToFile(String str) throws IOException 101 { 102 Writer out = new FileWriter("./RepeatableLogs.txt", true); 103 out.write(str); 104 out.close(); 105 } 106 107 108 109 110 155 156 protected Thread readThread(final String caseNum, final String name, 157 final long msecs, final long sleepSecs, 158 final String errMsg, final int secondOP) 159 { 160 return new Thread (name) 161 { 162 public void run() 163 { 164 Sync rlock = lock_.readLock(); 165 try { 166 if (! rlock.attempt(msecs)) { 167 logX(caseNum+"-"+name+" requesting read lock failed!\n"); 168 String str = caseNum + "-" + name + "-RL-0"; 169 postLockingResult(str); 170 return; 171 } 172 logX(caseNum+"-"+name+" requesting read lock succeeded!\n"); 174 String str = caseNum + "-" + name + "-RL-1"; 175 postLockingResult(str); 176 _sleep(sleepSecs); 177 178 if (secondOP == INVOKE_READ) 179 acquireReadLock(caseNum, name, msecs, errMsg); 180 else if (secondOP == INVOKE_WRITE) 181 acquireWriteLock(caseNum, name, msecs, errMsg); 182 else if (secondOP == INVOKE_UPGRADE) 183 acquireUpgradeLock(caseNum, name, msecs, errMsg); 184 185 rlock.release(); 186 logX(caseNum+"-"+name+" releasing read lock.\n"); 187 } catch (Exception ex) { 188 } 189 } 190 }; 191 } 192 193 199 protected Thread writeThread(final String caseNum, final String name, 200 final long msecs, final long sleepSecs, 201 final String errMsg, final int secondOP) 202 { 203 return new Thread (name) 204 { 205 public void run() 206 { 207 try { 208 Sync wlock = lock_.writeLock(); 209 if (! wlock.attempt(msecs)) { 210 logX(caseNum+"-"+name+" requesting write lock failed!\n"); 211 String str = caseNum + "-" + name + "-WL-0"; 212 postLockingResult(str); 213 return; 214 } 215 logX(caseNum+"-"+name+" requesting write lock succeeded!\n"); 217 String str = caseNum + "-" + name + "-WL-1"; 218 postLockingResult(str); 219 _sleep(sleepSecs); 220 221 if (secondOP == INVOKE_READ) 222 acquireReadLock(caseNum, name, msecs, errMsg); 223 else if (secondOP == INVOKE_WRITE) 224 acquireWriteLock(caseNum, name, msecs, errMsg); 225 else if (secondOP == INVOKE_UPGRADE) 226 acquireUpgradeLock(caseNum, name, msecs, errMsg); 227 228 wlock.release(); 229 logX(caseNum+"-"+name+" releasing write lock.\n"); 230 } catch (Exception ex) { 231 } 232 } 233 }; 234 } 235 236 243 protected Thread upgradeThread(final String caseNum, final String name, 244 final long msecs, final String errMsg) 245 { 246 return new Thread (name) 247 { 248 public void run() 249 { 250 try { 251 Sync rlock = lock_.readLock(); 252 Sync wlock = null; 253 if (! rlock.attempt(msecs)) { 254 logX(caseNum+"-"+name+" requesting read lock failed!\n"); 255 String str = caseNum + "-" + name + "-RL-0"; 256 postLockingResult(str); 257 return; 258 } 259 logX(caseNum+"-"+name+" requesting read lock succeeded (upgrade later)!\n"); 261 _sleep(SLEEP_MSECS/2); 262 String str = caseNum + "-" + name + "-UL-"; 263 if ((wlock = lock_.upgradeLockAttempt(msecs)) == null) 264 { 265 logX(caseNum+"-"+name+" requesting upgrade lock failed!\n"); 266 str += "0"; 267 } 268 else 269 { 270 logX(caseNum+"-"+name+" requesting upgrade lock succeeded!\n"); 271 str += "1"; 272 } 273 postLockingResult(str); 274 _sleep(SLEEP_MSECS); 276 if (wlock != null) 277 { 278 wlock.release(); 279 logX(caseNum+"-"+name+" releasing upgrade lock.\n"); 280 } 281 rlock.release(); 282 } catch (Exception ex) { 283 } 284 } 285 }; 286 } 287 288 289 290 291 297 298 protected void acquireReadLock(final String caseNum, final String name, 299 final long msecs, final String errMsg) 300 { 301 try { 302 Sync rlock = lock_.readLock(); 303 if (! rlock.attempt(msecs)) { 304 logX(caseNum+"-"+name+" requesting read lock failed!\n"); 305 String str = caseNum + "-" + name + "-RL-0"; 306 postLockingResult(str); 307 return; 308 } 309 logX(caseNum+"-"+name+" requesting read lock succeeded!\n"); 311 String str = caseNum + "-" + name + "-RL-1"; 312 postLockingResult(str); 313 _sleep(SLEEP_MSECS); 314 rlock.release(); 315 logX(caseNum+"-"+name+" releasing read lock.\n"); 316 } catch (Exception ex) { 317 } 318 } 319 320 324 protected void acquireWriteLock(final String caseNum, final String name, 325 final long msecs, final String errMsg) 326 { 327 try { 328 Sync wlock = lock_.writeLock(); 329 if (! wlock.attempt(msecs)) { 330 logX(caseNum+"-"+name+" requesting write lock failed!\n"); 331 String str = caseNum + "-" + name + "-WL-0"; 332 postLockingResult(str); 333 return; 334 } 335 logX(caseNum+"-"+name+" requesting write lock succeeded!\n"); 337 String str = caseNum + "-" + name + "-WL-1"; 338 postLockingResult(str); 339 _sleep(SLEEP_MSECS); 340 wlock.release(); 341 logX(caseNum+"-"+name+" releasing write lock.\n"); 342 } catch (Exception ex) { 343 } 344 } 345 346 350 protected void acquireUpgradeLock(final String caseNum, final String name, 351 final long msecs, final String errMsg) 352 { 353 try { 354 Sync ulock = null; 355 if ((ulock = lock_.upgradeLockAttempt(msecs)) == null) { 356 logX(caseNum+"-"+name+" requesting upgrade lock failed!\n"); 357 String str = caseNum + "-" + name + "-UL-0"; 358 postLockingResult(str); 359 return; 360 } 361 logX(caseNum+"-"+name+" requesting upgrade lock succeeded!\n"); 363 String str = caseNum + "-" + name + "-UL-1"; 364 postLockingResult(str); 365 _sleep(SLEEP_MSECS); 366 ulock.release(); 367 logX(caseNum+"-"+name+" releasing upgrade lock.\n"); 368 } catch (Exception ex) { 369 } 370 } 371 372 373 374 375 378 protected synchronized void cleanLockingResult() 379 { 380 lockResult.removeAllElements(); 381 } 382 383 386 protected synchronized void postLockingResult(Object obj) 387 { 388 logX(" Added *" + (String )obj + "* to the result vector\n"); 389 lockResult.addElement(obj); 392 } 393 394 397 protected synchronized boolean checkLockingResult(String expected) 398 { 399 boolean rc = false; 400 for (int i=0; i<lockResult.size(); i++) 401 { 402 Object ele = lockResult.elementAt(i); 403 String str = (String )ele; 404 if (expected.equals(str)) 405 { 406 rc = true; 407 break; 408 } 409 } 410 if (rc) 411 logX(" Searching for *" + expected + "* SUCCEEDED.\n"); 412 else 413 logX(" Searching for *" + expected + "* FAILED.\n"); 414 return rc; 415 } 416 417 418 419 420 421 public void testWriteWithMultipleReaders() throws Exception 422 { 423 String caseNum = "10"; 424 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS*2, 425 "1st read lock attempt failed", NO_MORE_OP); 426 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS, 427 "2nd read lock attempt failed", INVOKE_WRITE); 428 429 t1.start(); 430 t2.start(); 431 t1.join(3000); 432 t2.join(3000); 433 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 434 checkLockingResult(caseNum+"-t2-RL-1") && 435 checkLockingResult(caseNum+"-t2-WL-0")); 436 cleanLockingResult(); 437 if (t1.isAlive() || t2.isAlive()) 439 fail("Possible deadlock resulted in testRead."); 440 } 441 442 443 public void testUpgradeWithMultipleReadersOn1() throws Exception 444 { 445 String caseNum = "11"; 446 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 447 "1st read lock attempt failed", INVOKE_WRITE); 448 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS*2, 449 "2nd read lock attempt failed", NO_MORE_OP); 450 451 t1.start(); 452 t2.start(); 453 t1.join(3000); 454 t2.join(3000); 455 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 456 checkLockingResult(caseNum+"-t2-RL-1") && 457 checkLockingResult(caseNum+"-t1-WL-0")); 458 cleanLockingResult(); 459 if (t1.isAlive() || t2.isAlive()) 461 fail("Possible deadlock resulted in testRead."); 462 } 463 464 465 public void testUpgradeReadLock() throws Exception 466 { 467 String caseNum = "2"; 468 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 469 "1st read lock attempt failed", INVOKE_UPGRADE); 470 471 t1.start(); 472 t1.join(3000); 473 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 474 checkLockingResult(caseNum+"-t1-UL-1")); 475 cleanLockingResult(); 476 } 477 478 479 480 public void testReadThenWrite() throws Exception 481 { 482 String caseNum = "3"; 483 acquireReadLock(caseNum, "t1", 0, "1st read lock attempt failed"); 484 acquireWriteLock(caseNum, "t1.1", 0, "2nd write lock attempt failed"); 485 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 486 checkLockingResult(caseNum+"-t1.1-WL-1")); 487 cleanLockingResult(); 488 } 489 490 491 492 493 public void testWriteThenRead() throws Exception 494 { 495 String caseNum = "5"; 496 acquireWriteLock(caseNum, "t1", 0, "1st write lock attempt failed"); 497 acquireReadLock(caseNum, "t1.1", 0, "2nd read lock attempt failed"); 498 assertTrue(checkLockingResult(caseNum+"-t1-WL-1") && 499 checkLockingResult(caseNum+"-t1.1-RL-1")); 500 cleanLockingResult(); 501 } 502 503 504 public void testMultipleReadlock() throws Exception 505 { 506 String caseNum = "6"; 507 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 508 "1st read lock attempt failed", NO_MORE_OP); 509 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS, 510 "2nd read lock attempt failed", NO_MORE_OP); 511 512 t1.start(); 513 t2.start(); 514 t1.join(3000); 515 t2.join(3000); 516 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 517 checkLockingResult(caseNum+"-t2-RL-1")); 518 cleanLockingResult(); 519 if (t1.isAlive() || t2.isAlive()) 521 fail("Possible deadlock resulted in testRead."); 522 } 523 524 525 public void testWriteWithExistingReader() throws Exception 526 { 527 String caseNum = "8"; 528 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 529 "1st write lock attempt failed", NO_MORE_OP); 530 Thread t2=writeThread(caseNum, "t2", 0, SLEEP_MSECS, 531 "2nd read lock attempt failed", NO_MORE_OP); 532 533 t1.start(); 534 t2.start(); 535 t1.join(3000); 536 t2.join(3000); 537 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 538 checkLockingResult(caseNum+"-t2-WL-0")); 539 cleanLockingResult(); 540 if (t1.isAlive() || t2.isAlive()) 542 fail("Possible deadlock resulted in testRead."); 543 } 544 545 546 public void testReadWithExistingWriter() throws Exception 547 { 548 String caseNum = "13"; 549 Thread t1=writeThread(caseNum, "t1", 0, SLEEP_MSECS, 550 "1st write lock attempt failed", NO_MORE_OP); 551 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS, 552 "2nd read lock attempt failed", NO_MORE_OP); 553 554 t1.start(); 555 t2.start(); 556 t1.join(3000); 557 t2.join(3000); 558 assertTrue(checkLockingResult(caseNum+"-t1-WL-1") && 559 checkLockingResult(caseNum+"-t2-RL-0")); 560 cleanLockingResult(); 561 if (t1.isAlive() || t2.isAlive()) 563 fail("Possible deadlock resulted in testRead."); 564 } 565 566 567 public void testMultipleWritelocks() throws Exception 568 { 569 String caseNum = "14"; 570 Thread t1=writeThread(caseNum, "t1", 0, SLEEP_MSECS, 571 "1st write lock attempt failed", NO_MORE_OP); 572 Thread t2=writeThread(caseNum, "t2", 0, SLEEP_MSECS, 573 "2nd write lock attempt failed", NO_MORE_OP); 574 575 t1.start(); 576 t2.start(); 577 t1.join(3000); 578 t2.join(3000); 579 assertTrue(checkLockingResult(caseNum+"-t1-WL-1") && 580 checkLockingResult(caseNum+"-t2-WL-0")); 581 cleanLockingResult(); 582 if (t1.isAlive() || t2.isAlive()) 584 fail("Possible deadlock resulted in testRead."); 585 } 586 587 588 public void testUpgradeWithExistingReader() throws Exception 589 { 590 String caseNum = "7"; 591 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 592 "1st read lock attempt failed", NO_MORE_OP); 593 Thread t2=upgradeThread(caseNum, "t2", 0, 594 "2nd upgrade lock attempt failed"); 595 596 t1.start(); 597 t2.start(); 598 t1.join(3000); 599 t2.join(3000); 600 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 601 checkLockingResult(caseNum+"-t2-UL-0")); 602 cleanLockingResult(); 603 if (t1.isAlive() || t2.isAlive()) 605 fail("Possible deadlock resulted in testRead."); 606 } 607 608 609 public void testUpgradeWithMultipleReaders() throws Exception 610 { 611 String caseNum = "9"; 612 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS*2, 613 "1st read lock attempt failed", NO_MORE_OP); 614 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS, 615 "2nd read lock attempt failed", INVOKE_UPGRADE); 616 617 t1.start(); 618 t2.start(); 619 t1.join(3000); 620 t2.join(3000); 621 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 622 checkLockingResult(caseNum+"-t2-RL-1") && 623 checkLockingResult(caseNum+"-t2-UL-0")); 624 cleanLockingResult(); 625 if (t1.isAlive() || t2.isAlive()) 627 fail("Possible deadlock resulted in testRead."); 628 } 629 } 630 631 | Popular Tags |