[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/statetransfer ...
Brian Stansberry
brian.stansberry at jboss.com
Tue Jan 2 22:56:51 EST 2007
User: bstansberry
Date: 07/01/02 22:56:51
Modified: tests/functional/org/jboss/cache/statetransfer
VersionedTestBase.java
Log:
[JBCACHE-913] Test that eviction works properly with state transfer nodes
Revision Changes Path
1.24 +182 -1 JBossCache/tests/functional/org/jboss/cache/statetransfer/VersionedTestBase.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: VersionedTestBase.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/statetransfer/VersionedTestBase.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -b -r1.23 -r1.24
--- VersionedTestBase.java 2 Jan 2007 17:15:30 -0000 1.23
+++ VersionedTestBase.java 3 Jan 2007 03:56:51 -0000 1.24
@@ -7,16 +7,22 @@
package org.jboss.cache.statetransfer;
+import org.jboss.cache.Cache;
import org.jboss.cache.CacheException;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
+import org.jboss.cache.Node;
import org.jboss.cache.Region;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.factories.DefaultCacheFactory;
+import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.loader.CacheLoader;
import org.jboss.cache.marshall.InactiveRegionException;
import org.jboss.cache.misc.TestingUtil;
import java.lang.reflect.Method;
import java.util.Random;
+import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@@ -740,6 +746,181 @@
}
+ /**
+ * Test for JBCACHE-913
+ *
+ * @throws Exception
+ */
+ public void testEvictionSeesStateTransfer() throws Exception
+ {
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ Configuration c = parser.parseFile("META-INF/replSync-eviction-service.xml");
+ Cache cache1 = DefaultCacheFactory.getInstance().createCache(c, false);
+ cache1.start();
+
+ cache1.put(Fqn.fromString("/a/b/c"), "key", "value");
+
+ c = parser.parseFile("META-INF/replSync-eviction-service.xml");
+ Cache cache2 = DefaultCacheFactory.getInstance().createCache(c, false);
+ cache2.start();
+
+ Region region = cache2.getRegion(Fqn.ROOT, false);
+ // We expect events for /a, /a/b and /a/b/c
+ assertEquals("Saw the expected number of node events", 3, region.nodeEventQueueSize());
+ }
+
+ /**
+ * Further test for JBCACHE-913
+ *
+ * @throws Exception
+ */
+ public void testEvictionAfterStateTransfer() throws Exception
+ {
+ XmlConfigurationParser parser = new XmlConfigurationParser();
+ Configuration c = parser.parseFile("META-INF/replSync-eviction-service.xml");
+ Cache cache1 = DefaultCacheFactory.getInstance().createCache(c, false);
+ cache1.start();
+
+ for (int i = 0; i < 25000; i++)
+ {
+ cache1.put(Fqn.fromString("/base/" + i), "key", "base" + i);
+ if (i < 5)
+ cache1.put(Fqn.fromString("/org/jboss/test/data/" + i), "key", "data" + i);
+ }
+
+ c = parser.parseFile("META-INF/replSync-eviction-service.xml");
+ final Cache cache2 = DefaultCacheFactory.getInstance().createCache(c, false);
+ cache2.start();
+
+ Node parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/test/data"));
+ Set children = parent.getChildren();
+ assertEquals("All data children transferred", 5, children.size());
+ parent = cache2.getRoot().getChild(Fqn.fromString("/base"));
+ children = parent.getChildren();
+ assertEquals("All base children transferred", 25000, children.size());
+
+ // Sleep 2.5 secs so the nodes we are about to create in data won't
+ // exceed the 4 sec TTL when eviction thread runs
+ TestingUtil.sleepThread(2500);
+
+ class Putter extends Thread
+ {
+ Cache cache = null;
+ boolean stopped = false;
+ Exception ex = null;
+ public void run()
+ {
+ int i = 25000;
+ while (!stopped)
+ {
+ try
+ {
+ cache.put(Fqn.fromString("/base/" + i), "key", "base" + i);
+ cache.put(Fqn.fromString("/org/jboss/test/data/" + i), "key", "data"+i);
+ i++;
+ }
+ catch (Exception e)
+ {
+ ex = e;
+ }
+ }
+ }
+ }
+ Putter p1= new Putter();
+ p1.cache = cache1;
+ p1.start();
+ Putter p2= new Putter();
+ p2.cache = cache2;
+ p2.start();
+
+ Random rnd = new Random();
+ TestingUtil.sleepThread(rnd.nextInt(200));
+
+ int maxCountBase = 0;
+ int maxCountData = 0;
+ boolean sawBaseDecrease = false;
+ boolean sawDataDecrease = false;
+ long start = System.currentTimeMillis();
+ while ((System.currentTimeMillis() - start) < 10000)
+ {
+ parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/test/data"));
+ children = parent.getChildren();
+ if (children != null)
+ {
+ int dataCount = children.size();
+ if (dataCount < maxCountData)
+ {
+ System.out.println("data " + dataCount + " < " + maxCountData + " elapsed = " + (System.currentTimeMillis() - start));
+ sawDataDecrease = true;
+ }
+ else
+ {
+ maxCountData = dataCount;
+ }
+ }
+
+ parent = cache2.getRoot().getChild(Fqn.fromString("/base"));
+ children = parent.getChildren();
+ if (children != null)
+ {
+ int baseCount = children.size();
+ if (baseCount < maxCountBase)
+ {
+ System.out.println("base " + baseCount + " < " + maxCountBase+ " elapsed = " + (System.currentTimeMillis() - start));
+ sawBaseDecrease = true;
+ }
+ else
+ {
+ maxCountBase = baseCount;
+ }
+ }
+
+ if (sawDataDecrease && sawBaseDecrease)
+ {
+ break;
+ }
+
+ TestingUtil.sleepThread(50);
+ }
+
+ p1.stopped = true;
+ p2.stopped = true;
+ p1.join(1000);
+ p2.join(1000);
+
+ assertTrue("Saw data decrease", sawDataDecrease);
+ assertTrue("Saw base decrease", sawBaseDecrease);
+ assertNull("No exceptions in p1", p1.ex);
+ assertNull("No exceptions in p2", p2.ex);
+
+ // Sleep 5.1 secs so we are sure the eviction thread ran
+ TestingUtil.sleepThread(5100);
+
+ parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/test/data"));
+ children = parent.getChildren();
+ if (children != null)
+ {
+ System.out.println(children.size());
+ assertTrue("Excess children evicted", children.size() <= 5);
+ }
+ parent = cache2.getRoot().getChild(Fqn.fromString("/base"));
+ children = parent.getChildren();
+ if (children != null)
+ {
+ System.out.println(children.size());
+ assertTrue("Excess children evicted", children.size() <= 25000);
+ }
+
+ // Sleep more to let the eviction thread run again,
+ // which will evict all data nodes due to their ttl of 4 secs
+ TestingUtil.sleepThread(5100);
+
+ parent = cache2.getRoot().getChild(Fqn.fromString("/org/jboss/test/data"));
+ children = parent.getChildren();
+ if (children != null)
+ assertEquals("All data children evicted", 0, children.size());
+ }
+
private Object createBen(ClassLoader loader) throws Exception
{
Class addrClazz = loader.loadClass(ADDRESS_CLASSNAME);
More information about the jboss-cvs-commits
mailing list