Author: manik.surtani(a)jboss.com
Date: 2008-04-29 09:27:54 -0400 (Tue, 29 Apr 2008)
New Revision: 5750
Added:
core/trunk/src/test/java/org/jboss/cache/misc/ViewChangeListener.java
Modified:
core/trunk/src/test/java/org/jboss/cache/api/CacheSPITest.java
core/trunk/src/test/java/org/jboss/cache/loader/SingletonStoreCacheLoaderTest.java
Log:
Better RPC
Modified: core/trunk/src/test/java/org/jboss/cache/api/CacheSPITest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/api/CacheSPITest.java 2008-04-29 13:12:43 UTC
(rev 5749)
+++ core/trunk/src/test/java/org/jboss/cache/api/CacheSPITest.java 2008-04-29 13:27:54 UTC
(rev 5750)
@@ -6,16 +6,13 @@
import org.jboss.cache.config.Configuration.CacheMode;
import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
import org.jboss.cache.misc.TestingUtil;
-import org.jboss.cache.notifications.annotation.CacheListener;
-import org.jboss.cache.notifications.annotation.ViewChanged;
-import org.jboss.cache.notifications.event.ViewChangedEvent;
+import org.jboss.cache.misc.ViewChangeListener;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.List;
-import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@Test(groups = "functional")
@@ -103,31 +100,10 @@
cache2.start();
assertTrue("Cache1 is still coordinator",
cache1.getRPCManager().isCoordinator());
assertFalse("Cache2 is not coordinator",
cache2.getRPCManager().isCoordinator());
- final CountDownLatch latch = new CountDownLatch(1);
- ViewChangeNotifier notifier = new ViewChangeNotifier(latch);
- cache2.addCacheListener(notifier);
+ ViewChangeListener viewChangeListener = new ViewChangeListener(cache2);
cache1.stop();
// wait till cache2 gets the view change notification
- boolean viewChangeReceived = latch.await(60, TimeUnit.SECONDS);
- assert viewChangeReceived : "Should have received a view change!";
+ assert viewChangeListener.waitForViewChange(60, TimeUnit.SECONDS) : "Should
have received a view change!";
assertTrue("Cache2 is coordinator",
cache2.getRPCManager().isCoordinator());
}
-
- @CacheListener
- public static class ViewChangeNotifier
- {
- CountDownLatch latch;
-
- public ViewChangeNotifier(CountDownLatch latch)
- {
- this.latch = latch;
- }
-
- @ViewChanged
- public void handleViewChange(ViewChangedEvent e)
- {
- if (!e.isPre()) latch.countDown();
- }
- }
-
}
\ No newline at end of file
Modified:
core/trunk/src/test/java/org/jboss/cache/loader/SingletonStoreCacheLoaderTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/loader/SingletonStoreCacheLoaderTest.java 2008-04-29
13:12:43 UTC (rev 5749)
+++
core/trunk/src/test/java/org/jboss/cache/loader/SingletonStoreCacheLoaderTest.java 2008-04-29
13:27:54 UTC (rev 5750)
@@ -15,6 +15,7 @@
import org.jboss.cache.config.Configuration.CacheMode;
import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
import org.jboss.cache.factories.XmlConfigurationParser;
+import org.jboss.cache.misc.ViewChangeListener;
import org.jboss.cache.xml.XmlHelper;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.AfterMethod;
@@ -36,7 +37,7 @@
*
* @author <a href="mailto:galder.zamarreno@jboss.com">Galder
Zamarreno</a>
*/
-@Test(groups = {"functional"})
+@Test(groups = "functional")
public class SingletonStoreCacheLoaderTest extends AbstractCacheLoaderTestBase
{
private static final Log log =
LogFactory.getLog(SingletonStoreCacheLoaderTest.class);
@@ -146,8 +147,10 @@
assertFalse(cl3.exists(fqn("/e")));
assertFalse(cl3.exists(fqn("/e/f/g")));
+ ViewChangeListener viewChangeListener = new ViewChangeListener(cache2);
+
stopCache1();
- Thread.sleep(1000);
+ viewChangeListener.waitForViewChange(60, TimeUnit.SECONDS);
SingletonStoreCacheLoader scl2 = (SingletonStoreCacheLoader)
cache2.getCacheLoaderManager().getCacheLoader();
waitForPushStateCompletion(scl2.getPushStateFuture());
@@ -178,8 +181,9 @@
assertFalse(cl3.exists(fqn("/e/f/h")));
assertFalse(cl3.exists(fqn("/i")));
+ viewChangeListener = new ViewChangeListener(cache3);
stopCache2();
- Thread.sleep(1000);
+ viewChangeListener.waitForViewChange(60, TimeUnit.SECONDS);
SingletonStoreCacheLoader scl3 = (SingletonStoreCacheLoader)
cache3.getCacheLoaderManager().getCacheLoader();
waitForPushStateCompletion(scl3.getPushStateFuture());
@@ -271,9 +275,7 @@
return new ActiveStatusModifier(mscl);
}
- protected CacheLoaderConfig getSingletonStoreCacheLoaderConfig(String
cacheloaderClass, boolean singletonEnabled,
- boolean
pushStateWhenCoordinator,
- int
pushStateWhenCoordinatorTimeout) throws Exception
+ protected CacheLoaderConfig getSingletonStoreCacheLoaderConfig(String
cacheloaderClass) throws Exception
{
String xml = "<config>\n" +
"<passivation>false</passivation>\n" +
@@ -297,13 +299,13 @@
private void initSingletonNonPushCache(CacheSPI cache) throws Exception
{
cache.getConfiguration().setCacheLoaderConfig(getSingletonStoreCacheLoaderConfig(
- DummyInMemoryCacheLoader.class.getName(), true, false, 1000));
+ DummyInMemoryCacheLoader.class.getName()));
}
private void initSingletonWithPushCache(CacheSPI cache) throws Exception
{
cache.getConfiguration().setCacheLoaderConfig(getSingletonStoreCacheLoaderConfig(
- DummyInMemoryCacheLoader.class.getName(), true, true, 1000));
+ DummyInMemoryCacheLoader.class.getName()));
}
private CacheLoader getDelegatingCacheLoader(CacheSPI cache)
Added: core/trunk/src/test/java/org/jboss/cache/misc/ViewChangeListener.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/misc/ViewChangeListener.java
(rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/misc/ViewChangeListener.java 2008-04-29
13:27:54 UTC (rev 5750)
@@ -0,0 +1,55 @@
+package org.jboss.cache.misc;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.notifications.annotation.CacheListener;
+import org.jboss.cache.notifications.annotation.ViewChanged;
+import org.jboss.cache.notifications.event.ViewChangedEvent;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A class that registers a cache listener with a given cache, and waits for a view
change on the cache.
+ * <p/>
+ * Sample usage:
+ * <pre>
+ * Cache c = getCache();
+ * ViewChangeListener vcl = new ViewChangeListener(c);
+ * assert vcl.waitForViewChange(60, TimeUnit.SECONDS); // will block for up to 60
seconds for a view change on cache c
+ * </pre>
+ */
+@CacheListener
+public class ViewChangeListener
+{
+ CountDownLatch latch;
+
+ /**
+ * Constructs a view change listener
+ *
+ * @param cache cache to listen on for view change events
+ */
+ public ViewChangeListener(Cache cache)
+ {
+ this.latch = new CountDownLatch(1);
+ cache.addCacheListener(this);
+ }
+
+ @ViewChanged
+ public void handleViewChange(ViewChangedEvent e)
+ {
+ if (!e.isPre()) latch.countDown();
+ }
+
+ /**
+ * Waits for up to millis milliseconds for a view change to be received.
+ *
+ * @param timeout length of time to wait for a view change
+ * @param unit time unit to use
+ * @return true if a view change is received, false otherwise.
+ * @throws InterruptedException
+ */
+ public boolean waitForViewChange(long timeout, TimeUnit unit) throws
InterruptedException
+ {
+ return latch.await(timeout, unit);
+ }
+}
\ No newline at end of file
Show replies by date