KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package org.jboss.cache.optimistic;
6
7 import org.jboss.cache.CacheImpl;
8 import org.jboss.cache.Fqn;
9 import org.jboss.cache.GlobalTransaction;
10 import org.jboss.cache.TransactionTable;
11 import org.jboss.cache.config.Configuration;
12 import org.jboss.cache.loader.SamplePojo;
13 import org.jboss.cache.misc.TestingUtil;
14 import org.jboss.cache.transaction.DummyTransactionManager;
15
16 import javax.transaction.Transaction JavaDoc;
17
18 public class AsyncCacheTest extends AbstractOptimisticTestCase
19 {
20
21    CacheImpl cache, cache2;
22
23    protected void setUp() throws Exception JavaDoc
24    {
25       cache = createReplicatedCache(Configuration.CacheMode.REPL_ASYNC);
26       cache2 = createReplicatedCache(Configuration.CacheMode.REPL_ASYNC);
27    }
28
29    protected void tearDown()
30    {
31       super.tearDown();
32       destroyCache(cache);
33       destroyCache(cache2);
34       cache = null;
35       cache2 = null;
36    }
37
38    public AsyncCacheTest(String JavaDoc s)
39    {
40       super(s);
41    }
42
43    public void testRemoteCacheBroadcast() throws Exception JavaDoc
44    {
45
46       assertEquals(2, cache.getMembers().size());
47       assertEquals(2, cache2.getMembers().size());
48
49       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
50
51       //start local transaction
52
mgr.begin();
53       Transaction tx = mgr.getTransaction();
54
55       //this sets
56
GlobalTransaction gtx = cache.getCurrentTransaction(tx);
57
58       SamplePojo pojo = new SamplePojo(21, "test");
59
60       cache.put("/one/two", "key1", pojo);
61
62       //GlobalTransaction gtx = cache.getCurrentTransaction(tx);
63
TransactionTable table = cache.getTransactionTable();
64       assertNotNull(mgr.getTransaction());
65       mgr.commit();
66
67
68       assertNull(mgr.getTransaction());
69
70       //assert that the local cache is in the right state
71
assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
72       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
73
74       assertTrue(cache.exists(Fqn.fromString("/one/two")));
75       assertTrue(cache.exists(Fqn.fromString("/one")));
76       assertEquals(pojo, cache.get(Fqn.fromString("/one/two"), "key1"));
77
78       // allow changes to replicate since this is async
79
TestingUtil.sleepThread((long) 1000);
80
81       assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
82       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
83
84       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
85       assertTrue(cache2.exists(Fqn.fromString("/one")));
86       assertEquals(pojo, cache2.get(Fqn.fromString("/one/two"), "key1"));
87    }
88
89
90    public void testTwoWayRemoteCacheBroadcast() throws Exception JavaDoc
91    {
92       assertEquals(2, cache.getMembers().size());
93       assertEquals(2, cache2.getMembers().size());
94
95       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
96
97       //start local transaction
98
mgr.begin();
99       Transaction tx = mgr.getTransaction();
100
101       //this sets
102
cache.getCurrentTransaction(tx);
103
104       SamplePojo pojo = new SamplePojo(21, "test");
105
106       cache.put("/one/two", "key1", pojo);
107
108       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
109       TransactionTable table = cache.getTransactionTable();
110       assertNotNull(mgr.getTransaction());
111       mgr.commit();
112
113
114       assertNull(mgr.getTransaction());
115
116       //assert that the local cache is in the right state
117
assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
118       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
119
120       assertTrue(cache.exists(Fqn.fromString("/one/two")));
121       assertTrue(cache.exists(Fqn.fromString("/one")));
122       assertEquals(pojo, cache.get(Fqn.fromString("/one/two"), "key1"));
123
124       // let the async calls complete
125
TestingUtil.sleepThread((long) 1000);
126
127       assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
128       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
129
130       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
131       assertTrue(cache2.exists(Fqn.fromString("/one")));
132
133       assertEquals(pojo, cache2.get(Fqn.fromString("/one/two"), "key1"));
134    }
135 }
136
Popular Tags