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.NonBlockingWriterLock; 14 import java.io.*; 15 import java.util.*; 16 17 32 public class NonBlockingWriterLockUnitTestCase extends TestCase 33 { 34 static final NonBlockingWriterLock lock_ = new NonBlockingWriterLock(); 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 NonBlockingWriterLockUnitTestCase(String name) 43 { 44 super(name); 45 } 46 47 48 public static void main(String [] args) throws Exception 49 { 50 log("\nBeginning NonBlockingWriterLock 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(NonBlockingWriterLockUnitTestCase.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("./ReadCommittedLog.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-1")); 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-1")); 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 505 public void testMultipleReadlock() throws Exception 506 { 507 String caseNum = "6"; 508 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 509 "1st read lock attempt failed", NO_MORE_OP); 510 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS, 511 "2nd read lock attempt failed", NO_MORE_OP); 512 513 t1.start(); 514 t2.start(); 515 t1.join(3000); 516 t2.join(3000); 517 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 518 checkLockingResult(caseNum+"-t2-RL-1")); 519 cleanLockingResult(); 520 if (t1.isAlive() || t2.isAlive()) 522 fail("Possible deadlock resulted in testRead."); 523 } 524 525 526 public void testWriteWithExistingReader() throws Exception 527 { 528 String caseNum = "8"; 529 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 530 "1st write lock attempt failed", NO_MORE_OP); 531 Thread t2=writeThread(caseNum, "t2", 0, SLEEP_MSECS, 532 "2nd read lock attempt failed", NO_MORE_OP); 533 534 t1.start(); 535 t2.start(); 536 t1.join(3000); 537 t2.join(3000); 538 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 539 checkLockingResult(caseNum+"-t2-WL-1")); 540 cleanLockingResult(); 541 if (t1.isAlive() || t2.isAlive()) 543 fail("Possible deadlock resulted in testRead."); 544 } 545 546 547 public void testReadWithExistingWriter() throws Exception 548 { 549 String caseNum = "13"; 550 Thread t1=writeThread(caseNum, "t1", 0, SLEEP_MSECS, 551 "1st write lock attempt failed", NO_MORE_OP); 552 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS, 553 "2nd read lock attempt failed", NO_MORE_OP); 554 555 t1.start(); 556 t2.start(); 557 t1.join(3000); 558 t2.join(3000); 559 assertTrue(checkLockingResult(caseNum+"-t1-WL-1") && 560 checkLockingResult(caseNum+"-t2-RL-0")); 561 cleanLockingResult(); 562 if (t1.isAlive() || t2.isAlive()) 564 fail("Possible deadlock resulted in testRead."); 565 } 566 567 568 public void testMultipleWritelocks() throws Exception 569 { 570 String caseNum = "14"; 571 Thread t1=writeThread(caseNum, "t1", 0, SLEEP_MSECS, 572 "1st write lock attempt failed", NO_MORE_OP); 573 Thread t2=writeThread(caseNum, "t2", 0, SLEEP_MSECS, 574 "2nd write lock attempt failed", NO_MORE_OP); 575 576 t1.start(); 577 t2.start(); 578 t1.join(3000); 579 t2.join(3000); 580 assertTrue(checkLockingResult(caseNum+"-t1-WL-1") && 581 checkLockingResult(caseNum+"-t2-WL-0")); 582 cleanLockingResult(); 583 if (t1.isAlive() || t2.isAlive()) 585 fail("Possible deadlock resulted in testRead."); 586 } 587 588 589 public void testUpgradeWithExistingReader() throws Exception 590 { 591 String caseNum = "7"; 592 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS, 593 "1st read lock attempt failed", NO_MORE_OP); 594 Thread t2=upgradeThread(caseNum, "t2", 0, 595 "2nd upgrade lock attempt failed"); 596 597 t1.start(); 598 t2.start(); 599 t1.join(3000); 600 t2.join(3000); 601 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 602 checkLockingResult(caseNum+"-t2-UL-1")); 603 cleanLockingResult(); 604 if (t1.isAlive() || t2.isAlive()) 606 fail("Possible deadlock resulted in testRead."); 607 } 608 609 610 public void testUpgradeWithMultipleReaders() throws Exception 611 { 612 String caseNum = "9"; 613 Thread t1=readThread(caseNum, "t1", 0, SLEEP_MSECS*2, 614 "1st read lock attempt failed", NO_MORE_OP); 615 Thread t2=readThread(caseNum, "t2", 0, SLEEP_MSECS, 616 "2nd read lock attempt failed", INVOKE_UPGRADE); 617 618 t1.start(); 619 t2.start(); 620 t1.join(3000); 621 t2.join(3000); 622 assertTrue(checkLockingResult(caseNum+"-t1-RL-1") && 623 checkLockingResult(caseNum+"-t2-RL-1") && 624 checkLockingResult(caseNum+"-t2-UL-1")); 625 cleanLockingResult(); 626 if (t1.isAlive() || t2.isAlive()) 628 fail("Possible deadlock resulted in testRead."); 629 } 630 } 631 632 | Popular Tags |