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; |
17 |
| import javax.transaction.Transaction; |
18 |
| import java.util.Set; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class FullStackInterceptorTest extends AbstractOptimisticTestCase |
24 |
| { |
25 |
| |
26 |
| private Log log = LogFactory.getLog(FullStackInterceptorTest.class); |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
16
| public FullStackInterceptorTest(String name)
|
32 |
| { |
33 |
16
| super(name);
|
34 |
| } |
35 |
| |
36 |
| private int groupIncreaser = 0; |
37 |
| |
38 |
1
| public void testLocalTransaction() throws Exception
|
39 |
| { |
40 |
| |
41 |
1
| CacheImpl cache = createCacheWithListener();
|
42 |
| |
43 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
44 |
1
| assertNull(mgr.getTransaction());
|
45 |
| |
46 |
1
| mgr.begin();
|
47 |
| |
48 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
49 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
50 |
| |
51 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
52 |
| |
53 |
1
| cache.put("/one/two", "key1", pojo);
|
54 |
| |
55 |
1
| mgr.commit();
|
56 |
| |
57 |
1
| assertNull(mgr.getTransaction());
|
58 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
59 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
60 |
| |
61 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
62 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
63 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
64 |
| |
65 |
| |
66 |
1
| destroyCache(cache);
|
67 |
| |
68 |
| } |
69 |
| |
70 |
1
| public void testNoLocalTransaction() throws Exception
|
71 |
| { |
72 |
1
| TestListener listener = new TestListener();
|
73 |
| |
74 |
1
| CacheImpl cache = createCacheWithListener(listener);
|
75 |
| |
76 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
77 |
1
| assertNull(mgr.getTransaction());
|
78 |
| |
79 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
80 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
81 |
| |
82 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
83 |
| |
84 |
1
| cache.put("/one/two", "key1", pojo);
|
85 |
| |
86 |
1
| assertNull(mgr.getTransaction());
|
87 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
88 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
89 |
| |
90 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
91 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
92 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
|
93 |
| .isLocked()); |
94 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
|
95 |
| .isLocked()); |
96 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
97 |
1
| assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
|
98 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
99 |
| |
100 |
1
| assertEquals(2, listener.getNodesAdded());
|
101 |
| |
102 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
103 |
1
| destroyCache(cache);
|
104 |
| |
105 |
| } |
106 |
| |
107 |
1
| public void testSingleInstanceCommit() throws Exception
|
108 |
| { |
109 |
1
| groupIncreaser++;
|
110 |
1
| CacheImpl cache = createCacheWithListener();
|
111 |
| |
112 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
113 |
1
| assertNull(mgr.getTransaction());
|
114 |
| |
115 |
1
| mgr.begin();
|
116 |
| |
117 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
118 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
119 |
| |
120 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
121 |
| |
122 |
1
| cache.put("/one/two", "key1", pojo);
|
123 |
| |
124 |
1
| mgr.commit();
|
125 |
| |
126 |
1
| assertNull(mgr.getTransaction());
|
127 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
128 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
129 |
| |
130 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
131 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
132 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
133 |
| |
134 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
135 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
136 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
|
137 |
| .isLocked()); |
138 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
|
139 |
| .isLocked()); |
140 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
141 |
1
| assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
|
142 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
143 |
1
| destroyCache(cache);
|
144 |
| |
145 |
| } |
146 |
| |
147 |
1
| public void testSingleInstanceRollback() throws Exception
|
148 |
| { |
149 |
1
| groupIncreaser++;
|
150 |
1
| CacheImpl cache = createSyncReplicatedCache();
|
151 |
| |
152 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
153 |
1
| assertNull(mgr.getTransaction());
|
154 |
| |
155 |
1
| mgr.begin();
|
156 |
| |
157 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
158 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
159 |
| |
160 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
161 |
| |
162 |
1
| cache.put("/one/two", "key1", pojo);
|
163 |
| |
164 |
1
| mgr.rollback();
|
165 |
| |
166 |
1
| assertNull(mgr.getTransaction());
|
167 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
168 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
169 |
| |
170 |
1
| assertEquals(false, cache.exists(Fqn.fromString("/one/two")));
|
171 |
1
| assertNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
172 |
| |
173 |
1
| destroyCache(cache);
|
174 |
| |
175 |
| } |
176 |
| |
177 |
1
| public void testSingleInstanceDuplicateCommit() throws Exception
|
178 |
| { |
179 |
1
| groupIncreaser++;
|
180 |
1
| CacheImpl cache = createSyncReplicatedCache();
|
181 |
| |
182 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
183 |
1
| assertNull(mgr.getTransaction());
|
184 |
| |
185 |
1
| mgr.begin();
|
186 |
| |
187 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
188 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
189 |
| |
190 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
191 |
| |
192 |
1
| cache.put("/one/two", "key1", pojo);
|
193 |
| |
194 |
1
| mgr.commit();
|
195 |
| |
196 |
1
| assertNull(mgr.getTransaction());
|
197 |
| |
198 |
1
| boolean fail = false;
|
199 |
1
| try
|
200 |
| { |
201 |
1
| mgr.commit();
|
202 |
| } |
203 |
| catch (Exception e) |
204 |
| { |
205 |
1
| fail = true;
|
206 |
| |
207 |
| } |
208 |
| |
209 |
1
| assertEquals(true, fail);
|
210 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
211 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
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 |
1
| destroyCache(cache);
|
224 |
| |
225 |
| } |
226 |
| |
227 |
1
| public void testValidationFailCommit() throws Exception
|
228 |
| { |
229 |
1
| groupIncreaser++;
|
230 |
1
| CacheImpl cache = createSyncReplicatedCache();
|
231 |
| |
232 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
233 |
1
| assertNull(mgr.getTransaction());
|
234 |
| |
235 |
1
| mgr.begin();
|
236 |
1
| Transaction tx = mgr.getTransaction();
|
237 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
238 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
239 |
| |
240 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
241 |
| |
242 |
1
| cache.put("/one/two", "key1", pojo);
|
243 |
| |
244 |
1
| mgr.suspend();
|
245 |
| |
246 |
1
| assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
|
247 |
1
| assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
|
248 |
| |
249 |
1
| assertNull(mgr.getTransaction());
|
250 |
| |
251 |
1
| mgr.begin();
|
252 |
| |
253 |
1
| SamplePojo pojo2 = new SamplePojo(22, "test2");
|
254 |
| |
255 |
1
| cache.put("/one/two", "key1", pojo2);
|
256 |
| |
257 |
1
| mgr.commit();
|
258 |
| |
259 |
1
| assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
|
260 |
1
| assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
|
261 |
| |
262 |
1
| mgr.resume(tx);
|
263 |
| |
264 |
1
| boolean fail = false;
|
265 |
1
| try
|
266 |
| { |
267 |
1
| mgr.commit();
|
268 |
| } |
269 |
| catch (Exception e) |
270 |
| { |
271 |
1
| fail = true;
|
272 |
| |
273 |
| } |
274 |
1
| assertNull(mgr.getTransaction());
|
275 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
276 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
277 |
| |
278 |
1
| assertEquals(true, fail);
|
279 |
| |
280 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
281 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
282 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
|
283 |
| .isLocked()); |
284 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
|
285 |
| .isLocked()); |
286 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
287 |
1
| assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
|
288 |
1
| assertEquals(pojo2, cache.get(Fqn.fromString("/one/two"), "key1"));
|
289 |
| |
290 |
1
| destroyCache(cache);
|
291 |
| |
292 |
| } |
293 |
| |
294 |
1
| public void test2InstanceCommit() throws Exception
|
295 |
| { |
296 |
1
| groupIncreaser++;
|
297 |
1
| CacheImpl cache = createSyncReplicatedCache();
|
298 |
1
| CacheImpl cache2 = createSyncReplicatedCache();
|
299 |
| |
300 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
301 |
1
| assertNull(mgr.getTransaction());
|
302 |
| |
303 |
1
| mgr.begin();
|
304 |
| |
305 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
306 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
307 |
| |
308 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
309 |
| |
310 |
1
| cache.put("/one/two", "key1", pojo);
|
311 |
| |
312 |
1
| mgr.commit();
|
313 |
| |
314 |
| |
315 |
1
| assertNull(mgr.getTransaction());
|
316 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
317 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
318 |
| |
319 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
320 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
321 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
322 |
| |
323 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
324 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
325 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
|
326 |
| .isLocked()); |
327 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
|
328 |
| .isLocked()); |
329 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
330 |
1
| assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
|
331 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
332 |
| |
333 |
| |
334 |
1
| assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
|
335 |
1
| assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
|
336 |
| |
337 |
1
| assertTrue(cache2.exists(Fqn.fromString("/one/two")));
|
338 |
1
| assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
|
339 |
1
| assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
|
340 |
| |
341 |
1
| assertTrue(cache2.exists(Fqn.fromString("/one/two")));
|
342 |
1
| assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
|
343 |
1
| assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/"))).getLock().isLocked());
|
344 |
1
| assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one"))).getLock().isLocked());
|
345 |
1
| assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
346 |
1
| assertNotNull(cache2.get(Fqn.fromString("/one")).getChild("two"));
|
347 |
1
| assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
|
348 |
| |
349 |
1
| destroyCache(cache);
|
350 |
1
| destroyCache(cache2);
|
351 |
| } |
352 |
| |
353 |
1
| public void test2InstanceRemove() throws Exception
|
354 |
| { |
355 |
1
| groupIncreaser++;
|
356 |
1
| CacheImpl cache = createSyncReplicatedCache();
|
357 |
1
| CacheImpl cache2 = createSyncReplicatedCache();
|
358 |
| |
359 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
360 |
1
| assertNull(mgr.getTransaction());
|
361 |
| |
362 |
1
| mgr.begin();
|
363 |
| |
364 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
365 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
366 |
| |
367 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
368 |
| |
369 |
1
| cache.put("/one/two", "key1", pojo);
|
370 |
| |
371 |
1
| mgr.commit();
|
372 |
| |
373 |
| |
374 |
1
| assertNull(mgr.getTransaction());
|
375 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
376 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
377 |
| |
378 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
379 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
380 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
381 |
| |
382 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
383 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
384 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock()
|
385 |
| .isLocked()); |
386 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock()
|
387 |
| .isLocked()); |
388 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
389 |
1
| assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
|
390 |
1
| assertNotNull(cache.get(Fqn.fromString("/one/two"), "key1"));
|
391 |
| |
392 |
| |
393 |
1
| assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
|
394 |
1
| assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
|
395 |
| |
396 |
1
| assertTrue(cache2.exists(Fqn.fromString("/one/two")));
|
397 |
1
| assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
|
398 |
1
| assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
|
399 |
| |
400 |
1
| assertTrue(cache2.exists(Fqn.fromString("/one/two")));
|
401 |
1
| assertNotNull(cache2.get(Fqn.fromString("/")).getChild("one"));
|
402 |
1
| assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/"))).getLock().isLocked());
|
403 |
1
| assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one"))).getLock().isLocked());
|
404 |
1
| assertEquals(false, ((NodeSPI) cache2.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
405 |
1
| assertNotNull(cache2.get(Fqn.fromString("/one")).getChild("two"));
|
406 |
1
| assertNotNull(cache2.get(Fqn.fromString("/one/two"), "key1"));
|
407 |
| |
408 |
1
| cache.remove("/one/two");
|
409 |
1
| log.debug(" C1 " + cache.get("/one/two"));
|
410 |
1
| log.debug(" C2 " + cache2.get("/one/two"));
|
411 |
| |
412 |
1
| assertEquals(false, cache.exists("/one/two"));
|
413 |
1
| assertEquals(false, cache2.exists("/one/two"));
|
414 |
| |
415 |
1
| assertEquals(null, cache.get("/one/two", "key1"));
|
416 |
1
| assertEquals(null, cache2.get("/one/two", "key1"));
|
417 |
1
| destroyCache(cache);
|
418 |
1
| destroyCache(cache2);
|
419 |
| } |
420 |
| |
421 |
1
| public void testValidationFailCommit2Instances() throws Exception
|
422 |
| { |
423 |
1
| groupIncreaser++;
|
424 |
1
| CacheImpl cache = createSyncReplicatedCache();
|
425 |
1
| CacheImpl cache2 = createSyncReplicatedCache();
|
426 |
| |
427 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
428 |
1
| assertNull(mgr.getTransaction());
|
429 |
| |
430 |
1
| mgr.begin();
|
431 |
1
| Transaction tx = mgr.getTransaction();
|
432 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
433 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
434 |
| |
435 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
436 |
| |
437 |
1
| cache.put("/one/two", "key1", pojo);
|
438 |
| |
439 |
1
| mgr.suspend();
|
440 |
| |
441 |
1
| assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
|
442 |
1
| assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
|
443 |
| |
444 |
1
| GlobalTransaction gtx = cache.getCurrentTransaction(tx);
|
445 |
1
| TransactionTable table = cache.getTransactionTable();
|
446 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table
|
447 |
| .get(gtx); |
448 |
| |
449 |
| |
450 |
1
| assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
|
451 |
1
| assertNull(mgr.getTransaction());
|
452 |
| |
453 |
1
| mgr.begin();
|
454 |
| |
455 |
1
| SamplePojo pojo2 = new SamplePojo(22, "test2");
|
456 |
| |
457 |
1
| cache2.put("/one/two", "key1", pojo2);
|
458 |
| |
459 |
1
| mgr.commit();
|
460 |
| |
461 |
1
| assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
|
462 |
1
| assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
|
463 |
| |
464 |
1
| mgr.resume(tx);
|
465 |
| |
466 |
1
| boolean fail = false;
|
467 |
1
| try
|
468 |
| { |
469 |
1
| mgr.commit();
|
470 |
| } |
471 |
| catch (Exception e) |
472 |
| { |
473 |
1
| fail = true;
|
474 |
| |
475 |
| } |
476 |
| |
477 |
1
| assertEquals(true, fail);
|
478 |
1
| assertNull(mgr.getTransaction());
|
479 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
480 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
481 |
| |
482 |
1
| assertEquals(0, entry.getTransactionWorkSpace().getNodes().size());
|
483 |
| |
484 |
1
| assertTrue(cache.exists(Fqn.fromString("/one/two")));
|
485 |
1
| assertNotNull(cache.get(Fqn.fromString("/")).getChild("one"));
|
486 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/"))).getLock().isLocked());
|
487 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one"))).getLock().isLocked());
|
488 |
1
| assertEquals(false, ((NodeSPI) cache.get(Fqn.fromString("/one/two"))).getLock().isLocked());
|
489 |
1
| assertNotNull(cache.get(Fqn.fromString("/one")).getChild("two"));
|
490 |
1
| assertEquals(pojo2, cache.get(Fqn.fromString("/one/two"), "key1"));
|
491 |
| |
492 |
1
| destroyCache(cache);
|
493 |
1
| destroyCache(cache2);
|
494 |
| |
495 |
| } |
496 |
| |
497 |
1
| public void testGetKeyValIsolationTransaction() throws Exception
|
498 |
| { |
499 |
1
| SamplePojo pojo1 = new SamplePojo(21, "test-1");
|
500 |
1
| SamplePojo pojo2 = new SamplePojo(21, "test-2");
|
501 |
| |
502 |
1
| CacheImpl cache = createCacheWithListener();
|
503 |
| |
504 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
505 |
1
| assertNull(mgr.getTransaction());
|
506 |
| |
507 |
| |
508 |
1
| mgr.begin();
|
509 |
| |
510 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
511 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
512 |
| |
513 |
1
| cache.put("/one/two", "key1", pojo1);
|
514 |
| |
515 |
1
| mgr.commit();
|
516 |
| |
517 |
1
| mgr.begin();
|
518 |
1
| Transaction tx = mgr.getTransaction();
|
519 |
1
| System.out.println("Current TX " + mgr.getTransaction());
|
520 |
1
| assertEquals(pojo1, cache.get("/one/two", "key1"));
|
521 |
| |
522 |
| |
523 |
1
| mgr.suspend();
|
524 |
| |
525 |
1
| mgr.begin();
|
526 |
1
| System.out.println("Current TX " + mgr.getTransaction());
|
527 |
1
| cache.put("/one/two", "key2", pojo2);
|
528 |
| |
529 |
| |
530 |
| |
531 |
| |
532 |
1
| mgr.commit();
|
533 |
| |
534 |
| |
535 |
1
| assertEquals(pojo2, cache.get("/one/two", "key2"));
|
536 |
1
| System.out.println("Current TX " + mgr.getTransaction());
|
537 |
| |
538 |
1
| mgr.resume(tx);
|
539 |
1
| System.out.println("Current TX " + mgr.getTransaction());
|
540 |
| |
541 |
1
| assertEquals(null, cache.get("/one/two", "key2"));
|
542 |
1
| mgr.commit();
|
543 |
1
| destroyCache(cache);
|
544 |
| } |
545 |
| |
546 |
1
| public void testGetKeysIsolationTransaction() throws Exception
|
547 |
| { |
548 |
| |
549 |
1
| CacheImpl cache = createCacheWithListener();
|
550 |
| |
551 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
552 |
0
| if (mgr.getTransaction() != null) mgr.rollback();
|
553 |
1
| assertNull(mgr.getTransaction());
|
554 |
| |
555 |
| |
556 |
1
| mgr.begin();
|
557 |
| |
558 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
559 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
560 |
| |
561 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
562 |
| |
563 |
1
| cache.put("/one/two", "key1", pojo);
|
564 |
| |
565 |
1
| mgr.commit();
|
566 |
| |
567 |
1
| mgr.begin();
|
568 |
1
| Transaction tx = mgr.getTransaction();
|
569 |
1
| assertEquals(1, cache.getKeys("/one/two").size());
|
570 |
| |
571 |
1
| mgr.suspend();
|
572 |
| |
573 |
1
| mgr.begin();
|
574 |
1
| cache.put("/one/two", "key2", pojo);
|
575 |
| |
576 |
1
| mgr.commit();
|
577 |
| |
578 |
| |
579 |
1
| assertEquals(2, cache.getKeys("/one/two").size());
|
580 |
| |
581 |
| |
582 |
1
| mgr.resume(tx);
|
583 |
| |
584 |
1
| assertEquals(1, cache.getKeys("/one/two").size());
|
585 |
1
| mgr.commit();
|
586 |
1
| destroyCache(cache);
|
587 |
| |
588 |
| } |
589 |
| |
590 |
| |
591 |
1
| public void testTxRollbackThroughConcurrentWrite() throws Exception
|
592 |
| { |
593 |
1
| CacheImpl cache = createCacheWithListener();
|
594 |
1
| Set keys;
|
595 |
| |
596 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
597 |
0
| if (mgr.getTransaction() != null) mgr.rollback();
|
598 |
1
| assertNull(mgr.getTransaction());
|
599 |
| |
600 |
| |
601 |
1
| mgr.begin();
|
602 |
1
| assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
|
603 |
1
| assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
|
604 |
1
| cache.put("/one/two", "key1", "val1");
|
605 |
1
| mgr.commit();
|
606 |
1
| keys = cache.getKeys("/one/two");
|
607 |
1
| System.out.println("keys after TX #1: " + keys);
|
608 |
1
| assertEquals(1, keys.size());
|
609 |
| |
610 |
| |
611 |
1
| mgr.begin();
|
612 |
1
| Transaction tx = mgr.getTransaction();
|
613 |
1
| cache.put("/one/two", "key2", "val2");
|
614 |
| |
615 |
| |
616 |
1
| mgr.suspend();
|
617 |
| |
618 |
| |
619 |
1
| mgr.begin();
|
620 |
1
| cache.put("/one/two", "key3", "val3");
|
621 |
1
| mgr.commit();
|
622 |
| |
623 |
| |
624 |
1
| keys = cache.getKeys("/one/two");
|
625 |
1
| System.out.println("keys after TX #3 committed: " + keys);
|
626 |
1
| assertEquals(2, keys.size());
|
627 |
| |
628 |
| |
629 |
1
| mgr.resume(tx);
|
630 |
| |
631 |
1
| keys = cache.getKeys("/one/two");
|
632 |
1
| System.out.println("keys after TX #2 resumed (in private workspace of TX #2): " + keys);
|
633 |
1
| assertEquals(2, keys.size());
|
634 |
| |
635 |
| |
636 |
1
| try
|
637 |
| { |
638 |
1
| mgr.commit();
|
639 |
0
| fail("TX should fail as other TX incremented version number");
|
640 |
| } |
641 |
| catch (RollbackException rollback_ex) |
642 |
| { |
643 |
1
| 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 |
1
| keys = cache.getKeys("/one/two");
|
648 |
1
| System.out.println("keys after TX #2 was rolled back: " + keys);
|
649 |
1
| assertEquals(2, keys.size());
|
650 |
1
| destroyCache(cache);
|
651 |
| } |
652 |
| |
653 |
| |
654 |
11
| protected CacheImpl createSyncReplicatedCache() throws Exception
|
655 |
| { |
656 |
11
| return createReplicatedCache("temp" + groupIncreaser, Configuration.CacheMode.REPL_SYNC);
|
657 |
| } |
658 |
| |
659 |
1
| public void testPuts() throws Exception
|
660 |
| { |
661 |
1
| CacheImpl cache = createCache();
|
662 |
1
| Transaction tx;
|
663 |
| |
664 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
665 |
1
| Assert.assertNull(cache.get(fqn));
|
666 |
| |
667 |
1
| mgr.begin();
|
668 |
1
| cache.put(fqn, key, value);
|
669 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
670 |
1
| tx = mgr.getTransaction();
|
671 |
1
| mgr.suspend();
|
672 |
| |
673 |
1
| mgr.begin();
|
674 |
1
| Assert.assertNull(cache.get(fqn, key));
|
675 |
1
| mgr.commit();
|
676 |
| |
677 |
1
| mgr.resume(tx);
|
678 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
679 |
1
| mgr.commit();
|
680 |
| |
681 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
682 |
| |
683 |
| } |
684 |
| |
685 |
1
| public void testRemoves() throws Exception
|
686 |
| { |
687 |
1
| CacheImpl cache = createCache();
|
688 |
1
| Transaction tx;
|
689 |
| |
690 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
691 |
1
| Assert.assertNull(cache.get(fqn));
|
692 |
1
| cache.put(fqn, key, value);
|
693 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
694 |
| |
695 |
1
| mgr.begin();
|
696 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
697 |
1
| cache.remove(fqn);
|
698 |
1
| Assert.assertNull(cache.get(fqn));
|
699 |
1
| tx = mgr.getTransaction();
|
700 |
1
| mgr.suspend();
|
701 |
| |
702 |
1
| mgr.begin();
|
703 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
704 |
1
| mgr.commit();
|
705 |
| |
706 |
1
| mgr.resume(tx);
|
707 |
1
| Assert.assertNull(cache.get(fqn));
|
708 |
1
| mgr.commit();
|
709 |
| |
710 |
1
| Assert.assertNull(cache.get(fqn));
|
711 |
| } |
712 |
| |
713 |
| |
714 |
1
| public void testRemovesBeforeGet() throws Exception
|
715 |
| { |
716 |
1
| CacheImpl cache = createCache();
|
717 |
1
| Transaction tx;
|
718 |
| |
719 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
720 |
1
| Assert.assertNull(cache.get(fqn));
|
721 |
1
| cache.put(fqn, key, value);
|
722 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
723 |
| |
724 |
1
| mgr.begin();
|
725 |
1
| cache.remove(fqn);
|
726 |
1
| Assert.assertNull(cache.get(fqn));
|
727 |
1
| tx = mgr.getTransaction();
|
728 |
1
| mgr.suspend();
|
729 |
| |
730 |
1
| mgr.begin();
|
731 |
1
| Assert.assertEquals(value, cache.get(fqn, key));
|
732 |
1
| mgr.commit();
|
733 |
| |
734 |
1
| mgr.resume(tx);
|
735 |
1
| Assert.assertNull(cache.get(fqn));
|
736 |
1
| mgr.commit();
|
737 |
| |
738 |
1
| Assert.assertNull(cache.get(fqn));
|
739 |
| } |
740 |
| |
741 |
1
| public void testLoopedPutAndGet() throws Exception
|
742 |
| { |
743 |
1
| try
|
744 |
| { |
745 |
1
| log.debug("Starting test");
|
746 |
1
| CacheImpl cache1 = createSyncReplicatedCache();
|
747 |
1
| CacheImpl cache2 = createSyncReplicatedCache();
|
748 |
1
| log.debug("Created caches");
|
749 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
750 |
| |
751 |
1
| int numLoops = 5, numPuts = 5;
|
752 |
| |
753 |
| |
754 |
1
| log.debug("Starting " + numLoops + " loops");
|
755 |
1
| for (int i = 0; i < numLoops; i++)
|
756 |
| { |
757 |
5
| log.debug(" *** in loop " + i);
|
758 |
5
| mgr.begin();
|
759 |
5
| for (int j = 0; j < numPuts; j++)
|
760 |
| { |
761 |
25
| cache1.put(Fqn.fromString("/profiler/node" + i), "key" + j, "value" + j);
|
762 |
| } |
763 |
5
| log.debug("*** >> Out of put loop");
|
764 |
5
| mgr.commit();
|
765 |
| |
766 |
| } |
767 |
| |
768 |
1
| destroyCache(cache1);
|
769 |
1
| destroyCache(cache2);
|
770 |
| } |
771 |
| catch (Exception e) |
772 |
| { |
773 |
0
| log.debug("Error: ", e);
|
774 |
0
| Assert.assertFalse("Threw exception!", true);
|
775 |
0
| throw e;
|
776 |
| } |
777 |
| } |
778 |
| |
779 |
| } |