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