KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.optimistic;
2
3 import junit.framework.Assert;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.jboss.cache.CacheImpl;
7 import org.jboss.cache.Fqn;
8 import org.jboss.cache.GlobalTransaction;
9 import org.jboss.cache.NodeSPI;
10 import org.jboss.cache.OptimisticTransactionEntry;
11 import org.jboss.cache.TransactionTable;
12 import org.jboss.cache.config.Configuration;
13 import org.jboss.cache.loader.SamplePojo;
14 import org.jboss.cache.transaction.DummyTransactionManager;
15
16 import javax.transaction.RollbackException JavaDoc;
17 import javax.transaction.Transaction JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * @author xenephon
22  */

23 public class FullStackInterceptorTest extends AbstractOptimisticTestCase
24 {
25
26    private Log log = LogFactory.getLog(FullStackInterceptorTest.class);
27
28    /**
29     * @param name
30     */

31    public FullStackInterceptorTest(String JavaDoc name)
32    {
33       super(name);
34    }
35
36    private int groupIncreaser = 0;
37
38    public void testLocalTransaction() throws Exception JavaDoc
39    {
40
41       CacheImpl cache = createCacheWithListener();
42
43       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
44       assertNull(mgr.getTransaction());
45
46       mgr.begin();
47
48       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
49       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
50
51       SamplePojo pojo = new SamplePojo(21, "test");
52
53       cache.put("/one/two", "key1", pojo);
54
55       mgr.commit();
56
57       assertNull(mgr.getTransaction());
58       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
59       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
60
61       assertTrue(cache.exists(Fqn.fromString("/one/two")));
62       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
63       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
64       // flesh this out a bit more
65

66       destroyCache(cache);
67
68    }
69
70    public void testNoLocalTransaction() throws Exception JavaDoc
71    {
72       TestListener listener = new TestListener();
73
74       CacheImpl cache = createCacheWithListener(listener);
75
76       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
77       assertNull(mgr.getTransaction());
78
79       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
80       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
81
82       SamplePojo pojo = new SamplePojo(21, "test");
83
84       cache.put("/one/two", "key1", pojo);
85
86       assertNull(mgr.getTransaction());
87       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
88       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
89
90       assertTrue(cache.exists(Fqn.fromString("/one/two")));
91       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
92       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
93               .isLocked());
94       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
95               .isLocked());
96       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
97       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
98       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
99
100       assertEquals(2, listener.getNodesAdded());
101
102       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
103       destroyCache(cache);
104
105    }
106
107    public void testSingleInstanceCommit() throws Exception JavaDoc
108    {
109       groupIncreaser++;
110       CacheImpl cache = createCacheWithListener();
111
112       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
113       assertNull(mgr.getTransaction());
114
115       mgr.begin();
116
117       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
118       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
119
120       SamplePojo pojo = new SamplePojo(21, "test");
121
122       cache.put("/one/two", "key1", pojo);
123
124       mgr.commit();
125
126       assertNull(mgr.getTransaction());
127       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
128       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
129
130       assertTrue(cache.exists(Fqn.fromString("/one/two")));
131       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
132       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
133
134       assertTrue(cache.exists(Fqn.fromString("/one/two")));
135       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
136       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
137               .isLocked());
138       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
139               .isLocked());
140       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
141       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
142       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
143       destroyCache(cache);
144
145    }
146
147    public void testSingleInstanceRollback() throws Exception JavaDoc
148    {
149       groupIncreaser++;
150       CacheImpl cache = createSyncReplicatedCache();
151
152       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
153       assertNull(mgr.getTransaction());
154
155       mgr.begin();
156
157       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
158       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
159
160       SamplePojo pojo = new SamplePojo(21, "test");
161
162       cache.put("/one/two", "key1", pojo);
163
164       mgr.rollback();
165
166       assertNull(mgr.getTransaction());
167       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
168       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
169
170       assertEquals(false, cache.exists(Fqn.fromString("/one/two")));
171       assertNull(cache.get(Fqn.fromString("/")).getChild("one"));
172
173       destroyCache(cache);
174
175    }
176
177    public void testSingleInstanceDuplicateCommit() throws Exception JavaDoc
178    {
179       groupIncreaser++;
180       CacheImpl cache = createSyncReplicatedCache();
181
182       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
183       assertNull(mgr.getTransaction());
184
185       mgr.begin();
186
187       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
188       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
189
190       SamplePojo pojo = new SamplePojo(21, "test");
191
192       cache.put("/one/two", "key1", pojo);
193
194       mgr.commit();
195
196       assertNull(mgr.getTransaction());
197
198       boolean fail = false;
199       try
200       {
201          mgr.commit();
202       }
203       catch (Exception JavaDoc e)
204       {
205          fail = true;
206
207       }
208
209       assertEquals(true, fail);
210       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
211       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
212
213       assertTrue(cache.exists(Fqn.fromString("/one/two")));
214       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
215       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
216               .isLocked());
217       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
218               .isLocked());
219       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
220       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
221       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
222
223       destroyCache(cache);
224
225    }
226
227    public void testValidationFailCommit() throws Exception JavaDoc
228    {
229       groupIncreaser++;
230       CacheImpl cache = createSyncReplicatedCache();
231
232       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
233       assertNull(mgr.getTransaction());
234
235       mgr.begin();
236       Transaction tx = mgr.getTransaction();
237       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
238       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
239
240       SamplePojo pojo = new SamplePojo(21, "test");
241
242       cache.put("/one/two", "key1", pojo);
243
244       mgr.suspend();
245
246       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
247       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
248
249       assertNull(mgr.getTransaction());
250
251       mgr.begin();
252
253       SamplePojo pojo2 = new SamplePojo(22, "test2");
254
255       cache.put("/one/two", "key1", pojo2);
256
257       mgr.commit();
258
259       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
260       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
261
262       mgr.resume(tx);
263
264       boolean fail = false;
265       try
266       {
267          mgr.commit();
268       }
269       catch (Exception JavaDoc e)
270       {
271          fail = true;
272
273       }
274       assertNull(mgr.getTransaction());
275       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
276       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
277
278       assertEquals(true, fail);
279
280       assertTrue(cache.exists(Fqn.fromString("/one/two")));
281       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
282       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
283               .isLocked());
284       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
285               .isLocked());
286       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
287       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
288       assertEquals(pojo2, cache.get(Fqn.fromString("/one/two"), "key1"));
289
290       destroyCache(cache);
291
292    }
293
294    public void test2InstanceCommit() throws Exception JavaDoc
295    {
296       groupIncreaser++;
297       CacheImpl cache = createSyncReplicatedCache();
298       CacheImpl cache2 = createSyncReplicatedCache();
299
300       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
301       assertNull(mgr.getTransaction());
302
303       mgr.begin();
304
305       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
306       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
307
308       SamplePojo pojo = new SamplePojo(21, "test");
309
310       cache.put("/one/two", "key1", pojo);
311
312       mgr.commit();
313
314       // cache asserts
315
assertNull(mgr.getTransaction());
316       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
317       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
318
319       assertTrue(cache.exists(Fqn.fromString("/one/two")));
320       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
321       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
322
323       assertTrue(cache.exists(Fqn.fromString("/one/two")));
324       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
325       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
326               .isLocked());
327       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
328               .isLocked());
329       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
330       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
331       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
332
333       // cache2 asserts
334
assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
335       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
336
337       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
338       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
339       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
340
341       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
342       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
343       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/"))).getLock().isLocked());
344       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one"))).getLock().isLocked());
345       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one/two"))).getLock().isLocked());
346       assertNotNull(cache2.get(Fqn.fromString("/one")).getChild("two"));
347       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
348
349       destroyCache(cache);
350       destroyCache(cache2);
351    }
352
353    public void test2InstanceRemove() throws Exception JavaDoc
354    {
355       groupIncreaser++;
356       CacheImpl cache = createSyncReplicatedCache();
357       CacheImpl cache2 = createSyncReplicatedCache();
358
359       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
360       assertNull(mgr.getTransaction());
361
362       mgr.begin();
363
364       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
365       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
366
367       SamplePojo pojo = new SamplePojo(21, "test");
368
369       cache.put("/one/two", "key1", pojo);
370
371       mgr.commit();
372
373       // cache asserts
374
assertNull(mgr.getTransaction());
375       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
376       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
377
378       assertTrue(cache.exists(Fqn.fromString("/one/two")));
379       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
380       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
381
382       assertTrue(cache.exists(Fqn.fromString("/one/two")));
383       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
384       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
385               .isLocked());
386       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
387               .isLocked());
388       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
389       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
390       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
391
392       // cache2 asserts
393
assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
394       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
395
396       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
397       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
398       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
399
400       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
401       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
402       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/"))).getLock().isLocked());
403       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one"))).getLock().isLocked());
404       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one/two"))).getLock().isLocked());
405       assertNotNull(cache2.get(Fqn.fromString("/one")).getChild("two"));
406       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
407
408       cache.remove("/one/two");
409       log.debug(" C1 " + cache.get("/one/two"));
410       log.debug(" C2 " + cache2.get("/one/two"));
411
412       assertEquals(false, cache.exists("/one/two"));
413       assertEquals(false, cache2.exists("/one/two"));
414
415       assertEquals(null, cache.get("/one/two", "key1"));
416       assertEquals(null, cache2.get("/one/two", "key1"));
417       destroyCache(cache);
418       destroyCache(cache2);
419    }
420
421    public void testValidationFailCommit2Instances() throws Exception JavaDoc
422    {
423       groupIncreaser++;
424       CacheImpl cache = createSyncReplicatedCache();
425       CacheImpl cache2 = createSyncReplicatedCache();
426
427       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
428       assertNull(mgr.getTransaction());
429
430       mgr.begin();
431       Transaction tx = mgr.getTransaction();
432       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
433       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
434
435       SamplePojo pojo = new SamplePojo(21, "test");
436
437       cache.put("/one/two", "key1", pojo);
438
439       mgr.suspend();
440
441       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
442       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
443
444       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
445       TransactionTable table = cache.getTransactionTable();
446       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table
447               .get(gtx);
448
449
450       assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
451       assertNull(mgr.getTransaction());
452
453       mgr.begin();
454
455       SamplePojo pojo2 = new SamplePojo(22, "test2");
456
457       cache2.put("/one/two", "key1", pojo2);
458
459       mgr.commit();
460
461       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
462       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
463
464       mgr.resume(tx);
465
466       boolean fail = false;
467       try
468       {
469          mgr.commit();
470       }
471       catch (Exception JavaDoc e)
472       {
473          fail = true;
474
475       }
476
477       assertEquals(true, fail);
478       assertNull(mgr.getTransaction());
479       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
480       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
481
482       assertEquals(0, entry.getTransactionWorkSpace().getNodes().size());
483
484       assertTrue(cache.exists(Fqn.fromString("/one/two")));
485       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
486       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock().isLocked());
487       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock().isLocked());
488       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
489       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
490       assertEquals(pojo2, cache.get(Fqn.fromString("/one/two"), "key1"));
491
492       destroyCache(cache);
493       destroyCache(cache2);
494
495    }
496
497    public void testGetKeyValIsolationTransaction() throws Exception JavaDoc
498    {
499       SamplePojo pojo1 = new SamplePojo(21, "test-1");
500       SamplePojo pojo2 = new SamplePojo(21, "test-2");
501
502       CacheImpl cache = createCacheWithListener();
503
504       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
505       assertNull(mgr.getTransaction());
506
507       // first put in a value
508
mgr.begin();
509
510       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
511       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
512
513       cache.put("/one/two", "key1", pojo1);
514
515       mgr.commit();
516
517       mgr.begin();
518       Transaction tx = mgr.getTransaction();
519       System.out.println("Current TX " + mgr.getTransaction());
520       assertEquals(pojo1, cache.get("/one/two", "key1"));
521
522       // start another
523
mgr.suspend();
524
525       mgr.begin();
526       System.out.println("Current TX " + mgr.getTransaction());
527       cache.put("/one/two", "key2", pojo2);
528
529       // assert we can see this INSIDE the existing tx
530
//assertEquals(pojo2, cache.get("/one/two", "key2"));
531

532       mgr.commit();
533
534       // assert we can see this outside the existing tx
535
assertEquals(pojo2, cache.get("/one/two", "key2"));
536       System.out.println("Current TX " + mgr.getTransaction());
537       // resume the suspended one
538
mgr.resume(tx);
539       System.out.println("Current TX " + mgr.getTransaction());
540       // assert we can't see the change from tx2 as we already touched the node
541
assertEquals(null, cache.get("/one/two", "key2"));
542       mgr.commit();
543       destroyCache(cache);
544    }
545
546    public void testGetKeysIsolationTransaction() throws Exception JavaDoc
547    {
548
549       CacheImpl cache = createCacheWithListener();
550
551       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
552       if (mgr.getTransaction() != null) mgr.rollback();
553       assertNull(mgr.getTransaction());
554
555       // first put in a value
556
mgr.begin();
557
558       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
559       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
560
561       SamplePojo pojo = new SamplePojo(21, "test");
562
563       cache.put("/one/two", "key1", pojo);
564
565       mgr.commit();
566
567       mgr.begin();
568       Transaction tx = mgr.getTransaction();
569       assertEquals(1, cache.getKeys("/one/two").size());
570       // start another
571
mgr.suspend();
572
573       mgr.begin();
574       cache.put("/one/two", "key2", pojo);
575
576       mgr.commit();
577
578       // assert we can see this outsode the existing tx
579
assertEquals(2, cache.getKeys("/one/two").size());
580
581       // resume the suspended one
582
mgr.resume(tx);
583       // assert we can't see thge change from tx2 as we already touched the node
584
assertEquals(1, cache.getKeys("/one/two").size());
585       mgr.commit();
586       destroyCache(cache);
587
588    }
589
590
591    public void testTxRollbackThroughConcurrentWrite() throws Exception JavaDoc
592    {
593       CacheImpl cache = createCacheWithListener();
594       Set JavaDoc keys;
595
596       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
597       if (mgr.getTransaction() != null) mgr.rollback();
598       assertNull(mgr.getTransaction());
599
600       // first put in a value
601
mgr.begin();
602       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
603       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
604       cache.put("/one/two", "key1", "val1");
605       mgr.commit();
606       keys = cache.getKeys("/one/two");
607       System.out.println("keys after TX #1: " + keys);
608       assertEquals(1, keys.size());
609
610       // First TX
611
mgr.begin();
612       Transaction tx = mgr.getTransaction();
613       cache.put("/one/two", "key2", "val2");// version for this is 1
614

615       // start another
616
mgr.suspend();
617
618       // Second TX
619
mgr.begin();
620       cache.put("/one/two", "key3", "val3");
621       mgr.commit();// now version is 2, attrs are key1 and key3
622

623       // assert we can see this outside the existing tx
624
keys = cache.getKeys("/one/two");
625       System.out.println("keys after TX #3 committed: " + keys);
626       assertEquals(2, keys.size());
627
628       // resume the suspended one
629
mgr.resume(tx);
630       // assert we can't see the change from tx2 as we already touched the node
631
keys = cache.getKeys("/one/two");
632       System.out.println("keys after TX #2 resumed (in private workspace of TX #2): " + keys);
633       assertEquals(2, keys.size());// we will see key1 and key2, but *not* key3
634

635       // this will fail as our workspace has version 1, whereas cache has 2; TX will be rolled back
636
try
637       {
638          mgr.commit();
639          fail("TX should fail as other TX incremented version number");
640       }
641       catch (RollbackException JavaDoc rollback_ex)
642       {
643          System.out.println("TX was rolled back because the other TX committed first and incremented version ID." +
644                  " This is the expected behavior");
645       }
646
647       keys = cache.getKeys("/one/two");
648       System.out.println("keys after TX #2 was rolled back: " + keys);
649       assertEquals(2, keys.size());// key1 and key2
650
destroyCache(cache);
651    }
652
653
654    protected CacheImpl createSyncReplicatedCache() throws Exception JavaDoc
655    {
656       return createReplicatedCache("temp" + groupIncreaser, Configuration.CacheMode.REPL_SYNC);
657    }
658
659    public void testPuts() throws Exception JavaDoc
660    {
661       CacheImpl cache = createCache();
662       Transaction tx;
663
664       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
665       Assert.assertNull(cache.get(fqn));
666
667       mgr.begin();
668       cache.put(fqn, key, value);
669       Assert.assertEquals(value, cache.get(fqn, key));
670       tx = mgr.getTransaction();
671       mgr.suspend();
672
673       mgr.begin();
674       Assert.assertNull(cache.get(fqn, key));
675       mgr.commit();
676
677       mgr.resume(tx);
678       Assert.assertEquals(value, cache.get(fqn, key));
679       mgr.commit();
680
681       Assert.assertEquals(value, cache.get(fqn, key));
682
683    }
684
685    public void testRemoves() throws Exception JavaDoc
686    {
687       CacheImpl cache = createCache();
688       Transaction tx;
689
690       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
691       Assert.assertNull(cache.get(fqn));
692       cache.put(fqn, key, value);
693       Assert.assertEquals(value, cache.get(fqn, key));
694
695       mgr.begin();
696       Assert.assertEquals(value, cache.get(fqn, key));
697       cache.remove(fqn);
698       Assert.assertNull(cache.get(fqn));
699       tx = mgr.getTransaction();
700       mgr.suspend();
701
702       mgr.begin();
703       Assert.assertEquals(value, cache.get(fqn, key));
704       mgr.commit();
705
706       mgr.resume(tx);
707       Assert.assertNull(cache.get(fqn));
708       mgr.commit();
709
710       Assert.assertNull(cache.get(fqn));
711    }
712
713
714    public void testRemovesBeforeGet() throws Exception JavaDoc
715    {
716       CacheImpl cache = createCache();
717       Transaction tx;
718
719       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
720       Assert.assertNull(cache.get(fqn));
721       cache.put(fqn, key, value);
722       Assert.assertEquals(value, cache.get(fqn, key));
723
724       mgr.begin();
725       cache.remove(fqn);
726       Assert.assertNull(cache.get(fqn));
727       tx = mgr.getTransaction();
728       mgr.suspend();
729
730       mgr.begin();
731       Assert.assertEquals(value, cache.get(fqn, key));
732       mgr.commit();
733
734       mgr.resume(tx);
735       Assert.assertNull(cache.get(fqn));
736       mgr.commit();
737
738       Assert.assertNull(cache.get(fqn));
739    }
740
741    public void testLoopedPutAndGet() throws Exception JavaDoc
742    {
743       try
744       {
745          log.debug("Starting test");
746          CacheImpl cache1 = createSyncReplicatedCache();
747          CacheImpl cache2 = createSyncReplicatedCache();
748          log.debug("Created caches");
749          DummyTransactionManager mgr = DummyTransactionManager.getInstance();
750
751          int numLoops = 5, numPuts = 5;
752
753
754          log.debug("Starting " + numLoops + " loops");
755          for (int i = 0; i < numLoops; i++)
756          {
757             log.debug(" *** in loop " + i);
758             mgr.begin();
759             for (int j = 0; j < numPuts; j++)
760             {
761                cache1.put(Fqn.fromString("/profiler/node" + i), "key" + j, "value" + j);
762             }
763             log.debug("*** >> Out of put loop");
764             mgr.commit();
765             //cache2.get(Fqn.fromString("/profiler/node" + i));
766
}
767
768          destroyCache(cache1);
769          destroyCache(cache2);
770       }
771       catch (Exception JavaDoc e)
772       {
773          log.debug("Error: ", e);
774          Assert.assertFalse("Threw exception!", true);
775          throw e;
776       }
777    }
778
779 }
780
Popular Tags