KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > CacheTest


1 /*
2  * Created on 17-Feb-2005
3  *
4  */

5 package org.jboss.cache.optimistic;
6
7 import junit.framework.Assert;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.GlobalTransaction;
13 import org.jboss.cache.OptimisticTransactionEntry;
14 import org.jboss.cache.TransactionTable;
15 import org.jboss.cache.config.Configuration;
16 import org.jboss.cache.loader.SamplePojo;
17 import org.jboss.cache.marshall.MethodCall;
18 import org.jboss.cache.marshall.MethodCallFactory;
19 import org.jboss.cache.marshall.MethodDeclarations;
20 import org.jboss.cache.transaction.DummyTransactionManager;
21
22 import javax.transaction.RollbackException JavaDoc;
23 import javax.transaction.Transaction JavaDoc;
24 import javax.transaction.TransactionManager JavaDoc;
25 import java.util.List JavaDoc;
26
27 public class CacheTest extends AbstractOptimisticTestCase
28 {
29    Log log = LogFactory.getLog(CacheTest.class);
30
31    public CacheTest(String JavaDoc s)
32    {
33       super(s);
34    }
35
36    private CacheImpl c;
37
38    protected void setUp() throws Exception JavaDoc
39    {
40       c = createCache();
41    }
42
43    protected void tearDown()
44    {
45       super.tearDown();
46       if (c != null)
47          destroyCache(c);
48       c = null;
49    }
50
51    public void testExplicitTxFailure() throws Exception JavaDoc
52    {
53       // explicit.
54
TransactionManager mgr = c.getTransactionManager();
55       try
56       {
57          mgr.begin();
58          c.put("/a", "k", "v");
59          Transaction t = mgr.suspend();
60          c.put("/a", "k2", "v2");
61          mgr.resume(t);
62          mgr.commit();
63          Assert.assertTrue("Expecting a rollback exception!", false);
64       }
65       catch (RollbackException JavaDoc re)
66       {
67          Assert.assertTrue("Expecting a rollback exception!", true);
68       }
69    }
70
71    public void testImplicitTxFailure() throws Exception JavaDoc
72    {
73       // implicit (much harder to orchestrate...
74
int numThreads = 50;
75       ExceptionThread thread[] = new ExceptionThread[numThreads];
76
77       for (int i = 0; i < numThreads; i++)
78       {
79          thread[i] = new ExceptionThread()
80          {
81             public void run()
82             {
83                try
84                {
85                   c.put("/a", "k", "v");
86                }
87                catch (Exception JavaDoc e)
88                {
89                   log.fatal("*** Thew an exception!!", e);
90                   setException(e);
91                }
92             }
93          };
94       }
95
96       for (int i = 0; i < numThreads; i++) thread[i].start();
97       for (int i = 0; i < numThreads; i++) thread[i].join();
98       // test exceptions.
99
for (int i = 0; i < numThreads; i++)
100       {
101          Assert.assertNull("Thread " + thread[i].getName() + " threw exception!", thread[i].getException());
102       }
103    }
104
105    public void testLocalTransaction() throws Exception JavaDoc
106    {
107       MockInterceptor dummy = new MockInterceptor();
108       dummy.setCache(c);
109
110       c.setInterceptorChain(getAlteredInterceptorChain(dummy, c, true));
111
112       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
113       assertNull(mgr.getTransaction());
114
115       mgr.begin();
116
117       assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
118       assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
119
120       SamplePojo pojo = new SamplePojo(21, "test");
121
122       c.put("/one/two", "key1", pojo);
123
124       mgr.commit();
125
126       assertNull(mgr.getTransaction());
127       assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
128       assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
129
130       //make sure all calls were done in right order
131

132       List JavaDoc calls = dummy.getAllCalled();
133
134       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
135       assertEquals(MethodDeclarations.commitMethod, calls.get(1));
136    }
137
138    public void testRollbackTransaction() throws Exception JavaDoc
139    {
140
141       destroyCache(c);
142       c = createCacheWithListener();
143
144       MockInterceptor dummy = new MockInterceptor();
145       dummy.setCache(c);
146
147       c.setInterceptorChain(getAlteredInterceptorChain(dummy, c, true));
148
149       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
150       assertNull(mgr.getTransaction());
151       assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
152       assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
153
154       SamplePojo pojo = new SamplePojo(21, "test");
155       mgr.begin();
156       c.put("/one/two", "key1", pojo);
157       mgr.rollback();
158       assertNull(mgr.getTransaction());
159       assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
160       assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
161
162       //make sure all calls were done in right order
163

164       List JavaDoc calls = dummy.getAllCalled();
165
166       assertEquals(1, calls.size());
167       assertEquals(MethodDeclarations.rollbackMethod, calls.get(0));
168    }
169
170    public void testRemotePrepareTransaction() throws Throwable JavaDoc
171    {
172       destroyCache(c);
173       c = createCacheWithListener();
174
175       MockInterceptor dummy = new MockInterceptor();
176       dummy.setCache(c);
177
178       c.setInterceptorChain(getAlteredInterceptorChain(dummy, c, true));
179       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
180
181       //start local transaction
182
mgr.begin();
183       Transaction tx = mgr.getTransaction();
184
185       //this sets
186
c.getCurrentTransaction(tx);
187
188       SamplePojo pojo = new SamplePojo(21, "test");
189
190       c.put("/one/two", "key1", pojo);
191
192       GlobalTransaction gtx = c.getCurrentTransaction(tx);
193       TransactionTable table = c.getTransactionTable();
194       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
195       assertNotNull(mgr.getTransaction());
196       mgr.commit();
197
198
199       GlobalTransaction remoteGtx = new GlobalTransaction();
200
201       remoteGtx.setAddress(new DummyAddress());
202       //hack the method call to make it have the remote gtx
203
MethodCall meth = entry.getModifications().get(0);
204
205       meth.getArgs()[0] = remoteGtx;
206       //call our remote method
207
MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
208       c._replicate(prepareMethod);
209
210       //our thread should be null
211
assertNull(mgr.getTransaction());
212
213       // there should be a registration for the remote gtx
214
assertNotNull(table.get(remoteGtx));
215       assertNotNull(table.getLocalTransaction(remoteGtx));
216       //assert that this is populated
217
assertEquals(1, table.get(remoteGtx).getModifications().size());
218
219       //assert that the remote prepare has populated the local workspace
220
OptimisticTransactionEntry opEntry = (OptimisticTransactionEntry) table.get(gtx);
221
222       assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
223       assertEquals(1, entry.getModifications().size());
224       List JavaDoc calls = dummy.getAllCalled();
225       assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
226
227       assertEquals(1, c.getTransactionTable().getNumGlobalTransactions());
228       assertEquals(1, c.getTransactionTable().getNumLocalTransactions());
229    }
230
231    public void testRemoteCacheBroadcast() throws Exception JavaDoc
232    {
233       destroyCache(c);
234
235       CacheImpl cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
236       CacheImpl cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
237       assertEquals(2, cache.getMembers().size());
238       assertEquals(2, cache2.getMembers().size());
239
240
241       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
242
243       //start local transaction
244
mgr.begin();
245       Transaction tx = mgr.getTransaction();
246
247       //this sets
248
GlobalTransaction gtx = cache.getCurrentTransaction(tx);
249
250       SamplePojo pojo = new SamplePojo(21, "test");
251
252       cache.put("/one/two", "key1", pojo);
253
254       //GlobalTransaction gtx = cache.getCurrentTransaction(tx);
255
TransactionTable table = cache.getTransactionTable();
256       assertNotNull(mgr.getTransaction());
257       mgr.commit();
258
259
260       assertNull(mgr.getTransaction());
261
262       //assert that the local cache is in the right state
263
assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
264       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
265
266       assertTrue(cache.exists(Fqn.fromString("/one/two")));
267       assertTrue(cache.exists(Fqn.fromString("/one")));
268       assertEquals(pojo, cache.get(Fqn.fromString("/one/two"), "key1"));
269
270       assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
271       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
272
273       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
274       assertTrue(cache2.exists(Fqn.fromString("/one")));
275       assertEquals(pojo, cache2.get(Fqn.fromString("/one/two"), "key1"));
276
277
278       destroyCache(cache);
279       destroyCache(cache2);
280    }
281
282
283    public void testTwoWayRemoteCacheBroadcast() throws Exception JavaDoc
284    {
285
286       destroyCache(c);
287       CacheImpl cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
288       CacheImpl cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
289       assertEquals(2, cache.getMembers().size());
290       assertEquals(2, cache2.getMembers().size());
291
292
293       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
294
295       //start local transaction
296
mgr.begin();
297       Transaction tx = mgr.getTransaction();
298
299       //this sets
300
cache.getCurrentTransaction(tx);
301
302       SamplePojo pojo = new SamplePojo(21, "test");
303
304       cache.put("/one/two", "key1", pojo);
305
306       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
307       TransactionTable table = cache.getTransactionTable();
308       assertNotNull(mgr.getTransaction());
309       mgr.commit();
310
311
312       assertNull(mgr.getTransaction());
313
314       //assert that the local cache is in the right state
315
assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
316       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
317
318       assertTrue(cache.exists(Fqn.fromString("/one/two")));
319       assertTrue(cache.exists(Fqn.fromString("/one")));
320       assertEquals(pojo, cache.get(Fqn.fromString("/one/two"), "key1"));
321
322
323       assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
324       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
325
326       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
327       assertTrue(cache2.exists(Fqn.fromString("/one")));
328
329       assertEquals(pojo, cache2.get(Fqn.fromString("/one/two"), "key1"));
330
331
332       destroyCache(cache);
333       destroyCache(cache2);
334
335
336    }
337
338
339    public void testRemotePessCacheBroadcast() throws Exception JavaDoc
340    {
341       destroyCache(c);
342
343       CacheImpl cache = createPessimisticCache();
344       CacheImpl cache2 = createPessimisticCache();
345
346       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
347
348       //start local transaction
349
mgr.begin();
350       Transaction tx = mgr.getTransaction();
351
352       //this sets
353
cache.getCurrentTransaction(tx);
354
355       SamplePojo pojo = new SamplePojo(21, "test");
356
357       cache.put("/one/two", "key1", pojo);
358
359
360       mgr.commit();
361
362       destroyCache(cache);
363       destroyCache(cache2);
364
365    }
366
367    public void testConcurrentNodeRemoval() throws Exception JavaDoc
368    {
369       c.put(fqn, "key", "value");
370
371       // now start a tx to change the value in fqn
372
TransactionManager mgr = c.getTransactionManager();
373       mgr.begin();
374
375       c.put(fqn, "key2", "value2");
376
377       Transaction tx = mgr.suspend();
378
379       // now remove the original node...
380
c.remove(fqn);
381
382       mgr.resume(tx);
383       // now try and commit this - this should fail.
384
boolean ok = false;
385       try
386       {
387          mgr.commit();
388       }
389       catch (RollbackException JavaDoc rbe)
390       {
391          ok = true;
392       }
393
394       Assert.assertTrue("Concurrent mod should result in a rollback", ok);
395       // now assert that the node has in fact been removed.
396
Assert.assertTrue("The node should have been removed!", !c.exists(fqn));
397
398    }
399
400    public void testConcurrentNodeModification() throws Exception JavaDoc
401    {
402       c.put(fqn, "key", "value");
403
404       // now start a tx to change the value in fqn
405
TransactionManager mgr = c.getTransactionManager();
406       mgr.begin();
407
408       c.put(fqn, "key2", "value2");
409
410       Transaction tx = mgr.suspend();
411
412       // now change the original node...
413
c.put(fqn, "key3", "value3");
414
415       mgr.resume(tx);
416       // now try and commit this - this should fail.
417
boolean ok = false;
418       try
419       {
420          mgr.commit();
421       }
422       catch (RollbackException JavaDoc rbe)
423       {
424          ok = true;
425       }
426
427       Assert.assertTrue("Concurrent mod should result in a rollback", ok);
428    }
429
430    public void testRemoveAndCreate() throws Exception JavaDoc
431    {
432       c = createCache();
433       c.put(fqn, "key", "value");
434       TransactionManager tm = c.getTransactionManager();
435       Fqn f = Fqn.fromString("/person/test2");
436       tm.begin();
437       c.put(f, "test", "test");
438       tm.commit();
439
440       tm.begin();
441       c.removeNode(f);
442       c.put(f, "test", "test");
443       tm.commit();
444    }
445 }
446
Popular Tags