1 7 package org.jboss.cache.lock; 8 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.GlobalTransaction; 16 import org.jboss.cache.NodeSPI; 17 import org.jboss.cache.misc.TestingUtil; 18 19 20 27 public class IdentityLockTest extends TestCase 28 { 29 NodeLock lock_; 30 Object other_ = new Object (); 31 Log logger_ = LogFactory.getLog(IdentityLockTest.class); 32 static Throwable thread_ex = null; 33 final NodeSPI NODE = null; 34 35 36 public IdentityLockTest(String name) 37 { 38 super(name); 39 } 40 41 protected void setUp() throws Exception 42 { 43 super.setUp(); 44 lock_ = new IdentityLock(NODE); 45 46 } 49 50 protected void tearDown() throws Exception 51 { 52 super.tearDown(); 53 lock_.releaseAll(); 54 lock_ = null; 55 thread_ex = null; 56 } 57 58 protected void setLevelRW() 59 { 60 log("set lock level to RWUpgrade ..."); 61 LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 62 lock_ = new IdentityLock(NODE); 63 } 64 65 protected void setLevelSerial() 66 { 67 log("set lock level to SimpleLock ..."); 68 LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE); 69 lock_ = new IdentityLock(NODE); 70 } 71 72 protected GlobalTransaction getGlobalTransactionFromThread() 73 { 74 return GlobalTransaction.create(null); 75 } 76 77 public void testNullOwner_RWLock() throws InterruptedException 78 { 79 setLevelRW(); 80 nullOwner(); 81 } 82 83 public void testNullOwner_SimpleLock() throws InterruptedException 84 { 85 setLevelSerial(); 86 nullOwner(); 87 } 88 89 protected void nullOwner() throws InterruptedException 90 { 91 log("testNullOwner ..."); 92 try 93 { 94 GlobalTransaction gtx = getGlobalTransactionFromThread(); 95 lock_.acquireWriteLock(gtx, 50); 96 lock_.release(gtx); 97 98 lock_.acquireReadLock(gtx, 50); 99 lock_.release(gtx); 100 } 101 catch (LockingException e) 102 { 103 fail(e.toString()); 104 } 105 catch (TimeoutException e) 106 { 107 fail(e.toString()); 108 } 109 } 110 111 public void testNullOwner2_RWLock() throws InterruptedException 112 { 113 setLevelRW(); 114 nullOwner2(); 115 } 116 117 public void testNullOwner2_SimpleLock() throws InterruptedException 118 { 119 setLevelSerial(); 120 nullOwner2(); 121 } 122 123 protected void nullOwner2() throws InterruptedException 124 { 125 log("testNullOwner2 ..."); 126 try 127 { 128 GlobalTransaction gtx = getGlobalTransactionFromThread(); 129 lock_.acquireReadLock(gtx, 50); 130 lock_.acquireWriteLock(gtx, 50); lock_.release(gtx); 132 } 133 catch (LockingException e) 134 { 135 fail(e.toString()); 136 } 137 catch (TimeoutException e2) 138 { 139 fail(e2.toString()); 140 } 141 } 142 143 public void testNullOwner3_RWLock() throws InterruptedException 144 { 145 setLevelRW(); 146 nullOwner3(); 147 } 148 149 public void testNullOwner3_SimpleLock() throws InterruptedException 150 { 151 setLevelSerial(); 152 nullOwner3(); 153 } 154 155 public void nullOwner3() throws InterruptedException 156 { 157 log("testNullOwner3 ..."); 158 try 159 { 160 GlobalTransaction gtx = getGlobalTransactionFromThread(); 161 lock_.acquireWriteLock(gtx, 50); 162 lock_.acquireReadLock(gtx, 50); lock_.release(gtx); 164 } 165 catch (LockingException e) 166 { 167 fail(e.toString()); 168 } 169 catch (TimeoutException e2) 170 { 171 fail(e2.toString()); 172 } 173 } 174 175 public void testAcquireAndRelease_RWLock() throws InterruptedException 176 { 177 setLevelRW(); 178 acquireAndRelease(); 179 } 180 181 public void testAcquireAndRelease_SimpleLock() throws InterruptedException 182 { 183 setLevelSerial(); 184 acquireAndRelease(); 185 } 186 187 public void acquireAndRelease() throws InterruptedException 188 { 189 log("testAcquireAndRelease ..."); 190 try 191 { 192 lock_.acquireReadLock(this, 50); 193 assertTrue("Is the lock owner", lock_.isOwner(this)); 194 assertTrue(lock_.getReaderOwners().contains(this)); 195 196 lock_.acquireReadLock(this, 50); assertTrue("Is the lock owner", lock_.isOwner(this)); 198 assertTrue(lock_.getReaderOwners().contains(this)); 199 200 lock_.acquireWriteLock(this, 50); assertTrue("Is the lock owner", lock_.isOwner(this)); 202 assertTrue(!lock_.getReaderOwners().contains(this)); 203 assertTrue(lock_.getWriterOwner().equals(this)); 204 205 lock_.release(this); 206 assertTrue(!lock_.isOwner(this)); 207 } 208 catch (LockingException e) 209 { 210 fail(e.toString()); 211 } 212 catch (TimeoutException e2) 213 { 214 fail(e2.toString()); 215 } 216 } 217 218 public void acquireAndRelease2_RWLock() throws InterruptedException 219 { 220 setLevelRW(); 221 log("testAcquireAndRelease2 ..."); 222 try 223 { 224 lock_.acquireReadLock(this, 10); 225 } 226 catch (LockingException e) 227 { 228 fail(e.toString()); 229 } 230 catch (TimeoutException e2) 231 { 232 fail(e2.toString()); 233 } 234 235 try 236 { 237 lock_.acquireReadLock(other_, 10); } 239 catch (LockingException e) 240 { 241 fail(e.toString()); 242 } 243 catch (TimeoutException e2) 244 { 245 fail(e2.toString()); 246 } 247 248 try 249 { 250 lock_.acquireWriteLock(other_, 50); } 252 catch (LockingException e) 253 { 254 fail(e.toString()); 255 } 256 catch (TimeoutException e2) 257 { 258 assertTrue(true); 259 } 260 261 lock_.release(this); 262 263 try 264 { 265 lock_.acquireWriteLock(other_, 10); } 267 catch (LockingException e) 268 { 269 fail(e.toString()); 270 } 271 catch (TimeoutException e2) 272 { 273 fail(e2.toString()); 274 } 275 276 lock_.releaseAll(); 277 } 278 279 280 public void acquireAndRelease2_SimpleLock() throws InterruptedException 281 { 282 setLevelSerial(); 283 log("testAcquireAndRelease2 ..."); 284 try 285 { 286 lock_.acquireReadLock(this, 10); 287 } 288 catch (LockingException e) 289 { 290 fail(e.toString()); 291 } 292 catch (TimeoutException e2) 293 { 294 fail(e2.toString()); 295 } 296 297 try 298 { 299 lock_.acquireReadLock(other_, 10); fail("Acquire read lock for other. Should fail."); 301 } 302 catch (LockingException e) 303 { 304 fail(e.toString()); 305 } 306 catch (TimeoutException e2) 307 { 308 assertTrue(true); 310 } 311 312 try 313 { 314 lock_.acquireWriteLock(other_, 50); fail("Acquire read lock for other. Should fail."); 316 } 317 catch (LockingException e) 318 { 319 fail(e.toString()); 320 } 321 catch (TimeoutException e2) 322 { 323 assertTrue(true); 324 } 325 326 lock_.release(this); 327 328 try 329 { 330 lock_.acquireWriteLock(other_, 10); } 332 catch (LockingException e) 333 { 334 fail(e.toString()); 335 } 336 catch (TimeoutException e2) 337 { 338 fail(e2.toString()); 339 } 340 341 lock_.releaseAll(); 342 } 343 344 public void testThreadedAccess_RWLock() throws Throwable 345 { 346 setLevelRW(); 347 log("testThreadedAccess_RWLock ..."); 348 final Object o1 = new Object (); 349 final Object o2 = new Object (); 350 351 System.out.println(""); 352 Thread t1 = new Thread () 354 { 355 public void run() 356 { 357 try 358 { 359 log("o1 acquiring lock"); 360 lock_.acquireReadLock(o1, 50); 361 log("o1: OK"); 362 } 363 catch (Throwable e) 364 { 365 log("o1: FAIL"); 366 thread_ex = e; 367 } 368 } 369 }; 370 371 Thread t2 = new Thread () 373 { 374 public void run() 375 { 376 try 377 { 378 log("o2 acquiring lock"); 379 lock_.acquireWriteLock(o2, 2000); 380 log("o2: OK"); 381 } 382 catch (Throwable e) 383 { 384 log("o2: FAIL"); 385 thread_ex = e; 386 } 387 } 388 }; 389 390 Thread t3 = new Thread () 392 { 393 public void run() 394 { 395 try 396 { 397 log("o1 acquiring lock"); 398 lock_.acquireWriteLock(o1, 10); 399 log("o1: OK"); 400 } 401 catch (Throwable e) 402 { 403 log("o1: FAIL"); 404 thread_ex = e; 405 } 406 } 407 }; 408 409 t1.start(); 410 t2.start(); 411 TestingUtil.sleepThread(1000); 412 413 assertTrue(lock_.isOwner(o1)); 415 TestingUtil.sleepThread(100); 416 assertTrue(lock_.isOwner(o1)); 418 419 t3.start(); 420 TestingUtil.sleepThread(100); 421 assertTrue(lock_.isOwner(o1)); 423 424 log("o1 releasing lock"); 426 lock_.release(o1); 427 log("o1: OK"); 428 429 TestingUtil.sleepThread(200); 430 434 t1.join(20000); 435 t2.join(20000); 436 t3.join(20000); 437 if (thread_ex != null) 438 { 439 throw thread_ex; 440 } 441 } 442 443 444 public void testThreadedAccess_SimpleLock() throws Throwable 445 { 446 setLevelSerial(); 447 log("testThreadedAccess_SimpleLock() ..."); 448 final Object o1 = new Object (); 449 final Object o2 = new Object (); 450 451 System.out.println(""); 452 Thread t1 = new Thread () 454 { 455 public void run() 456 { 457 try 458 { 459 log("o1 acquiring lock"); 460 lock_.acquireReadLock(o1, 50); 461 log("o1: OK"); 462 } 463 catch (Throwable e) 464 { 465 log("o1: FAIL"); 466 thread_ex = e; 467 } 468 } 469 }; 470 471 Thread t2 = new Thread () 473 { 474 public void run() 475 { 476 try 477 { 478 log("o2 acquiring lock"); 479 lock_.acquireWriteLock(o2, 2000); 480 log("o2: OK"); 481 } 482 catch (Throwable e) 483 { 484 log("o2: FAIL"); 485 thread_ex = e; 486 } 487 } 488 }; 489 490 Thread t3 = new Thread () 492 { 493 public void run() 494 { 495 try 496 { 497 log("o1 acquiring lock"); 498 lock_.acquireWriteLock(o1, 10); 499 log("o1: OK"); 500 } 501 catch (Throwable e) 502 { 503 log("o1: FAIL"); 504 thread_ex = e; 505 } 506 } 507 }; 508 509 t1.start(); 510 t2.start(); 511 TestingUtil.sleepThread(1000); 512 513 assertTrue(lock_.isOwner(o1)); 515 TestingUtil.sleepThread(100); 516 assertTrue(lock_.isOwner(o1)); 518 519 t3.start(); 520 TestingUtil.sleepThread(100); 521 assertTrue(lock_.isOwner(o1)); 523 524 log("o1 releasing lock"); 526 lock_.release(o1); 527 log("o1: OK"); 528 529 TestingUtil.sleepThread(200); 530 534 t1.join(20000); 535 t2.join(20000); 536 t3.join(20000); 537 if (thread_ex != null) 538 { 539 throw thread_ex; 540 } 541 } 542 543 public void testReadAndReleaseAll() 544 { 545 setLevelRW(); 546 log("testReadAndReleaseAll() ..."); 547 final Object o1 = new Object (); 548 final Object o2 = new Object (); 549 System.out.println(""); 550 551 try 553 { 554 log("o1: acquiring"); 555 lock_.acquireReadLock(o1, 50); 556 log("o1: OK"); 557 log("o2: acquiring"); 558 lock_.acquireReadLock(o2, 50); 559 log("o2: OK"); 560 } 561 catch (Throwable t) 562 { 563 log("read lock: FAIL"); 564 fail(t.getMessage()); 565 } 566 567 Thread t1 = new Thread () 568 { 569 public void run() 570 { 571 try 572 { 573 log("calling releaseAll()"); 574 lock_.releaseAll(); 575 log("releaseAll(): OK"); 576 } 577 catch (Throwable e) 578 { 579 log("releaseAll(): FAIL"); 580 thread_ex = e; 581 } 582 } 583 }; 584 585 try 586 { 587 t1.setDaemon(true); 588 t1.start(); 589 TestingUtil.sleepThread(1000); 590 591 assertFalse("Lock map cleared", lock_.isReadLocked()); 592 } 593 finally 594 { 595 lock_.release(o1); 598 lock_.release(o2); 599 } 600 } 601 602 public void testWriteAndReleaseAll() 603 { 604 setLevelSerial(); 605 log("testWriteAndReleaseAll() ..."); 606 final Object o1 = new Object (); 607 System.out.println(""); 608 609 try 611 { 612 log("o1: acquiring"); 613 lock_.acquireWriteLock(o1, 50); 614 log("o1: OK"); 615 } 616 catch (Throwable t) 617 { 618 log("write lock: FAIL"); 619 fail(t.getMessage()); 620 } 621 622 Thread t1 = new Thread () 623 { 624 public void run() 625 { 626 try 627 { 628 log("calling releaseAll()"); 629 lock_.releaseAll(); 630 log("releaseAll(): OK"); 631 } 632 catch (Throwable e) 633 { 634 log("releaseAll(): FAIL"); 635 thread_ex = e; 636 } 637 } 638 }; 639 640 try 641 { 642 t1.setDaemon(true); 643 t1.start(); 644 TestingUtil.sleepThread(1000); 645 646 assertFalse("Lock map cleared", lock_.isReadLocked()); 647 } 648 finally 649 { 650 lock_.release(o1); 653 } 654 } 655 656 void log(String msg) 657 { 658 logger_.info("-- [" + Thread.currentThread() + "]: " + msg); 660 } 661 662 public static Test suite() 663 { 664 TestSuite s = new TestSuite(IdentityLockTest.class); 665 return s; 666 } 667 668 public static void main(String [] args) 669 { 670 junit.textui.TestRunner.run(suite()); 671 } 672 673 } 674 | Popular Tags |