Author: manik.surtani(a)jboss.com
Date: 2008-06-27 17:25:42 -0400 (Fri, 27 Jun 2008)
New Revision: 6107
Removed:
core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockParentForChildInsertRemoveTest.java
core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockTest.java
Log:
More MVCC tests
Deleted:
core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockParentForChildInsertRemoveTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockParentForChildInsertRemoveTest.java 2008-06-27
17:28:57 UTC (rev 6106)
+++
core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockParentForChildInsertRemoveTest.java 2008-06-27
21:25:42 UTC (rev 6107)
@@ -1,12 +0,0 @@
-package org.jboss.cache.api.mvcc;
-
-import org.testng.annotations.Test;
-
-@Test(groups = {"functional", "mvcc"})
-public class LockParentForChildInsertRemoveTest extends LockTest
-{
- public LockParentForChildInsertRemoveTest()
- {
- lockParentForInsertRemove = true;
- }
-}
Deleted: core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockTest.java 2008-06-27 17:28:57
UTC (rev 6106)
+++ core/trunk/src/test/java/org/jboss/cache/api/mvcc/LockTest.java 2008-06-27 21:25:42
UTC (rev 6107)
@@ -1,423 +0,0 @@
-package org.jboss.cache.api.mvcc;
-
-import org.jboss.cache.Cache;
-import org.jboss.cache.DefaultCacheFactory;
-import org.jboss.cache.Fqn;
-import org.jboss.cache.config.Configuration.CacheMode;
-import org.jboss.cache.config.Configuration.NodeLockingScheme;
-import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
-import org.jboss.cache.invocation.InvocationContextContainer;
-import org.jboss.cache.lock.IsolationLevel;
-import org.jboss.cache.lock.LockManager;
-import org.jboss.cache.lock.MVCCLockManager.LockContainer;
-import org.jboss.cache.transaction.DummyTransactionManagerLookup;
-import org.jboss.cache.util.TestingUtil;
-import org.testng.annotations.AfterMethod;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
-
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-import java.util.Collections;
-
-/**
- * @author Manik Surtani (<a
href="mailto:manik@jboss.org">manik@jboss.org</a>)
- * @since 3.0
- */
-@Test(groups = {"functional", "mvcc"})
-public class LockTest
-{
- Cache<String, String> cache;
- TransactionManager tm;
- Fqn A = Fqn.fromString("/a");
- Fqn AB = Fqn.fromString("/a/b");
- Fqn ABC = Fqn.fromString("/a/b/c");
- Fqn ABCD = Fqn.fromString("/a/b/c/d");
- LockManager lockManager;
- InvocationContextContainer icc;
- boolean lockParentForInsertRemove = false;
- boolean repeatableRead = true;
-
-
- @BeforeMethod
- public void setUp()
- {
- cache = new DefaultCacheFactory<String,
String>().createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.LOCAL),
false);
- cache.getConfiguration().setNodeLockingScheme(NodeLockingScheme.MVCC);
-
cache.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
-
cache.getConfiguration().setLockParentForChildInsertRemove(lockParentForInsertRemove);
- cache.getConfiguration().setIsolationLevel(repeatableRead ?
IsolationLevel.REPEATABLE_READ : IsolationLevel.READ_COMMITTED);
- cache.start();
- lockManager =
TestingUtil.extractComponentRegistry(cache).getComponent(LockManager.class);
- icc =
TestingUtil.extractComponentRegistry(cache).getComponent(InvocationContextContainer.class);
- tm =
TestingUtil.extractComponentRegistry(cache).getComponent(TransactionManager.class);
- }
-
- @AfterMethod
- public void tearDown()
- {
- TestingUtil.killCaches(cache);
- }
-
- private void assertLocked(Fqn fqn)
- {
- assert lockManager.isLocked(fqn) : fqn + " not locked!";
- assert icc.get().getLocks().contains(fqn) : "Lock not recorded for " +
fqn;
- }
-
- private void assertNotLocked(Fqn fqn)
- {
- // can't rely on the negative test since other nodes may share the same lock
with lock striping.
-// assert !lockManager.isLocked(fqn) : fqn + " is locked!";
- assert !icc.get().getLocks().contains(fqn) : fqn + " lock recorded!";
- }
-
- private void assertNoLocks()
- {
- LockContainer lc = (LockContainer) TestingUtil.extractField(lockManager,
"lockContainer");
- assert lc.getNumLocksHeld() == 0 : "Stale locks exist!" +
lockManager.printLockInfo();
- assert icc.get().getLocks().isEmpty() : "Stale (?) locks recorded! " +
icc.get().getLocks();
- }
-
- public void testLocksOnPutKeyVal() throws Exception
- {
- tm.begin();
- cache.put(AB, "k", "v");
- if (lockParentForInsertRemove)
- assertLocked(Fqn.ROOT);
- else
- assertNotLocked(Fqn.ROOT);
- assertLocked(A);
- assertLocked(AB);
- assertNotLocked(ABC);
- tm.commit();
-
- assertNoLocks();
-
- tm.begin();
- assert cache.get(AB, "k").equals("v");
- assertNotLocked(Fqn.ROOT);
- assertNotLocked(A);
- assertNotLocked(AB);
- assertNotLocked(ABC);
- tm.commit();
-
- assertNoLocks();
-
- tm.begin();
- cache.put(ABC, "k", "v");
- assertNotLocked(Fqn.ROOT);
- assertNotLocked(A);
- if (lockParentForInsertRemove)
- assertLocked(AB);
- else
- assertNotLocked(AB);
- assertLocked(ABC);
- tm.commit();
-
- assertNoLocks();
- }
-
- public void testLocksOnPutData() throws Exception
- {
- tm.begin();
- cache.put(AB, Collections.singletonMap("k", "v"));
- if (lockParentForInsertRemove)
- assertLocked(Fqn.ROOT);
- else
- assertNotLocked(Fqn.ROOT);
- assertLocked(A);
- assertLocked(AB);
- assertNotLocked(ABC);
- tm.commit();
-
- assertNoLocks();
-
- tm.begin();
- assert cache.get(AB, "k").equals("v");
- assertNotLocked(Fqn.ROOT);
- assertNotLocked(A);
- assertNotLocked(AB);
- assertNotLocked(ABC);
- tm.commit();
-
- assertNoLocks();
-
- tm.begin();
- cache.put(ABC, Collections.singletonMap("k", "v"));
- assertNotLocked(Fqn.ROOT);
- assertNotLocked(A);
- if (lockParentForInsertRemove)
- assertLocked(AB);
- else
- assertNotLocked(AB);
- assertLocked(ABC);
- tm.commit();
-
- assertNoLocks();
- }
-
- public void testLocksOnRemoveNode() throws Exception
- {
- // init some data on a node
- cache.put(AB, Collections.singletonMap("k", "v"));
-
- assert "v".equals(cache.get(AB, "k"));
-
- tm.begin();
- cache.removeNode(AB);
- assertLocked(AB);
- if (lockParentForInsertRemove)
- assertLocked(A);
- else
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assert cache.getNode(AB) == null : "Should not exist";
- assertNoLocks();
- }
-
- public void testLocksOnEvictNode() throws Exception
- {
- // init some data on a node
- cache.put(AB, Collections.singletonMap("k", "v"));
-
- assert "v".equals(cache.get(AB, "k"));
-
- tm.begin();
- cache.evict(AB);
- assertLocked(AB);
- if (lockParentForInsertRemove)
- assertLocked(A);
- else
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assert cache.getNode(AB) == null : "Should not exist";
- assertNoLocks();
- }
-
- public void testLocksOnEvictRecursiveNode() throws Exception
- {
- // init some data on a node
- cache.put(AB, Collections.singletonMap("k", "v"));
- cache.put(ABC, Collections.singletonMap("k", "v"));
- cache.put(ABCD, Collections.singletonMap("k", "v"));
-
- assert "v".equals(cache.get(AB, "k"));
- assert "v".equals(cache.get(ABC, "k"));
- assert "v".equals(cache.get(ABCD, "k"));
-
- tm.begin();
- cache.evict(AB, true);
- assertLocked(AB);
- assertLocked(ABC);
- assertLocked(ABCD);
- if (lockParentForInsertRemove)
- assertLocked(A);
- else
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assert cache.getNode(AB) == null : "Should not exist";
- assertNoLocks();
- }
-
- public void testLocksOnRemoveNonexistentNode() throws Exception
- {
- assert cache.getNode(AB) == null : "Should not exist";
-
- tm.begin();
- cache.removeNode(AB);
- assertLocked(AB);
- if (lockParentForInsertRemove)
- assertLocked(A);
- else
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assert cache.getNode(AB) == null : "Should not exist";
- assertNoLocks();
- }
-
- public void testLocksOnEvictNonexistentNode() throws Exception
- {
- assert cache.getNode(AB) == null : "Should not exist";
-
- tm.begin();
- cache.evict(AB);
- assertLocked(AB);
- if (lockParentForInsertRemove)
- assertLocked(A);
- else
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assert cache.getNode(AB) == null : "Should not exist";
- assertNoLocks();
- }
-
- public void testLocksOnRemoveData() throws Exception
- {
- // init some data on a node
- cache.put(AB, "k", "v");
- cache.put(AB, "k2", "v2");
-
- assert "v".equals(cache.get(AB, "k"));
- assert "v2".equals(cache.get(AB, "k2"));
-
- // remove
- tm.begin();
- Object x = cache.remove(AB, "k");
- assert x.equals("v");
- assertLocked(AB);
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assert cache.get(AB, "k") == null : "Should not exist";
- assert "v2".equals(cache.get(AB, "k2"));
- assertNoLocks();
-
- // clearData
- tm.begin();
- cache.clearData(AB);
- assertLocked(AB);
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
-
- assert cache.get(AB, "k") == null : "Should not exist";
- assert cache.get(AB, "k2") == null : "Should not exist";
- assertNoLocks();
-
- // nonexistent key
- assert cache.get(AB, "k3") == null : "Should not exist";
- tm.begin();
- cache.remove(AB, "k3");
- assertLocked(AB);
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assertNoLocks();
- }
-
- public void testLocksOnRemoveDataNonExistentNode() throws Exception
- {
- assert cache.getNode(AB) == null : "Should not exist";
-
- tm.begin();
- cache.remove(AB, "k");
- assertNotLocked(AB);
- assertNotLocked(A);
- assertNotLocked(Fqn.ROOT);
- tm.commit();
- assert cache.getNode(AB) == null : "Should not exist";
- }
-
- public void testReadMethods() throws Exception
- {
- cache.put(AB, "k", "v");
-
- tm.begin();
- assert "v".equals(cache.get(AB, "k"));
- assertNoLocks();
- tm.commit();
- assertNoLocks();
-
- tm.begin();
- assert cache.getData(AB).containsKey("k");
- assertNoLocks();
- tm.commit();
- assertNoLocks();
-
- tm.begin();
- assert cache.getKeys(AB).contains("k");
- assertNoLocks();
- tm.commit();
- assertNoLocks();
-
- tm.begin();
- assert cache.getNode(AB) != null;
- assertNoLocks();
- tm.commit();
- assertNoLocks();
-
- tm.begin();
- assert !(cache.getNode(A).getChildrenNames().isEmpty());
- assertNoLocks();
- tm.commit();
- assertNoLocks();
- }
-
- public void testWriteDoesntBlockRead() throws Exception
- {
- cache.put(AB, "k", "v");
-
- // start a write.
- tm.begin();
- cache.put(AB, "k2", "v2");
- assertLocked(AB);
- Transaction write = tm.suspend();
-
- // now start a read and confirm that the write doesn't block it.
- tm.begin();
- assert "v".equals(cache.get(AB, "k"));
- assert null == cache.get(AB, "k2") : "Should not see uncommitted
changes";
- Transaction read = tm.suspend();
-
- // commit the write
- tm.resume(write);
- tm.commit();
-
- assertNoLocks();
-
- tm.resume(read);
- if (repeatableRead)
- assert null == cache.get(AB, "k2") : "Should have repeatable
read";
- else
- assert "v2".equals(cache.get(AB, "k2")) : "Read
committed should see committed changes";
- tm.commit();
- assertNoLocks();
- }
-
- public void testWriteDoesntBlockReadNonexistent() throws Exception
- {
- // start a write.
- tm.begin();
- cache.put(AB, "k", "v");
- assertLocked(AB);
- Transaction write = tm.suspend();
-
- // now start a read and confirm that the write doesn't block it.
- tm.begin();
- assert null == cache.get(AB, "k") : "Should not see uncommitted
changes";
- assert null == cache.getNode(AB);
- Transaction read = tm.suspend();
-
- // commit the write
- tm.resume(write);
- tm.commit();
-
- assertNoLocks();
-
- tm.resume(read);
- if (repeatableRead)
- {
- assert null == cache.get(AB, "k") : "Should have repeatable
read";
- assert null == cache.getNode(AB);
- }
- else
- {
- assert "v".equals(cache.get(AB, "k")) : "Read committed
should see committed changes";
- assert null != cache.getNode(AB);
- }
- tm.commit();
- assertNoLocks();
- }
-
- // TODO: Test write conflicts - with and without allowing write skew. Multiple cases
involving concurrent removes, concurrent put + remove, etc.
- // TODO: Test Replication with MVCC
- // TODO: Test state transfer with MVCC
- // TODO: Test Cache loading with MVCC
- // TODO: Test spreading of Fqns among locks. Need a better algorithm?
-}