[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/loader ...
Manik Surtani
manik at jboss.org
Mon Jun 18 17:42:20 EDT 2007
User: msurtani
Date: 07/06/18 17:42:20
Modified: tests/functional/org/jboss/cache/loader
DummyInMemoryCacheLoader.java
CacheLoaderTestsBase.java
Log:
Experiment with cache loader thread safety
Revision Changes Path
1.9 +14 -10 JBossCache/tests/functional/org/jboss/cache/loader/DummyInMemoryCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: DummyInMemoryCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/loader/DummyInMemoryCacheLoader.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- DummyInMemoryCacheLoader.java 29 May 2007 13:07:41 -0000 1.8
+++ DummyInMemoryCacheLoader.java 18 Jun 2007 21:42:20 -0000 1.9
@@ -6,6 +6,7 @@
*/
package org.jboss.cache.loader;
+import net.jcip.annotations.ThreadSafe;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
@@ -24,6 +25,7 @@
*
* @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
*/
+ at ThreadSafe
public class DummyInMemoryCacheLoader extends AbstractCacheLoader
{
protected Map<Fqn, DummyNode> nodes = new ConcurrentHashMap<Fqn, DummyNode>();
@@ -41,7 +43,7 @@
public Set getChildrenNames(Fqn fqn) throws Exception
{
- log.debug("Calling getChildrenNames on Fqn " + fqn + ". Data map = " + nodes);
+ if (log.isDebugEnabled()) log.debug("Calling getChildrenNames on Fqn " + fqn + ". Data map = " + nodes);
if (!nodes.containsKey(fqn))
{
log.debug("node not in loader");
@@ -69,8 +71,10 @@
public Map get(Fqn name) throws Exception
{
- Map d = nodes.containsKey(name) ? nodes.get(name).data : null;
- log.debug("Getting data for fqn " + name + " = " + d);
+ DummyNode dn = nodes.get(name);
+ Map d = dn != null ? dn.data : null;
+
+ if (log.isDebugEnabled()) log.debug("Getting data for fqn " + name + " = " + d);
return d;
}
@@ -90,7 +94,7 @@
nodes.put(name, n);
// we need to make sure parents get put in as well.
recursivelyPutParentsIfNeeded(name);
- log.debug("Did a put on " + name + ", data is " + n.data);
+ if (log.isDebugEnabled()) log.debug("Did a put on " + name + ", data is " + n.data);
return old;
}
@@ -105,7 +109,7 @@
nodes.put(name, n);
// we need to make sure parents get put in as well.
recursivelyPutParentsIfNeeded(name);
- log.debug("Did a put on " + name + ", data is " + n.data);
+ if (log.isDebugEnabled()) log.debug("Did a put on " + name + ", data is " + n.data);
}
private void recursivelyPutParentsIfNeeded(Fqn node)
1.49 +169 -1 JBossCache/tests/functional/org/jboss/cache/loader/CacheLoaderTestsBase.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheLoaderTestsBase.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/loader/CacheLoaderTestsBase.java,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -b -r1.48 -r1.49
--- CacheLoaderTestsBase.java 23 May 2007 10:28:54 -0000 1.48
+++ CacheLoaderTestsBase.java 18 Jun 2007 21:42:20 -0000 1.49
@@ -26,12 +26,14 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.CountDownLatch;
/**
* Commons tests for all CacheLoaders
*
* @author Bela Ban
- * @version $Id: CacheLoaderTestsBase.java,v 1.48 2007/05/23 10:28:54 msurtani Exp $
+ * @version $Id: CacheLoaderTestsBase.java,v 1.49 2007/06/18 21:42:20 msurtani Exp $
*/
abstract public class CacheLoaderTestsBase extends AbstractCacheLoaderTestBase
{
@@ -2030,6 +2032,172 @@
}
+ public void testCacheLoaderThreadSafety() throws Exception
+ {
+ final CountDownLatch latch = new CountDownLatch(1);
+ final Fqn fqn = Fqn.fromString("/a/b/c");
+ loader.put(fqn, "k", "v");
+ final int loops = 10000;
+ final Set<Exception> exceptions = new CopyOnWriteArraySet<Exception>();
+
+
+ Thread remover = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ latch.await();
+ for (int i = 0; i < loops; i++)
+ {
+ loader.remove(fqn);
+ }
+ }
+ catch (Exception e)
+ {
+ exceptions.add(e);
+ }
+ }
+ };
+
+ remover.start();
+
+ Thread remover2 = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ latch.await();
+ for (int i = 0; i < loops; i++)
+ {
+ loader.remove(fqn, "k");
+ }
+ }
+ catch (Exception e)
+ {
+ exceptions.add(e);
+ }
+ }
+ };
+
+ remover2.start();
+
+
+ Thread reader = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ latch.await();
+ for (int i = 0; i < loops; i++)
+ {
+ loader.get(fqn);
+ }
+ }
+ catch (Exception e)
+ {
+ exceptions.add(e);
+ }
+ }
+ };
+ reader.start();
+
+ Thread reader2 = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ latch.await();
+ for (int i = 0; i < loops; i++)
+ {
+ loader.getChildrenNames(fqn.getParent());
+ }
+ }
+ catch (Exception e)
+ {
+ exceptions.add(e);
+ }
+ }
+ };
+ reader2.start();
+
+ Thread reader3 = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ latch.await();
+ for (int i = 0; i < loops; i++)
+ {
+ loader.getChildrenNames(fqn);
+ }
+ }
+ catch (Exception e)
+ {
+ exceptions.add(e);
+ }
+ }
+ };
+ reader3.start();
+
+
+ Thread writer = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ latch.await();
+ for (int i = 0; i < loops; i++)
+ {
+ loader.put(fqn, "k", "v");
+ }
+ }
+ catch (Exception e)
+ {
+ exceptions.add(e);
+ }
+ }
+ };
+ writer.start();
+
+ Thread writer2 = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ latch.await();
+ for (int i = 0; i < loops; i++)
+ {
+ loader.put(fqn, new HashMap());
+ }
+ }
+ catch (Exception e)
+ {
+ exceptions.add(e);
+ }
+ }
+ };
+ writer2.start();
+
+
+ latch.countDown();
+ reader.join();
+ reader2.join();
+ reader3.join();
+ remover.join();
+ remover2.join();
+ writer.join();
+ writer2.join();
+
+ for (Exception e : exceptions) throw e;
+ }
+
protected TransactionManager getTransactionManager()
{
return DummyTransactionManager.getInstance();
More information about the jboss-cvs-commits
mailing list