1 22 23 package org.jboss.cache.pojo.optimistic; 24 25 import junit.framework.TestCase; 26 import org.jboss.cache.Fqn; 27 import org.jboss.cache.CacheListener; 28 import org.jboss.cache.CacheSPI; 29 import org.jboss.cache.pojo.PojoCache; 30 import org.jboss.cache.pojo.PojoCacheFactory; 31 import org.jboss.cache.marshall.MethodCall; 32 import org.jboss.cache.marshall.MethodCallFactory; 33 import org.jboss.cache.marshall.MethodDeclarations; 34 import org.jboss.cache.interceptors.Interceptor; 35 import org.jboss.cache.interceptors.InvocationContextInterceptor; 36 import org.jboss.cache.interceptors.TxInterceptor; 37 import org.jboss.cache.interceptors.OptimisticReplicationInterceptor; 38 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor; 39 import org.jboss.cache.interceptors.OptimisticNodeInterceptor; 40 import org.jboss.cache.transaction.DummyTransactionManager; 41 import org.jboss.cache.misc.TestingUtil; 42 import org.jboss.cache.lock.IsolationLevel; 43 import org.jboss.cache.factories.XmlConfigurationParser; 44 import org.jboss.cache.xml.XmlHelper; 45 import org.jboss.cache.optimistic.TestListener; 46 import org.jboss.cache.optimistic.DefaultDataVersion; 47 import org.jboss.cache.config.Configuration; 48 import org.jboss.cache.config.CacheLoaderConfig; 49 import org.w3c.dom.Element ; 50 51 import javax.transaction.TransactionManager ; 52 import javax.transaction.SystemException ; 53 import java.io.File ; 54 import java.util.Random ; 55 import java.util.List ; 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 59 62 public abstract class AbstractOptimisticTestCase extends TestCase 63 { 64 private int instanceNumber; 65 66 protected Fqn fqn = Fqn.fromString("/blah"); 68 protected String key = "myKey", value = "myValue"; 69 70 protected String getTempDir() 71 { 72 return getTempDir("tempdir"); 73 } 74 75 private String getTempDir(String name) 76 { 77 String tempDir = System.getProperty("java.io.tmpdir", "/tmp"); 78 tempDir = tempDir + File.separator + name; 79 System.out.println("tmpdir property: " + System.getProperty("java.io.tmpdir")); 80 System.out.println("Attempting to create dir [" + tempDir + "]"); 81 File tempDirFile = new File (tempDir); 82 if (!tempDirFile.exists()) 83 { 84 tempDirFile.mkdirs(); 85 } 86 return tempDir; 87 } 88 89 public AbstractOptimisticTestCase(String name) 90 { 91 super(name); 92 } 93 94 protected PojoCache createCacheUnstarted() throws Exception 95 { 96 return createCacheUnstarted(true); 97 } 98 99 protected PojoCache createCacheUnstarted(boolean optimistic) throws Exception 100 { 101 Configuration c = new Configuration(); 102 if (optimistic) c.setNodeLockingScheme("OPTIMISTIC"); 103 104 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 105 c.setCacheMode("LOCAL"); 106 PojoCache cache = PojoCacheFactory.createCache(c, false); 107 return cache; 108 } 109 110 protected PojoCache createCacheWithListener() throws Exception 111 { 112 return createCacheWithListener(new TestListener()); 113 } 114 115 protected PojoCache createCacheWithListener(CacheListener listener) throws Exception 116 { 117 PojoCache cache = createCacheUnstarted(); 118 cache.create(); 119 cache.start(); 120 cache.getCache().addCacheListener(listener); 121 return cache; 122 } 123 124 130 protected PojoCache createCacheWithLoader() throws Exception 131 { 132 return createCacheWithLoader(false); 133 } 134 135 protected CacheLoaderConfig getCacheLoaderConfig(boolean shared, String filename, boolean passivation) throws Exception 136 { 137 String xml = " <config>\n" + 138 " <passivation>" + passivation + "</passivation>\n" + 139 " <preload></preload>\n" + 140 " <shared>" + shared + "</shared>\n" + 141 " <cacheloader>\n" + 142 " <class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 143 " <properties>\n" + 144 " </properties>\n" + 145 " <async>false</async>\n" + 146 " <fetchPersistentState>" + (!shared) + "</fetchPersistentState>\n" + 147 " <ignoreModifications>false</ignoreModifications>\n" + 148 " </cacheloader>\n" + 149 " </config>"; 150 Element element = XmlHelper.stringToElement(xml); 151 return XmlConfigurationParser.parseCacheLoaderConfig(element); 152 } 153 154 protected PojoCache createCacheWithLoader(boolean passivationEnabled) throws Exception 155 { 156 PojoCache cache = createCacheUnstarted(); 157 Configuration c = cache.getCache().getConfiguration(); 158 c.setCacheLoaderConfig(getCacheLoaderConfig(true, getTempDir(), passivationEnabled)); 159 cache.create(); 160 cache.start(); 161 return cache; 162 } 163 164 protected PojoCache createCache() throws Exception 165 { 166 PojoCache cache = createCacheUnstarted(); 167 cache.create(); 168 cache.start(); 169 return cache; 170 } 171 172 protected void destroyCache(PojoCache c) 173 { 174 c.stop(); 175 c.destroy(); 176 } 177 178 179 protected PojoCache createPessimisticCache() throws Exception 180 { 181 Configuration c = new Configuration(); 182 c.setClusterName("name"); 183 c.setInitialStateRetrievalTimeout(5000); 184 c.setClusterConfig(getDefaultProperties()); 185 c.setCacheMode(Configuration.CacheMode.REPL_SYNC); 186 c.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 187 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 188 189 PojoCache cache = PojoCacheFactory.createCache(c, false); 190 cache.create(); 191 cache.start(); 192 193 194 return cache; 195 } 196 197 protected PojoCache createPessimisticCacheLocal() throws Exception 198 { 199 Configuration c = new Configuration(); 200 201 c.setClusterName("name"); 202 c.setInitialStateRetrievalTimeout(5000); 203 c.setClusterConfig(getDefaultProperties()); 204 205 c.setCacheMode(Configuration.CacheMode.LOCAL); 206 c.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 207 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 208 PojoCache cache = PojoCacheFactory.createCache(c, false); 209 cache.create(); 210 cache.start(); 211 212 return cache; 213 } 214 215 protected String getDefaultProperties() 216 { 217 return "UDP(mcast_addr=228.1.2.3;mcast_port=48866;ip_ttl=32;" + 218 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;loopback=true;ip_mcast=true;bind_addr=127.0.0.1):" + 219 "PING(timeout=1000;num_initial_members=2):" + 220 "MERGE2(min_interval=5000;max_interval=10000):" + 221 "FD_SOCK:" + 222 "VERIFY_SUSPECT(timeout=1500):" + 223 "pbcast.NAKACK(gc_lag=50;max_xmit_size=8192;retransmit_timeout=600,1200,2400,4800):" + 224 "UNICAST(timeout=600,1200,2400,4800):" + 225 "pbcast.STABLE(desired_avg_gossip=20000):" + 226 "FRAG(frag_size=8192;down_thread=false;up_thread=false):" + 227 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" + 228 "shun=false;print_local_addr=true):" + 229 "pbcast.STATE_TRANSFER"; 230 } 231 232 protected PojoCache createReplicatedCache(Configuration.CacheMode mode) throws Exception 233 { 234 return createReplicatedCache("test", mode); 235 } 236 237 protected PojoCache createReplicatedCache(String name, Configuration.CacheMode mode) throws Exception 238 { 239 Configuration c = new Configuration(); 240 241 c.setClusterName(name); 242 c.setInitialStateRetrievalTimeout(5000); 243 c.setClusterConfig(getDefaultProperties()); 244 c.setCacheMode(mode); 245 if (mode == Configuration.CacheMode.REPL_SYNC) 246 { 247 c.setSyncCommitPhase(true); 249 c.setSyncRollbackPhase(true); 250 } 251 c.setNodeLockingScheme("OPTIMISTIC"); 252 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 253 PojoCache cache = PojoCacheFactory.createCache(c, false); 254 cache.create(); 255 cache.start(); 256 257 return cache; 258 } 259 260 protected PojoCache createReplicatedCacheWithLoader(boolean shared, Configuration.CacheMode cacheMode) throws Exception 261 { 262 return createReplicatedCacheWithLoader("temp-loader", shared, cacheMode); 263 } 264 265 protected PojoCache createReplicatedCacheWithLoader(boolean shared) throws Exception 266 { 267 return createReplicatedCacheWithLoader("temp-loader", shared, Configuration.CacheMode.REPL_SYNC); 268 } 269 270 protected PojoCache createReplicatedCacheWithLoader(String name, boolean shared) throws Exception 271 { 272 return createReplicatedCacheWithLoader(name, shared, Configuration.CacheMode.REPL_SYNC); 273 } 274 275 protected PojoCache createReplicatedCacheWithLoader(String name, boolean shared, Configuration.CacheMode cacheMode) throws Exception 276 { 277 Configuration c = new Configuration(); 278 c.setClusterName(name); 279 c.setInitialStateRetrievalTimeout(5000); 280 c.setClusterConfig(getDefaultProperties()); 281 c.setCacheMode(cacheMode); 282 c.setSyncCommitPhase(true); 283 c.setSyncRollbackPhase(true); 284 c.setNodeLockingScheme("OPTIMISTIC"); 285 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 286 c.setCacheLoaderConfig(getCacheLoaderConfig(shared, shared ? getTempDir(name + "-shared") : getTempDir(name + instanceNumber++), false)); 287 288 PojoCache cache = PojoCacheFactory.createCache(c, false); 289 cache.create(); 290 cache.start(); 291 return cache; 292 } 293 294 protected Random random; 295 296 protected void randomSleep(int min, int max) 297 { 298 if (random == null) random = new Random (); 299 long l = -1; 300 while (l < min) l = random.nextInt(max); 301 TestingUtil.sleepThread(l); 302 } 303 304 protected void tearDown() 305 { 306 TransactionManager mgr = DummyTransactionManager.getInstance(); 307 try 308 { 309 if (mgr.getTransaction() != null) 310 { 311 mgr.rollback(); 312 } 313 } 314 catch (SystemException e) 315 { 316 } 318 } 319 320 protected Interceptor getAlteredInterceptorChain(Interceptor newLast, CacheSPI spi, boolean replicated) 321 { 322 Interceptor ici = new InvocationContextInterceptor(); 323 ici.setCache(spi); 324 325 Interceptor txInterceptor = new TxInterceptor(); 326 txInterceptor.setCache(spi); 327 328 Interceptor replicationInterceptor = new OptimisticReplicationInterceptor(); 329 replicationInterceptor.setCache(spi); 330 331 Interceptor createInterceptor = new OptimisticCreateIfNotExistsInterceptor(); 332 createInterceptor.setCache(spi); 333 334 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 335 nodeInterceptor.setCache(spi); 336 337 ici.setNext(txInterceptor); 338 if (replicated) 339 { 340 txInterceptor.setNext(replicationInterceptor); 341 replicationInterceptor.setNext(createInterceptor); 342 } 343 else 344 { 345 txInterceptor.setNext(createInterceptor); 346 } 347 createInterceptor.setNext(nodeInterceptor); 348 nodeInterceptor.setNext(newLast); 349 350 return ici; 351 } 352 353 public abstract class ExceptionThread extends Thread 354 { 355 protected Exception exception; 356 357 public void setException(Exception e) 358 { 359 exception = e; 360 } 361 362 public Exception getException() 363 { 364 return exception; 365 } 366 } 367 368 protected List injectDataVersion(List modifications) 369 { 370 List newList = new ArrayList (); 371 Iterator mi = modifications.iterator(); 372 while (mi.hasNext()) 373 { 374 MethodCall c = (MethodCall) mi.next(); 375 Object [] oa = c.getArgs(); 376 Object [] na = new Object [oa.length + 1]; 377 System.out.println("*** " + oa.length); 378 for (int i = 0; i < oa.length; i++) na[i] = oa[i]; 379 na[oa.length] = new DefaultDataVersion(); 380 newList.add(MethodCallFactory.create(MethodDeclarations.getVersionedMethod(c.getMethodId()), na)); 381 } 382 return newList; 383 } 384 385 } 386 | Popular Tags |