[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/lock ...

Bela Ban bela.ban at jboss.com
Mon Dec 4 07:33:40 EST 2006


  User: bela    
  Date: 06/12/04 07:33:40

  Modified:    tests/functional/org/jboss/cache/lock 
                        ReentrantWriterPreference2Readers1WriterLockTest.java
  Log:
  ns
  
  Revision  Changes    Path
  1.5       +117 -113  JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreference2Readers1WriterLockTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: ReentrantWriterPreference2Readers1WriterLockTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/lock/ReentrantWriterPreference2Readers1WriterLockTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- ReentrantWriterPreference2Readers1WriterLockTest.java	4 Dec 2006 12:03:33 -0000	1.4
  +++ ReentrantWriterPreference2Readers1WriterLockTest.java	4 Dec 2006 12:33:40 -0000	1.5
  @@ -11,179 +11,185 @@
   import junit.framework.TestSuite;
   
   import java.util.concurrent.locks.ReentrantReadWriteLock;
  +import java.util.concurrent.TimeUnit;
   
   
   /**
    * Tests ReentrantWriterPreferenceReadWriteLock
  - *
    * @author Bela Ban
  - * @version $Id: ReentrantWriterPreference2Readers1WriterLockTest.java,v 1.4 2006/12/04 12:03:33 bela Exp $
  + * @version $Id: ReentrantWriterPreference2Readers1WriterLockTest.java,v 1.5 2006/12/04 12:33:40 bela Exp $
    */
  -public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase
  -{
  -   // ReentrantWriterPreferenceReadWriteLock lock;
  +public class ReentrantWriterPreference2Readers1WriterLockTest extends TestCase {
      ReentrantReadWriteLock lock;
      ReentrantReadWriteLock.ReadLock rl;
      ReentrantReadWriteLock.WriteLock wl;
  -   Exception thread_ex = null;
  +   Exception thread_ex=null;
   
  -   protected void setUp() throws Exception
  -   {
  +   protected void setUp() throws Exception {
         super.setUp();
  -      // lock=new ReentrantWriterPreferenceReadWriteLock();
  -      //lock=new SimpleReadWriteLock();
  -      lock = new ReentrantReadWriteLock();
  -      rl = lock.readLock();
  -      wl = lock.writeLock();
  -      thread_ex = null;
  +      lock=new ReentrantReadWriteLock();
  +      rl=lock.readLock();
  +      wl=lock.writeLock();
  +      thread_ex=null;
      }
   
  -   protected void tearDown() throws Exception
  -   {
  +   protected void tearDown() throws Exception {
         super.tearDown();
  -      lock = null;
  -      if (thread_ex != null)
  +      lock=null;
  +      if(thread_ex != null)
            throw thread_ex;
      }
   
   
  -   private static void log(String msg)
  -   {
  +   private void log(String msg) {
         System.out.println(System.currentTimeMillis() + "  " + Thread.currentThread() +
                 " [" + Thread.currentThread().getName() + "]: " + msg);
      }
   
  -   public void test2ReadersAnd1Writer() throws InterruptedException
  -   {
  -      Upgrader upgrader = new Upgrader("Upgrader");
  -      Reader reader = new Reader("Reader");
  -      upgrader.start();
  +
  +   public void testSimpleUpgradeFromReadLockToWriteLock() {
  +      int readers, writers;
  +      try {
  +         rl.lock();
  +         readers=lock.getReadLockCount();
  +         assertEquals(1, readers);
  +         boolean wl_acquired=wl.tryLock(500, TimeUnit.MILLISECONDS);
  +         if(!wl_acquired) {
  +            fail("write lock could not be acquired");
  +            return;
  +         }
  +         readers=lock.getReadLockCount();
  +         assertEquals(1, readers);
  +         writers=lock.getWriteHoldCount();
  +         assertEquals(1, writers);
  +      }
  +      catch(InterruptedException e) {
  +
  +      }
  +      finally {
  +         rl.unlock();
  +         if(lock.getWriteHoldCount() > 0)
  +            wl.unlock();
  +      }
  +   }
  +
  +   public void test2ReadersAnd1Writer() throws InterruptedException {
  +      int readers, writers;
  +      Upgrader upgrader=new Upgrader("Upgrader");
  +      Reader reader=new Reader("Reader");
         reader.start();
  +      sleepThread(500);
  +
  +      readers=lock.getReadLockCount();
  +      assertEquals(1, readers);
   
  +      upgrader.start();
         sleepThread(500);
  -      synchronized (upgrader)
  -      {
  +
  +      readers=lock.getReadLockCount();
  +      assertEquals(2, readers);
  +
  +      synchronized(upgrader) { // writer upgrades from RL to WL, this should fail
            upgrader.notify();
         }
         sleepThread(500);
  -      synchronized (reader)
  -      {
  +
  +      readers=lock.getReadLockCount();
  +      assertEquals(2, readers);
  +      writers=lock.getWriteHoldCount();
  +      assertEquals(0, writers);
  +
  +      synchronized(reader) { // reader unlocks its RL, now writer should be able to upgrade to a WL
            reader.notify();
         }
         reader.join();
  -      upgrader.join(3000);
  -      assertTrue("Known failure. See JBCACHE-461; This is due to a bug in ReentrantWriterPreferenceReadWriteLock !", upgrader.wasUpgradeSuccessful());
  -   }
   
  +      readers=lock.getReadLockCount();
  +      assertEquals(1, readers);
  +      writers=lock.getWriteHoldCount();
  +      assertEquals(1, writers);
   
  -   class Reader extends Thread
  -   {
  -
  -      public Reader(String name)
  -      {
  -         super(name);
  +       synchronized(upgrader) { // writer releases WL
  +         upgrader.notify();
         }
  +      sleepThread(500);
  +      readers=lock.getReadLockCount();
  +      assertEquals(0, readers);
  +      writers=lock.getWriteHoldCount();
  +      assertEquals(0, writers);
   
  -      public void run()
  -      {
  -         try
  -         {
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("acquiring RL");
  -            rl.tryLock();
  -            //rl.acquire();
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("acquired RL");
  -            synchronized (this)
  -            {
  -               this.wait();
  -            }
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("releasing RL");
  -            rl.unlock();
  -            //rl.release();
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("released RL");
  -         }
  -         catch (InterruptedException e)
  -         {
  -            ;
  -         }
  -      }
  +      upgrader.join(3000);
  +      assertTrue("Known failure. See JBCACHE-461; This is due to a potential bug in ReentrantWriterPreferenceReadWriteLock !",
  +                 upgrader.wasUpgradeSuccessful());
      }
   
   
  -   class Writer extends Thread
  -   {
  +   private class Reader extends Thread {
   
  -      public Writer(String name)
  -      {
  +      public Reader(String name) {
            super(name);
         }
   
  -      public void run()
  -      {
  -         try
  -         {
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("acquiring WL");
  -            //wl.acquire();
  -            wl.tryLock();
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("acquired WL");
  -            synchronized (this)
  -            {
  +      public void run() {
  +         try {
  +            log("acquiring RL");
  +            rl.lock();
  +            log("acquired RL");
  +            synchronized(this) {
                  this.wait();
               }
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("releasing WL");
  -            //wl.release();
  -            wl.unlock();
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("released WL");
            }
  -         catch (InterruptedException e)
  -         {
  -            ;
  +         catch(InterruptedException e) {
  +         }
  +         finally {
  +            log("releasing RL");
  +            rl.unlock();
  +            log("released RL");
            }
         }
      }
   
   
  -   class Upgrader extends Thread
  -   {
  -      boolean upgradeSuccessful = false;
  +   private class Upgrader extends Thread {
  +      boolean upgradeSuccessful=false;
   
  -      public Upgrader(String name)
  -      {
  +      public Upgrader(String name) {
            super(name);
         }
   
  -      public boolean wasUpgradeSuccessful()
  -      {
  +      public boolean wasUpgradeSuccessful() {
            return upgradeSuccessful;
         }
   
   
  -      public void run()
  -      {
  -         try
  -         {
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("acquiring RL");
  -            //rl.acquire();
  -            rl.tryLock();
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("acquired RL");
  -            synchronized (this)
  -            {
  +      public void run() {
  +         try {
  +            log("acquiring RL");
  +            rl.lock();
  +            log("acquired RL");
  +            synchronized(this) {
  +               this.wait();
  +            }
  +            log("attempting to acquire WL");
  +            wl.lock();
  +            upgradeSuccessful=true;
  +            log("acquired WL");
  +
  +
  +            synchronized(this) {
                  this.wait();
               }
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("attempting to acquire WL");
  -            // rl.release();
  -            //wl.acquire();
  -            wl.tryLock();
  -            upgradeSuccessful = true;
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("acquired WL");
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("releasing WL/RL");
  -            //wl.release();
  +            log("releasing WL");
               rl.unlock();
  -            ReentrantWriterPreference2Readers1WriterLockTest.log("released WL/RL");
  +            log("released WL");
            }
  -         catch (InterruptedException e)
  -         {
  +         catch(InterruptedException e) {
               ;
            }
  +         finally {
  +            wl.unlock();
  +            rl.unlock();
  +         }
         }
      }
   
  @@ -196,13 +202,11 @@
         }
      }
   
  -   public static Test suite()
  -   {
  +   public static Test suite() {
         return new TestSuite(ReentrantWriterPreference2Readers1WriterLockTest.class);
      }
   
  -   public static void main(String[] args)
  -   {
  +   public static void main(String[] args) {
         junit.textui.TestRunner.run(ReentrantWriterPreference2Readers1WriterLockTest.suite());
      }
   
  
  
  



More information about the jboss-cvs-commits mailing list