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