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