[jbosscache-commits] JBoss Cache SVN: r6015 - in core/trunk/src/test/java/org/jboss/cache/util: concurrent and 1 other directories.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Tue Jun 24 12:59:43 EDT 2008


Author: manik.surtani at jboss.com
Date: 2008-06-24 12:59:43 -0400 (Tue, 24 Jun 2008)
New Revision: 6015

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

Added: 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	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/util/concurrent/locks/OwnableReentrantLockTest.java	2008-06-24 16:59:43 UTC (rev 6015)
@@ -0,0 +1,285 @@
+package org.jboss.cache.util.concurrent.locks;
+
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.transaction.GlobalTransaction;
+import org.testng.annotations.Test;
+
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+ at Test(groups = "unit")
+public class OwnableReentrantLockTest
+{
+   public void testReentrancyThread()
+   {
+      InvocationContextContainer icc = new InvocationContextContainer();
+      OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+
+      lock.lock(); // locked by current thread
+      assert lock.getOwner().equals(Thread.currentThread());
+      assert lock.getHoldCount() == 1;
+
+      lock.lock();
+      assert lock.getOwner().equals(Thread.currentThread());
+      assert lock.getHoldCount() == 2;
+
+      lock.unlock();
+      assert lock.getOwner().equals(Thread.currentThread());
+      assert lock.getHoldCount() == 1;
+
+      lock.unlock();
+      assert lock.getOwner() == null;
+      assert lock.getHoldCount() == 0;
+   }
+
+   public void testReentrancyGtx()
+   {
+      InvocationContextContainer icc = new InvocationContextContainer();
+      OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+
+      // create and set a gtx
+      GlobalTransaction gtx = new GlobalTransaction();
+      gtx.setId(10);
+      icc.get().setGlobalTransaction(gtx);
+
+      lock.lock(); // locked by current thread
+      assert lock.getOwner().equals(gtx);
+      assert lock.getHoldCount() == 1;
+
+      lock.lock();
+      assert lock.getOwner().equals(gtx);
+      assert lock.getHoldCount() == 2;
+
+      lock.unlock();
+      assert lock.getOwner().equals(gtx);
+      assert lock.getHoldCount() == 1;
+
+      lock.unlock();
+      assert lock.getOwner() == null;
+      assert lock.getHoldCount() == 0;
+   }
+
+   public void testReentrancyNotSameGtx()
+   {
+      InvocationContextContainer icc = new InvocationContextContainer();
+      OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+
+      // create and set a gtx
+      GlobalTransaction gtx = new GlobalTransaction();
+      gtx.setId(10);
+      icc.get().setGlobalTransaction(gtx);
+
+      GlobalTransaction gtx2 = new GlobalTransaction();
+      gtx2.setId(10);
+
+      assert gtx != gtx2;
+
+      lock.lock(); // locked by current thread
+      assert lock.getOwner().equals(gtx);
+      assert lock.getHoldCount() == 1;
+
+      icc.get().setGlobalTransaction(gtx2);
+      lock.lock();
+      assert lock.getOwner().equals(gtx2);
+      assert lock.getHoldCount() == 2;
+
+      lock.unlock();
+      assert lock.getOwner().equals(gtx2);
+      assert lock.getHoldCount() == 1;
+
+      icc.get().setGlobalTransaction(gtx);
+      lock.unlock();
+      assert lock.getOwner() == null;
+      assert lock.getHoldCount() == 0;
+   }
+
+   public void testThreadLockedByThread() throws InterruptedException
+   {
+      InvocationContextContainer icc = new InvocationContextContainer();
+      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+      final AtomicBoolean acquired = new AtomicBoolean(false);
+
+      lock.lock();
+      assert lock.getOwner().equals(Thread.currentThread());
+      assert lock.getHoldCount() == 1;
+
+      Thread t = new Thread()
+      {
+         public void run()
+         {
+            try
+            {
+               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+            }
+            catch (InterruptedException e)
+            {
+               // do nothing
+            }
+         }
+      };
+
+      t.start();
+      t.join();
+
+      assert !acquired.get() : "Second thread should not have acquired lock";
+
+      lock.unlock();
+      assert !lock.isLocked();
+   }
+
+   public void testThreadLockedByGtx() throws InterruptedException
+   {
+      InvocationContextContainer icc = new InvocationContextContainer();
+      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+      GlobalTransaction gtx = new GlobalTransaction();
+      gtx.setId(10);
+      icc.get().setGlobalTransaction(gtx);
+      final AtomicBoolean acquired = new AtomicBoolean(false);
+
+      lock.lock();
+      assert lock.getOwner().equals(gtx);
+      assert lock.getHoldCount() == 1;
+
+      Thread t = new Thread()
+      {
+         public void run()
+         {
+            try
+            {
+               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+            }
+            catch (InterruptedException e)
+            {
+               // do nothing
+            }
+         }
+      };
+
+      t.start();
+      t.join();
+
+      assert !acquired.get() : "Second thread should not have acquired lock";
+
+      lock.unlock();
+      assert !lock.isLocked();
+   }
+
+   public void testGtxLockedByThread() throws InterruptedException
+   {
+      final InvocationContextContainer icc = new InvocationContextContainer();
+      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+      final AtomicBoolean acquired = new AtomicBoolean(false);
+
+      lock.lock();
+      assert lock.getOwner().equals(Thread.currentThread());
+      assert lock.getHoldCount() == 1;
+
+      Thread t = new Thread()
+      {
+         public void run()
+         {
+            try
+            {
+               GlobalTransaction gtx = new GlobalTransaction();
+               gtx.setId(10);
+               icc.get().setGlobalTransaction(gtx);
+               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+            }
+            catch (InterruptedException e)
+            {
+               // do nothing
+            }
+         }
+      };
+
+      t.start();
+      t.join();
+
+      assert !acquired.get() : "Second thread should not have acquired lock";
+
+      lock.unlock();
+      assert !lock.isLocked();
+   }
+
+   public void testGtxLockedByGtxFail() throws InterruptedException
+   {
+      final InvocationContextContainer icc = new InvocationContextContainer();
+      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+      final AtomicBoolean acquired = new AtomicBoolean(false);
+      GlobalTransaction gtx = new GlobalTransaction();
+      gtx.setId(10);
+      icc.get().setGlobalTransaction(gtx);
+
+      lock.lock();
+      assert lock.getOwner().equals(gtx);
+      assert lock.getHoldCount() == 1;
+
+      Thread t = new Thread()
+      {
+         public void run()
+         {
+            try
+            {
+               GlobalTransaction gtx = new GlobalTransaction();
+               gtx.setId(20);
+               icc.get().setGlobalTransaction(gtx);
+               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+            }
+            catch (InterruptedException e)
+            {
+               // do nothing
+            }
+         }
+      };
+
+      t.start();
+      t.join();
+
+      assert !acquired.get() : "Second thread should not have acquired lock";
+
+      lock.unlock();
+      assert !lock.isLocked();
+   }
+
+   public void testGtxLockedByGtxSuccess() throws InterruptedException
+   {
+      final InvocationContextContainer icc = new InvocationContextContainer();
+      final OwnableReentrantLock lock = new OwnableReentrantLock(icc);
+      final AtomicBoolean acquired = new AtomicBoolean(false);
+      GlobalTransaction gtx = new GlobalTransaction();
+      gtx.setId(10);
+      icc.get().setGlobalTransaction(gtx);
+
+      lock.lock();
+      assert lock.getOwner().equals(gtx);
+      assert lock.getHoldCount() == 1;
+
+      Thread t = new Thread()
+      {
+         public void run()
+         {
+            try
+            {
+               GlobalTransaction gtx = new GlobalTransaction();
+               gtx.setId(10);
+               icc.get().setGlobalTransaction(gtx);
+               acquired.set(lock.tryLock(10, TimeUnit.MILLISECONDS));
+            }
+            catch (InterruptedException e)
+            {
+               // do nothing
+            }
+         }
+      };
+
+      t.start();
+      t.join();
+
+      assert acquired.get() : "Second thread should have acquired lock";
+      assert lock.getHoldCount() == 2;
+      lock.unlock();
+      lock.unlock();
+      assert !lock.isLocked();
+   }
+
+}




More information about the jbosscache-commits mailing list