[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/jmx ...
Manik Surtani
manik at jboss.org
Fri Jun 15 08:37:56 EDT 2007
User: msurtani
Date: 07/06/15 08:37:56
Modified: tests/functional/org/jboss/cache/jmx
CacheJmxWrapperTest.java NotificationTest.java
Log:
Patched tests to deal with async notifications being emitted, plus proper notifier shutdown/draining
Revision Changes Path
1.9 +49 -28 JBossCache/tests/functional/org/jboss/cache/jmx/CacheJmxWrapperTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CacheJmxWrapperTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/jmx/CacheJmxWrapperTest.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -b -r1.8 -r1.9
--- CacheJmxWrapperTest.java 11 Jun 2007 12:58:17 -0000 1.8
+++ CacheJmxWrapperTest.java 15 Jun 2007 12:37:56 -0000 1.9
@@ -1,12 +1,16 @@
package org.jboss.cache.jmx;
-import org.jboss.cache.AbstractCacheListener;
import org.jboss.cache.Cache;
-import org.jboss.cache.CacheSPI;
+import org.jboss.cache.CacheException;
+import org.jboss.cache.CacheImpl;
import org.jboss.cache.CacheStatus;
import org.jboss.cache.Fqn;
+import org.jboss.cache.config.CacheLoaderConfig;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.Configuration.CacheMode;
+import org.jboss.cache.loader.CacheLoader;
+import org.jboss.cache.loader.FileCacheLoader;
+import org.jboss.cache.loader.FileCacheLoaderConfig;
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.transaction.DummyTransactionManagerLookup;
import org.jgroups.Address;
@@ -15,6 +19,7 @@
import javax.management.ObjectName;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
+import java.util.ArrayList;
import java.util.List;
/**
@@ -283,7 +288,7 @@
cache.start();
System.out.println("cache locks after restart:\n" + cache.getLockInfo());
- assertTrue(cache.getNumberOfNodes() > 0);
+ assertEquals(0, cache.getNumberOfNodes());
assertEquals(0, cache.getNumberOfAttributes());
cache.stop();
@@ -294,25 +299,59 @@
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.
+
CacheJmxWrapper wrapper = new CacheJmxWrapper(createCache(createConfiguration()));
registerWrapper(wrapper);
assertEquals("Correct state", CacheStatus.INSTANTIATED, wrapper.getCacheStatus());
- DisruptLifecycleListener listener = new DisruptLifecycleListener();
- wrapper.getCache().addCacheListener(listener);
+// DisruptLifecycleListener listener = new DisruptLifecycleListener();
+// wrapper.getCache().addCacheListener(listener);
+
+
+ List<CacheLoaderConfig.IndividualCacheLoaderConfig> list = new ArrayList<CacheLoaderConfig.IndividualCacheLoaderConfig>(1);
+ list.add(new FileCacheLoaderConfig());
+ CacheLoaderConfig clc = new CacheLoaderConfig();
+ clc.setIndividualCacheLoaderConfigs(list);
+ wrapper.getCache().getConfiguration().setCacheLoaderConfig(clc);
+
wrapper.create();
+
+ CacheImpl ci = (CacheImpl) wrapper.getCache();
+
+ assertNotNull(ci.getCacheLoaderManager());
+
+ // now inject the cache loader with a mock cache loader
+ CacheLoader normalCacheLoader = ci.getCacheLoaderManager().getCacheLoader();
+ CacheLoader barfingCacheLoader = new FileCacheLoader()
+ {
+ public void start()
+ {
+ throw new CacheException("Prevent startup");
+ }
+
+ public void stop()
+ {
+ throw new CacheException("Prevent stop");
+ }
+ };
+
+ ci.getCacheLoaderManager().setCacheLoader(barfingCacheLoader);
+
assertEquals("Correct state", CacheStatus.CREATED, wrapper.getCacheStatus());
try
{
wrapper.start();
fail("Listener did not prevent start");
}
- catch (IllegalStateException good)
+ catch (CacheException good)
{
}
assertEquals("Correct state", CacheStatus.FAILED, wrapper.getCacheStatus());
- wrapper.getCache().removeCacheListener(listener);
+ ci.getCacheLoaderManager().setCacheLoader(normalCacheLoader);
wrapper.start();
@@ -322,20 +361,20 @@
assertTrue(wrapper.getNumberOfNodes() > 0);
assertEquals(0, wrapper.getNumberOfAttributes());
- wrapper.getCache().addCacheListener(listener);
+ ci.getCacheLoaderManager().setCacheLoader(barfingCacheLoader);
try
{
wrapper.stop();
fail("Listener did not prevent stop");
}
- catch (IllegalStateException good)
+ catch (CacheException good)
{
}
assertEquals("Correct state", CacheStatus.FAILED, wrapper.getCacheStatus());
- wrapper.getCache().removeCacheListener(listener);
+ ci.getCacheLoaderManager().setCacheLoader(normalCacheLoader);
wrapper.stop();
assertEquals("Correct state", CacheStatus.STOPPED, wrapper.getCacheStatus());
@@ -413,22 +452,4 @@
assertTrue("No spaces", html.indexOf(' ') == -1);
}
-
-
- 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");
- }
-
- }
}
1.5 +27 -20 JBossCache/tests/functional/org/jboss/cache/jmx/NotificationTest.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: NotificationTest.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/jmx/NotificationTest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- NotificationTest.java 4 Jun 2007 22:56:10 -0000 1.4
+++ NotificationTest.java 15 Jun 2007 12:37:56 -0000 1.5
@@ -11,6 +11,7 @@
import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
import org.jboss.cache.factories.XmlConfigurationParser;
import org.jboss.cache.loader.CacheLoader;
+import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.xml.XmlHelper;
import org.w3c.dom.Element;
@@ -26,7 +27,7 @@
* Functional tests for CacheJmxWrapper broadcast of cache event notifications
*
* @author Jerry Gauthier
- * @version $Id: NotificationTest.java,v 1.4 2007/06/04 22:56:10 bstansberry Exp $
+ * @version $Id: NotificationTest.java,v 1.5 2007/06/15 12:37:56 msurtani Exp $
*/
public class NotificationTest extends TestCase
{
@@ -36,6 +37,7 @@
protected static final String CURRENCY = "currency";
protected static final String POPULATION = "population";
protected static final String EUROPE_NODE = "Europe";
+ protected static final int LISTENER_WAIT_TIME = 200; // ms to wait for notifications to be delivered
public enum Type
{
@@ -162,6 +164,7 @@
m_server.removeNotificationListener(mgmt, listener);
// run the tests
+ TestingUtil.sleepThread(LISTENER_WAIT_TIME);
assertTrue("Expected CacheStarted notification", events.contains(Type.STARTED));
assertTrue("Expected CacheStopped notification", events.contains(Type.STOPPED));
assertTrue("Expected NodeCreated notification", events.contains(Type.PRECREATE));
@@ -200,6 +203,7 @@
cache.start();
try
{
+ TestingUtil.sleepThread(LISTENER_WAIT_TIME);
assertTrue("Expected CacheStarted notification", events.contains(Type.STARTED));
validateHealthyListener(listener);
}
@@ -232,6 +236,7 @@
cache.put("Europe/Albania", albania);
// run the tests
+ TestingUtil.sleepThread(LISTENER_WAIT_TIME);
assertTrue("Expected NodeModified notification", events.contains(Type.PREMODIFY));
assertTrue("Expected NodeModified notification", events.contains(Type.POSTMODIFY));
validateHealthyListener(listener);
@@ -258,6 +263,7 @@
boolean ok = false;
try
{
+ TestingUtil.sleepThread(LISTENER_WAIT_TIME);
assertTrue("Expected CacheStarted notification", events.contains(Type.STARTED));
m_server.removeNotificationListener(mgmt, listener);
@@ -268,6 +274,7 @@
cache.stop();
if (ok)
{
+ TestingUtil.sleepThread(LISTENER_WAIT_TIME);
assertFalse("Expected no CacheStopped notification", events.contains(Type.STOPPED));
validateHealthyListener(listener);
}
More information about the jboss-cvs-commits
mailing list