1 |
| package org.jboss.cache.transaction; |
2 |
| |
3 |
| import junit.framework.AssertionFailedError; |
4 |
| import junit.framework.Test; |
5 |
| import junit.framework.TestCase; |
6 |
| import junit.framework.TestSuite; |
7 |
| import org.jboss.cache.Cache; |
8 |
| import org.jboss.cache.CacheImpl; |
9 |
| import org.jboss.cache.DefaultCacheFactory; |
10 |
| import org.jboss.cache.DummyTransactionManagerLookup; |
11 |
| import org.jboss.cache.Fqn; |
12 |
| import org.jboss.cache.config.Configuration; |
13 |
| import org.jboss.cache.config.Configuration.CacheMode; |
14 |
| import org.jboss.cache.lock.IsolationLevel; |
15 |
| import org.jboss.cache.lock.TimeoutException; |
16 |
| |
17 |
| import javax.transaction.NotSupportedException; |
18 |
| import javax.transaction.SystemException; |
19 |
| import javax.transaction.Transaction; |
20 |
| import java.util.concurrent.CountDownLatch; |
21 |
| import java.util.concurrent.TimeUnit; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class IsolationLevelReadCommittedTest extends TestCase |
32 |
| { |
33 |
| |
34 |
| private Cache cache = null; |
35 |
| private final Fqn FQN = Fqn.fromString("/a/b/c"); |
36 |
| private final Fqn PARENT_FQN = FQN.getParent(); |
37 |
| private final String KEY = "key"; |
38 |
| private final String VALUE = "value"; |
39 |
| |
40 |
| private volatile boolean writerFailed; |
41 |
| private volatile boolean readerFailed; |
42 |
| private volatile AssertionFailedError writerError; |
43 |
| private volatile AssertionFailedError readerError; |
44 |
| |
45 |
2
| protected void setUp() throws Exception
|
46 |
| { |
47 |
2
| super.setUp();
|
48 |
| |
49 |
2
| writerFailed = false;
|
50 |
2
| readerFailed = false;
|
51 |
| |
52 |
2
| writerError = null;
|
53 |
2
| readerError = null;
|
54 |
| |
55 |
2
| Configuration config = new Configuration();
|
56 |
2
| config.setCacheMode(CacheMode.LOCAL);
|
57 |
2
| config.setIsolationLevel(IsolationLevel.READ_COMMITTED);
|
58 |
2
| config.setLockAcquisitionTimeout(1000);
|
59 |
2
| config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
|
60 |
2
| cache = DefaultCacheFactory.getInstance().createCache(config);
|
61 |
| } |
62 |
| |
63 |
| |
64 |
2
| protected void tearDown() throws Exception
|
65 |
| { |
66 |
2
| super.tearDown();
|
67 |
| |
68 |
2
| cache.stop();
|
69 |
2
| cache.destroy();
|
70 |
2
| cache = null;
|
71 |
| } |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
1
| public void testReadCommitted() throws Exception
|
81 |
| { |
82 |
1
| final CountDownLatch readerCanRead = new CountDownLatch(1);
|
83 |
1
| final CountDownLatch readerDone = new CountDownLatch(1);
|
84 |
1
| final CountDownLatch writerCanWrite = new CountDownLatch(1);
|
85 |
1
| final CountDownLatch writerCanRollback = new CountDownLatch(1);
|
86 |
1
| final CountDownLatch writerDone = new CountDownLatch(1);
|
87 |
| |
88 |
1
| cache.put(FQN, KEY, VALUE);
|
89 |
1
| assertEquals(VALUE, cache.get(FQN, KEY));
|
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
1
| Thread readerThread = new Thread(new Runnable()
|
95 |
| { |
96 |
1
| public void run()
|
97 |
| { |
98 |
1
| Transaction tx = null;
|
99 |
1
| try
|
100 |
| { |
101 |
1
| tx = startTransaction();
|
102 |
| |
103 |
| |
104 |
1
| assertEquals("Could not read node with expected value!", VALUE, cache.get(FQN, KEY));
|
105 |
| |
106 |
1
| writerCanWrite.countDown();
|
107 |
| |
108 |
| |
109 |
| |
110 |
1
| readerCanRead.await();
|
111 |
| |
112 |
1
| try
|
113 |
| { |
114 |
| |
115 |
1
| assertEquals("thread w/ read lock can see subsequent uncommitted changes!!", VALUE, cache.get(FQN, KEY));
|
116 |
| } |
117 |
| catch (TimeoutException good) |
118 |
| { |
119 |
| |
120 |
| } |
121 |
| |
122 |
| |
123 |
1
| writerCanRollback.countDown();
|
124 |
| |
125 |
| |
126 |
1
| assertEquals("Could not read node with expected value!", VALUE, cache.get(FQN, KEY));
|
127 |
| } |
128 |
| catch (AssertionFailedError e) |
129 |
| { |
130 |
0
| readerError = e;
|
131 |
| } |
132 |
| catch (Throwable t) |
133 |
| { |
134 |
0
| t.printStackTrace();
|
135 |
0
| readerFailed = true;
|
136 |
| } |
137 |
| finally |
138 |
| { |
139 |
1
| System.out.println("reader thread exits");
|
140 |
1
| if (tx != null)
|
141 |
| { |
142 |
1
| try { tx.commit(); } catch (Exception e) {}
|
143 |
| } |
144 |
1
| writerCanWrite.countDown();
|
145 |
1
| writerCanRollback.countDown();
|
146 |
1
| readerDone.countDown();
|
147 |
| } |
148 |
| } |
149 |
| }, "READER"); |
150 |
1
| readerThread.start();
|
151 |
| |
152 |
| |
153 |
| |
154 |
1
| Thread writerThread = new Thread(new Runnable()
|
155 |
| { |
156 |
1
| public void run()
|
157 |
| { |
158 |
1
| try
|
159 |
| { |
160 |
| |
161 |
| |
162 |
1
| writerCanWrite.await(3, TimeUnit.SECONDS);
|
163 |
| |
164 |
1
| Transaction tx2 = startTransaction();
|
165 |
| |
166 |
| |
167 |
1
| cache.put(FQN, KEY, "this-shouldnt-be-visible");
|
168 |
| |
169 |
| |
170 |
1
| readerCanRead.countDown();
|
171 |
| |
172 |
| |
173 |
1
| writerCanWrite.await(3, TimeUnit.SECONDS);
|
174 |
| |
175 |
1
| System.out.println("rolling back");
|
176 |
| |
177 |
1
| tx2.rollback();
|
178 |
| } |
179 |
| catch (AssertionFailedError e) |
180 |
| { |
181 |
0
| writerError = e;
|
182 |
| } |
183 |
| catch (Throwable t) |
184 |
| { |
185 |
0
| t.printStackTrace();
|
186 |
0
| writerFailed = true;
|
187 |
| } |
188 |
| finally |
189 |
| { |
190 |
1
| System.out.println("writer thread exits");
|
191 |
1
| readerCanRead.countDown();
|
192 |
1
| writerDone.countDown();
|
193 |
| } |
194 |
| } |
195 |
| }, "WRITER"); |
196 |
1
| writerThread.start();
|
197 |
| |
198 |
| |
199 |
1
| readerDone.await();
|
200 |
1
| writerDone.await();
|
201 |
| |
202 |
| |
203 |
1
| if (readerError != null)
|
204 |
| { |
205 |
0
| throw readerError;
|
206 |
| } |
207 |
| |
208 |
1
| if (writerError != null)
|
209 |
| { |
210 |
0
| throw writerError;
|
211 |
| } |
212 |
| |
213 |
1
| if (readerFailed)
|
214 |
| { |
215 |
0
| fail("The reader thread exited incorrectly. Watch the log for previous stack traces");
|
216 |
| } |
217 |
| |
218 |
1
| if (writerFailed)
|
219 |
| { |
220 |
0
| fail("The writer thread exited incorrectly. Watch the log for previous stack traces");
|
221 |
| } |
222 |
| } |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
1
| public void testNodeRemoved() throws Exception
|
232 |
| { |
233 |
1
| final CountDownLatch readerCanRead = new CountDownLatch(1);
|
234 |
1
| final CountDownLatch readerDone = new CountDownLatch(1);
|
235 |
1
| final CountDownLatch writerDone = new CountDownLatch(1);
|
236 |
| |
237 |
1
| cache.put(FQN, KEY, VALUE);
|
238 |
1
| assertEquals(VALUE, cache.get(FQN, KEY));
|
239 |
| |
240 |
| |
241 |
| |
242 |
1
| Thread writerThread = new Thread(new Runnable()
|
243 |
| { |
244 |
1
| public void run()
|
245 |
| { |
246 |
1
| try
|
247 |
| { |
248 |
1
| Transaction tx = startTransaction();
|
249 |
| |
250 |
| |
251 |
1
| cache.removeNode(PARENT_FQN);
|
252 |
| |
253 |
| |
254 |
1
| readerCanRead.countDown();
|
255 |
| |
256 |
1
| readerDone.await();
|
257 |
| |
258 |
1
| tx.commit();
|
259 |
| } |
260 |
| catch (AssertionFailedError e) |
261 |
| { |
262 |
0
| writerError = e;
|
263 |
| } |
264 |
| catch (Throwable t) |
265 |
| { |
266 |
0
| t.printStackTrace();
|
267 |
0
| writerFailed = true;
|
268 |
| } |
269 |
| finally |
270 |
| { |
271 |
1
| System.out.println("writer thread exits");
|
272 |
1
| readerCanRead.countDown();
|
273 |
1
| writerDone.countDown();
|
274 |
| } |
275 |
| } |
276 |
| }, "WRITER"); |
277 |
1
| writerThread.start();
|
278 |
| |
279 |
1
| try
|
280 |
| { |
281 |
| |
282 |
| |
283 |
1
| readerCanRead.await();
|
284 |
| |
285 |
| |
286 |
1
| assertEquals("2nd thread cannot see uncommitted changes",
|
287 |
| VALUE, cache.get(FQN, KEY)); |
288 |
| } |
289 |
| catch (TimeoutException t) |
290 |
| { |
291 |
| |
292 |
| } |
293 |
| finally |
294 |
| { |
295 |
1
| System.out.println("reader thread exits");
|
296 |
1
| readerDone.countDown();
|
297 |
| } |
298 |
| |
299 |
| |
300 |
1
| writerDone.await();
|
301 |
| |
302 |
1
| assertNull("Node was removed", ((CacheImpl) cache).get(FQN));
|
303 |
| |
304 |
| |
305 |
| |
306 |
1
| if (writerError != null)
|
307 |
| { |
308 |
0
| throw writerError;
|
309 |
| } |
310 |
| |
311 |
1
| if (writerFailed)
|
312 |
| { |
313 |
0
| fail("The writer thread exited incorrectly. Watch the log for previous stack traces");
|
314 |
| } |
315 |
| |
316 |
| } |
317 |
| |
318 |
3
| private Transaction startTransaction() throws SystemException, NotSupportedException
|
319 |
| { |
320 |
3
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
321 |
3
| mgr.begin();
|
322 |
3
| return mgr.getTransaction();
|
323 |
| } |
324 |
| |
325 |
| |
326 |
1
| public static Test suite()
|
327 |
| { |
328 |
| |
329 |
1
| return new TestSuite(IsolationLevelReadCommittedTest.class);
|
330 |
| |
331 |
| } |
332 |
| |
333 |
| } |
334 |
| |