8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.locks.*; |
10 |
|
import java.util.concurrent.*; |
11 |
+ |
import java.io.*; |
12 |
|
|
13 |
< |
public class ReentrantLockTest extends TestCase { |
13 |
> |
public class ReentrantLockTest extends JSR166TestCase { |
14 |
|
static int HOLD_COUNT_TEST_LIMIT = 20; |
15 |
|
|
16 |
|
public static void main(String[] args) { |
21 |
|
return new TestSuite(ReentrantLockTest.class); |
22 |
|
} |
23 |
|
|
23 |
– |
private static long SHORT_DELAY_MS = 100; |
24 |
– |
private static long MEDIUM_DELAY_MS = 1000; |
25 |
– |
private static long LONG_DELAY_MS = 10000; |
26 |
– |
|
24 |
|
/* |
25 |
|
* Unlocks an unlocked lock, throws Illegal Monitor State |
26 |
|
* |
27 |
|
*/ |
31 |
– |
|
28 |
|
public void testIllegalMonitorStateException(){ |
29 |
|
ReentrantLock rl = new ReentrantLock(); |
30 |
|
try{ |
31 |
|
rl.unlock(); |
32 |
|
fail("Should of thown Illegal Monitor State Exception"); |
33 |
|
|
34 |
< |
}catch(IllegalMonitorStateException sucess){} |
34 |
> |
} catch(IllegalMonitorStateException success){} |
35 |
|
|
36 |
|
|
37 |
|
} |
49 |
|
public void run(){ |
50 |
|
try{ |
51 |
|
lock.lockInterruptibly(); |
52 |
< |
fail("should throw"); |
53 |
< |
}catch(InterruptedException sucess){} |
52 |
> |
threadFail("should throw"); |
53 |
> |
} catch(InterruptedException success){} |
54 |
|
} |
55 |
|
}); |
56 |
< |
t.start(); |
57 |
< |
t.interrupt(); |
58 |
< |
lock.unlock(); |
56 |
> |
try { |
57 |
> |
t.start(); |
58 |
> |
t.interrupt(); |
59 |
> |
lock.unlock(); |
60 |
> |
t.join(); |
61 |
> |
} catch(Exception e){ |
62 |
> |
fail("unexpected exception"); |
63 |
> |
} |
64 |
|
} |
65 |
|
|
66 |
|
/* |
75 |
|
Thread t = new Thread(new Runnable() { |
76 |
|
public void run(){ |
77 |
|
try{ |
78 |
< |
lock.tryLock(1000,TimeUnit.MILLISECONDS); |
79 |
< |
fail("should throw"); |
80 |
< |
}catch(InterruptedException sucess){} |
78 |
> |
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
79 |
> |
threadFail("should throw"); |
80 |
> |
} catch(InterruptedException success){} |
81 |
|
} |
82 |
|
}); |
83 |
< |
t.start(); |
84 |
< |
t.interrupt(); |
83 |
> |
try { |
84 |
> |
t.start(); |
85 |
> |
t.interrupt(); |
86 |
> |
} catch(Exception e){ |
87 |
> |
fail("unexpected exception"); |
88 |
> |
} |
89 |
|
} |
90 |
|
|
91 |
+ |
|
92 |
+ |
public void testTryLockWhenLocked() { |
93 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
94 |
+ |
lock.lock(); |
95 |
+ |
Thread t = new Thread(new Runnable() { |
96 |
+ |
public void run(){ |
97 |
+ |
threadAssertFalse(lock.tryLock()); |
98 |
+ |
} |
99 |
+ |
}); |
100 |
+ |
try { |
101 |
+ |
t.start(); |
102 |
+ |
t.join(); |
103 |
+ |
lock.unlock(); |
104 |
+ |
} catch(Exception e){ |
105 |
+ |
fail("unexpected exception"); |
106 |
+ |
} |
107 |
+ |
} |
108 |
+ |
|
109 |
+ |
public void testTryLock_Timeout(){ |
110 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
111 |
+ |
lock.lock(); |
112 |
+ |
Thread t = new Thread(new Runnable() { |
113 |
+ |
public void run(){ |
114 |
+ |
try { |
115 |
+ |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
116 |
+ |
} catch (Exception ex) { |
117 |
+ |
threadFail("unexpected exception"); |
118 |
+ |
} |
119 |
+ |
} |
120 |
+ |
}); |
121 |
+ |
try { |
122 |
+ |
t.start(); |
123 |
+ |
t.join(); |
124 |
+ |
lock.unlock(); |
125 |
+ |
} catch(Exception e){ |
126 |
+ |
fail("unexpected exception"); |
127 |
+ |
} |
128 |
+ |
} |
129 |
|
|
130 |
|
public void testGetHoldCount() { |
131 |
|
ReentrantLock lock = new ReentrantLock(); |
152 |
|
public void run() { |
153 |
|
lock.lock(); |
154 |
|
try { |
155 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
155 |
> |
Thread.sleep(SMALL_DELAY_MS); |
156 |
|
} |
157 |
< |
catch(Exception e) {} |
157 |
> |
catch(Exception e) { |
158 |
> |
threadFail("unexpected exception"); |
159 |
> |
} |
160 |
|
lock.unlock(); |
161 |
|
} |
162 |
|
}); |
169 |
|
} catch(Exception e){ |
170 |
|
fail("unexpected exception"); |
171 |
|
} |
127 |
– |
|
172 |
|
} |
173 |
|
|
130 |
– |
/* |
131 |
– |
* current thread locks interruptibly the thread |
132 |
– |
* another thread tries to aquire the lock and blocks |
133 |
– |
* on the call. interrupt the attempted aquireLock |
134 |
– |
* assert that the first lock() call actually locked the lock |
135 |
– |
* assert that the current thread is the one holding the lock |
136 |
– |
*/ |
174 |
|
|
175 |
< |
public void testLockedInterruptibly() { |
175 |
> |
public void testLockInterruptibly() { |
176 |
|
final ReentrantLock lock = new ReentrantLock(); |
177 |
< |
try {lock.lockInterruptibly();} catch(Exception e) {} |
177 |
> |
try { |
178 |
> |
lock.lockInterruptibly(); |
179 |
> |
} catch(Exception e) { |
180 |
> |
fail("unexpected exception"); |
181 |
> |
} |
182 |
|
Thread t = new Thread(new Runnable() { |
183 |
|
public void run() { |
184 |
|
try { |
185 |
|
lock.lockInterruptibly(); |
186 |
< |
fail("Failed to generate an Interrupted Exception"); |
186 |
> |
threadFail("should throw"); |
187 |
|
} |
188 |
|
catch(InterruptedException e) {} |
189 |
|
} |
190 |
|
}); |
191 |
< |
t.start(); |
192 |
< |
t.interrupt(); |
193 |
< |
assertTrue(lock.isLocked()); |
194 |
< |
assertTrue(lock.isHeldByCurrentThread()); |
191 |
> |
try { |
192 |
> |
t.start(); |
193 |
> |
t.interrupt(); |
194 |
> |
assertTrue(lock.isLocked()); |
195 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
196 |
> |
t.join(); |
197 |
> |
} catch(Exception e){ |
198 |
> |
fail("unexpected exception"); |
199 |
> |
} |
200 |
> |
} |
201 |
> |
|
202 |
> |
public void testAwait_IllegalMonitor() { |
203 |
> |
final ReentrantLock lock = new ReentrantLock(); |
204 |
> |
final Condition c = lock.newCondition(); |
205 |
> |
try { |
206 |
> |
c.await(); |
207 |
> |
fail("should throw"); |
208 |
> |
} |
209 |
> |
catch (IllegalMonitorStateException success) { |
210 |
> |
} |
211 |
> |
catch (Exception ex) { |
212 |
> |
fail("should throw IMSE"); |
213 |
> |
} |
214 |
> |
} |
215 |
> |
|
216 |
> |
public void testSignal_IllegalMonitor() { |
217 |
> |
final ReentrantLock lock = new ReentrantLock(); |
218 |
> |
final Condition c = lock.newCondition(); |
219 |
> |
try { |
220 |
> |
c.signal(); |
221 |
> |
fail("should throw"); |
222 |
> |
} |
223 |
> |
catch (IllegalMonitorStateException success) { |
224 |
> |
} |
225 |
> |
catch (Exception ex) { |
226 |
> |
fail("should throw IMSE"); |
227 |
> |
} |
228 |
> |
} |
229 |
> |
|
230 |
> |
public void testAwaitNanos_Timeout() { |
231 |
> |
final ReentrantLock lock = new ReentrantLock(); |
232 |
> |
final Condition c = lock.newCondition(); |
233 |
> |
try { |
234 |
> |
lock.lock(); |
235 |
> |
long t = c.awaitNanos(100); |
236 |
> |
assertTrue(t <= 0); |
237 |
> |
lock.unlock(); |
238 |
> |
} |
239 |
> |
catch (Exception ex) { |
240 |
> |
fail("unexpected exception"); |
241 |
> |
} |
242 |
> |
} |
243 |
> |
|
244 |
> |
public void testAwait_Timeout() { |
245 |
> |
final ReentrantLock lock = new ReentrantLock(); |
246 |
> |
final Condition c = lock.newCondition(); |
247 |
> |
try { |
248 |
> |
lock.lock(); |
249 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
250 |
> |
lock.unlock(); |
251 |
> |
} |
252 |
> |
catch (Exception ex) { |
253 |
> |
fail("unexpected exception"); |
254 |
> |
} |
255 |
> |
} |
256 |
> |
|
257 |
> |
public void testAwaitUntil_Timeout() { |
258 |
> |
final ReentrantLock lock = new ReentrantLock(); |
259 |
> |
final Condition c = lock.newCondition(); |
260 |
> |
try { |
261 |
> |
lock.lock(); |
262 |
> |
java.util.Date d = new java.util.Date(); |
263 |
> |
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
264 |
> |
lock.unlock(); |
265 |
> |
} |
266 |
> |
catch (Exception ex) { |
267 |
> |
fail("unexpected exception"); |
268 |
> |
} |
269 |
> |
} |
270 |
> |
|
271 |
> |
public void testAwait() { |
272 |
> |
final ReentrantLock lock = new ReentrantLock(); |
273 |
> |
final Condition c = lock.newCondition(); |
274 |
> |
Thread t = new Thread(new Runnable() { |
275 |
> |
public void run() { |
276 |
> |
try { |
277 |
> |
lock.lock(); |
278 |
> |
c.await(); |
279 |
> |
lock.unlock(); |
280 |
> |
} |
281 |
> |
catch(InterruptedException e) { |
282 |
> |
threadFail("unexpected exception"); |
283 |
> |
} |
284 |
> |
} |
285 |
> |
}); |
286 |
> |
|
287 |
> |
try { |
288 |
> |
t.start(); |
289 |
> |
Thread.sleep(SHORT_DELAY_MS); |
290 |
> |
lock.lock(); |
291 |
> |
c.signal(); |
292 |
> |
lock.unlock(); |
293 |
> |
t.join(SHORT_DELAY_MS); |
294 |
> |
assertFalse(t.isAlive()); |
295 |
> |
} |
296 |
> |
catch (Exception ex) { |
297 |
> |
fail("unexpected exception"); |
298 |
> |
} |
299 |
> |
} |
300 |
> |
|
301 |
> |
public void testAwaitUninterruptibly() { |
302 |
> |
final ReentrantLock lock = new ReentrantLock(); |
303 |
> |
final Condition c = lock.newCondition(); |
304 |
> |
Thread t = new Thread(new Runnable() { |
305 |
> |
public void run() { |
306 |
> |
lock.lock(); |
307 |
> |
c.awaitUninterruptibly(); |
308 |
> |
lock.unlock(); |
309 |
> |
} |
310 |
> |
}); |
311 |
> |
|
312 |
> |
try { |
313 |
> |
t.start(); |
314 |
> |
Thread.sleep(SHORT_DELAY_MS); |
315 |
> |
t.interrupt(); |
316 |
> |
lock.lock(); |
317 |
> |
c.signal(); |
318 |
> |
lock.unlock(); |
319 |
> |
t.join(SHORT_DELAY_MS); |
320 |
> |
assertFalse(t.isAlive()); |
321 |
> |
} |
322 |
> |
catch (Exception ex) { |
323 |
> |
fail("unexpected exception"); |
324 |
> |
} |
325 |
> |
} |
326 |
> |
|
327 |
> |
public void testAwait_Interrupt() { |
328 |
> |
final ReentrantLock lock = new ReentrantLock(); |
329 |
> |
final Condition c = lock.newCondition(); |
330 |
> |
Thread t = new Thread(new Runnable() { |
331 |
> |
public void run() { |
332 |
> |
try { |
333 |
> |
lock.lock(); |
334 |
> |
c.await(); |
335 |
> |
lock.unlock(); |
336 |
> |
threadFail("should throw"); |
337 |
> |
} |
338 |
> |
catch(InterruptedException success) { |
339 |
> |
} |
340 |
> |
} |
341 |
> |
}); |
342 |
> |
|
343 |
> |
try { |
344 |
> |
t.start(); |
345 |
> |
Thread.sleep(SHORT_DELAY_MS); |
346 |
> |
t.interrupt(); |
347 |
> |
t.join(SHORT_DELAY_MS); |
348 |
> |
assertFalse(t.isAlive()); |
349 |
> |
} |
350 |
> |
catch (Exception ex) { |
351 |
> |
fail("unexpected exception"); |
352 |
> |
} |
353 |
> |
} |
354 |
> |
|
355 |
> |
public void testAwaitNanos_Interrupt() { |
356 |
> |
final ReentrantLock lock = new ReentrantLock(); |
357 |
> |
final Condition c = lock.newCondition(); |
358 |
> |
Thread t = new Thread(new Runnable() { |
359 |
> |
public void run() { |
360 |
> |
try { |
361 |
> |
lock.lock(); |
362 |
> |
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
363 |
> |
lock.unlock(); |
364 |
> |
threadFail("should throw"); |
365 |
> |
} |
366 |
> |
catch(InterruptedException success) { |
367 |
> |
} |
368 |
> |
} |
369 |
> |
}); |
370 |
> |
|
371 |
> |
try { |
372 |
> |
t.start(); |
373 |
> |
Thread.sleep(SHORT_DELAY_MS); |
374 |
> |
t.interrupt(); |
375 |
> |
t.join(SHORT_DELAY_MS); |
376 |
> |
assertFalse(t.isAlive()); |
377 |
> |
} |
378 |
> |
catch (Exception ex) { |
379 |
> |
fail("unexpected exception"); |
380 |
> |
} |
381 |
> |
} |
382 |
> |
|
383 |
> |
public void testAwaitUntil_Interrupt() { |
384 |
> |
final ReentrantLock lock = new ReentrantLock(); |
385 |
> |
final Condition c = lock.newCondition(); |
386 |
> |
Thread t = new Thread(new Runnable() { |
387 |
> |
public void run() { |
388 |
> |
try { |
389 |
> |
lock.lock(); |
390 |
> |
java.util.Date d = new java.util.Date(); |
391 |
> |
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
392 |
> |
lock.unlock(); |
393 |
> |
threadFail("should throw"); |
394 |
> |
} |
395 |
> |
catch(InterruptedException success) { |
396 |
> |
} |
397 |
> |
} |
398 |
> |
}); |
399 |
> |
|
400 |
> |
try { |
401 |
> |
t.start(); |
402 |
> |
Thread.sleep(SHORT_DELAY_MS); |
403 |
> |
t.interrupt(); |
404 |
> |
t.join(SHORT_DELAY_MS); |
405 |
> |
assertFalse(t.isAlive()); |
406 |
> |
} |
407 |
> |
catch (Exception ex) { |
408 |
> |
fail("unexpected exception"); |
409 |
> |
} |
410 |
> |
} |
411 |
> |
|
412 |
> |
public void testSignalAll() { |
413 |
> |
final ReentrantLock lock = new ReentrantLock(); |
414 |
> |
final Condition c = lock.newCondition(); |
415 |
> |
Thread t1 = new Thread(new Runnable() { |
416 |
> |
public void run() { |
417 |
> |
try { |
418 |
> |
lock.lock(); |
419 |
> |
c.await(); |
420 |
> |
lock.unlock(); |
421 |
> |
} |
422 |
> |
catch(InterruptedException e) { |
423 |
> |
threadFail("unexpected exception"); |
424 |
> |
} |
425 |
> |
} |
426 |
> |
}); |
427 |
> |
|
428 |
> |
Thread t2 = new Thread(new Runnable() { |
429 |
> |
public void run() { |
430 |
> |
try { |
431 |
> |
lock.lock(); |
432 |
> |
c.await(); |
433 |
> |
lock.unlock(); |
434 |
> |
} |
435 |
> |
catch(InterruptedException e) { |
436 |
> |
threadFail("unexpected exception"); |
437 |
> |
} |
438 |
> |
} |
439 |
> |
}); |
440 |
> |
|
441 |
> |
try { |
442 |
> |
t1.start(); |
443 |
> |
t2.start(); |
444 |
> |
Thread.sleep(SHORT_DELAY_MS); |
445 |
> |
lock.lock(); |
446 |
> |
c.signalAll(); |
447 |
> |
lock.unlock(); |
448 |
> |
t1.join(SHORT_DELAY_MS); |
449 |
> |
t2.join(SHORT_DELAY_MS); |
450 |
> |
assertFalse(t1.isAlive()); |
451 |
> |
assertFalse(t2.isAlive()); |
452 |
> |
} |
453 |
> |
catch (Exception ex) { |
454 |
> |
fail("unexpected exception"); |
455 |
> |
} |
456 |
> |
} |
457 |
> |
|
458 |
> |
public void testSerialization() { |
459 |
> |
ReentrantLock l = new ReentrantLock(); |
460 |
> |
l.lock(); |
461 |
> |
l.unlock(); |
462 |
> |
|
463 |
> |
try { |
464 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
465 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
466 |
> |
out.writeObject(l); |
467 |
> |
out.close(); |
468 |
> |
|
469 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
470 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
471 |
> |
ReentrantLock r = (ReentrantLock) in.readObject(); |
472 |
> |
r.lock(); |
473 |
> |
r.unlock(); |
474 |
> |
} catch(Exception e){ |
475 |
> |
e.printStackTrace(); |
476 |
> |
fail("unexpected exception"); |
477 |
> |
} |
478 |
|
} |
155 |
– |
|
479 |
|
|
480 |
|
} |