1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| package org.jboss.cache.lock; |
10 |
| |
11 |
| import junit.framework.Test; |
12 |
| import junit.framework.TestCase; |
13 |
| import junit.framework.TestSuite; |
14 |
| import org.apache.commons.logging.Log; |
15 |
| import org.jboss.cache.CacheImpl; |
16 |
| import org.jboss.cache.DefaultCacheFactory; |
17 |
| import org.jboss.cache.Fqn; |
18 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
19 |
| |
20 |
| import javax.naming.Context; |
21 |
| import javax.naming.InitialContext; |
22 |
| import javax.transaction.UserTransaction; |
23 |
| import java.util.Properties; |
24 |
| import java.util.Set; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class LockReleaseTest extends TestCase |
33 |
| { |
34 |
| CacheImpl cache = null; |
35 |
| UserTransaction tx = null; |
36 |
| Log log; |
37 |
| Properties p = null; |
38 |
| String old_factory = null; |
39 |
| final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; |
40 |
| final Fqn NODE1 = Fqn.fromString("/test"); |
41 |
| final Fqn NODE2 = Fqn.fromString("/my/test"); |
42 |
| final String KEY = "key"; |
43 |
| final String VAL1 = "val1"; |
44 |
| final String VAL2 = "val2"; |
45 |
| |
46 |
| |
47 |
11
| public LockReleaseTest(String name)
|
48 |
| { |
49 |
11
| super(name);
|
50 |
| } |
51 |
| |
52 |
11
| public void setUp() throws Exception
|
53 |
| { |
54 |
11
| super.setUp();
|
55 |
11
| old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
|
56 |
11
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
|
57 |
11
| DummyTransactionManager.getInstance();
|
58 |
11
| if (p == null)
|
59 |
| { |
60 |
11
| p = new Properties();
|
61 |
11
| p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
|
62 |
| } |
63 |
11
| tx = (UserTransaction) new InitialContext(p).lookup("UserTransaction");
|
64 |
| } |
65 |
| |
66 |
11
| public void tearDown() throws Exception
|
67 |
| { |
68 |
11
| super.tearDown();
|
69 |
11
| if (cache != null)
|
70 |
| { |
71 |
11
| cache.stop();
|
72 |
| } |
73 |
| |
74 |
| |
75 |
11
| DummyTransactionManager.destroy();
|
76 |
11
| if (old_factory != null)
|
77 |
| { |
78 |
10
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
|
79 |
10
| old_factory = null;
|
80 |
| } |
81 |
| |
82 |
11
| if (tx != null)
|
83 |
| { |
84 |
11
| try
|
85 |
| { |
86 |
11
| tx.rollback();
|
87 |
| } |
88 |
| catch (Throwable t) |
89 |
| { |
90 |
| } |
91 |
11
| tx = null;
|
92 |
| } |
93 |
| } |
94 |
| |
95 |
11
| CacheImpl createCache(IsolationLevel level) throws Exception
|
96 |
| { |
97 |
11
| CacheImpl c = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
98 |
11
| c.getConfiguration().setClusterName("test");
|
99 |
11
| c.getConfiguration().setInitialStateRetrievalTimeout(10000);
|
100 |
11
| c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
|
101 |
11
| c.getConfiguration().setLockAcquisitionTimeout(500);
|
102 |
11
| c.getConfiguration().setIsolationLevel(level);
|
103 |
11
| c.create();
|
104 |
11
| c.start();
|
105 |
11
| return c;
|
106 |
| } |
107 |
| |
108 |
| |
109 |
1
| public void testReadWithReadUncommitted() throws Exception
|
110 |
| { |
111 |
1
| testReadLockRelease(IsolationLevel.READ_UNCOMMITTED);
|
112 |
| } |
113 |
| |
114 |
1
| public void testWriteWithReadUncommitted() throws Exception
|
115 |
| { |
116 |
1
| testWriteLockRelease(IsolationLevel.READ_UNCOMMITTED);
|
117 |
| } |
118 |
| |
119 |
| |
120 |
1
| public void testReadWithReadCommitted() throws Exception
|
121 |
| { |
122 |
1
| testReadLockRelease(IsolationLevel.READ_COMMITTED);
|
123 |
| } |
124 |
| |
125 |
1
| public void testWriteWithReadCommitted() throws Exception
|
126 |
| { |
127 |
1
| testWriteLockRelease(IsolationLevel.READ_COMMITTED);
|
128 |
| } |
129 |
| |
130 |
| |
131 |
1
| public void testReadWithRepeatableRead() throws Exception
|
132 |
| { |
133 |
1
| testReadLockRelease(IsolationLevel.REPEATABLE_READ);
|
134 |
| } |
135 |
| |
136 |
1
| public void testWriteWithRepeatableRead() throws Exception
|
137 |
| { |
138 |
1
| testWriteLockRelease(IsolationLevel.REPEATABLE_READ);
|
139 |
| } |
140 |
| |
141 |
| |
142 |
1
| public void testReadWithSerialzable() throws Exception
|
143 |
| { |
144 |
1
| testReadLockRelease(IsolationLevel.SERIALIZABLE);
|
145 |
| } |
146 |
| |
147 |
1
| public void testWriteWithSerializable() throws Exception
|
148 |
| { |
149 |
1
| testWriteLockRelease(IsolationLevel.SERIALIZABLE);
|
150 |
| } |
151 |
| |
152 |
| |
153 |
1
| public void testGetKeys() throws Exception
|
154 |
| { |
155 |
1
| cache = createCache(IsolationLevel.REPEATABLE_READ);
|
156 |
| |
157 |
1
| cache.put(NODE1, KEY, VAL1);
|
158 |
1
| cache.put(NODE2, KEY, VAL1);
|
159 |
1
| assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
|
160 |
| |
161 |
1
| Set keys = cache.getKeys(NODE1);
|
162 |
1
| System.out.println("keys of " + NODE1 + " are " + keys);
|
163 |
1
| assertEquals("getKeys() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld());
|
164 |
| |
165 |
1
| tx.begin();
|
166 |
1
| keys = cache.getKeys(NODE1);
|
167 |
1
| assertEquals("we should hold 1 read locks now: ", 2, cache.getNumberOfLocksHeld());
|
168 |
1
| keys = cache.getKeys(NODE2);
|
169 |
1
| assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld());
|
170 |
1
| tx.commit();
|
171 |
1
| assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
|
172 |
| } |
173 |
| |
174 |
| |
175 |
1
| public void testGetChildrenNames() throws Exception
|
176 |
| { |
177 |
1
| cache = createCache(IsolationLevel.REPEATABLE_READ);
|
178 |
| |
179 |
1
| cache.put(NODE1, KEY, VAL1);
|
180 |
1
| cache.put(NODE2, KEY, VAL1);
|
181 |
1
| assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
|
182 |
| |
183 |
1
| Set keys = cache.getChildrenNames(NODE2);
|
184 |
1
| System.out.println("keys of " + NODE2 + " are " + keys);
|
185 |
1
| assertEquals("getChildrenNames() called outside the TX should have released all locks", 0,
|
186 |
| cache.getNumberOfLocksHeld()); |
187 |
| |
188 |
1
| tx.begin();
|
189 |
1
| keys = cache.getChildrenNames(NODE1);
|
190 |
1
| assertEquals("we should hold 1 read locks now: ", 2, cache.getNumberOfLocksHeld());
|
191 |
1
| keys = cache.getChildrenNames(NODE2);
|
192 |
1
| assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld());
|
193 |
1
| tx.commit();
|
194 |
1
| assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
|
195 |
| } |
196 |
| |
197 |
1
| public void testPrint() throws Exception
|
198 |
| { |
199 |
1
| cache = createCache(IsolationLevel.REPEATABLE_READ);
|
200 |
| |
201 |
1
| cache.put(NODE1, KEY, VAL1);
|
202 |
1
| cache.put(NODE2, KEY, VAL1);
|
203 |
1
| assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
|
204 |
| |
205 |
1
| cache.print(NODE1);
|
206 |
1
| assertEquals("print() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld());
|
207 |
| |
208 |
1
| tx.begin();
|
209 |
1
| cache.print(NODE1);
|
210 |
1
| assertEquals("we should hold 1 read locks now (for print()): ", 2, cache.getNumberOfLocksHeld());
|
211 |
1
| cache.print(NODE2);
|
212 |
1
| assertEquals("we should hold 3 read locks now (for print()): ", 4, cache.getNumberOfLocksHeld());
|
213 |
1
| tx.commit();
|
214 |
1
| assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
|
215 |
| } |
216 |
| |
217 |
| |
218 |
4
| void testReadLockRelease(IsolationLevel level) throws Exception
|
219 |
| { |
220 |
4
| cache = createCache(level);
|
221 |
| |
222 |
4
| cache.put(NODE1, KEY, VAL1);
|
223 |
4
| cache.put(NODE2, KEY, VAL1);
|
224 |
| |
225 |
4
| assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
|
226 |
| |
227 |
4
| tx.begin();
|
228 |
4
| assertEquals(VAL1, cache.get(NODE1, KEY));
|
229 |
4
| assertEquals(VAL1, cache.get(NODE2, KEY));
|
230 |
4
| assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld());
|
231 |
4
| tx.commit();
|
232 |
4
| assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
|
233 |
| } |
234 |
| |
235 |
4
| void testWriteLockRelease(IsolationLevel level) throws Exception
|
236 |
| { |
237 |
4
| cache = createCache(level);
|
238 |
| |
239 |
4
| cache.put(NODE1, KEY, VAL1);
|
240 |
4
| cache.put(NODE2, KEY, VAL1);
|
241 |
| |
242 |
4
| assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
|
243 |
| |
244 |
4
| tx.begin();
|
245 |
4
| cache.put(NODE1, KEY, VAL1);
|
246 |
4
| cache.put(NODE2, KEY, VAL1);
|
247 |
4
| assertEquals("we should hold 3 write locks now: ", 4, cache.getNumberOfLocksHeld());
|
248 |
4
| tx.commit();
|
249 |
4
| assertEquals("we should have released all 3 write locks: ", 0, cache.getNumberOfLocksHeld());
|
250 |
| } |
251 |
| |
252 |
1
| public static Test suite() throws Exception
|
253 |
| { |
254 |
1
| return new TestSuite(LockReleaseTest.class);
|
255 |
| } |
256 |
| |
257 |
0
| public static void main(String[] args) throws Exception
|
258 |
| { |
259 |
0
| junit.textui.TestRunner.run(suite());
|
260 |
| } |
261 |
| |
262 |
| |
263 |
| } |