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