[jbosscache-commits] JBoss Cache SVN: r6016 - core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Jun 24 13:29:04 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-06-24 13:29:04 -0400 (Tue, 24 Jun 2008)
New Revision: 6016

Modified:
   core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java
Log:
Added unit test for OwnableReentrantLock

Modified: core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java	2008-06-24 16:59:43 UTC (rev 6015)
+++ core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java	2008-06-24 17:29:04 UTC (rev 6016)
@@ -4,6 +4,11 @@
 import org.jboss.cache.transaction.GlobalTransaction;
 import org.testng.annotations.Test;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
@@ -19,7 +24,7 @@
       assert lock.getOwner().equals(Thread.currentThread());
       assert lock.getHoldCount() == 1;
 
-      lock.lock();
+      assert lock.tryLock();
       assert lock.getOwner().equals(Thread.currentThread());
       assert lock.getHoldCount() == 2;
 
@@ -98,7 +103,9 @@
       InvocationContextContainer icc = new InvocationContextContainer();
       final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
       final AtomicBoolean acquired = new AtomicBoolean(false);
+      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
 
+
       lock.lock();
       assert lock.getOwner().equals(Thread.currentThread());
       assert lock.getHoldCount() == 1;
@@ -115,6 +122,16 @@
             {
                // do nothing
             }
+
+            try
+            {
+               lock.unlock();
+            }
+            catch (IllegalMonitorStateException e)
+            {
+               // expected
+               threwExceptionOnRelease.set(true);
+            }
          }
       };
 
@@ -122,6 +139,7 @@
       t.join();
 
       assert !acquired.get() : "Second thread should not have acquired lock";
+      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
 
       lock.unlock();
       assert !lock.isLocked();
@@ -135,6 +153,7 @@
       gtx.setId(10);
       icc.get().setGlobalTransaction(gtx);
       final AtomicBoolean acquired = new AtomicBoolean(false);
+      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
 
       lock.lock();
       assert lock.getOwner().equals(gtx);
@@ -152,6 +171,16 @@
             {
                // do nothing
             }
+
+            try
+            {
+               lock.unlock();
+            }
+            catch (IllegalMonitorStateException e)
+            {
+               // expected
+               threwExceptionOnRelease.set(true);
+            }
          }
       };
 
@@ -159,6 +188,7 @@
       t.join();
 
       assert !acquired.get() : "Second thread should not have acquired lock";
+      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
 
       lock.unlock();
       assert !lock.isLocked();
@@ -169,6 +199,7 @@
       final InvocationContextContainer icc = new InvocationContextContainer();
       final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
       final AtomicBoolean acquired = new AtomicBoolean(false);
+      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
 
       lock.lock();
       assert lock.getOwner().equals(Thread.currentThread());
@@ -189,6 +220,16 @@
             {
                // do nothing
             }
+
+            try
+            {
+               lock.unlock();
+            }
+            catch (IllegalMonitorStateException e)
+            {
+               // expected
+               threwExceptionOnRelease.set(true);
+            }
          }
       };
 
@@ -196,6 +237,7 @@
       t.join();
 
       assert !acquired.get() : "Second thread should not have acquired lock";
+      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
 
       lock.unlock();
       assert !lock.isLocked();
@@ -206,6 +248,7 @@
       final InvocationContextContainer icc = new InvocationContextContainer();
       final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
       final AtomicBoolean acquired = new AtomicBoolean(false);
+      final AtomicBoolean threwExceptionOnRelease = new AtomicBoolean(false);
       GlobalTransaction gtx = new GlobalTransaction();
       gtx.setId(10);
       icc.get().setGlobalTransaction(gtx);
@@ -229,6 +272,16 @@
             {
                // do nothing
             }
+
+            try
+            {
+               lock.unlock();
+            }
+            catch (IllegalMonitorStateException e)
+            {
+               // expected
+               threwExceptionOnRelease.set(true);
+            }
          }
       };
 
@@ -236,6 +289,7 @@
       t.join();
 
       assert !acquired.get() : "Second thread should not have acquired lock";
+      assert threwExceptionOnRelease.get() : "Second thread should have thrown an exception trying to release lock";
 
       lock.unlock();
       assert !lock.isLocked();
@@ -282,4 +336,35 @@
       assert !lock.isLocked();
    }
 
+   public void satisfyCodeCoverage() throws InterruptedException
+   {
+      final InvocationContextContainer icc = new InvocationContextContainer();
+      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+      System.out.println(lock.toString());
+      lock.lockInterruptibly();
+      System.out.println(lock.toString());
+
+      assert lock.newCondition() != null;
+      assert lock.isHeldExclusively();
+      lock.unlock();
+      assert lock.isHeldExclusively();
+   }
+
+   public void testSerialization() throws IOException, ClassNotFoundException
+   {
+      final InvocationContextContainer icc = new InvocationContextContainer();
+      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+      lock.lock();
+      assert lock.isLocked();
+      ByteArrayOutputStream baos = new ByteArrayOutputStream();
+      ObjectOutputStream oos = new ObjectOutputStream(baos);
+      oos.writeObject(lock);
+      oos.close();
+      baos.close();
+      ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
+      OwnableReentrantLock l2 = (OwnableReentrantLock) ois.readObject();
+
+      assert !l2.isLocked();
+      assert l2.getOwner() == null;
+   }
 }




More information about the jbosscache-commits mailing list