1 7 package org.jboss.cache.buddyreplication; 8 9 import junit.framework.TestCase; 10 import org.jboss.cache.Cache; 11 import org.jboss.cache.CacheImpl; 12 import org.jboss.cache.Fqn; 13 import org.jboss.cache.config.BuddyReplicationConfig; 14 import org.jboss.cache.config.CacheLoaderConfig; 15 import org.jboss.cache.config.Configuration; 16 import org.jboss.cache.factories.XmlConfigurationParser; 17 import org.jboss.cache.loader.CacheLoader; 18 import org.jboss.cache.loader.CacheLoaderManager; 19 import org.jboss.cache.misc.TestingUtil; 20 import org.jboss.cache.xml.XmlHelper; 21 import org.jgroups.Address; 22 import org.w3c.dom.Element ; 23 24 import javax.transaction.TransactionManager ; 25 import java.io.File ; 26 27 32 public abstract class BuddyReplicationTestsBase extends TestCase 33 { 34 protected static int VIEW_BLOCK_TIMEOUT = 5000; 35 36 protected CacheImpl createCache(int numBuddies, String buddyPoolName) throws Exception 37 { 38 return createCache(numBuddies, buddyPoolName, false, true); 39 } 40 41 protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation) throws Exception 42 { 43 return createCache(numBuddies, buddyPoolName, useDataGravitation, true); 44 } 45 46 protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean start) throws Exception 47 { 48 return createCache(false, numBuddies, buddyPoolName, useDataGravitation, true, start); 49 } 50 51 protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean start) throws Exception 52 { 53 return createCache(optimisticLocks, numBuddies, buddyPoolName, useDataGravitation, true, start); 54 } 55 56 protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception 57 { 58 return createCache(false, numBuddies, buddyPoolName, useDataGravitation, removeOnFind, start); 59 } 60 61 protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception 62 { 63 CacheImpl c = new CacheImpl(); 64 c.getConfiguration().setCacheMode("REPL_SYNC"); 65 c.getConfiguration().setClusterName("BuddyReplicationTest"); 66 String xmlString = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" + 68 "<buddyCommunicationTimeout>600000</buddyCommunicationTimeout>\n" + 69 " <buddyLocatorClass>org.jboss.cache.buddyreplication.NextMemberBuddyLocator</buddyLocatorClass>\n" + 70 " <autoDataGravitation>" + useDataGravitation + "</autoDataGravitation>\n" + 71 " <dataGravitationRemoveOnFind>" + removeOnFind + "</dataGravitationRemoveOnFind>\n" + 72 " <buddyLocatorProperties>numBuddies = " + numBuddies + "</buddyLocatorProperties>\n"; 73 74 if (buddyPoolName != null) xmlString += "<buddyPoolName>" + buddyPoolName + "</buddyPoolName>"; 75 xmlString += "</config>"; 76 Element element = XmlHelper.stringToElement(xmlString); 77 BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element); 78 c.getConfiguration().setBuddyReplicationConfig(config); 79 80 c.getConfiguration().setFetchInMemoryState(true); 81 if (optimisticLocks) 82 { 83 c.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC); 84 } 85 c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 86 c.getConfiguration().setSyncCommitPhase(true); 88 configureMultiplexer(c); 90 91 if (start) 92 { 93 c.start(); 94 validateMultiplexer(c); 95 } 96 return c; 97 } 98 99 106 protected void configureMultiplexer(Cache cache) throws Exception 107 { 108 } 110 111 118 protected void validateMultiplexer(Cache cache) 119 { 120 assertFalse("Cache is not using multiplexer", cache.getConfiguration().isUsingMultiplexer()); 121 } 122 123 protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool) throws Exception 124 { 125 return createCaches(1, numCaches, useBuddyPool, false); 126 } 127 128 protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception 129 { 130 return createCaches(1, numCaches, useBuddyPool, useDataGravitation, optimisticLocks); 131 } 132 133 protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception 134 { 135 return createCaches(1, numCaches, useBuddyPool, useDataGravitation); 136 } 137 138 protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation) throws Exception 139 { 140 return this.createCachesWithCacheLoader(numCaches, useDataGravitation, removeOnFind, passivation, false); 141 } 142 143 protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent) throws Exception 144 { 145 String tmpLoc = System.getProperty("java.io.tmpdir", "/tmp"); 146 tmpLoc += File.separator + "BuddyReplicationTestsBase-"; 147 148 CacheImpl[] caches = new CacheImpl[numCaches]; 149 for (int i = 0; i < numCaches; i++) 150 { 151 caches[i] = createCacheWithCacheLoader((tmpLoc + i), useDataGravitation, removeOnFind, passivation, fetchPersistent, true); 152 } 153 154 TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT); 156 TestingUtil.sleepThread(getSleepTimeout()); 157 return caches; 158 } 159 160 protected CacheImpl createCacheWithCacheLoader(String location, boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent, boolean start) throws Exception 161 { 162 CacheImpl cache = createCache(1, null, useDataGravitation, removeOnFind, false); 163 164 String cloader = "<config>\n" + 165 "<passivation>" + passivation + "</passivation>\n" + 166 "<preload></preload>\n" + 167 168 "<cacheloader>\n" + 169 "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 170 "<properties>location=" + (location) + "</properties>\n" + 171 "<async>false</async>\n" + 172 "<shared>false</shared>\n" + 173 "<fetchPersistentState>" + fetchPersistent + "</fetchPersistentState>\n" + 174 "</cacheloader>\n" + 175 "</config>"; 176 177 Element element = XmlHelper.stringToElement(cloader); 178 CacheLoaderConfig cfg = XmlConfigurationParser.parseCacheLoaderConfig(element); 179 cache.getConfiguration().setCacheLoaderConfig(cfg); 180 if (start) 181 { 182 cache.start(); 183 } 184 185 return cache; 186 } 187 188 protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool) throws Exception 189 { 190 return createCaches(numBuddies, numCaches, useBuddyPool, false); 191 } 192 193 protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception 194 { 195 return createCaches(numBuddies, numCaches, useBuddyPool, useDataGravitation, false); 196 } 197 198 protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception 199 { 200 CacheImpl[] caches = new CacheImpl[numCaches]; 201 for (int i = 0; i < numCaches; i++) 202 { 203 caches[i] = createCache(optimisticLocks, numBuddies, useBuddyPool ? Character.toString((char) ('A' + i)) : null, useDataGravitation, true); 204 } 205 206 TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT); 208 TestingUtil.sleepThread(getSleepTimeout()); 209 return caches; 210 } 211 212 protected void cleanup(CacheImpl[] caches) throws Exception 213 { 214 System.out.println("*** cleaning up"); 215 216 if (caches != null) 217 { 218 for (int i = 0; i < caches.length; i++) 219 { 220 if (caches[i] != null) 221 { 222 destroyCache(caches[i]); 223 caches[i] = null; 224 } 225 } 226 } 227 } 228 229 private void destroyCache(CacheImpl c) throws Exception 230 { 231 TransactionManager tm = c.getTransactionManager(); 232 if (tm != null && tm.getTransaction() != null) 233 { 234 try 235 { 236 tm.rollback(); 237 } 238 catch (Exception e) 239 { 240 } 241 } 242 CacheLoaderManager clm = c.getCacheLoaderManager(); 243 if (clm != null) 244 { 245 CacheLoader cl = c.getCacheLoaderManager().getCacheLoader(); 246 if (cl != null) cl.remove(Fqn.ROOT); 247 } 248 c.stop(); 249 } 250 251 protected void printBuddyGroup(CacheImpl cache) 252 { 253 BuddyGroup bg = cache.getBuddyManager().buddyGroup; 254 System.out.println("*** " + bg); 255 System.out.println(" Groups I participate in: " + cache.getBuddyManager().buddyGroupsIParticipateIn.keySet()); 256 } 257 258 261 protected int getSleepTimeout() 262 { 263 return 1000; 264 } 265 266 protected void assertIsBuddy(CacheImpl dataOwner, CacheImpl buddy, boolean onlyBuddy) 267 { 268 Address dataOwnerLocalAddress = dataOwner.getLocalAddress(); 269 Address buddyLocalAddress = buddy.getLocalAddress(); 270 271 System.out.println("*** assert with groups. Testing that " + buddyLocalAddress + " is a buddy for owner " + dataOwnerLocalAddress + " only buddy? " + onlyBuddy); 272 printBuddyGroup(dataOwner); 273 274 BuddyManager dataOwnerBuddyManager = dataOwner.getBuddyManager(); 275 BuddyManager buddyBuddyManager = buddy.getBuddyManager(); 276 277 if (onlyBuddy) assertEquals("Should only have one buddy", 1, dataOwnerBuddyManager.getBuddyAddresses().size()); 279 280 assertTrue(buddyLocalAddress + " should be a buddy to " + dataOwnerLocalAddress, dataOwnerBuddyManager.getBuddyAddresses().contains(buddyLocalAddress)); 281 282 BuddyGroup group = buddyBuddyManager.buddyGroupsIParticipateIn.get(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress)); 284 System.out.println("*** Buddy's version of dataOwner's group " + group); 285 286 assertTrue("buddy's list of groups it participates in should contain data owner's group name", buddyBuddyManager.buddyGroupsIParticipateIn.containsKey(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress))); 287 if (onlyBuddy) assertEquals(1, group.getBuddies().size()); 288 assertTrue(buddyLocalAddress + " should be a buddy to " + group.getGroupName(), group.getBuddies().contains(buddyLocalAddress)); 289 } 290 291 protected void dumpCacheContents(CacheImpl[] caches) 292 { 293 System.out.println("**** START: Cache Contents ****"); 294 for (int i = 0; i < caches.length; i++) 295 { 296 if (caches[i] == null) 297 { 298 System.out.println(" ** Cache " + i + " is null!"); 299 } 300 else 301 { 302 System.out.println(" ** Cache " + i + " is " + caches[i].getLocalAddress()); 303 System.out.println(" " + caches[i].printLockInfo()); 304 } 305 } 306 System.out.println("**** END: Cache Contents ****"); 307 } 308 309 protected void assertNoLocks(CacheImpl[] caches) 310 { 311 for (int i = 0; i < caches.length; i++) 312 { 313 if (caches[i] != null) assertEquals(0, caches[i].getNumberOfLocksHeld()); 314 } 315 } 316 317 } 318 | Popular Tags |