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