1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.optimistic; |
8 |
| |
9 |
| import org.jboss.cache.CacheImpl; |
10 |
| import org.jboss.cache.Fqn; |
11 |
| import org.jboss.cache.GlobalTransaction; |
12 |
| import org.jboss.cache.NodeSPI; |
13 |
| import org.jboss.cache.OptimisticTransactionEntry; |
14 |
| import org.jboss.cache.TransactionTable; |
15 |
| import org.jboss.cache.interceptors.Interceptor; |
16 |
| import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor; |
17 |
| import org.jboss.cache.interceptors.OptimisticLockingInterceptor; |
18 |
| import org.jboss.cache.interceptors.OptimisticNodeInterceptor; |
19 |
| import org.jboss.cache.loader.SamplePojo; |
20 |
| import org.jboss.cache.lock.NodeLock; |
21 |
| import org.jboss.cache.marshall.MethodCall; |
22 |
| import org.jboss.cache.marshall.MethodCallFactory; |
23 |
| import org.jboss.cache.marshall.MethodDeclarations; |
24 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
25 |
| |
26 |
| import javax.transaction.Transaction; |
27 |
| import java.util.HashMap; |
28 |
| import java.util.Iterator; |
29 |
| import java.util.Map; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class OpLockingInterceptorTest extends AbstractOptimisticTestCase |
35 |
| { |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
3
| public OpLockingInterceptorTest(String name)
|
42 |
| { |
43 |
3
| super(name);
|
44 |
| |
45 |
| } |
46 |
| |
47 |
1
| public void testTransactionPrepareMethod() throws Exception
|
48 |
| { |
49 |
| |
50 |
1
| TestListener listener = new TestListener();
|
51 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
52 |
| |
53 |
1
| Interceptor lockingInterceptor = new OptimisticLockingInterceptor();
|
54 |
1
| lockingInterceptor.setCache(cache);
|
55 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
56 |
1
| interceptor.setCache(cache);
|
57 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
58 |
1
| nodeInterceptor.setCache(cache);
|
59 |
1
| MockInterceptor dummy = new MockInterceptor();
|
60 |
1
| dummy.setCache(cache);
|
61 |
1
| lockingInterceptor.setNext(interceptor);
|
62 |
1
| interceptor.setNext(nodeInterceptor);
|
63 |
1
| nodeInterceptor.setNext(dummy);
|
64 |
| |
65 |
1
| cache.setInterceptorChain(lockingInterceptor);
|
66 |
| |
67 |
| |
68 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
69 |
1
| mgr.begin();
|
70 |
1
| Transaction tx = mgr.getTransaction();
|
71 |
| |
72 |
| |
73 |
1
| cache.getInvocationContext().setTransaction(tx);
|
74 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
75 |
| |
76 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
77 |
1
| Map temp = new HashMap();
|
78 |
1
| temp.put("key1", pojo);
|
79 |
1
| cache.put("/one/two", temp);
|
80 |
| |
81 |
1
| assertEquals(null, dummy.getCalled());
|
82 |
1
| TransactionTable table = cache.getTransactionTable();
|
83 |
| |
84 |
1
| GlobalTransaction gtx = table.get(tx);
|
85 |
| |
86 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
87 |
| |
88 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
1
| assertEquals(3, workspace.getNodes().size());
|
96 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
97 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
98 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
99 |
1
| assertTrue(entry.getLocks().isEmpty());
|
100 |
1
| assertEquals(1, entry.getModifications().size());
|
101 |
1
| assertTrue(!cache.exists("/one/two"));
|
102 |
1
| assertEquals(null, dummy.getCalled());
|
103 |
| |
104 |
| |
105 |
1
| MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object[]{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE});
|
106 |
1
| try
|
107 |
| { |
108 |
1
| cache._replicate(prepareMethod);
|
109 |
| } |
110 |
| catch (Throwable t) |
111 |
| { |
112 |
| |
113 |
| } |
114 |
| |
115 |
| |
116 |
1
| assertEquals(3, workspace.getNodes().size());
|
117 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
118 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
119 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
120 |
1
| assertEquals(3, entry.getLocks().size());
|
121 |
1
| for (Iterator it = entry.getLocks().iterator(); it.hasNext();)
|
122 |
| { |
123 |
3
| NodeLock lock = (NodeLock) it.next();
|
124 |
3
| assertTrue(lock.isWriteLocked());
|
125 |
3
| assertEquals(gtx, lock.getWriterOwner());
|
126 |
| } |
127 |
1
| assertEquals(1, entry.getModifications().size());
|
128 |
1
| assertTrue(!cache.exists("/one/two"));
|
129 |
| |
130 |
| |
131 |
| |
132 |
1
| mgr.commit();
|
133 |
| |
134 |
1
| cache.stop();
|
135 |
| |
136 |
| } |
137 |
| |
138 |
1
| public void testTransactionCommitMethod() throws Exception
|
139 |
| { |
140 |
| |
141 |
1
| TestListener listener = new TestListener();
|
142 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
143 |
| |
144 |
1
| Interceptor lockingInterceptor = new OptimisticLockingInterceptor();
|
145 |
1
| lockingInterceptor.setCache(cache);
|
146 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
147 |
1
| interceptor.setCache(cache);
|
148 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
149 |
1
| nodeInterceptor.setCache(cache);
|
150 |
1
| MockInterceptor dummy = new MockInterceptor();
|
151 |
1
| dummy.setCache(cache);
|
152 |
1
| lockingInterceptor.setNext(interceptor);
|
153 |
1
| interceptor.setNext(nodeInterceptor);
|
154 |
1
| nodeInterceptor.setNext(dummy);
|
155 |
| |
156 |
1
| cache.setInterceptorChain(lockingInterceptor);
|
157 |
| |
158 |
| |
159 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
160 |
1
| mgr.begin();
|
161 |
1
| Transaction tx = mgr.getTransaction();
|
162 |
| |
163 |
| |
164 |
1
| cache.getInvocationContext().setTransaction(tx);
|
165 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
166 |
| |
167 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
168 |
1
| Map temp = new HashMap();
|
169 |
1
| temp.put("key1", pojo);
|
170 |
1
| cache.put("/one/two", temp);
|
171 |
| |
172 |
1
| assertEquals(null, dummy.getCalled());
|
173 |
1
| TransactionTable table = cache.getTransactionTable();
|
174 |
| |
175 |
1
| GlobalTransaction gtx = table.get(tx);
|
176 |
| |
177 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
178 |
| |
179 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
1
| assertEquals(3, workspace.getNodes().size());
|
187 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
188 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
189 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
190 |
1
| assertTrue(entry.getLocks().isEmpty());
|
191 |
1
| assertEquals(1, entry.getModifications().size());
|
192 |
1
| assertTrue(!cache.exists("/one/two"));
|
193 |
1
| assertEquals(null, dummy.getCalled());
|
194 |
| |
195 |
| |
196 |
1
| MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object[]{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE});
|
197 |
1
| try
|
198 |
| { |
199 |
1
| cache._replicate(prepareMethod);
|
200 |
| } |
201 |
| catch (Throwable t) |
202 |
| { |
203 |
| |
204 |
| } |
205 |
| |
206 |
| |
207 |
1
| assertEquals(3, workspace.getNodes().size());
|
208 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
209 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
210 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
211 |
1
| assertEquals(3, entry.getLocks().size());
|
212 |
1
| for (Iterator it = entry.getLocks().iterator(); it.hasNext();)
|
213 |
| { |
214 |
3
| NodeLock lock = (NodeLock) it.next();
|
215 |
3
| assertTrue(lock.isWriteLocked());
|
216 |
3
| assertEquals(gtx, lock.getWriterOwner());
|
217 |
| } |
218 |
1
| assertEquals(1, entry.getModifications().size());
|
219 |
1
| assertTrue(!cache.exists("/one/two"));
|
220 |
| |
221 |
1
| assertEquals(MethodDeclarations.optimisticPrepareMethod, dummy.getCalled());
|
222 |
| |
223 |
| |
224 |
1
| MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, new Object[]{gtx});
|
225 |
1
| try
|
226 |
| { |
227 |
1
| cache._replicate(commitMethod);
|
228 |
| } |
229 |
| catch (Throwable t) |
230 |
| { |
231 |
0
| fail();
|
232 |
| } |
233 |
1
| assertEquals(3, entry.getLocks().size());
|
234 |
1
| for (Iterator it = entry.getLocks().iterator(); it.hasNext();)
|
235 |
| { |
236 |
3
| NodeLock lock = (NodeLock) it.next();
|
237 |
3
| assertEquals(false, lock.isLocked());
|
238 |
| |
239 |
| } |
240 |
| |
241 |
1
| int i = 0;
|
242 |
1
| for (Iterator it = workspace.getNodes().values().iterator(); it.hasNext();)
|
243 |
| { |
244 |
3
| NodeSPI node = ((WorkspaceNode) it.next()).getNode();
|
245 |
3
| assertEquals(node.getLock(), entry.getLocks().get(i));
|
246 |
3
| i++;
|
247 |
| } |
248 |
1
| assertEquals(MethodDeclarations.commitMethod, dummy.getCalled());
|
249 |
1
| mgr.commit();
|
250 |
| |
251 |
1
| cache.stop();
|
252 |
| |
253 |
| } |
254 |
| |
255 |
1
| public void testTransactionRollbackMethod() throws Exception
|
256 |
| { |
257 |
| |
258 |
1
| TestListener listener = new TestListener();
|
259 |
1
| final CacheImpl cache = createCacheWithListener(listener);
|
260 |
| |
261 |
1
| Interceptor lockingInterceptor = new OptimisticLockingInterceptor();
|
262 |
1
| lockingInterceptor.setCache(cache);
|
263 |
1
| Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
|
264 |
1
| interceptor.setCache(cache);
|
265 |
1
| Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
|
266 |
1
| nodeInterceptor.setCache(cache);
|
267 |
1
| MockInterceptor dummy = new MockInterceptor();
|
268 |
1
| dummy.setCache(cache);
|
269 |
1
| lockingInterceptor.setNext(interceptor);
|
270 |
1
| interceptor.setNext(nodeInterceptor);
|
271 |
1
| nodeInterceptor.setNext(dummy);
|
272 |
| |
273 |
1
| cache.setInterceptorChain(lockingInterceptor);
|
274 |
| |
275 |
| |
276 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
277 |
1
| mgr.begin();
|
278 |
1
| Transaction tx = mgr.getTransaction();
|
279 |
| |
280 |
| |
281 |
1
| cache.getInvocationContext().setTransaction(tx);
|
282 |
1
| cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
|
283 |
| |
284 |
1
| SamplePojo pojo = new SamplePojo(21, "test");
|
285 |
1
| Map temp = new HashMap();
|
286 |
1
| temp.put("key1", pojo);
|
287 |
1
| cache.put("/one/two", temp);
|
288 |
| |
289 |
1
| assertEquals(null, dummy.getCalled());
|
290 |
1
| TransactionTable table = cache.getTransactionTable();
|
291 |
| |
292 |
1
| GlobalTransaction gtx = table.get(tx);
|
293 |
| |
294 |
1
| OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
|
295 |
| |
296 |
1
| TransactionWorkspace workspace = entry.getTransactionWorkSpace();
|
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
| |
302 |
| |
303 |
1
| assertEquals(3, workspace.getNodes().size());
|
304 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
305 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
306 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
307 |
1
| assertTrue(entry.getLocks().isEmpty());
|
308 |
1
| assertEquals(1, entry.getModifications().size());
|
309 |
1
| assertTrue(!cache.exists("/one/two"));
|
310 |
1
| assertEquals(null, dummy.getCalled());
|
311 |
| |
312 |
| |
313 |
1
| MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object[]{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE});
|
314 |
1
| try
|
315 |
| { |
316 |
1
| cache._replicate(prepareMethod);
|
317 |
| } |
318 |
| catch (Throwable t) |
319 |
| { |
320 |
| |
321 |
| } |
322 |
| |
323 |
| |
324 |
1
| assertEquals(3, workspace.getNodes().size());
|
325 |
1
| assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
|
326 |
1
| assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
|
327 |
1
| assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size());
|
328 |
1
| assertEquals(3, entry.getLocks().size());
|
329 |
1
| for (Iterator it = entry.getLocks().iterator(); it.hasNext();)
|
330 |
| { |
331 |
3
| NodeLock lock = (NodeLock) it.next();
|
332 |
3
| assertTrue(lock.isWriteLocked());
|
333 |
3
| assertEquals(gtx, lock.getWriterOwner());
|
334 |
| } |
335 |
1
| assertEquals(1, entry.getModifications().size());
|
336 |
1
| assertTrue(!cache.exists("/one/two"));
|
337 |
1
| assertEquals(MethodDeclarations.optimisticPrepareMethod, dummy.getCalled());
|
338 |
| |
339 |
| |
340 |
1
| MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, new Object[]{gtx});
|
341 |
1
| try
|
342 |
| { |
343 |
1
| cache._replicate(rollbackMethod);
|
344 |
| } |
345 |
| catch (Throwable t) |
346 |
| { |
347 |
0
| fail();
|
348 |
| } |
349 |
1
| assertEquals(3, entry.getLocks().size());
|
350 |
1
| for (Iterator it = entry.getLocks().iterator(); it.hasNext();)
|
351 |
| { |
352 |
3
| NodeLock lock = (NodeLock) it.next();
|
353 |
3
| assertEquals(false, lock.isLocked());
|
354 |
| |
355 |
| } |
356 |
| |
357 |
1
| int i = 0;
|
358 |
1
| for (Iterator it = workspace.getNodes().values().iterator(); it.hasNext();)
|
359 |
| { |
360 |
3
| NodeSPI node = ((WorkspaceNode) it.next()).getNode();
|
361 |
3
| assertEquals(node.getLock(), entry.getLocks().get(i));
|
362 |
3
| i++;
|
363 |
| } |
364 |
1
| assertEquals(MethodDeclarations.rollbackMethod, dummy.getCalled());
|
365 |
1
| mgr.commit();
|
366 |
| |
367 |
1
| cache.stop();
|
368 |
| |
369 |
| } |
370 |
| |
371 |
| } |