[jbosscache-commits] JBoss Cache SVN: r4650 - core/trunk/src/test/java/org/jboss/cache/statetransfer.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Fri Oct 19 13:37:24 EDT 2007


Author: manik.surtani at jboss.com
Date: 2007-10-19 13:37:24 -0400 (Fri, 19 Oct 2007)
New Revision: 4650

Added:
   core/trunk/src/test/java/org/jboss/cache/statetransfer/DataVersionTransferTest.java
Log:
Added test for JBCACHE-1202

Added: core/trunk/src/test/java/org/jboss/cache/statetransfer/DataVersionTransferTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/statetransfer/DataVersionTransferTest.java	                        (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/statetransfer/DataVersionTransferTest.java	2007-10-19 17:37:24 UTC (rev 4650)
@@ -0,0 +1,216 @@
+package org.jboss.cache.statetransfer;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.DefaultCacheFactory;
+import org.jboss.cache.Fqn;
+import org.jboss.cache.NodeSPI;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.misc.TestingUtil;
+import org.jboss.cache.optimistic.DataVersion;
+import org.jboss.cache.optimistic.DataVersioningException;
+import org.jboss.cache.optimistic.DefaultDataVersion;
+import org.jboss.cache.transaction.DummyTransactionManagerLookup;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Tests whether data versions are transferred along with state
+ *
+ * @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
+ * @since 2.1.0
+ */
+ at Test(groups = {"functional"})
+public class DataVersionTransferTest
+{
+   private List<Cache<Object, Object>> caches = new ArrayList<Cache<Object, Object>>(2);
+
+   @BeforeMethod
+   public void setUp()
+   {
+
+      caches.add(DefaultCacheFactory.getInstance().createCache(false));
+      caches.get(0).getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+      caches.get(0).getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
+      caches.get(0).getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
+      caches.get(0).start();
+
+      caches.add(DefaultCacheFactory.getInstance().createCache(false));
+      caches.get(1).getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
+      caches.get(1).getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
+      caches.get(1).getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
+   }
+
+   @AfterMethod
+   public void tearDown()
+   {
+      if (caches != null)
+      {
+         for (Cache cache: caches)
+         {
+            try
+            {
+               cache.getConfiguration().getRuntimeConfig().getTransactionManager().rollback();
+            }
+            catch (Exception e)
+            {
+               // do nothing?
+            }
+            cache.stop();
+         }
+      }
+   }
+
+   public void testStateTransferDefaultVersions() throws Exception
+   {
+      Fqn f = Fqn.fromString("/one/two/three");
+      caches.get(0).put(f, "k", "v");
+      caches.get(0).put(f, "k1", "v1");
+      caches.get(0).remove(f, "k1");
+
+      NodeSPI n = (NodeSPI) caches.get(0).getRoot().getChild(f);
+      DataVersion dv = n.getVersion();
+
+      assert dv instanceof DefaultDataVersion : "Should be an instance of DefaultDataVersion";
+
+      assert ((DefaultDataVersion) dv).getRawVersion() == 3 : "Should have accurate data version";
+
+      // now start next cache instance
+      caches.get(1).start();
+
+      TestingUtil.blockUntilViewsReceived(10000, caches.get(0), caches.get(1));
+
+      assert caches.get(1).get(f, "k").equals("v") : "Value should have transferred";
+
+      n = (NodeSPI) caches.get(1).getRoot().getChild(f);
+
+      dv = n.getVersion();
+
+      assert dv instanceof DefaultDataVersion : "Should be an instance of DefaultDataVersion";
+
+      assert ((DefaultDataVersion) dv).getRawVersion() == 3 : "Version should have transferred";
+   }
+
+   public void testStateTransferCustomVersion() throws Exception
+   {
+      Fqn f = Fqn.fromString("/one/two/three");
+      caches.get(0).getInvocationContext().getOptionOverrides().setDataVersion(new CharVersion('A'));
+      caches.get(0).put(f, "k", "v");
+      caches.get(0).getInvocationContext().getOptionOverrides().setDataVersion(new CharVersion('B'));
+      caches.get(0).put(f, "k1", "v1");
+      caches.get(0).getInvocationContext().getOptionOverrides().setDataVersion(new CharVersion('C'));
+      caches.get(0).remove(f, "k1");
+
+      NodeSPI n = (NodeSPI) caches.get(0).getRoot().getChild(f);
+      DataVersion dv = n.getVersion();
+
+      assert dv instanceof CharVersion : "Should be an instance of CharVersion";
+
+      assert ((CharVersion) dv).version == 'C' : "Should have accurate data version";
+
+      // now start next cache instance
+      caches.get(1).start();
+
+      TestingUtil.blockUntilViewsReceived(10000, caches.get(0), caches.get(1));
+
+      assert caches.get(1).get(f, "k").equals("v") : "Value should have transferred";
+
+      n = (NodeSPI) caches.get(1).getRoot().getChild(f);
+
+      dv = n.getVersion();
+
+      assert dv instanceof CharVersion : "Should be an instance of CharVersion";
+
+      assert ((CharVersion) dv).version == 'C' : "Version should have transferred";
+   }
+
+   public void testStateTransferDefaultVersionAfterRemoval() throws Exception
+   {
+      Fqn f = Fqn.fromString("/one/two/three");
+      caches.get(0).put(f, "k", "v");
+      caches.get(0).put(f, "k1", "v1");
+      caches.get(0).removeNode(f);
+
+      NodeSPI n = (NodeSPI) caches.get(0).getRoot().getChild(f);
+      DataVersion dv = n.getVersion();
+
+      assert dv instanceof DefaultDataVersion : "Should be an instance of DefaultDataVersion";
+
+      assert ((DefaultDataVersion) dv).getRawVersion() == 3 : "Should have accurate data version";
+
+      // now start next cache instance
+      caches.get(1).start();
+
+      TestingUtil.blockUntilViewsReceived(10000, caches.get(0), caches.get(1));
+
+      assert caches.get(1).get(f, "k")== null : "Should be removed";
+
+      n = (NodeSPI) caches.get(1).getRoot().getChild(f);
+
+      dv = n.getVersion();
+
+      assert dv instanceof DefaultDataVersion : "Should be an instance of DefaultDataVersion";
+
+      assert ((DefaultDataVersion) dv).getRawVersion() == 3 : "Version should have transferred";
+   }
+
+   public void testStateTransferCustomVersionAfterRemoval() throws Exception
+   {
+      Fqn f = Fqn.fromString("/one/two/three");
+      caches.get(0).getInvocationContext().getOptionOverrides().setDataVersion(new CharVersion('A'));
+      caches.get(0).put(f, "k", "v");
+      caches.get(0).getInvocationContext().getOptionOverrides().setDataVersion(new CharVersion('B'));
+      caches.get(0).put(f, "k1", "v1");
+      caches.get(0).getInvocationContext().getOptionOverrides().setDataVersion(new CharVersion('C'));
+      caches.get(0).removeNode(f);
+
+      NodeSPI n = (NodeSPI) caches.get(0).getRoot().getChild(f);
+      DataVersion dv = n.getVersion();
+
+      assert dv instanceof CharVersion : "Should be an instance of CharVersion";
+
+      assert ((CharVersion) dv).version == 'C' : "Should have accurate data version";
+
+      // now start next cache instance
+      caches.get(1).start();
+
+      TestingUtil.blockUntilViewsReceived(10000, caches.get(0), caches.get(1));
+
+      assert caches.get(1).get(f, "k")== null : "Should be removed";
+
+      n = (NodeSPI) caches.get(1).getRoot().getChild(f);
+
+      dv = n.getVersion();
+
+      assert dv instanceof CharVersion : "Should be an instance of CharVersion";
+
+      assert ((CharVersion) dv).version == 'C' : "Version should have transferred";
+   }
+
+   public static class CharVersion implements DataVersion
+   {
+      private char version = 'A';
+
+      public CharVersion(char version)
+      {
+         this.version = version;
+      }
+
+      public boolean newerThan(DataVersion other)
+      {
+         if (other instanceof CharVersion)
+         {
+            CharVersion otherVersion = (CharVersion) other;
+            return version > otherVersion.version;
+         }
+         else
+         {
+            throw new DataVersioningException("Incompatible version types");
+         }
+      }
+   }
+
+}




More information about the jbosscache-commits mailing list