[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/api ...
Manik Surtani
msurtani at jboss.com
Fri Sep 15 20:23:35 EDT 2006
User: msurtani
Date: 06/09/15 20:23:35
Modified: tests/functional/org/jboss/cache/api NodeAPITest.java
NodeMoveAPITest.java
Added: tests/functional/org/jboss/cache/api
NodeMoveOptimisticTest.java
NodeReplicatedMoveOptimisticTest.java
NodeReplicatedMoveTest.java
Log:
Updates to the move() API plus more UTs
Revision Changes Path
1.3 +15 -0 JBossCache/tests/functional/org/jboss/cache/api/NodeAPITest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NodeAPITest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/api/NodeAPITest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- NodeAPITest.java 15 Sep 2006 18:01:02 -0000 1.2
+++ NodeAPITest.java 16 Sep 2006 00:23:35 -0000 1.3
@@ -72,6 +72,11 @@
assertTrue(rootNode.getChildren().contains(nodeA));
assertTrue(rootNode.getChildren().contains(nodeD));
assertEquals(2, rootNode.getChildren().size());
+
+ rootNode.removeChild(A);
+ assertFalse(rootNode.getChildren().contains(nodeA));
+ assertTrue(rootNode.getChildren().contains(nodeD));
+ assertEquals(1, rootNode.getChildren().size());
}
public void testLocking() throws Exception
@@ -147,4 +152,14 @@
assertNull(rootNode.getChild(new Fqn("nonexistent")));
}
+
+ public void testClearingData()
+ {
+ rootNode.put("k", "v");
+ rootNode.put("k2", "v2");
+ assertEquals(2, rootNode.getKeys().size());
+ rootNode.clearData();
+ assertEquals(0, rootNode.getKeys().size());
+ assertTrue(rootNode.getData().isEmpty());
+ }
}
1.3 +252 -54 JBossCache/tests/functional/org/jboss/cache/api/NodeMoveAPITest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NodeMoveAPITest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/api/NodeMoveAPITest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- NodeMoveAPITest.java 15 Sep 2006 18:03:26 -0000 1.2
+++ NodeMoveAPITest.java 16 Sep 2006 00:23:35 -0000 1.3
@@ -1,12 +1,19 @@
package org.jboss.cache.api;
-import junit.framework.TestCase;
+import EDU.oswego.cs.dl.util.concurrent.Latch;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
import org.jboss.cache.Node;
+import org.jboss.cache.NodeNotExistsException;
+import org.jboss.cache.TreeCacheProxyImpl;
+import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.DefaultCacheFactory;
+import org.jboss.cache.loader.AbstractCacheLoaderTestBase;
+import org.jboss.cache.loader.DummyInMemoryCacheLoader;
+import org.jboss.cache.misc.TestingUtil;
import javax.transaction.TransactionManager;
+import java.util.Random;
/**
* Excercises and tests the new move() api
@@ -14,18 +21,24 @@
* @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
* @since 2.0.0
*/
-public class NodeMoveAPITest extends TestCase
+public class NodeMoveAPITest extends AbstractCacheLoaderTestBase
{
- private Node rootNode;
- private CacheSPI cache;
- private TransactionManager tm;
- private static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn.fromString("/d"), E = Fqn.fromString("/e");
+ protected Node rootNode, nodeA, nodeB, nodeC, nodeD, nodeE;
+ protected CacheSPI cache;
+ protected TransactionManager tm;
+ protected static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn.fromString("/d"), E = Fqn.fromString("/e");
+ protected Object k = "key", vA = "valueA", vB = "valueB", vC = "valueC", vD = "valueD", vE = "valueE";
- protected void setUp()
+ protected boolean optimistic = false;
+
+
+ protected void setUp() throws Exception
{
// start a single cache instance
- cache = (CacheSPI) new DefaultCacheFactory().createCache("META-INF/local-tx-service.xml");
+ cache = (CacheSPI) new DefaultCacheFactory().createCache("META-INF/local-tx-service.xml", false);
+ cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
+ cache.start();
rootNode = cache.getRoot();
tm = cache.getTransactionManager();
}
@@ -39,13 +52,11 @@
public void testBasicMove()
{
- Object k = "key", vA = "valueA", vB = "valueB", vC = "valueC";
-
- Node nodeA = rootNode.addChild(A);
+ nodeA = rootNode.addChild(A);
nodeA.put(k, vA);
- Node nodeB = rootNode.addChild(B);
+ nodeB = rootNode.addChild(B);
nodeB.put(k, vB);
- Node nodeC = nodeA.addChild(C);
+ nodeC = nodeA.addChild(C);
nodeC.put(k, vC);
assertTrue(rootNode.hasChild(A));
@@ -81,17 +92,15 @@
public void testMoveWithChildren()
{
- Object k = "key", vA = "valueA", vB = "valueB", vC = "valueC", vD = "valueD", vE = "valueE";
-
- Node nodeA = rootNode.addChild(A);
+ nodeA = rootNode.addChild(A);
nodeA.put(k, vA);
- Node nodeB = rootNode.addChild(B);
+ nodeB = rootNode.addChild(B);
nodeB.put(k, vB);
- Node nodeC = nodeA.addChild(C);
+ nodeC = nodeA.addChild(C);
nodeC.put(k, vC);
- Node nodeD = nodeC.addChild(D);
+ nodeD = nodeC.addChild(D);
nodeD.put(k, vD);
- Node nodeE = nodeD.addChild(E);
+ nodeE = nodeD.addChild(E);
nodeE.put(k, vE);
assertTrue(rootNode.hasChild(A));
@@ -141,53 +150,242 @@
assertEquals(nodeD, nodeE.getParent());
}
- public void testTxCommit()
+ public void testTxCommit() throws Exception
{
- fail("Implement me");
+ nodeA = rootNode.addChild(A);
+ nodeB = nodeA.addChild(B);
+
+ assertEquals(rootNode, nodeA.getParent());
+ assertEquals(nodeA, nodeB.getParent());
+ assertEquals(nodeA, rootNode.getChildren().iterator().next());
+ assertEquals(nodeB, nodeA.getChildren().iterator().next());
+
+ tm.begin();
+ // move node B up to hang off the root
+ nodeB.move(rootNode);
+ tm.commit();
+
+ assertEquals(rootNode, nodeA.getParent());
+ assertEquals(rootNode, nodeB.getParent());
+
+ assertTrue(rootNode.getChildren().contains(nodeA));
+ assertTrue(rootNode.getChildren().contains(nodeB));
+
+ assertTrue(nodeA.getChildren().isEmpty());
}
- public void testTxRollback()
+ public void testTxRollback() throws Exception
{
- fail("Implement me");
+ nodeA = rootNode.addChild(A);
+ nodeB = nodeA.addChild(B);
+
+ assertEquals(rootNode, nodeA.getParent());
+ assertEquals(nodeA, nodeB.getParent());
+ assertEquals(nodeA, rootNode.getChildren().iterator().next());
+ assertEquals(nodeB, nodeA.getChildren().iterator().next());
+
+
+ tm.begin();
+ // move node B up to hang off the root
+ nodeB.move(rootNode);
+
+ assertEquals(rootNode, nodeA.getParent());
+ assertEquals(rootNode, nodeB.getParent());
+ assertTrue(rootNode.getChildren().contains(nodeA));
+ assertTrue(rootNode.getChildren().contains(nodeB));
+ assertTrue(nodeA.getChildren().isEmpty());
+
+
+ tm.rollback();
+
+ // should revert
+ assertEquals(rootNode, nodeA.getParent());
+ assertEquals(nodeA, nodeB.getParent());
+ assertEquals(nodeA, rootNode.getChildren().iterator().next());
+ assertEquals(nodeB, nodeA.getChildren().iterator().next());
}
- public void testWithCacheloaders()
+ public void testWithCacheloaders() throws Exception
{
- fail("Implement me");
+ cache.stop();
+ cache.getConfiguration().setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", DummyInMemoryCacheLoader.class.getName(), null, false, false, false));
+ cache.start();
+
+ DummyInMemoryCacheLoader loader = (DummyInMemoryCacheLoader) cache.getCacheLoader();
+
+ rootNode.put("key", "value");
+
+ assertEquals("value", loader.get(Fqn.ROOT).get("key"));
+
+ nodeA = rootNode.addChild(A);
+ nodeA.put(k, vA);
+ nodeB = rootNode.addChild(B);
+ nodeB.put(k, vB);
+ nodeC = nodeA.addChild(C);
+ nodeC.put(k, vC);
+ nodeD = nodeC.addChild(D);
+ nodeD.put(k, vD);
+ nodeE = nodeD.addChild(E);
+ nodeE.put(k, vE);
+
+ assertTrue(rootNode.hasChild(A));
+ assertTrue(rootNode.hasChild(B));
+ assertFalse(rootNode.hasChild(C));
+ assertTrue(nodeA.hasChild(C));
+ assertTrue(nodeC.hasChild(D));
+ assertTrue(nodeD.hasChild(E));
+
+ // test data
+ assertEquals(vA, nodeA.get(k));
+ assertEquals(vB, nodeB.get(k));
+ assertEquals(vC, nodeC.get(k));
+ assertEquals(vD, nodeD.get(k));
+ assertEquals(vE, nodeE.get(k));
+
+ // parentage
+ assertEquals(rootNode, nodeA.getParent());
+ assertEquals(rootNode, nodeB.getParent());
+ assertEquals(nodeA, nodeC.getParent());
+ assertEquals(nodeC, nodeD.getParent());
+ assertEquals(nodeD, nodeE.getParent());
+
+ // move
+ nodeC.move(nodeB);
+
+ assertTrue(rootNode.hasChild(A));
+ assertTrue(rootNode.hasChild(B));
+ assertFalse(rootNode.hasChild(C));
+ assertFalse(nodeA.hasChild(C));
+ assertTrue(nodeB.hasChild(C));
+ assertTrue(nodeC.hasChild(D));
+ assertTrue(nodeD.hasChild(E));
+
+ // test data
+ assertEquals(vA, nodeA.get(k));
+ assertEquals(vB, nodeB.get(k));
+ assertEquals(vC, nodeC.get(k));
+ assertEquals(vD, nodeD.get(k));
+ assertEquals(vE, nodeE.get(k));
+
+ // parentage
+ assertEquals(rootNode, nodeA.getParent());
+ assertEquals(rootNode, nodeB.getParent());
+ assertEquals(nodeB, nodeC.getParent());
+ assertEquals(nodeC, nodeD.getParent());
+ assertEquals(nodeD, nodeE.getParent());
+
+ //now inspect the loader.
+ assertEquals(vA, loader.get(nodeA.getFqn()).get(k));
+ assertEquals(vB, loader.get(nodeB.getFqn()).get(k));
+ assertEquals(vC, loader.get(nodeC.getFqn()).get(k));
+ assertEquals(vD, loader.get(nodeD.getFqn()).get(k));
+ assertEquals(vE, loader.get(nodeE.getFqn()).get(k));
}
- public void testLocks()
+ public void testWithPassivation()
{
- fail("Implement me");
+ fail("implement me!");
}
- public void testReplicatability()
+ public void testLocks() throws Exception
{
- fail("Implement me");
+ nodeA = rootNode.addChild(A);
+ nodeB = nodeA.addChild(B);
+ nodeC = rootNode.addChild(C);
+ assertEquals(0, ((TreeCacheProxyImpl) rootNode).treeCache.getNumberOfLocksHeld());
+ tm.begin();
+
+ nodeB.move(nodeC);
+// nodeC should have a RL, nodeA should have a RL, nodeB should have a WL
+
+ System.out.println("LOCKS: " + ((TreeCacheProxyImpl) rootNode).treeCache.printLockInfo());
+
+ assertEquals(3, ((TreeCacheProxyImpl) rootNode).treeCache.getNumberOfLocksHeld());
+
+
+ tm.commit();
+
+ assertEquals(0, ((TreeCacheProxyImpl) rootNode).treeCache.getNumberOfLocksHeld());
}
- public void testInvalidations()
+ public void testConcurrency() throws InterruptedException
{
- fail("Implement me");
- }
+ final int N = 3; // number of threads
+ final int loops = 1 << 6; // number of loops
+ // tests a tree structure as such:
+ // /a
+ // /b
+ // /c
+ // /d
+ // /e
+ // /x
+ // /y
+
+ // N threads constantly move /x and /y around to hang off either /a ~ /e randomly.
+
+ final Fqn FQN_A = A, FQN_B = B, FQN_C = C, FQN_D = D, FQN_E = E, FQN_X = new Fqn("/x"), FQN_Y = new Fqn("/y");
+
+ // set up the initial structure.
+ final Node[] NODES = {
+ rootNode.addChild(FQN_A), rootNode.addChild(FQN_B),
+ rootNode.addChild(FQN_C), rootNode.addChild(FQN_D), rootNode.addChild(FQN_E)
+ };
- public void testReplTxCommit()
+ final Node NODE_X = NODES[0].addChild(FQN_X);
+ final Node NODE_Y = NODES[1].addChild(FQN_Y);
+
+ Thread[] movers = new Thread[N];
+ final Latch latch = new Latch();
+ final Random r = new Random();
+
+ for (int i = 0; i < N; i++)
+ {
+ movers[i] = new Thread("Mover-" + i)
+ {
+ public void run()
{
- fail("Implement me");
+ try
+ {
+ latch.acquire();
}
-
- public void testReplTxRollback()
+ catch (InterruptedException e)
{
- fail("Implement me");
}
- public void testConcurrency()
+ for (int counter = 0; counter < loops; counter++)
{
- fail("Implement me");
+
+ System.out.println(getName() + ": Attempt " + counter);
+ try
+ {
+ NODE_X.move(NODES[r.nextInt(NODES.length)]);
+ }
+ catch (NodeNotExistsException e)
+ {
+ // this may happen ...
+ }
+ TestingUtil.sleepRandom(250);
+ try
+ {
+ NODE_Y.move(NODES[r.nextInt(NODES.length)]);
+ }
+ catch (NodeNotExistsException e)
+ {
+ // this may happen ...
+ }
+ TestingUtil.sleepRandom(250);
+ }
+ }
+ };
+ movers[i].start();
}
- public void testReplConcurrency()
+ latch.release();
+
+ for (Thread t : movers)
{
- fail("Implement me");
+ t.join();
+ }
+
}
}
1.1 date: 2006/09/16 00:23:35; author: msurtani; state: Exp;JBossCache/tests/functional/org/jboss/cache/api/NodeMoveOptimisticTest.java
Index: NodeMoveOptimisticTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.api;
public class NodeMoveOptimisticTest extends NodeMoveAPITest
{
public NodeMoveOptimisticTest()
{
optimistic = true;
}
}
1.1 date: 2006/09/16 00:23:35; author: msurtani; state: Exp;JBossCache/tests/functional/org/jboss/cache/api/NodeReplicatedMoveOptimisticTest.java
Index: NodeReplicatedMoveOptimisticTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.api;
public class NodeReplicatedMoveOptimisticTest extends NodeReplicatedMoveTest
{
public NodeReplicatedMoveOptimisticTest()
{
optimistic = true;
}
}
1.1 date: 2006/09/16 00:23:35; author: msurtani; state: Exp;JBossCache/tests/functional/org/jboss/cache/api/NodeReplicatedMoveTest.java
Index: NodeReplicatedMoveTest.java
===================================================================
/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.cache.api;
import junit.framework.TestCase;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
import org.jboss.cache.Node;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.factories.DefaultCacheFactory;
import javax.transaction.TransactionManager;
public class NodeReplicatedMoveTest extends TestCase
{
protected Node rootNode, nodeA, nodeB, nodeC, nodeD, nodeE;
protected CacheSPI cache;
protected TransactionManager tm;
protected static final Fqn A = Fqn.fromString("/a"), B = Fqn.fromString("/b"), C = Fqn.fromString("/c"), D = Fqn.fromString("/d"), E = Fqn.fromString("/e");
protected Object k = "key", vA = "valueA", vB = "valueB", vC = "valueC", vD = "valueD", vE = "valueE";
protected boolean optimistic = false;
protected void setUp() throws Exception
{
// start a single cache instance
cache = (CacheSPI) new DefaultCacheFactory().createCache("META-INF/local-tx-service.xml", false);
cache.getConfiguration().setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC);
cache.start();
rootNode = cache.getRoot();
tm = cache.getTransactionManager();
}
protected void tearDown()
{
if (cache != null) cache.stop();
if (rootNode != null) rootNode = null;
}
public void testReplConcurrency()
{
fail("Implement me");
}
public void testReplicatability()
{
}
public void testInvalidations()
{
fail("Implement me");
}
public void testReplTxCommit()
{
fail("Implement me");
}
public void testReplTxRollback()
{
fail("Implement me");
}
}
More information about the jboss-cvs-commits
mailing list