1 7 package org.jboss.cache.buddyreplication; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.CacheImpl; 12 import org.jboss.cache.misc.TestingUtil; 13 14 19 public class BuddyReplicationContentTest extends BuddyReplicationTestsBase 20 { 21 private String key = "key"; 22 private String value = "value"; 23 private CacheImpl[] caches; 24 private Log log = LogFactory.getLog(BuddyGroupAssignmentTest.class); 25 26 27 private void assertNoStaleLocks(CacheImpl[] caches) 28 { 29 for (CacheImpl cache : caches) assertNoStaleLocks(cache); 30 } 31 32 private void assertNoStaleLocks(CacheImpl cache) 33 { 34 assertEquals("Number of locks in cache instance " + cache.toString(true) + " should be 0", 0, cache.getNumberOfLocksHeld()); 35 } 36 37 protected void setUp() throws Exception 38 { 39 log.debug("Starting setUp()"); 40 super.setUp(); 41 log.debug("Finishing setUp()"); 42 } 43 44 protected void tearDown() throws Exception 45 { 46 super.tearDown(); 47 log.debug("Starting tearDown()"); 48 cleanup(caches); 49 log.debug("Finishing tearDown()"); 50 } 51 52 public void testSimplePut() throws Exception 53 { 54 log.debug("Running testSimplePut"); 55 caches = createCaches(3, false); 56 57 String fqn = "/test"; 58 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn; 59 60 assertNoStaleLocks(caches); 61 62 caches[0].put(fqn, key, value); 64 65 assertNoStaleLocks(caches); 66 67 assertEquals(value, caches[0].get(fqn, key)); 69 assertNull("Should be null", caches[1].get(fqn, key)); 70 assertNull("Should be null", caches[2].get(fqn, key)); 71 72 74 assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key)); 75 assertNull("Should be null", caches[2].get(backupFqn, key)); 76 77 assertNoStaleLocks(caches); 78 } 79 80 public void testPutAndRemove() throws Exception 81 { 82 log.debug("Running testPutAndRemove"); 83 caches = createCaches(3, false); 84 85 String fqn = "/test"; 86 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn; 87 88 assertNoStaleLocks(caches); 89 90 caches[0].put(fqn, key, value); 92 93 assertNoStaleLocks(caches); 94 95 assertEquals(value, caches[0].get(fqn, key)); 97 assertNull("Should be null", caches[1].get(fqn, key)); 98 assertNull("Should be null", caches[2].get(fqn, key)); 99 100 102 assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key)); 103 assertNull("Should be null", caches[2].get(backupFqn, key)); 104 105 assertNoStaleLocks(caches); 106 107 caches[0].remove(fqn); 109 assertNoStaleLocks(caches); 110 111 112 assertNull("Should be null", caches[0].get(fqn, key)); 113 assertNull("Should be null", caches[1].get(fqn, key)); 114 assertNull("Should be null", caches[2].get(fqn, key)); 115 116 assertNull("Should be null", caches[0].get(backupFqn, key)); 118 assertNull("Should be null", caches[1].get(backupFqn, key)); 119 assertNull("Should be null", caches[2].get(backupFqn, key)); 120 121 assertNoStaleLocks(caches); 122 } 123 124 public void testPutAndRemove2() throws Exception 125 { 126 log.debug("Running testPutAndRemove2"); 127 caches = createCaches(2, 4, false); 128 129 String fqn = "/test"; 130 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn; 131 132 assertNoStaleLocks(caches); 134 135 caches[0].put(fqn, key, value); 136 137 assertNoStaleLocks(caches); 138 139 assertEquals(value, caches[0].get(fqn, key)); 141 assertNull("Should be null", caches[1].get(fqn, key)); 142 assertNull("Should be null", caches[2].get(fqn, key)); 143 assertNull("Should be null", caches[3].get(fqn, key)); 144 145 assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key)); 147 assertEquals("Buddy should have data in backup tree", value, caches[2].get(backupFqn, key)); 148 assertNull("Should be null", caches[3].get(backupFqn, key)); 149 150 assertNoStaleLocks(caches); 151 152 caches[0].remove(fqn); 154 assertNoStaleLocks(caches); 155 156 assertNull("Should be null", caches[0].get(fqn, key)); 157 assertNull("Should be null", caches[1].get(fqn, key)); 158 assertNull("Should be null", caches[2].get(fqn, key)); 159 assertNull("Should be null", caches[3].get(fqn, key)); 160 161 assertNull("Should be null", caches[0].get(backupFqn, key)); 163 assertNull("Should be null", caches[1].get(backupFqn, key)); 164 assertNull("Should be null", caches[2].get(backupFqn, key)); 165 assertNull("Should be null", caches[3].get(backupFqn, key)); 166 167 assertNoStaleLocks(caches); 168 } 169 170 public void testBuddyJoin() throws Exception 171 { 172 log.debug("Running testBuddyJoin"); 173 caches = createCaches(2, false); 174 CacheImpl cache2 = null; 175 176 try 177 { 178 String fqn = "/test"; 179 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[1].getLocalAddress()) + fqn; 180 181 assertNoStaleLocks(caches); 182 183 caches[1].put(fqn, key, value); 185 186 assertNoStaleLocks(caches); 187 188 assertEquals(value, caches[1].get(fqn, key)); 190 assertNull("Should be null", caches[0].get(fqn, key)); 191 192 assertEquals("Buddy should have data in backup tree", value, caches[0].get(backupFqn, key)); 194 195 assertNoStaleLocks(caches); 196 197 cache2 = createCache(1, null); 199 200 TestingUtil.sleepThread(500); 202 203 assertIsBuddy(caches[1], cache2, true); 205 assertIsBuddy(caches[0], caches[1], true); 207 assertIsBuddy(cache2, caches[0], true); 209 210 assertNull("This backup data should have been removed", caches[0].get(backupFqn, key)); 212 213 assertEquals("Backup state should have been transferred to this new cache instance", value, cache2.get(backupFqn, key)); 215 216 caches[1].remove(fqn); 217 assertNoStaleLocks(caches); 218 219 220 assertNull("Should be null", caches[0].get(fqn, key)); 221 assertNull("Should be null", caches[1].get(fqn, key)); 222 assertNull("Should be null", cache2.get(fqn, key)); 223 224 assertNull("Should be null", caches[0].get(backupFqn, key)); 226 assertNull("Should be null", caches[1].get(backupFqn, key)); 227 assertNull("Should be null", cache2.get(backupFqn, key)); 228 229 assertNoStaleLocks(caches); 230 231 } 232 finally 233 { 234 if (cache2 != null) cache2.stop(); 235 } 236 } 237 238 public void testCompleteStateSurvival() throws Exception 239 { 240 log.debug("Running testCompleteStateSurvival"); 241 caches = null; 242 243 caches = createCaches(3, false, true); 244 caches[0].put("/0", "key", "value"); 245 caches[1].put("/1", "key", "value"); 246 caches[2].put("/2", "key", "value"); 247 248 TestingUtil.sleepThread(getSleepTimeout()); 249 250 caches[2].stop(); 251 252 System.out.println("*** " + caches[0].printLockInfo()); 253 System.out.println("*** " + caches[1].printLockInfo()); 254 System.out.println("*** " + caches[2].printLockInfo()); 255 256 257 TestingUtil.sleepThread(getSleepTimeout()); 258 259 assertEquals("value", caches[0].get("/2", "key")); 260 261 TestingUtil.sleepThread(getSleepTimeout()); 262 263 caches[1].stop(); 264 265 267 268 assertEquals("value", caches[0].get("/0", "key")); 269 assertEquals("value", caches[1].get("/1", "key")); 270 assertEquals("value", caches[2].get("/2", "key")); 271 } 272 } 273 | Popular Tags |