1 |
| package org.jboss.cache.options; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.CacheSPI; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.NodeSPI; |
8 |
| import org.jboss.cache.config.Configuration; |
9 |
| import org.jboss.cache.lock.NodeLock; |
10 |
| |
11 |
| import javax.transaction.TransactionManager; |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| public class ForceWriteLockTest extends TestCase |
20 |
| { |
21 |
| private CacheSPI cache; |
22 |
| private Fqn fqn = Fqn.fromString("/a/b"); |
23 |
| private TransactionManager tm; |
24 |
| |
25 |
4
| protected void setUp()
|
26 |
| { |
27 |
4
| Configuration c = new Configuration();
|
28 |
4
| c.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
29 |
4
| cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(c);
|
30 |
4
| tm = cache.getTransactionManager();
|
31 |
| } |
32 |
| |
33 |
4
| protected void tearDown()
|
34 |
| { |
35 |
4
| try
|
36 |
| { |
37 |
4
| tm.getTransaction().rollback();
|
38 |
| } |
39 |
| catch (Exception e) |
40 |
| { |
41 |
| |
42 |
| } |
43 |
4
| cache.stop();
|
44 |
| } |
45 |
| |
46 |
4
| public void testControl() throws Exception
|
47 |
| { |
48 |
4
| cache.put(fqn, "k", "v");
|
49 |
4
| tm.begin();
|
50 |
4
| cache.get(fqn, "k");
|
51 |
| |
52 |
4
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn.getParent(), false);
|
53 |
4
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, false);
|
54 |
4
| tm.commit();
|
55 |
4
| assertNotLocked(fqn);
|
56 |
| } |
57 |
| |
58 |
| |
59 |
1
| public void testForceWithGetTx() throws Exception
|
60 |
| { |
61 |
1
| cache.put(fqn, "k", "v");
|
62 |
1
| tm.begin();
|
63 |
1
| cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
|
64 |
1
| cache.get(fqn, "k");
|
65 |
| |
66 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn.getParent(), false);
|
67 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
|
68 |
1
| tm.commit();
|
69 |
1
| assertNotLocked(fqn);
|
70 |
| |
71 |
| |
72 |
1
| testControl();
|
73 |
| } |
74 |
| |
75 |
1
| public void testForceWithPutTx() throws Exception
|
76 |
| { |
77 |
1
| cache.put(fqn, "k", "v");
|
78 |
1
| tm.begin();
|
79 |
1
| cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
|
80 |
1
| cache.put(fqn, "k", "v2");
|
81 |
| |
82 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn.getParent(), false);
|
83 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
|
84 |
1
| tm.commit();
|
85 |
1
| assertNotLocked(fqn);
|
86 |
| |
87 |
| |
88 |
1
| testControl();
|
89 |
| } |
90 |
| |
91 |
1
| public void testForceWithRemoveTx() throws Exception
|
92 |
| { |
93 |
1
| cache.put(fqn, "k", "v");
|
94 |
1
| tm.begin();
|
95 |
1
| cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
|
96 |
1
| cache.remove(fqn, "k");
|
97 |
| |
98 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn.getParent(), false);
|
99 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
|
100 |
1
| tm.commit();
|
101 |
1
| assertNotLocked(fqn);
|
102 |
| |
103 |
| |
104 |
1
| testControl();
|
105 |
| } |
106 |
| |
107 |
7
| private void assertNotLocked(Fqn fqn) throws Exception
|
108 |
| { |
109 |
7
| NodeSPI n = cache.peek(fqn, true);
|
110 |
7
| NodeLock lock = n.getLock();
|
111 |
7
| assertFalse("node " + fqn + " is locked!", lock.isLocked());
|
112 |
| } |
113 |
| |
114 |
14
| private void assertLocked(Object owner, Fqn fqn, boolean write_locked) throws Exception
|
115 |
| { |
116 |
14
| NodeSPI n = cache.peek(fqn, true);
|
117 |
0
| if (owner == null) owner = Thread.currentThread();
|
118 |
14
| NodeLock lock = n.getLock();
|
119 |
14
| assertTrue("node " + fqn + " is not locked", lock.isLocked());
|
120 |
14
| if (write_locked)
|
121 |
| { |
122 |
3
| assertTrue("node " + fqn + " is not write-locked" + (lock.isReadLocked() ? " but is read-locked instead!" : "!"), lock.isWriteLocked());
|
123 |
| } |
124 |
| else |
125 |
| { |
126 |
11
| assertTrue("node " + fqn + " is not read-locked" + (lock.isWriteLocked() ? " but is write-locked instead!" : "!"), lock.isReadLocked());
|
127 |
| } |
128 |
14
| assertTrue("owner " + owner + "is not owner for lock " + lock, lock.isOwner(owner));
|
129 |
| } |
130 |
| |
131 |
| } |