Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 247   Methods: 13
NCLOC: 198   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
ReentrantWriterPreference2Readers1WriterLockTest.java 50% 71% 84.6% 71.4%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.lock;
 8   
 9    import junit.framework.Test;
 10    import junit.framework.TestCase;
 11    import junit.framework.TestSuite;
 12   
 13    import java.util.concurrent.TimeUnit;
 14    import java.util.concurrent.locks.ReentrantReadWriteLock;
 15   
 16    /**
 17    * Tests ReentrantWriterPreferenceReadWriteLock
 18    *
 19    * @author Bela Ban
 20    * @version $Id: ReentrantWriterPreference2Readers1WriterLockTest.java,v 1.7 2007/05/23 10:28:52 msurtani Exp $
 21    */
 22    public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase
 23    {
 24    ReentrantReadWriteLock lock;
 25    ReentrantReadWriteLock.ReadLock rl;
 26    ReentrantReadWriteLock.WriteLock wl;
 27    Exception thread_ex = null;
 28   
 29  2 protected void setUp() throws Exception
 30    {
 31  2 super.setUp();
 32  2 lock = new ReentrantReadWriteLock();
 33  2 rl = lock.readLock();
 34  2 wl = lock.writeLock();
 35  2 thread_ex = null;
 36    }
 37   
 38  2 protected void tearDown() throws Exception
 39    {
 40  2 super.tearDown();
 41  2 lock = null;
 42  2 if (thread_ex != null)
 43  0 throw thread_ex;
 44    }
 45   
 46   
 47  7 private void log(String msg)
 48    {
 49  7 System.out.println(System.currentTimeMillis() + " " + Thread.currentThread() +
 50    " [" + Thread.currentThread().getName() + "]: " + msg);
 51    }
 52   
 53   
 54  1 public void testSimpleUpgradeFromReadLockToWriteLock()
 55    {
 56  1 int readers, writers;
 57  1 try
 58    {
 59  1 rl.lock();
 60  1 readers = lock.getReadLockCount();
 61  1 assertEquals(1, readers);
 62  1 boolean wl_acquired = wl.tryLock(500, TimeUnit.MILLISECONDS);
 63  1 if (!wl_acquired)
 64    {
 65  1 fail("write lock could not be acquired");
 66  0 return;
 67    }
 68  0 readers = lock.getReadLockCount();
 69  0 assertEquals(1, readers);
 70  0 writers = lock.getWriteHoldCount();
 71  0 assertEquals(1, writers);
 72    }
 73    catch (InterruptedException e)
 74    {
 75   
 76    }
 77    finally
 78    {
 79  1 rl.unlock();
 80  1 if (lock.getWriteHoldCount() > 0)
 81  0 wl.unlock();
 82    }
 83    }
 84   
 85  1 public void test2ReadersAnd1Writer() throws InterruptedException
 86    {
 87  1 int readers, writers;
 88  1 Upgrader upgrader = new Upgrader("Upgrader");
 89  1 Reader reader = new Reader("Reader");
 90  1 reader.start();
 91  1 sleepThread(500);
 92   
 93  1 readers = lock.getReadLockCount();
 94  1 assertEquals(1, readers);
 95   
 96  1 upgrader.start();
 97  1 sleepThread(500);
 98   
 99  1 readers = lock.getReadLockCount();
 100  1 assertEquals(2, readers);
 101   
 102  1 synchronized (upgrader)
 103    { // writer upgrades from RL to WL, this should fail
 104  1 upgrader.notify();
 105    }
 106  1 sleepThread(500);
 107   
 108  1 readers = lock.getReadLockCount();
 109  1 assertEquals(2, readers);
 110  1 writers = lock.getWriteHoldCount();
 111  1 assertEquals(0, writers);
 112   
 113  1 synchronized (reader)
 114    { // reader unlocks its RL, now writer should be able to upgrade to a WL
 115  1 reader.notify();
 116    }
 117  1 reader.join();
 118   
 119  1 readers = lock.getReadLockCount();
 120  1 assertEquals(1, readers);
 121  1 writers = lock.getWriteHoldCount();
 122  1 assertEquals(1, writers);
 123   
 124  0 synchronized (upgrader)
 125    { // writer releases WL
 126  0 upgrader.notify();
 127    }
 128  0 sleepThread(500);
 129  0 readers = lock.getReadLockCount();
 130  0 assertEquals(0, readers);
 131  0 writers = lock.getWriteHoldCount();
 132  0 assertEquals(0, writers);
 133   
 134  0 upgrader.join(3000);
 135  0 assertTrue("Known failure. See JBCACHE-461; This is due to a potential bug in ReentrantWriterPreferenceReadWriteLock !",
 136    upgrader.wasUpgradeSuccessful());
 137    }
 138   
 139   
 140    private class Reader extends Thread
 141    {
 142   
 143  1 public Reader(String name)
 144    {
 145  1 super(name);
 146    }
 147   
 148  1 public void run()
 149    {
 150  1 try
 151    {
 152  1 log("acquiring RL");
 153  1 rl.lock();
 154  1 log("acquired RL");
 155  1 synchronized (this)
 156    {
 157  1 this.wait();
 158    }
 159    }
 160    catch (InterruptedException e)
 161    {
 162    }
 163    finally
 164    {
 165  1 log("releasing RL");
 166  1 rl.unlock();
 167  1 log("released RL");
 168    }
 169    }
 170    }
 171   
 172   
 173    private class Upgrader extends Thread
 174    {
 175    boolean upgradeSuccessful = false;
 176   
 177  1 public Upgrader(String name)
 178    {
 179  1 super(name);
 180    }
 181   
 182  0 public boolean wasUpgradeSuccessful()
 183    {
 184  0 return upgradeSuccessful;
 185    }
 186   
 187   
 188  1 public void run()
 189    {
 190  1 try
 191    {
 192  1 log("acquiring RL");
 193  1 rl.lock();
 194  1 log("acquired RL");
 195  1 synchronized (this)
 196    {
 197  1 this.wait();
 198    }
 199  1 log("attempting to acquire WL");
 200  1 wl.lock();
 201  0 upgradeSuccessful = true;
 202  0 log("acquired WL");
 203   
 204   
 205  0 synchronized (this)
 206    {
 207  0 this.wait();
 208    }
 209  0 log("releasing WL");
 210  0 rl.unlock();
 211  0 log("released WL");
 212    }
 213    catch (InterruptedException e)
 214    {
 215    ;
 216    }
 217    finally
 218    {
 219  0 wl.unlock();
 220  0 rl.unlock();
 221    }
 222    }
 223    }
 224   
 225   
 226  3 static void sleepThread(long timeout)
 227    {
 228  3 try
 229    {
 230  3 Thread.sleep(timeout);
 231    }
 232    catch (InterruptedException e)
 233    {
 234    }
 235    }
 236   
 237  1 public static Test suite()
 238    {
 239  1 return new TestSuite(ReentrantWriterPreference2Readers1WriterLockTest.class);
 240    }
 241   
 242  0 public static void main(String[] args)
 243    {
 244  0 junit.textui.TestRunner.run(ReentrantWriterPreference2Readers1WriterLockTest.suite());
 245    }
 246   
 247    }