[jboss-cvs] JBossCache/tests/functional/org/jboss/cache ...
Manik Surtani
manik at jboss.org
Fri Jun 15 08:02:58 EDT 2007
User: msurtani
Date: 07/06/15 08:02:58
Modified: tests/functional/org/jboss/cache CallbackTest.java
LifeCycleTest.java
Log:
Patched tests to deal with async notifications being emitted
Revision Changes Path
1.17 +8 -47 JBossCache/tests/functional/org/jboss/cache/CallbackTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CallbackTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/CallbackTest.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- CallbackTest.java 7 Feb 2007 22:06:39 -0000 1.16
+++ CallbackTest.java 15 Jun 2007 12:02:58 -0000 1.17
@@ -5,6 +5,7 @@
import junit.framework.TestSuite;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.lock.IsolationLevel;
+import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.transaction.DummyTransactionManager;
import javax.transaction.NotSupportedException;
@@ -14,7 +15,7 @@
* Tests whether modifications within callbacks (TreeCacheListener) are handled correctly
*
* @author Bela Ban
- * @version $Id: CallbackTest.java,v 1.16 2007/02/07 22:06:39 genman Exp $
+ * @version $Id: CallbackTest.java,v 1.17 2007/06/15 12:02:58 msurtani Exp $
*/
public class CallbackTest extends TestCase
{
@@ -24,6 +25,7 @@
final Fqn FQN_B = Fqn.fromString("/b");
final String KEY = "key";
final String VALUE = "value";
+ private final static long CALLBACK_WAIT_TIME = 200; // ms to wait for notifications to be delivered (as they are async)
protected void setUp() throws Exception
@@ -55,6 +57,7 @@
cache.put(FQN_A, null);
assertTrue(cache.exists(FQN_A));
+ TestingUtil.sleepThread(CALLBACK_WAIT_TIME); // sleep a bit so the callback is processed
assertTrue(cache.exists(FQN_B));//created by callback
assertEquals(cache.getLockTable().size(), 0);
System.out.println("cache locks:\n" + cache.printLockInfo());
@@ -68,6 +71,7 @@
cache.put(FQN_A, null);
assertTrue(cache.exists(FQN_A));
+ TestingUtil.sleepThread(CALLBACK_WAIT_TIME); // sleep a bit so the callback is processed
assertEquals(cache.getLockTable().size(), 0);
System.out.println("cache locks:\n" + cache.printLockInfo());
assertEquals(0, cache.getNumberOfLocksHeld());
@@ -82,6 +86,7 @@
cache.put("/a", null);
assertTrue(cache.exists(FQN_A));
assertTrue(cache.exists(FQN_B));
+ TestingUtil.sleepThread(CALLBACK_WAIT_TIME); // sleep a bit so the callback is processed
assertEquals(cache.getLockTable().size(), 0);
System.out.println("cache locks:\n" + cache.printLockInfo());
assertEquals(0, cache.getNumberOfLocksHeld());
@@ -96,6 +101,7 @@
cache.put(FQN_A, null);
tx.commit();
assertTrue(cache.exists(FQN_A));
+ TestingUtil.sleepThread(CALLBACK_WAIT_TIME); // sleep a bit so the callback is processed
assertEquals(0, cache.getNumberOfLocksHeld());
}
@@ -115,55 +121,10 @@
tx.rollback();
}
assertFalse(cache.exists(FQN_A));
+ TestingUtil.sleepThread(CALLBACK_WAIT_TIME); // sleep a bit so the callback is processed
assertEquals(0, cache.getNumberOfLocksHeld());
- // assertFalse(cache.exists("/a")); -- fix: reason is that CreateIfNotExists interceptor is *before*
- // PessimisticLockInterceptor, which registers for TX completion, we don't even get that far
}
- /*public void testSyncReplicationWith2Caches() throws Exception, NotSupportedException {
- cache=createCache(CacheImpl.REPL_SYNC, IsolationLevel.SERIALIZABLE);
- cache2=createCache(CacheImpl.REPL_SYNC, IsolationLevel.SERIALIZABLE);
- cache.setSyncCommitPhase(true);
- cache2.setSyncCommitPhase(true);
-
- assertEquals(2, cache.getMembers().size());
- System.out.println("view is correct: " + cache.getMembers() + " (2 members)");
- cache2.addTreeCacheListener(new PutListener(cache2));
-
- tx=startTransaction();
- cache.put("/a", null);
- tx.commit();
-
- // 1. cache: put("/a")
- // 2. tx.commit() in cache causes 2PC to cache2
- // 3. cache2 is updated with "/a"
- // 4. listener in cache2 is called, creates FQN
- // 5. cache2 replicates FQN over to cache as part of its own 2PC protocol
-
- assertTrue(cache.exists("/a")); // original modification to cache
- assertTrue(cache.exists(FQN)); // result of replication from cache
- assertTrue(cache2.exists("/a")); // result of replication from cache
- assertTrue(cache2.exists(FQN)); // created by listener, triggered by replication of "/a" from cache
- TransactionTable tx_table1, tx_table2;
- tx_table1=cache.getTransactionTable();
- tx_table2=cache2.getTransactionTable();
- System.out.println("tx_table1=" + tx_table1 + ", tx_table2=" + tx_table2);
- assertEquals(0, tx_table1.getNumLocalTransactions());
- assertEquals(0, tx_table2.getNumLocalTransactions());
- assertEquals(0, tx_table1.getNumGlobalTransactions());
- assertEquals(0, tx_table2.getNumGlobalTransactions());
- }*/
-
- /* public static void main(String[] args) {
- try {
- new CallbackTest().testSyncReplicationWith2Caches();
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- }*/
-
-
CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception
{
Configuration c = new Configuration();
1.12 +48 -28 JBossCache/tests/functional/org/jboss/cache/LifeCycleTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: LifeCycleTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/LifeCycleTest.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- LifeCycleTest.java 10 Jun 2007 19:45:13 -0000 1.11
+++ LifeCycleTest.java 15 Jun 2007 12:02:58 -0000 1.12
@@ -5,18 +5,24 @@
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.jboss.cache.config.CacheLoaderConfig;
import org.jboss.cache.config.Configuration;
+import org.jboss.cache.loader.CacheLoader;
+import org.jboss.cache.loader.FileCacheLoader;
+import org.jboss.cache.loader.FileCacheLoaderConfig;
import org.jboss.cache.transaction.DummyTransactionManager;
import javax.transaction.NotSupportedException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
+import java.util.ArrayList;
+import java.util.List;
/**
* Tests restart (stop-destroy-create-start) of CacheImpl
*
* @author Bela Ban
- * @version $Id: LifeCycleTest.java,v 1.11 2007/06/10 19:45:13 msurtani Exp $
+ * @version $Id: LifeCycleTest.java,v 1.12 2007/06/15 12:02:58 msurtani Exp $
*/
public class LifeCycleTest extends TestCase
{
@@ -128,24 +134,56 @@
public void testFailedStart() throws Exception
{
+ // since listener notifications are now delivered asynchronously via a thread pool, exceptions in cacheStarted()
+ // will not cause the startup to fail. Instead I've changed the test to use a mock cache loader which
+ // will barf.
+
CacheImpl cache = createCache(Configuration.CacheMode.LOCAL);
assertEquals("Correct state", CacheStatus.INSTANTIATED, cache.getCacheStatus());
- DisruptLifecycleListener listener = new DisruptLifecycleListener();
- cache.addCacheListener(listener);
+
+// DisruptLifecycleListener listener = new DisruptLifecycleListener();
+// cache.addCacheListener(listener);
+
+ List<CacheLoaderConfig.IndividualCacheLoaderConfig> list = new ArrayList<CacheLoaderConfig.IndividualCacheLoaderConfig>(1);
+ list.add(new FileCacheLoaderConfig());
+ CacheLoaderConfig clc = new CacheLoaderConfig();
+ clc.setIndividualCacheLoaderConfigs(list);
+ cache.getConfiguration().setCacheLoaderConfig(clc);
cache.create();
+
+ assertNotNull(cache.getCacheLoaderManager());
+
+ // now inject the cache loader with a mock cache loader
+ CacheLoader normalCacheLoader = cache.getCacheLoaderManager().getCacheLoader();
+ CacheLoader barfingCacheLoader = new FileCacheLoader()
+ {
+ public void start()
+ {
+ throw new CacheException("Prevent startup");
+ }
+
+ public void stop()
+ {
+ throw new CacheException("Prevent stop");
+ }
+ };
+
+ cache.getCacheLoaderManager().setCacheLoader(barfingCacheLoader);
+
+
assertEquals("Correct state", CacheStatus.CREATED, cache.getCacheStatus());
try
{
cache.start();
- fail("Listener did not prevent start");
+ fail("Barfing cache loader did not prevent start");
}
- catch (IllegalStateException good)
+ catch (CacheException good)
{
}
assertEquals("Correct state", CacheStatus.FAILED, cache.getCacheStatus());
- cache.removeCacheListener(listener);
+ cache.getCacheLoaderManager().setCacheLoader(normalCacheLoader);
cache.start();
@@ -155,20 +193,20 @@
assertTrue(cache.getNumberOfNodes() > 0);
assertEquals(0, cache.getNumberOfLocksHeld());
- cache.addCacheListener(listener);
+ cache.getCacheLoaderManager().setCacheLoader(barfingCacheLoader);
try
{
cache.stop();
- fail("Listener did not prevent stop");
+ fail("Barfing cache loader did not prevent stop");
}
- catch (IllegalStateException good)
+ catch (CacheException good)
{
}
assertEquals("Correct state", CacheStatus.FAILED, cache.getCacheStatus());
- cache.removeCacheListener(listener);
+ cache.getCacheLoaderManager().setCacheLoader(normalCacheLoader);
cache.stop();
assertEquals("Correct state", CacheStatus.STOPPED, cache.getCacheStatus());
@@ -212,24 +250,6 @@
startCache(c);
}
-
- class DisruptLifecycleListener extends AbstractCacheListener
- {
-
- @Override
- public void cacheStarted(CacheSPI cache)
- {
- throw new IllegalStateException("I don't want to start");
- }
-
- @Override
- public void cacheStopped(CacheSPI cache)
- {
- throw new IllegalStateException("I don't want to stop");
- }
-
- }
-
public static Test suite()
{
return new TestSuite(LifeCycleTest.class);
More information about the jboss-cvs-commits
mailing list