Author: manik.surtani(a)jboss.com
Date: 2009-02-26 10:05:48 -0500 (Thu, 26 Feb 2009)
New Revision: 7797
Added:
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoaderConfig.java
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummySharedInMemoryCacheLoaderConfig.java
core/trunk/src/test/java/org/jboss/cache/statetransfer/NBSTCacheLoaderTest.java
Modified:
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferManager.java
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoader.java
core/trunk/src/test/java/org/jboss/cache/statetransfer/NonBlockingStateTransferTest.java
core/trunk/src/test/java/org/jboss/cache/util/TestingUtil.java
Log:
NBST tweaks + test for NBST with loaders
Modified:
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferManager.java
===================================================================
---
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferManager.java 2009-02-26
14:56:19 UTC (rev 7796)
+++
core/trunk/src/main/java/org/jboss/cache/statetransfer/DefaultStateTransferManager.java 2009-02-26
15:05:48 UTC (rev 7797)
@@ -82,7 +82,8 @@
public void start()
{
fetchTransientState = configuration.isFetchInMemoryState();
- fetchPersistentState = cacheLoaderManager != null &&
cacheLoaderManager.isFetchPersistentState();
+ // do not do state transfers if the cache loader config is shared
+ fetchPersistentState = cacheLoaderManager != null &&
cacheLoaderManager.isFetchPersistentState() &&
!configuration.getCacheLoaderConfig().isShared();
stateRetrievalTimeout = configuration.getStateRetrievalTimeout();
}
Modified:
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoader.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoader.java 2009-02-26
14:56:19 UTC (rev 7796)
+++
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoader.java 2009-02-26
15:05:48 UTC (rev 7797)
@@ -290,5 +290,5 @@
public void wipe()
{
nodes.clear();
- }
+ }
}
Added:
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoaderConfig.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoaderConfig.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummyInMemoryCacheLoaderConfig.java 2009-02-26
15:05:48 UTC (rev 7797)
@@ -0,0 +1,40 @@
+package org.jboss.cache.loader.testloaders;
+
+import org.jboss.cache.config.CacheLoaderConfig;
+
+import java.util.Properties;
+import java.io.IOException;
+
+public class DummyInMemoryCacheLoaderConfig extends
CacheLoaderConfig.IndividualCacheLoaderConfig
+{
+ String storeName;
+ public DummyInMemoryCacheLoaderConfig()
+ {
+ setClassName(DummyInMemoryCacheLoader.class.getName());
+ }
+
+ public DummyInMemoryCacheLoaderConfig(String storeName)
+ {
+ setClassName(DummyInMemoryCacheLoader.class.getName());
+ this.storeName = storeName;
+ }
+
+ public void setProperties(Properties p)
+ {
+ if (storeName != null)
+ {
+ if (p == null) p = new Properties();
+ p.setProperty("bin", storeName);
+ }
+ super.setProperties(p);
+ }
+
+ public void setProperties(String s) throws IOException
+ {
+ super.setProperties(s);
+ if (storeName != null)
+ {
+ this.properties.setProperty("bin", storeName);
+ }
+ }
+}
Added:
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummySharedInMemoryCacheLoaderConfig.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummySharedInMemoryCacheLoaderConfig.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/loader/testloaders/DummySharedInMemoryCacheLoaderConfig.java 2009-02-26
15:05:48 UTC (rev 7797)
@@ -0,0 +1,15 @@
+package org.jboss.cache.loader.testloaders;
+
+public class DummySharedInMemoryCacheLoaderConfig extends DummyInMemoryCacheLoaderConfig
+{
+ public DummySharedInMemoryCacheLoaderConfig()
+ {
+ setClassName(DummySharedInMemoryCacheLoader.class.getName());
+ }
+
+ public DummySharedInMemoryCacheLoaderConfig(String storeName)
+ {
+ super(storeName);
+ setClassName(DummySharedInMemoryCacheLoader.class.getName());
+ }
+}
Added: core/trunk/src/test/java/org/jboss/cache/statetransfer/NBSTCacheLoaderTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/statetransfer/NBSTCacheLoaderTest.java
(rev 0)
+++
core/trunk/src/test/java/org/jboss/cache/statetransfer/NBSTCacheLoaderTest.java 2009-02-26
15:05:48 UTC (rev 7797)
@@ -0,0 +1,91 @@
+package org.jboss.cache.statetransfer;
+
+import org.jboss.cache.CacheSPI;
+import org.jboss.cache.config.CacheLoaderConfig;
+import org.jboss.cache.loader.CacheLoader;
+import org.jboss.cache.loader.testloaders.DummySharedInMemoryCacheLoaderConfig;
+import org.jboss.cache.util.TestingUtil;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+
+@Test(groups = "functional", testName =
"statetransfer.NBSTCacheLoaderTest", enabled = false)
+public class NBSTCacheLoaderTest extends NonBlockingStateTransferTest
+{
+ int id;
+ ThreadLocal<Boolean> sharedCacheLoader = new ThreadLocal<Boolean>()
+ {
+ protected Boolean initialValue()
+ {
+ return false;
+ }
+ };
+
+ @Override
+ protected CacheSPI<Object, Object> createCache(String name, boolean start)
throws IOException
+ {
+ CacheSPI<Object, Object> c = super.createCache(name, false);
+
+ CacheLoaderConfig clmc = new CacheLoaderConfig();
+ DummySharedInMemoryCacheLoaderConfig clc = new
DummySharedInMemoryCacheLoaderConfig("store number " + id++);
+ clc.setFetchPersistentState(true);
+ clc.setProperties("debug=true");
+ clmc.setShared(sharedCacheLoader.get());
+ clmc.addIndividualCacheLoaderConfig(clc);
+ c.getConfiguration().setCacheLoaderConfig(clmc);
+
+ if (start) c.start();
+ return c;
+ }
+
+ @Override
+ protected void writeInitialData(final CacheSPI<Object, Object> cache)
+ {
+ super.writeInitialData(cache);
+ cache.evict(A_B);
+ cache.evict(A_C);
+ cache.evict(A_D);
+ }
+
+ protected void verifyInitialDataOnLoader(CacheSPI<Object, Object> c) throws
Exception
+ {
+ CacheLoader l = TestingUtil.getCacheLoader(c);
+ assertEquals("Incorrect name for /a/b on loader", JOE,
l.get(A_B).get("name"));
+ assertEquals("Incorrect age for /a/b on loader", TWENTY,
l.get(A_B).get("age"));
+ assertEquals("Incorrect name for /a/c on loader", BOB,
l.get(A_C).get("name"));
+ assertEquals("Incorrect age for /a/c on loader", FORTY,
l.get(A_C).get("age"));
+ }
+
+ protected void verifyNoData(CacheSPI<Object, Object> c)
+ {
+ assert c.getRoot().getChildrenNames().isEmpty(): "Cache should be
empty!";
+ }
+
+ protected void verifyNoDataOnLoader(CacheSPI<Object, Object> c) throws
Exception
+ {
+ CacheLoader l = TestingUtil.getCacheLoader(c);
+ assertNull("Node /a/b should not exist on loader", l.get(A_B));
+ assertNull("Node /a/c should not exist on loader", l.get(A_C));
+ assertNull("Node /a/d should not exist on loader", l.get(A_D));
+ }
+
+
+ public void testSharedLoader() throws Exception
+ {
+ sharedCacheLoader.set(true);
+ CacheSPI<Object, Object> c1 = createCache("testSharedLoader",
true);
+ writeInitialData(c1);
+
+ // starting the second cache would initialize an in-memory state transfer but not a
persistent one since the loader is shared
+ CacheSPI<Object, Object> c2 = createCache("testSharedLoader",
true);
+
+ TestingUtil.blockUntilViewsReceived(60000, c1, c2);
+
+ verifyInitialDataOnLoader(c1);
+ verifyInitialData(c1);
+
+ verifyNoDataOnLoader(c2);
+ verifyNoData(c2);
+ }
+}
Modified:
core/trunk/src/test/java/org/jboss/cache/statetransfer/NonBlockingStateTransferTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/statetransfer/NonBlockingStateTransferTest.java 2009-02-26
14:56:19 UTC (rev 7796)
+++
core/trunk/src/test/java/org/jboss/cache/statetransfer/NonBlockingStateTransferTest.java 2009-02-26
15:05:48 UTC (rev 7797)
@@ -123,13 +123,13 @@
}
}
- private CacheSPI<Object, Object> createCache(String name)
+ private CacheSPI<Object, Object> createCache(String name) throws IOException
{
return createCache(name, true);
}
- private CacheSPI<Object, Object> createCache(String name, boolean start)
+ protected CacheSPI<Object, Object> createCache(String name, boolean start)
throws IOException
{
Configuration config =
UnitTestConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC);
config.setSyncCommitPhase(true);
@@ -298,7 +298,7 @@
log.info("testSTWithWritingTxThread end - " + testCount);
}
- private void thirdWritingCacheTest(boolean tx, String name) throws
InterruptedException
+ private void thirdWritingCacheTest(boolean tx, String name) throws
InterruptedException, IOException
{
CacheSPI<Object, Object> cache1 = null, cache2 = null, cache3 = null;
try
@@ -336,7 +336,7 @@
}
}
- private void verifyInitialData(CacheSPI<Object, Object> cache2)
+ protected void verifyInitialData(CacheSPI<Object, Object> cache2)
{
assertEquals("Incorrect name for /a/b", JOE, cache2.get(A_B,
"name"));
assertEquals("Incorrect age for /a/b", TWENTY, cache2.get(A_B,
"age"));
@@ -344,7 +344,7 @@
assertEquals("Incorrect age for /a/c", FORTY, cache2.get(A_C,
"age"));
}
- private void writeInitialData(final CacheSPI<Object, Object> cache1)
+ protected void writeInitialData(final CacheSPI<Object, Object> cache1)
{
cache1.put(A_B, "name", JOE);
cache1.put(A_B, "age", TWENTY);
@@ -352,7 +352,7 @@
cache1.put(A_C, "age", FORTY);
}
- private void writingThreadTest(boolean tx, String name) throws InterruptedException
+ private void writingThreadTest(boolean tx, String name) throws InterruptedException,
IOException
{
CacheSPI<Object, Object> cache1 = null, cache2 = null;
try
Modified: core/trunk/src/test/java/org/jboss/cache/util/TestingUtil.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/TestingUtil.java 2009-02-26 14:56:19 UTC
(rev 7796)
+++ core/trunk/src/test/java/org/jboss/cache/util/TestingUtil.java 2009-02-26 15:05:48 UTC
(rev 7797)
@@ -11,6 +11,7 @@
import org.jboss.cache.CacheSPI;
import org.jboss.cache.CacheStatus;
import org.jboss.cache.Fqn;
+import org.jboss.cache.UnitTestCacheFactory;
import org.jboss.cache.commands.CommandsFactory;
import org.jboss.cache.commands.VisitableCommand;
import org.jboss.cache.factories.ComponentRegistry;
@@ -20,17 +21,16 @@
import org.jboss.cache.loader.CacheLoader;
import org.jboss.cache.loader.CacheLoaderManager;
import org.jboss.cache.lock.LockManager;
+import org.jgroups.Channel;
import org.jgroups.JChannel;
+import javax.transaction.TransactionManager;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
-import javax.transaction.TransactionManager;
-import org.jboss.cache.UnitTestCacheFactory;
-import org.jgroups.Channel;
/**
* Utilities for unit testing JBossCache.
@@ -720,4 +720,10 @@
public static int getThreadId() {
return threadID.get();
}
+
+ public static CacheLoader getCacheLoader(Cache<?, ?> c)
+ {
+ CacheLoaderManager clm =
extractComponentRegistry(c).getComponent(CacheLoaderManager.class);
+ return clm == null ? null : clm.getCacheLoader();
+ }
}