Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 353   Methods: 26
NCLOC: 293   Classes: 4
 
 Source file Conditionals Statements Methods TOTAL
ReentrantWriterPreferenceReadWriteLockTest.java 50% 76.2% 76.9% 76%
coverage coverage
 1    package org.jboss.cache.lock;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6    import org.jboss.cache.misc.TestingUtil;
 7   
 8    import java.util.concurrent.TimeUnit;
 9    import java.util.concurrent.locks.Lock;
 10   
 11    /**
 12    * Tests ReentrantWriterPreferenceReadWriteLock
 13    *
 14    * @author Bela Ban
 15    * @version $Id: ReentrantWriterPreferenceReadWriteLockTest.java,v 1.5 2007/05/23 10:28:52 msurtani Exp $
 16    */
 17    public class ReentrantWriterPreferenceReadWriteLockTest extends TestCase
 18    {
 19    // ReentrantWriterPreferenceReadWriteLock lock;
 20    SimpleReadWriteLock lock;
 21    Lock rl, wl;
 22    Exception thread_ex = null;
 23   
 24  10 protected void setUp() throws Exception
 25    {
 26  10 super.setUp();
 27    // lock=new ReentrantWriterPreferenceReadWriteLock();
 28  10 lock = new SimpleReadWriteLock();
 29  10 rl = lock.readLock();
 30  10 wl = lock.writeLock();
 31  10 thread_ex = null;
 32    }
 33   
 34  10 protected void tearDown() throws Exception
 35    {
 36  10 super.tearDown();
 37  10 lock = null;
 38  10 if (thread_ex != null)
 39  0 throw thread_ex;
 40    }
 41   
 42  1 public void testMultipleReadLockAcquisitions() throws InterruptedException
 43    {
 44  1 rl.lock();
 45  1 rl.lock();
 46    }
 47   
 48  1 public void testInterruptedLockAcquisition()
 49    {
 50  1 Thread.currentThread().interrupt();
 51  1 try
 52    {
 53  1 rl.lockInterruptibly();
 54  0 fail("thread should be in interrupted status");
 55    }
 56    catch (InterruptedException e)
 57    {
 58    }
 59    finally
 60    {
 61  1 try
 62    {
 63  1 rl.unlock();
 64  0 fail("unlock() should throw an IllegalStateException");
 65    }
 66    catch (IllegalMonitorStateException illegalStateEx)
 67    {
 68  1 assertTrue(true);
 69    }
 70    }
 71    }
 72   
 73  1 public void testMultipleWriteLockAcquisitions() throws InterruptedException
 74    {
 75  1 wl.lock();
 76  1 wl.lock();
 77    }
 78   
 79  1 public void testMultipleReadLockReleases() throws InterruptedException
 80    {
 81  1 rl.lock();
 82  1 rl.unlock();
 83  1 try
 84    {
 85  1 rl.unlock();
 86  0 fail("we should not get here, cannot lock RL once but unlock twice");
 87    }
 88    catch (IllegalMonitorStateException illegalState)
 89    {
 90    // this is as expected
 91    }
 92    }
 93   
 94   
 95  0 public void acquireReadAndWriteLocks() throws InterruptedException
 96    {
 97  0 rl.lock();
 98  0 rl.lock();
 99  0 boolean fl = wl.tryLock(4000, TimeUnit.MILLISECONDS);
 100  0 assertTrue(fl);
 101    }
 102   
 103   
 104  0 public void acquireWriteThenReadLock() throws InterruptedException
 105    {
 106  0 wl.lock();
 107  0 rl.lock();
 108  0 wl.unlock();
 109  0 rl.unlock();
 110    }
 111   
 112  1 public void testMultipleWriteLockReleases() throws InterruptedException
 113    {
 114  1 wl.lock();
 115  1 wl.unlock();
 116  1 try
 117    {
 118  1 wl.unlock();
 119  0 fail("expected");
 120    }
 121    catch (IllegalMonitorStateException e)
 122    {
 123    }
 124    }
 125   
 126  1 public void testAcquireWriteLockAfterReadLock() throws InterruptedException
 127    {
 128  1 rl.lock();
 129  1 rl.unlock();
 130  1 wl.lock();
 131    }
 132   
 133   
 134  1 public void testAcquiringReadLockedLockWithRead() throws InterruptedException
 135    {
 136  1 new Thread()
 137    {
 138  1 public void run()
 139    {
 140  1 try
 141    {
 142  1 rl.lockInterruptibly();
 143    }
 144    catch (InterruptedException e)
 145    {
 146    }
 147    }
 148    }.start();
 149   
 150  1 TestingUtil.sleepThread(500);
 151   
 152    // now we have a RL by another thread
 153   
 154  1 boolean flag = rl.tryLock(3000, TimeUnit.MILLISECONDS);
 155  1 assertTrue(flag);
 156  1 flag = wl.tryLock(3000, TimeUnit.MILLISECONDS);
 157  1 assertFalse(flag);
 158    }
 159   
 160  1 public void testAcquiringReadLockedLock() throws InterruptedException
 161    {
 162  1 new Thread()
 163    {
 164  1 public void run()
 165    {
 166  1 try
 167    {
 168  1 rl.lockInterruptibly();
 169    }
 170    catch (InterruptedException e)
 171    {
 172    }
 173    }
 174    }.start();
 175   
 176  1 TestingUtil.sleepThread(500);
 177   
 178    // now we have a RL by another thread
 179  1 boolean flag = wl.tryLock(3000, TimeUnit.MILLISECONDS);
 180  1 assertFalse(flag);
 181    }
 182   
 183  1 public void testWriteThenReadByDifferentTx() throws InterruptedException
 184    {
 185  1 Writer writer = new Writer("Writer");
 186  1 Reader reader = new Reader("Reader");
 187  1 writer.start();
 188  1 TestingUtil.sleepThread(500);
 189  1 reader.start();
 190  1 TestingUtil.sleepThread(1000);
 191   
 192  1 synchronized (writer)
 193    {
 194  1 log("terminating Writer");
 195  1 writer.notify();
 196    }
 197  1 TestingUtil.sleepThread(500);
 198  1 synchronized (reader)
 199    {
 200  1 reader.notify();
 201    }
 202  1 writer.join();
 203  1 reader.join();
 204    }
 205   
 206  1 public void testReadThenWriteByDifferentTx() throws InterruptedException
 207    {
 208  1 Writer writer = new Writer("Writer");
 209  1 Reader reader = new Reader("Reader");
 210   
 211  1 reader.start();
 212  1 TestingUtil.sleepThread(500);
 213  1 writer.start();
 214  1 TestingUtil.sleepThread(1000);
 215   
 216  1 synchronized (reader)
 217    {
 218  1 log("terminating Reader");
 219  1 reader.notify();
 220    }
 221   
 222  1 TestingUtil.sleepThread(500);
 223  1 synchronized (writer)
 224    {
 225  1 writer.notify();
 226    }
 227  1 writer.join();
 228  1 reader.join();
 229    }
 230   
 231   
 232  18 private static void log(String msg)
 233    {
 234  18 System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() +
 235    " [" + Thread.currentThread().getName() + "]: " + msg);
 236    }
 237   
 238    class Reader extends Thread
 239    {
 240   
 241  2 public Reader(String name)
 242    {
 243  2 super(name);
 244    }
 245   
 246  2 public void run()
 247    {
 248  2 try
 249    {
 250  2 log("acquiring RL");
 251  2 rl.lock();
 252  2 log("acquired RL");
 253  2 synchronized (this)
 254    {
 255  2 this.wait();
 256    }
 257  2 log("releasing RL");
 258  2 rl.unlock();
 259  2 log("released RL");
 260    }
 261    catch (InterruptedException e)
 262    {
 263    ;
 264    }
 265    }
 266    }
 267   
 268   
 269    class Writer extends Thread
 270    {
 271   
 272  2 public Writer(String name)
 273    {
 274  2 super(name);
 275    }
 276   
 277  2 public void run()
 278    {
 279  2 try
 280    {
 281  2 log("acquiring WL");
 282  2 wl.lock();
 283  2 log("acquired WL");
 284  2 synchronized (this)
 285    {
 286  2 this.wait();
 287    }
 288  2 log("releasing WL");
 289  2 wl.unlock();
 290  2 log("released WL");
 291    }
 292    catch (InterruptedException e)
 293    {
 294    ;
 295    }
 296    }
 297    }
 298   
 299   
 300    class Upgrader extends Thread
 301    {
 302    boolean upgradeSuccessful = false;
 303   
 304  0 public Upgrader(String name)
 305    {
 306  0 super(name);
 307    }
 308   
 309  0 public boolean wasUpgradeSuccessful()
 310    {
 311  0 return upgradeSuccessful;
 312    }
 313   
 314   
 315  0 public void run()
 316    {
 317  0 try
 318    {
 319  0 log("acquiring RL");
 320  0 rl.lock();
 321  0 log("acquired RL");
 322  0 synchronized (this)
 323    {
 324  0 this.wait();
 325    }
 326  0 log("attempting to lock WL");
 327    // rl.unlock();
 328  0 wl.lock();
 329  0 upgradeSuccessful = true;
 330  0 log("acquired WL");
 331  0 log("releasing WL/RL");
 332  0 wl.unlock();
 333  0 log("released WL/RL");
 334    }
 335    catch (InterruptedException e)
 336    {
 337    ;
 338    }
 339    }
 340    }
 341   
 342   
 343  1 public static Test suite()
 344    {
 345  1 return new TestSuite(ReentrantWriterPreferenceReadWriteLockTest.class);
 346    }
 347   
 348  0 public static void main(String[] args)
 349    {
 350  0 junit.textui.TestRunner.run(suite());
 351    }
 352   
 353    }