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.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 |
4
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, false);
|
52 |
4
| tm.commit();
|
53 |
4
| assertNotLocked(fqn);
|
54 |
| } |
55 |
| |
56 |
| |
57 |
1
| public void testForceWithGetTx() throws Exception
|
58 |
| { |
59 |
1
| cache.put(fqn, "k", "v");
|
60 |
1
| tm.begin();
|
61 |
1
| cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
|
62 |
1
| cache.get(fqn, "k");
|
63 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
|
64 |
1
| tm.commit();
|
65 |
1
| assertNotLocked(fqn);
|
66 |
| |
67 |
| |
68 |
1
| testControl();
|
69 |
| } |
70 |
| |
71 |
1
| public void testForceWithPutTx() throws Exception
|
72 |
| { |
73 |
1
| cache.put(fqn, "k", "v");
|
74 |
1
| tm.begin();
|
75 |
1
| cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
|
76 |
1
| cache.put(fqn, "k", "v2");
|
77 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
|
78 |
1
| tm.commit();
|
79 |
1
| assertNotLocked(fqn);
|
80 |
| |
81 |
| |
82 |
1
| testControl();
|
83 |
| } |
84 |
| |
85 |
1
| public void testForceWithRemoveTx() throws Exception
|
86 |
| { |
87 |
1
| cache.put(fqn, "k", "v");
|
88 |
1
| tm.begin();
|
89 |
1
| cache.getInvocationContext().getOptionOverrides().setForceWriteLock(true);
|
90 |
1
| cache.remove(fqn, "k");
|
91 |
1
| assertLocked(cache.getInvocationContext().getGlobalTransaction(), fqn, true);
|
92 |
1
| tm.commit();
|
93 |
1
| assertNotLocked(fqn);
|
94 |
| |
95 |
| |
96 |
1
| testControl();
|
97 |
| } |
98 |
| |
99 |
7
| private void assertNotLocked(Fqn fqn) throws Exception
|
100 |
| { |
101 |
7
| NodeSPI n = cache.peek(fqn, true);
|
102 |
7
| NodeLock lock = n.getLock();
|
103 |
7
| assertFalse("node " + fqn + " is locked!", lock.isLocked());
|
104 |
| } |
105 |
| |
106 |
7
| private void assertLocked(Object owner, Fqn fqn, boolean write_locked) throws Exception
|
107 |
| { |
108 |
7
| NodeSPI n = cache.peek(fqn, true);
|
109 |
0
| if (owner == null) owner = Thread.currentThread();
|
110 |
7
| NodeLock lock = n.getLock();
|
111 |
7
| assertTrue("node " + fqn + " is not locked", lock.isLocked());
|
112 |
7
| if (write_locked)
|
113 |
| { |
114 |
3
| assertTrue("node " + fqn + " is not write-locked" + (lock.isReadLocked() ? " but is read-locked instead!" : "!"), lock.isWriteLocked());
|
115 |
| } |
116 |
| else |
117 |
| { |
118 |
4
| assertTrue("node " + fqn + " is not read-locked" + (lock.isWriteLocked() ? " but is write-locked instead!" : "!"), lock.isReadLocked());
|
119 |
| } |
120 |
7
| assertTrue("owner " + owner + "is not owner for lock " + lock, lock.isOwner(owner));
|
121 |
| } |
122 |
| |
123 |
| } |