KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.optimistic;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.jboss.cache.CacheImpl;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.GlobalTransaction;
8 import org.jboss.cache.NodeSPI;
9 import org.jboss.cache.OptimisticTransactionEntry;
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 /**
19  * @author xenephon
20  */

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

29    public AsyncFullStackInterceptorTest(String JavaDoc name)
30    {
31       super(name);
32    }
33
34    private int groupIncreaser = 0;
35
36
37    public void testSingleInstanceRollback() throws Exception JavaDoc
38    {
39       groupIncreaser++;
40       CacheImpl cache = createAsyncReplicatedCache();
41
42       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
43       assertNull(mgr.getTransaction());
44
45       mgr.begin();
46
47       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
48       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
49
50       SamplePojo pojo = new SamplePojo(21, "test");
51
52       cache.put("/one/two", "key1", pojo);
53
54       mgr.rollback();
55
56       assertNull(mgr.getTransaction());
57       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
58       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
59
60       assertEquals(false, cache.exists(Fqn.fromString("/one/two")));
61       assertNull(cache.get(Fqn.fromString("/")).getChild("one"));
62
63       destroyCache(cache);
64
65    }
66
67    public void testSingleInstanceDuplicateCommit() throws Exception JavaDoc
68    {
69       groupIncreaser++;
70       CacheImpl cache = createAsyncReplicatedCache();
71
72       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
73       assertNull(mgr.getTransaction());
74
75       mgr.begin();
76
77       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
78       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
79
80       SamplePojo pojo = new SamplePojo(21, "test");
81
82       cache.put("/one/two", "key1", pojo);
83
84       mgr.commit();
85
86       assertNull(mgr.getTransaction());
87
88       boolean fail = false;
89       try
90       {
91          mgr.commit();
92       }
93       catch (Exception JavaDoc e)
94       {
95          fail = true;
96
97       }
98
99       assertEquals(true, fail);
100       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
101       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
102
103       assertTrue(cache.exists(Fqn.fromString("/one/two")));
104       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
105       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
106               .isLocked());
107       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
108               .isLocked());
109       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
110       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
111       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
112
113       destroyCache(cache);
114
115    }
116
117    public void testValidationFailCommit() throws Exception JavaDoc
118    {
119       groupIncreaser++;
120       CacheImpl cache = createAsyncReplicatedCache();
121
122       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
123       assertNull(mgr.getTransaction());
124
125       mgr.begin();
126       Transaction tx = mgr.getTransaction();
127       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
128       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
129
130       SamplePojo pojo = new SamplePojo(21, "test");
131
132       cache.put("/one/two", "key1", pojo);
133
134       mgr.suspend();
135
136       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
137       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
138
139       assertNull(mgr.getTransaction());
140
141       mgr.begin();
142
143       SamplePojo pojo2 = new SamplePojo(22, "test2");
144
145       cache.put("/one/two", "key1", pojo2);
146
147       mgr.commit();
148
149       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
150       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
151
152       mgr.resume(tx);
153
154       boolean fail = false;
155       try
156       {
157          mgr.commit();
158       }
159       catch (Exception JavaDoc e)
160       {
161          fail = true;
162
163       }
164       assertNull(mgr.getTransaction());
165       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
166       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
167
168       assertEquals(true, fail);
169
170       assertTrue(cache.exists(Fqn.fromString("/one/two")));
171       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
172       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
173               .isLocked());
174       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
175               .isLocked());
176       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
177       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
178       assertEquals(pojo2, cache.get(Fqn.fromString("/one/two"), "key1"));
179
180       destroyCache(cache);
181
182    }
183
184    public void test2InstanceCommit() throws Exception JavaDoc
185    {
186       groupIncreaser++;
187       CacheImpl cache = createAsyncReplicatedCache();
188       CacheImpl cache2 = createAsyncReplicatedCache();
189
190       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
191       assertNull(mgr.getTransaction());
192
193       mgr.begin();
194
195       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
196       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
197
198       SamplePojo pojo = new SamplePojo(21, "test");
199
200       cache.put("/one/two", "key1", pojo);
201
202       mgr.commit();
203
204       // cache asserts
205
assertNull(mgr.getTransaction());
206       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
207       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
208
209       assertTrue(cache.exists(Fqn.fromString("/one/two")));
210       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
211       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
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       // let async calls propagate
224
TestingUtil.sleepThread((long) 1000);
225
226       // cache2 asserts
227
assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
228       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
229
230       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
231       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
232       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
233
234       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
235       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
236       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/"))).getLock().isLocked());
237       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one"))).getLock().isLocked());
238       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one/two"))).getLock().isLocked());
239       assertNotNull(cache2.get(Fqn.fromString("/one")).getChild("two"));
240       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
241
242       destroyCache(cache);
243       destroyCache(cache2);
244    }
245
246    public void test2InstanceRemove() throws Exception JavaDoc
247    {
248       groupIncreaser++;
249       CacheImpl cache = createAsyncReplicatedCache();
250       CacheImpl cache2 = createAsyncReplicatedCache();
251
252       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
253       assertNull(mgr.getTransaction());
254
255       mgr.begin();
256
257       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
258       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
259
260       SamplePojo pojo = new SamplePojo(21, "test");
261
262       cache.put("/one/two", "key1", pojo);
263
264       mgr.commit();
265
266       // cache asserts
267
assertNull(mgr.getTransaction());
268       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
269       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
270
271       assertTrue(cache.exists(Fqn.fromString("/one/two")));
272       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
273       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
274
275       assertTrue(cache.exists(Fqn.fromString("/one/two")));
276       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
277       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
278               .isLocked());
279       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
280               .isLocked());
281       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
282       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
283       assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
284
285       // let async calls propagate
286
TestingUtil.sleepThread((long) 1000);
287
288       // cache2 asserts
289
assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
290       assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
291
292       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
293       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
294       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
295
296       assertTrue(cache2.exists(Fqn.fromString("/one/two")));
297       assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
298       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/"))).getLock().isLocked());
299       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one"))).getLock().isLocked());
300       assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one/two"))).getLock().isLocked());
301       assertNotNull(cache2.get(Fqn.fromString("/one")).getChild("two"));
302       assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
303
304       cache.remove("/one/two");
305       log.debug(" C1 " + cache.get("/one/two"));
306       assertEquals(false, cache.exists("/one/two"));
307       assertEquals(null, cache.get("/one/two", "key1"));
308
309       // let async calls propagate
310
TestingUtil.sleepThread((long) 1000);
311
312       log.debug(" C2 " + cache2.get("/one/two"));
313       assertEquals(false, cache2.exists("/one/two"));
314       assertEquals(null, cache2.get("/one/two", "key1"));
315
316       destroyCache(cache);
317       destroyCache(cache2);
318    }
319
320    public void testValidationFailCommit2Instances() throws Exception JavaDoc
321    {
322       groupIncreaser++;
323       CacheImpl cache = createAsyncReplicatedCache();
324       CacheImpl cache2 = createAsyncReplicatedCache();
325
326       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
327       assertNull(mgr.getTransaction());
328
329       mgr.begin();
330       Transaction tx = mgr.getTransaction();
331       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
332       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
333
334       SamplePojo pojo = new SamplePojo(21, "test");
335
336       cache.put("/one/two", "key1", pojo);
337
338       mgr.suspend();
339
340       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
341       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
342
343       GlobalTransaction gtx = cache.getCurrentTransaction(tx);
344       TransactionTable table = cache.getTransactionTable();
345       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table
346               .get(gtx);
347
348
349       assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
350       assertNull(mgr.getTransaction());
351
352       mgr.begin();
353
354       SamplePojo pojo2 = new SamplePojo(22, "test2");
355
356       cache2.put("/one/two", "key1", pojo2);
357
358       mgr.commit();
359
360       // let async calls propagate
361
TestingUtil.sleepThread((long) 1000);
362
363       assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
364       assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
365
366       mgr.resume(tx);
367
368       boolean fail = false;
369       try
370       {
371          mgr.commit();
372       }
373       catch (Exception JavaDoc e)
374       {
375          fail = true;
376
377       }
378
379       assertEquals(true, fail);
380       assertNull(mgr.getTransaction());
381       assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
382       assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
383
384       assertEquals(0, entry.getTransactionWorkSpace().getNodes().size());
385
386       assertTrue(cache.exists(Fqn.fromString("/one/two")));
387       assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
388       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock().isLocked());
389       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock().isLocked());
390       assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
391       assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
392       assertEquals(pojo2, cache.get(Fqn.fromString("/one/two"), "key1"));
393
394       destroyCache(cache);
395       destroyCache(cache2);
396
397    }
398
399    protected CacheImpl createAsyncReplicatedCache() throws Exception JavaDoc
400    {
401       return createReplicatedCache("temp" + groupIncreaser, Configuration.CacheMode.REPL_ASYNC);
402    }
403
404 }
405
Popular Tags