1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.options; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.jboss.cache.CacheImpl; |
11 |
| import org.jboss.cache.DefaultCacheFactory; |
12 |
| import org.jboss.cache.DummyTransactionManagerLookup; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.config.Configuration; |
15 |
| |
16 |
| import javax.transaction.TransactionManager; |
17 |
| import java.util.HashMap; |
18 |
| import java.util.Map; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| public class SuppressLockingTest extends TestCase |
26 |
| { |
27 |
| private Fqn fqn = Fqn.fromString("/blah"); |
28 |
| private Fqn fqn1 = Fqn.fromString("/blah/1"); |
29 |
| |
30 |
| private CacheImpl cache; |
31 |
| |
32 |
| private TransactionManager m; |
33 |
| |
34 |
4
| protected void setUp()
|
35 |
| { |
36 |
4
| Configuration config = new Configuration();
|
37 |
4
| config.setCacheMode(Configuration.CacheMode.LOCAL);
|
38 |
4
| config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
|
39 |
4
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(config);
|
40 |
4
| m = cache.getTransactionManager();
|
41 |
| } |
42 |
| |
43 |
4
| protected void tearDown()
|
44 |
| { |
45 |
4
| if (cache != null)
|
46 |
| { |
47 |
4
| cache.stop();
|
48 |
4
| cache = null;
|
49 |
| } |
50 |
4
| m = null;
|
51 |
| } |
52 |
| |
53 |
| |
54 |
1
| public void testSuppressionOfWriteLocks() throws Exception
|
55 |
| { |
56 |
1
| TransactionManager m = cache.getTransactionManager();
|
57 |
| |
58 |
1
| m.begin();
|
59 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
60 |
1
| cache.put(fqn, "x", "1");
|
61 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
62 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
63 |
1
| m.commit();
|
64 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
65 |
| |
66 |
1
| cache.remove(fqn);
|
67 |
| |
68 |
1
| m.begin();
|
69 |
1
| cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
70 |
1
| assertTrue(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
71 |
1
| cache.put(fqn, "x", "2");
|
72 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
73 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
74 |
1
| m.commit();
|
75 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
76 |
| |
77 |
| |
78 |
1
| cache.remove(fqn);
|
79 |
| |
80 |
1
| m.begin();
|
81 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
82 |
1
| cache.put(fqn, "x", "3");
|
83 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
84 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
85 |
1
| m.commit();
|
86 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
1
| public void testSuppressionOf2WriteLocks() throws Exception
|
95 |
| { |
96 |
1
| TransactionManager m = cache.getTransactionManager();
|
97 |
| |
98 |
1
| m.begin();
|
99 |
1
| cache.put(fqn, "x", "1");
|
100 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
101 |
1
| m.commit();
|
102 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
103 |
| |
104 |
1
| cache.remove(fqn);
|
105 |
| |
106 |
1
| m.begin();
|
107 |
1
| cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
108 |
1
| cache.put(fqn, "x", "2");
|
109 |
1
| cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
110 |
1
| cache.put(fqn1, "y", "3");
|
111 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
112 |
1
| m.commit();
|
113 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
114 |
| |
115 |
1
| Map map = new HashMap();
|
116 |
1
| map.put("x", "1");
|
117 |
1
| m.begin();
|
118 |
1
| cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
119 |
1
| cache.put(fqn, map);
|
120 |
1
| cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
121 |
1
| cache.put(fqn1, map);
|
122 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
123 |
1
| m.commit();
|
124 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
125 |
| |
126 |
| |
127 |
1
| cache.remove(fqn);
|
128 |
| |
129 |
1
| m.begin();
|
130 |
1
| cache.put(fqn, "x", "3");
|
131 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
132 |
1
| m.commit();
|
133 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
134 |
| } |
135 |
| |
136 |
1
| public void testSuppressionOfReadLocks() throws Exception
|
137 |
| { |
138 |
1
| cache.put(fqn, "x", "y");
|
139 |
| |
140 |
1
| m.begin();
|
141 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
142 |
1
| cache.get(fqn, "x");
|
143 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
144 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
145 |
1
| m.commit();
|
146 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
147 |
| |
148 |
1
| m.begin();
|
149 |
1
| cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
150 |
1
| assertTrue(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
151 |
1
| cache.get(fqn, "x");
|
152 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
153 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
154 |
1
| m.commit();
|
155 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
156 |
| |
157 |
| |
158 |
| |
159 |
1
| m.begin();
|
160 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
161 |
1
| cache.get(fqn, "x");
|
162 |
1
| assertFalse(cache.getInvocationContext().getOptionOverrides().isSuppressLocking());
|
163 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
164 |
1
| m.commit();
|
165 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
166 |
| } |
167 |
| |
168 |
1
| public void testNodeCreation()
|
169 |
| { |
170 |
1
| assertNull(cache.getRoot().getChild(fqn));
|
171 |
1
| cache.getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
172 |
1
| cache.put(fqn, "x", "y");
|
173 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
174 |
1
| assertEquals("y", cache.getRoot().getChild(fqn).get("x"));
|
175 |
| } |
176 |
| |
177 |
| } |