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