Author: manik.surtani(a)jboss.com
Date: 2008-06-04 11:47:45 -0400 (Wed, 04 Jun 2008)
New Revision: 5950
Modified:
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyAssignmentStateTransferTest.java
Log:
Replaced lousy Thread.sleeps with proper interthread comms
Modified:
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyAssignmentStateTransferTest.java
===================================================================
---
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyAssignmentStateTransferTest.java 2008-06-04
14:32:05 UTC (rev 5949)
+++
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyAssignmentStateTransferTest.java 2008-06-04
15:47:45 UTC (rev 5950)
@@ -6,13 +6,18 @@
*/
package org.jboss.cache.buddyreplication;
+import org.jboss.cache.Cache;
import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
-import org.jboss.cache.util.TestingUtil;
+import org.jboss.cache.notifications.annotation.BuddyGroupChanged;
+import org.jboss.cache.notifications.annotation.CacheListener;
+import org.jboss.cache.notifications.event.Event;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.Test;
import java.util.ArrayList;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
/**
* Tests how groups are formed and disbanded
@@ -30,6 +35,48 @@
return timeout;
}
+ @CacheListener
+ public static class BuddyJoinedListener
+ {
+ CountDownLatch latch;
+
+ public BuddyJoinedListener(CountDownLatch latch)
+ {
+ this.latch = latch;
+ }
+
+ @BuddyGroupChanged
+ public void buddyJoined(Event e)
+ {
+ latch.countDown();
+ }
+ }
+
+ private void createCacheWithLatch(CountDownLatch latch) throws Exception
+ {
+ CacheSPI<Object, Object> cache2 = createCache(1, "TEST", false,
false);
+ cache2.create();
+ cache2.addCacheListener(new BuddyJoinedListener(latch));
+ cache2.start();
+ caches.add(cache2);
+ }
+
+ private void replaceLatch(Cache<?, ?> cache, CountDownLatch newLatch)
+ {
+ BuddyJoinedListener bjl = null;
+ for (Object listener : cache.getCacheListeners())
+ {
+ if (listener instanceof BuddyJoinedListener)
+ {
+ bjl = (BuddyJoinedListener) listener;
+ break;
+ }
+ }
+
+ if (bjl != null) cache.removeCacheListener(bjl);
+ cache.addCacheListener(new BuddyJoinedListener(newLatch));
+ }
+
public void testNonRegionBasedStateTransfer() throws Exception
{
caches = new ArrayList<CacheSPI<Object, Object>>();
@@ -38,27 +85,30 @@
Fqn<String> main = Fqn.fromString("/a/b/c");
caches.get(0).put(main, "name", "Joe");
- caches.add(createCache(1, "TEST", false, true));
+ CountDownLatch latch = new CountDownLatch(1);
+ createCacheWithLatch(latch);
- TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]),
VIEW_BLOCK_TIMEOUT);
- TestingUtil.sleepThread(getSleepTimeout());
+ assert latch.await(getSleepTimeout(), TimeUnit.MILLISECONDS) : "Buddy groups
not formed after " + getSleepTimeout() + " millis!";
Fqn<String> test =
BuddyFqnTransformer.getBackupFqn(caches.get(0).getLocalAddress(), main);
assertEquals("State not transferred", "Joe",
caches.get(1).get(test, "name"));
- caches.add(createCache(1, "TEST", false, true));
+ latch = new CountDownLatch(1);
+ createCacheWithLatch(latch);
- TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]),
VIEW_BLOCK_TIMEOUT);
- TestingUtil.sleepThread(getSleepTimeout());
+ assert latch.await(getSleepTimeout(), TimeUnit.MILLISECONDS) : "Buddy groups
not formed after " + getSleepTimeout() + " millis!";
assertNull("State not transferred", caches.get(2).get(test,
"name"));
+ latch = new CountDownLatch(1);
+ replaceLatch(caches.get(2), latch);
+ replaceLatch(caches.get(0), latch);
// Make 2 the buddy of 0
caches.get(1).stop();
caches.set(1, null);
- TestingUtil.sleepThread(getSleepTimeout());
+ assert latch.await(getSleepTimeout(), TimeUnit.MILLISECONDS) : "Buddy groups
not formed after " + getSleepTimeout() + " millis!";
assertEquals("State transferred", "Joe",
caches.get(2).get(test, "name"));
}
@@ -67,6 +117,8 @@
{
caches = new ArrayList<CacheSPI<Object, Object>>();
+ CountDownLatch latch = new CountDownLatch(4);
+
caches.add(createCache(1, "TEST", false, false));
caches.add(createCache(1, "TEST", false, false));
caches.add(createCache(1, "TEST", false, false));
@@ -74,40 +126,31 @@
// caches[0] is not the backup node for caches[2] (although
// caches[2] *is* the backup node for caches[0]
caches.add(createCache(1, "TEST", false, false));
- caches.get(0).getConfiguration().setInactiveOnStartup(true);
- caches.get(1).getConfiguration().setInactiveOnStartup(true);
- caches.get(2).getConfiguration().setInactiveOnStartup(true);
- caches.get(3).getConfiguration().setInactiveOnStartup(true);
- caches.get(0).getConfiguration().setUseRegionBasedMarshalling(true);
- caches.get(1).getConfiguration().setUseRegionBasedMarshalling(true);
- caches.get(2).getConfiguration().setUseRegionBasedMarshalling(true);
- caches.get(3).getConfiguration().setUseRegionBasedMarshalling(true);
- caches.get(0).start();
- caches.get(1).start();
- caches.get(2).start();
- caches.get(3).start();
- TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]),
VIEW_BLOCK_TIMEOUT);
- TestingUtil.sleepThread(getSleepTimeout());
+ for (Cache c : caches)
+ {
+ c.getConfiguration().setInactiveOnStartup(true);
+ c.getConfiguration().setUseRegionBasedMarshalling(true);
+ c.create();
+ c.addCacheListener(new BuddyJoinedListener(latch));
+ }
+ for (Cache c : caches) c.start();
+
+ assert latch.await(getSleepTimeout(), TimeUnit.MILLISECONDS) : "Buddy groups
not formed after " + getSleepTimeout() + " millis!";
+
Fqn fqnA = Fqn.fromString("/a");
Fqn fqnD = Fqn.fromString("/d");
// FIXME We have to use a hack to get JBC to recognize that our regions are for
marshalling
ClassLoader cl = Fqn.class.getClassLoader();
- caches.get(0).getRegion(fqnA, true).registerContextClassLoader(cl);
- caches.get(1).getRegion(fqnA, true).registerContextClassLoader(cl);
- caches.get(2).getRegion(fqnA, true).registerContextClassLoader(cl);
- caches.get(3).getRegion(fqnA, true).registerContextClassLoader(cl);
- caches.get(0).getRegion(fqnD, true).registerContextClassLoader(cl);
- caches.get(1).getRegion(fqnD, true).registerContextClassLoader(cl);
- caches.get(2).getRegion(fqnD, true).registerContextClassLoader(cl);
- caches.get(3).getRegion(fqnD, true).registerContextClassLoader(cl);
+ for (Cache c : caches)
+ {
+ c.getRegion(fqnA, true).registerContextClassLoader(cl);
+ c.getRegion(fqnD, true).registerContextClassLoader(cl);
+ }
- caches.get(0).getRegion(fqnA, true).activate();
- caches.get(1).getRegion(fqnA, true).activate();
- caches.get(2).getRegion(fqnA, true).activate();
- caches.get(3).getRegion(fqnA, true).activate();
+ for (Cache c : caches) c.getRegion(fqnA, true).activate();
caches.get(0).getRegion(fqnD, true).activate();
caches.get(1).getRegion(fqnD, true).activate();
@@ -127,9 +170,12 @@
assertNull("No backup of /d", caches.get(2).get(testD,
"name"));
// Make 2 the buddy of 0 -- this should cause a push from 0 to 2
+ latch = new CountDownLatch(1);
+ replaceLatch(caches.get(2), latch);
+ replaceLatch(caches.get(0), latch);
caches.get(1).stop();
- TestingUtil.sleepThread(getSleepTimeout());
+ assert latch.await(getSleepTimeout(), TimeUnit.MILLISECONDS) : "Buddy groups
not formed after " + getSleepTimeout() + " millis!";
assertEquals("/a state transferred", "Joe",
caches.get(2).get(testA, "name"));
assertNull("/d state not transferred", caches.get(2).get(testD,
"name"));
@@ -145,22 +191,26 @@
public void testPersistentStateTransfer() throws Exception
{
caches = new ArrayList<CacheSPI<Object, Object>>();
+ CountDownLatch latch = new CountDownLatch(2);
caches.add(createCacheWithCacheLoader(false, false, false, true, false));
caches.get(0).getConfiguration().setFetchInMemoryState(false);
+ caches.get(0).create();
+ caches.get(0).addCacheListener(new BuddyJoinedListener(latch));
caches.get(0).start();
-
Fqn<String> main = Fqn.fromString("/a/b/c");
caches.get(0).put(main, "name", "Joe");
caches.add(createCacheWithCacheLoader(false, false, false, true, false));
caches.get(1).getConfiguration().setFetchInMemoryState(false);
+ caches.get(1).create();
+ caches.get(1).addCacheListener(new BuddyJoinedListener(latch));
+
caches.get(1).start();
- TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]),
VIEW_BLOCK_TIMEOUT);
- TestingUtil.sleepThread(getSleepTimeout());
+ assert latch.await(getSleepTimeout(), TimeUnit.MILLISECONDS) : "Buddy groups
not formed after " + getSleepTimeout() + " millis!";
Fqn test = BuddyFqnTransformer.getBackupFqn(caches.get(0).getLocalAddress(),
main);