Author: manik.surtani(a)jboss.com
Date: 2008-06-25 09:16:27 -0400 (Wed, 25 Jun 2008)
New Revision: 6019
Added:
core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java
core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java
core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java
core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java
core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java
Log:
Lock manager tests
Added:
core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/lock/AbstractLockManagerRecordingTest.java 2008-06-25
13:16:27 UTC (rev 6019)
@@ -0,0 +1,72 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.Fqn;
+import org.jboss.cache.InvocationContext;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.UnversionedNode;
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.invocation.NodeInvocationDelegate;
+import static org.jboss.cache.lock.LockType.WRITE;
+import org.jboss.cache.transaction.DummyTransaction;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.jboss.cache.transaction.GlobalTransaction;
+import org.jboss.cache.transaction.TransactionEntry;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.RollbackException;
+import javax.transaction.SystemException;
+
+@Test(groups = "unit")
+public abstract class AbstractLockManagerRecordingTest
+{
+ LockManager lm;
+ InvocationContextContainer icc;
+ protected boolean fqnBasedLocking = true;
+
+ @AfterMethod
+ public void tearDown()
+ {
+ lm = null;
+ }
+
+ public void testRecordingLocksNoTx() throws InterruptedException
+ {
+ Fqn fqn = Fqn.fromString("/a/b/c");
+ NodeSPI node = createNode(fqn);
+ InvocationContext ctx = icc.get();
+
+ // lock and record.
+ lm.lockAndRecord(node, WRITE, ctx);
+ assert ctx.getLocks().contains(fqnBasedLocking ? fqn : node.getLock());
+ assert ctx.getLocks().size() == 1;
+ assert lm.isLocked(node) : "Should be locked";
+ lm.unlock(ctx);
+ assert !lm.isLocked(node) : "Should not be locked";
+ }
+
+ public void testRecordingLocksWithTx() throws InterruptedException, SystemException,
RollbackException
+ {
+ Fqn fqn = Fqn.fromString("/a/b/c");
+ NodeSPI node = createNode(fqn);
+ InvocationContext ctx = icc.get();
+ ctx.setGlobalTransaction(new GlobalTransaction());
+ ctx.setTransaction(new DummyTransaction(DummyTransactionManager.getInstance()));
+ ctx.setTransactionEntry(new TransactionEntry(ctx.getTransaction()));
+
+ // lock and record.
+ lm.lockAndRecord(node, WRITE, ctx);
+ assert ctx.getLocks().contains(fqnBasedLocking ? fqn : node.getLock());
+ assert ctx.getTransactionEntry().getLocks().size() == 1;
+ assert lm.isLocked(node) : "Should be locked";
+ lm.unlock(ctx);
+ assert !lm.isLocked(node) : "Should not be locked";
+ }
+
+ protected NodeSPI createNode(Fqn fqn)
+ {
+ UnversionedNode un = new UnversionedNode(fqn);
+ un.injectDependencies(null, null, new LockStrategyFactory());
+ return new NodeInvocationDelegate(un);
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java
(rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerNoTxTest.java 2008-06-25
13:16:27 UTC (rev 6019)
@@ -0,0 +1,15 @@
+package org.jboss.cache.lock;
+
+import org.testng.annotations.Test;
+
+import javax.transaction.TransactionManager;
+
+@Test(groups = "unit")
+public class MVCCLockManagerNoTxTest extends MVCCLockManagerTest
+{
+ @Override
+ protected TransactionManager getTransactionManager()
+ {
+ return null; // force the use of JDK ReentrantLocks.
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerRecordingTest.java 2008-06-25
13:16:27 UTC (rev 6019)
@@ -0,0 +1,28 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.TransactionManager;
+
+/**
+ * @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
+ * @since 3.0
+ */
+@Test(groups = "unit")
+public class MVCCLockManagerRecordingTest extends AbstractLockManagerRecordingTest
+{
+ @BeforeMethod
+ public void setUp()
+ {
+ icc = new InvocationContextContainer();
+ MVCCLockManager mvccLockManager = new MVCCLockManager();
+ TransactionManager tm = DummyTransactionManager.getInstance();
+ mvccLockManager.injectDataContainer(null, null, tm, icc);
+ mvccLockManager.startLockManager();
+ lm = mvccLockManager;
+ }
+
+}
Added: core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java
(rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/lock/MVCCLockManagerTest.java 2008-06-25
13:16:27 UTC (rev 6019)
@@ -0,0 +1,98 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.Fqn;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.UnversionedNode;
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.jboss.cache.invocation.NodeInvocationDelegate;
+import static org.jboss.cache.lock.LockType.READ;
+import static org.jboss.cache.lock.LockType.WRITE;
+import org.jboss.cache.transaction.DummyTransactionManager;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.transaction.TransactionManager;
+
+@Test(groups = "unit")
+public class MVCCLockManagerTest
+{
+ MVCCLockManager lm;
+ InvocationContextContainer icc;
+
+ @BeforeMethod
+ public void setUp()
+ {
+ icc = new InvocationContextContainer();
+ lm = new MVCCLockManager();
+ TransactionManager tm = getTransactionManager();
+ lm.injectDataContainer(null, null, tm, icc);
+ lm.startLockManager();
+ }
+
+ protected TransactionManager getTransactionManager()
+ {
+ return DummyTransactionManager.getInstance(); // use this to ensure the lock
manager uses ownable reentrant locks
+ }
+
+ @AfterMethod
+ public void tearDown()
+ {
+ lm = null;
+ }
+
+ public void testUsingReadLocks()
+ {
+ // using any READ locks should throw an exception
+ try
+ {
+ lm.isLocked(null, READ);
+ assert false : "Should fail";
+ }
+ catch (Exception e)
+ {
+ // good
+ }
+
+ try
+ {
+ lm.getReadOwners(Fqn.ROOT);
+ assert false : "Should fail";
+ }
+ catch (Exception e)
+ {
+ // good
+ }
+
+ try
+ {
+ lm.getReadOwners((NodeSPI) null);
+ assert false : "Should fail";
+ }
+ catch (Exception e)
+ {
+ // good
+ }
+ }
+
+ public void testLockReentrancy() throws InterruptedException
+ {
+ Fqn fqn = Fqn.fromString("/a/b/c");
+ NodeSPI node = new NodeInvocationDelegate(new UnversionedNode(fqn));
+
+ assert lm.lock(fqn, WRITE, null);
+ assert lm.isLocked(node);
+
+ assert lm.lock(node, WRITE, null);
+ assert lm.isLocked(node);
+
+ lm.unlock(node, null);
+
+ assert lm.isLocked(node) : "Should still be locked";
+ assert lm.ownsLock(node, Thread.currentThread());
+
+ lm.unlock(fqn, null);
+
+ assert !lm.isLocked(node) : "Should not be locked";
+ }
+}
Added:
core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/lock/NodeBasedLockManagerRecordingTest.java 2008-06-25
13:16:27 UTC (rev 6019)
@@ -0,0 +1,21 @@
+package org.jboss.cache.lock;
+
+import org.jboss.cache.invocation.InvocationContextContainer;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
+ * @since 3.0
+ */
+@Test(groups = "unit")
+public class NodeBasedLockManagerRecordingTest extends AbstractLockManagerRecordingTest
+{
+ @BeforeMethod
+ public void setUp()
+ {
+ icc = new InvocationContextContainer();
+ lm = new NodeBasedLockManager();
+ fqnBasedLocking = false;
+ }
+}
Show replies by date