1 7 package org.jboss.cache.buddyreplication; 8 9 import EDU.oswego.cs.dl.util.concurrent.Latch; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.jboss.cache.CacheImpl; 13 import org.jboss.cache.misc.TestingUtil; 14 15 import java.util.Map ; 16 17 22 public class BuddyPoolBroadcastTest extends BuddyReplicationTestsBase 23 { 24 25 private CacheImpl[] caches; 26 private Log log = LogFactory.getLog(BuddyPoolBroadcastTest.class); 27 28 private void checkConsistentPoolState(CacheImpl[] caches) 29 { 30 for (int i = 0; i < caches.length; i++) 31 { 32 Map groupMap = caches[i].getBuddyManager().buddyPool; 33 for (int j = 0; j < caches.length; j++) 34 { 35 if (i != j) 36 { 37 Map groupMap2 = caches[j].getBuddyManager().buddyPool; 38 for (CacheImpl cache : caches) 39 { 40 assertEquals("Comparing contents of cache " + (i + 1) + " pool map with cache " + (j + 1), groupMap.get(cache), groupMap2.get(cache)); 41 } 42 } 43 } 44 } 45 } 46 47 48 protected void setUp() throws Exception 49 { 50 log.debug("Starting setUp()"); 51 super.setUp(); 52 log.debug("Finishing setUp()"); 53 } 54 55 protected void tearDown() throws Exception 56 { 57 log.debug("Starting tearDown()"); 58 super.tearDown(); 59 cleanup(caches); 60 log.debug("Finishing tearDown()"); 61 } 62 63 64 public void test2CachesWithPoolNames() throws Exception 65 { 66 log.debug("Running test2CachesWithPoolNames"); 67 caches = createCaches(2, true); 68 69 BuddyManager m = caches[0].getBuddyManager(); 70 Map groupMap = m.buddyPool; 71 72 assertEquals("A", groupMap.get(caches[0].getLocalAddress())); 73 assertEquals("B", groupMap.get(caches[1].getLocalAddress())); 74 } 75 76 public void test3CachesWithPoolNames() throws Exception 77 { 78 log.debug("Running test3CachesWithPoolNames"); 79 caches = createCaches(3, true); 80 81 BuddyManager m = caches[0].getBuddyManager(); 82 Map groupMap = m.buddyPool; 83 84 assertEquals("A", groupMap.get(caches[0].getLocalAddress())); 85 assertEquals("B", groupMap.get(caches[1].getLocalAddress())); 86 assertEquals("C", groupMap.get(caches[2].getLocalAddress())); 87 } 88 89 public void testBuddyPoolSync() throws Exception 90 { 91 log.debug("Running testBuddyPoolSync"); 92 caches = createCaches(3, true); 93 94 Map map = caches[0].getBuddyManager().buddyPool; 95 96 assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress())); 98 assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress())); 99 assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress())); 100 101 checkConsistentPoolState(caches); 103 } 104 105 public void testChangingBuddyPoolMembership() throws Exception 106 { 107 log.debug("Running testChangingBuddyPoolMembership"); 108 caches = createCaches(3, true); 109 110 Map map = caches[0].getBuddyManager().buddyPool; 111 112 assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress())); 114 assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress())); 115 assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress())); 116 117 checkConsistentPoolState(caches); 119 120 caches[1].stop(); 121 caches[1] = createCache(1, "Z"); 122 123 TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT); 124 TestingUtil.sleepThread(getSleepTimeout()); 125 126 assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress())); 128 assertEquals("Failed on cache 1", "Z", map.get(caches[1].getLocalAddress())); 129 assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress())); 130 131 checkConsistentPoolState(caches); 133 } 134 135 public void testConcurrency() throws Exception 136 { 137 log.debug("Running testConcurrency"); 138 int numCaches = 15; 139 caches = new CacheImpl[numCaches]; 140 Latch latch = new Latch(); 141 CacheStarter[] starters = new CacheStarter[numCaches]; 142 143 for (int i = 0; i < numCaches; i++) 144 { 145 caches[i] = createCache(1, new String (new char[]{(char) ('A' + i)}), false, false); 146 starters[i] = new CacheStarter(latch, caches[i]); 147 starters[i].start(); 148 } 149 150 TestingUtil.sleepThread(500); 152 latch.release(); 153 154 TestingUtil.blockUntilViewsReceived(caches, 240000); 156 TestingUtil.sleepThread(1000 * numCaches); 158 Map map = caches[0].getBuddyManager().buddyPool; 160 System.out.println(map); 161 for (int i = 0; i < numCaches; i++) 162 { 163 assertEquals("Failed on cache " + i + "(" + caches[i].getLocalAddress() + ")", new String (new char[]{(char) ('A' + i)}), map.get(caches[i].getLocalAddress())); 164 } 165 166 checkConsistentPoolState(caches); 167 } 168 169 public class CacheStarter extends Thread 170 { 171 private Latch latch; 172 private CacheImpl cache; 173 174 public CacheStarter(Latch latch, CacheImpl cache) 175 { 176 this.latch = latch; 177 this.cache = cache; 178 } 179 180 public void run() 181 { 182 try 183 { 184 latch.acquire(); 185 cache.start(); 186 } 187 catch (Exception e) 188 { 189 e.printStackTrace(); 190 } 191 } 192 } 193 194 } 195 | Popular Tags |