1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
20 |
|
return new TestSuite(ReentrantLockTest.class); |
21 |
|
} |
22 |
|
|
23 |
< |
static int HOLD_COUNT_TEST_LIMIT = 20; |
24 |
< |
|
23 |
> |
/** |
24 |
> |
* A runnable calling lockInterruptibly |
25 |
> |
*/ |
26 |
|
class InterruptibleLockRunnable implements Runnable { |
27 |
|
final ReentrantLock lock; |
28 |
|
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
29 |
< |
public void run(){ |
30 |
< |
try{ |
29 |
> |
public void run() { |
30 |
> |
try { |
31 |
|
lock.lockInterruptibly(); |
32 |
|
} catch(InterruptedException success){} |
33 |
|
} |
34 |
|
} |
35 |
|
|
36 |
< |
// Same, except must interrupt |
36 |
> |
|
37 |
> |
/** |
38 |
> |
* A runnable calling lockInterruptibly that expects to be |
39 |
> |
* interrupted |
40 |
> |
*/ |
41 |
|
class InterruptedLockRunnable implements Runnable { |
42 |
|
final ReentrantLock lock; |
43 |
|
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
44 |
< |
public void run(){ |
45 |
< |
try{ |
44 |
> |
public void run() { |
45 |
> |
try { |
46 |
|
lock.lockInterruptibly(); |
47 |
< |
threadFail("should throw"); |
47 |
> |
threadShouldThrow(); |
48 |
|
} catch(InterruptedException success){} |
49 |
|
} |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* To expose protected methods |
53 |
> |
* Subclass to expose protected methods |
54 |
|
*/ |
55 |
< |
static class MyReentrantLock extends ReentrantLock { |
56 |
< |
MyReentrantLock() { super(); } |
55 |
> |
static class PublicReentrantLock extends ReentrantLock { |
56 |
> |
PublicReentrantLock() { super(); } |
57 |
|
public Collection<Thread> getQueuedThreads() { |
58 |
|
return super.getQueuedThreads(); |
59 |
|
} |
54 |
– |
public ConditionObject newCondition() { |
55 |
– |
return new MyCondition(this); |
56 |
– |
} |
60 |
|
|
61 |
< |
static class MyCondition extends ReentrantLock.ConditionObject { |
62 |
< |
MyCondition(MyReentrantLock l) { super(l); } |
63 |
< |
public Collection<Thread> getWaitingThreads() { |
64 |
< |
return super.getWaitingThreads(); |
65 |
< |
} |
66 |
< |
} |
61 |
> |
} |
62 |
> |
|
63 |
> |
/** |
64 |
> |
* Constructor sets given fairness |
65 |
> |
*/ |
66 |
> |
public void testConstructor() { |
67 |
> |
ReentrantLock rl = new ReentrantLock(); |
68 |
> |
assertFalse(rl.isFair()); |
69 |
> |
ReentrantLock r2 = new ReentrantLock(true); |
70 |
> |
assertTrue(r2.isFair()); |
71 |
> |
} |
72 |
> |
|
73 |
> |
/** |
74 |
> |
* locking an unlocked lock succeeds |
75 |
> |
*/ |
76 |
> |
public void testLock() { |
77 |
> |
ReentrantLock rl = new ReentrantLock(); |
78 |
> |
rl.lock(); |
79 |
> |
assertTrue(rl.isLocked()); |
80 |
> |
rl.unlock(); |
81 |
> |
} |
82 |
|
|
83 |
+ |
/** |
84 |
+ |
* locking an unlocked fair lock succeeds |
85 |
+ |
*/ |
86 |
+ |
public void testFairLock() { |
87 |
+ |
ReentrantLock rl = new ReentrantLock(true); |
88 |
+ |
rl.lock(); |
89 |
+ |
assertTrue(rl.isLocked()); |
90 |
+ |
rl.unlock(); |
91 |
|
} |
92 |
|
|
93 |
< |
/* |
93 |
> |
/** |
94 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
95 |
|
*/ |
96 |
< |
public void testIllegalMonitorStateException(){ |
96 |
> |
public void testUnlock_IllegalMonitorStateException() { |
97 |
|
ReentrantLock rl = new ReentrantLock(); |
98 |
< |
try{ |
98 |
> |
try { |
99 |
|
rl.unlock(); |
100 |
< |
fail("Should of thown Illegal Monitor State Exception"); |
100 |
> |
shouldThrow(); |
101 |
|
|
102 |
|
} catch(IllegalMonitorStateException success){} |
103 |
|
} |
104 |
|
|
105 |
< |
/* |
106 |
< |
* lockInterruptibly is interruptible. |
105 |
> |
/** |
106 |
> |
* trylock on an unlocked lock succeeds |
107 |
|
*/ |
108 |
< |
public void testInterruptedException(){ |
109 |
< |
final ReentrantLock lock = new ReentrantLock(); |
110 |
< |
lock.lock(); |
111 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
112 |
< |
try { |
113 |
< |
t.start(); |
114 |
< |
t.interrupt(); |
89 |
< |
lock.unlock(); |
90 |
< |
t.join(); |
91 |
< |
} catch(Exception e){ |
92 |
< |
fail("unexpected exception"); |
93 |
< |
} |
94 |
< |
} |
108 |
> |
public void testTryLock() { |
109 |
> |
ReentrantLock rl = new ReentrantLock(); |
110 |
> |
assertTrue(rl.tryLock()); |
111 |
> |
assertTrue(rl.isLocked()); |
112 |
> |
rl.unlock(); |
113 |
> |
} |
114 |
> |
|
115 |
|
|
116 |
< |
/* |
117 |
< |
* getLockQueueLength reports number of waiting threads |
116 |
> |
/** |
117 |
> |
* getQueueLength reports number of waiting threads |
118 |
|
*/ |
119 |
< |
public void testgetLockQueueLength(){ |
119 |
> |
public void testGetQueueLength() { |
120 |
|
final ReentrantLock lock = new ReentrantLock(); |
121 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
122 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
123 |
|
try { |
124 |
< |
assertEquals(0, lock.getLockQueueLength()); |
124 |
> |
assertEquals(0, lock.getQueueLength()); |
125 |
|
lock.lock(); |
126 |
|
t1.start(); |
127 |
|
Thread.sleep(SHORT_DELAY_MS); |
128 |
< |
assertEquals(1, lock.getLockQueueLength()); |
128 |
> |
assertEquals(1, lock.getQueueLength()); |
129 |
|
t2.start(); |
130 |
|
Thread.sleep(SHORT_DELAY_MS); |
131 |
< |
assertEquals(2, lock.getLockQueueLength()); |
131 |
> |
assertEquals(2, lock.getQueueLength()); |
132 |
|
t1.interrupt(); |
133 |
|
Thread.sleep(SHORT_DELAY_MS); |
134 |
< |
assertEquals(1, lock.getLockQueueLength()); |
134 |
> |
assertEquals(1, lock.getQueueLength()); |
135 |
|
lock.unlock(); |
136 |
|
Thread.sleep(SHORT_DELAY_MS); |
137 |
< |
assertEquals(0, lock.getLockQueueLength()); |
137 |
> |
assertEquals(0, lock.getQueueLength()); |
138 |
|
t1.join(); |
139 |
|
t2.join(); |
140 |
|
} catch(Exception e){ |
141 |
< |
fail("unexpected exception"); |
141 |
> |
unexpectedException(); |
142 |
|
} |
143 |
|
} |
144 |
|
|
145 |
< |
/* |
145 |
> |
/** |
146 |
|
* getQueuedThreads includes waiting threads |
147 |
|
*/ |
148 |
< |
public void testGetQueuedThreads(){ |
149 |
< |
final MyReentrantLock lock = new MyReentrantLock(); |
148 |
> |
public void testGetQueuedThreads() { |
149 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
150 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
151 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
152 |
|
try { |
170 |
|
t1.join(); |
171 |
|
t2.join(); |
172 |
|
} catch(Exception e){ |
173 |
< |
fail("unexpected exception"); |
173 |
> |
unexpectedException(); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
< |
/* |
178 |
> |
/** |
179 |
|
* timed trylock is interruptible. |
180 |
|
*/ |
181 |
< |
public void testInterruptedException2(){ |
181 |
> |
public void testInterruptedException2() { |
182 |
|
final ReentrantLock lock = new ReentrantLock(); |
183 |
|
lock.lock(); |
184 |
|
Thread t = new Thread(new Runnable() { |
185 |
< |
public void run(){ |
186 |
< |
try{ |
185 |
> |
public void run() { |
186 |
> |
try { |
187 |
|
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
188 |
< |
threadFail("should throw"); |
188 |
> |
threadShouldThrow(); |
189 |
|
} catch(InterruptedException success){} |
190 |
|
} |
191 |
|
}); |
193 |
|
t.start(); |
194 |
|
t.interrupt(); |
195 |
|
} catch(Exception e){ |
196 |
< |
fail("unexpected exception"); |
196 |
> |
unexpectedException(); |
197 |
|
} |
198 |
|
} |
199 |
|
|
205 |
|
final ReentrantLock lock = new ReentrantLock(); |
206 |
|
lock.lock(); |
207 |
|
Thread t = new Thread(new Runnable() { |
208 |
< |
public void run(){ |
208 |
> |
public void run() { |
209 |
|
threadAssertFalse(lock.tryLock()); |
210 |
|
} |
211 |
|
}); |
214 |
|
t.join(); |
215 |
|
lock.unlock(); |
216 |
|
} catch(Exception e){ |
217 |
< |
fail("unexpected exception"); |
217 |
> |
unexpectedException(); |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* Timed Trylock on a locked lock times out |
222 |
> |
* Timed trylock on a locked lock times out |
223 |
|
*/ |
224 |
< |
public void testTryLock_Timeout(){ |
224 |
> |
public void testTryLock_Timeout() { |
225 |
|
final ReentrantLock lock = new ReentrantLock(); |
226 |
|
lock.lock(); |
227 |
|
Thread t = new Thread(new Runnable() { |
228 |
< |
public void run(){ |
228 |
> |
public void run() { |
229 |
|
try { |
230 |
|
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
231 |
|
} catch (Exception ex) { |
232 |
< |
threadFail("unexpected exception"); |
232 |
> |
threadUnexpectedException(); |
233 |
|
} |
234 |
|
} |
235 |
|
}); |
238 |
|
t.join(); |
239 |
|
lock.unlock(); |
240 |
|
} catch(Exception e){ |
241 |
< |
fail("unexpected exception"); |
241 |
> |
unexpectedException(); |
242 |
|
} |
243 |
|
} |
244 |
|
|
247 |
|
*/ |
248 |
|
public void testGetHoldCount() { |
249 |
|
ReentrantLock lock = new ReentrantLock(); |
250 |
< |
for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) { |
250 |
> |
for(int i = 1; i <= SIZE; i++) { |
251 |
|
lock.lock(); |
252 |
|
assertEquals(i,lock.getHoldCount()); |
253 |
|
} |
254 |
< |
for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) { |
254 |
> |
for(int i = SIZE; i > 0; i--) { |
255 |
|
lock.unlock(); |
256 |
|
assertEquals(i-1,lock.getHoldCount()); |
257 |
|
} |
274 |
|
Thread.sleep(SMALL_DELAY_MS); |
275 |
|
} |
276 |
|
catch(Exception e) { |
277 |
< |
threadFail("unexpected exception"); |
277 |
> |
threadUnexpectedException(); |
278 |
|
} |
279 |
|
lock.unlock(); |
280 |
|
} |
281 |
|
}); |
282 |
< |
try{ |
282 |
> |
try { |
283 |
|
t.start(); |
284 |
|
Thread.sleep(SHORT_DELAY_MS); |
285 |
|
assertTrue(lock.isLocked()); |
286 |
|
t.join(); |
287 |
|
assertFalse(lock.isLocked()); |
288 |
|
} catch(Exception e){ |
289 |
< |
fail("unexpected exception"); |
289 |
> |
unexpectedException(); |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
|
/** |
295 |
+ |
* lockInterruptibly is interruptible. |
296 |
+ |
*/ |
297 |
+ |
public void testLockInterruptibly1() { |
298 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
299 |
+ |
lock.lock(); |
300 |
+ |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
301 |
+ |
try { |
302 |
+ |
t.start(); |
303 |
+ |
t.interrupt(); |
304 |
+ |
lock.unlock(); |
305 |
+ |
t.join(); |
306 |
+ |
} catch(Exception e){ |
307 |
+ |
unexpectedException(); |
308 |
+ |
} |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
/** |
312 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
313 |
|
*/ |
314 |
< |
public void testLockInterruptibly() { |
314 |
> |
public void testLockInterruptibly2() { |
315 |
|
final ReentrantLock lock = new ReentrantLock(); |
316 |
|
try { |
317 |
|
lock.lockInterruptibly(); |
318 |
|
} catch(Exception e) { |
319 |
< |
fail("unexpected exception"); |
319 |
> |
unexpectedException(); |
320 |
|
} |
321 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
322 |
|
try { |
326 |
|
assertTrue(lock.isHeldByCurrentThread()); |
327 |
|
t.join(); |
328 |
|
} catch(Exception e){ |
329 |
< |
fail("unexpected exception"); |
329 |
> |
unexpectedException(); |
330 |
|
} |
331 |
|
} |
332 |
|
|
333 |
+ |
/** |
334 |
+ |
* Calling await without holding lock throws IllegalMonitorStateException |
335 |
+ |
*/ |
336 |
|
public void testAwait_IllegalMonitor() { |
337 |
|
final ReentrantLock lock = new ReentrantLock(); |
338 |
|
final Condition c = lock.newCondition(); |
339 |
|
try { |
340 |
|
c.await(); |
341 |
< |
fail("should throw"); |
341 |
> |
shouldThrow(); |
342 |
|
} |
343 |
|
catch (IllegalMonitorStateException success) { |
344 |
|
} |
345 |
|
catch (Exception ex) { |
346 |
< |
fail("should throw IMSE"); |
346 |
> |
unexpectedException(); |
347 |
|
} |
348 |
|
} |
349 |
|
|
350 |
+ |
/** |
351 |
+ |
* Calling signal without holding lock throws IllegalMonitorStateException |
352 |
+ |
*/ |
353 |
|
public void testSignal_IllegalMonitor() { |
354 |
|
final ReentrantLock lock = new ReentrantLock(); |
355 |
|
final Condition c = lock.newCondition(); |
356 |
|
try { |
357 |
|
c.signal(); |
358 |
< |
fail("should throw"); |
358 |
> |
shouldThrow(); |
359 |
|
} |
360 |
|
catch (IllegalMonitorStateException success) { |
361 |
|
} |
362 |
|
catch (Exception ex) { |
363 |
< |
fail("should throw IMSE"); |
363 |
> |
unexpectedException(); |
364 |
|
} |
365 |
|
} |
366 |
|
|
367 |
+ |
/** |
368 |
+ |
* awaitNanos without a signal times out |
369 |
+ |
*/ |
370 |
|
public void testAwaitNanos_Timeout() { |
371 |
|
final ReentrantLock lock = new ReentrantLock(); |
372 |
|
final Condition c = lock.newCondition(); |
377 |
|
lock.unlock(); |
378 |
|
} |
379 |
|
catch (Exception ex) { |
380 |
< |
fail("unexpected exception"); |
380 |
> |
unexpectedException(); |
381 |
|
} |
382 |
|
} |
383 |
|
|
384 |
+ |
/** |
385 |
+ |
* timed await without a signal times out |
386 |
+ |
*/ |
387 |
|
public void testAwait_Timeout() { |
388 |
|
final ReentrantLock lock = new ReentrantLock(); |
389 |
|
final Condition c = lock.newCondition(); |
393 |
|
lock.unlock(); |
394 |
|
} |
395 |
|
catch (Exception ex) { |
396 |
< |
fail("unexpected exception"); |
396 |
> |
unexpectedException(); |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
+ |
/** |
401 |
+ |
* awaitUntil without a signal times out |
402 |
+ |
*/ |
403 |
|
public void testAwaitUntil_Timeout() { |
404 |
|
final ReentrantLock lock = new ReentrantLock(); |
405 |
|
final Condition c = lock.newCondition(); |
410 |
|
lock.unlock(); |
411 |
|
} |
412 |
|
catch (Exception ex) { |
413 |
< |
fail("unexpected exception"); |
413 |
> |
unexpectedException(); |
414 |
|
} |
415 |
|
} |
416 |
|
|
417 |
+ |
/** |
418 |
+ |
* await returns when signalled |
419 |
+ |
*/ |
420 |
|
public void testAwait() { |
421 |
|
final ReentrantLock lock = new ReentrantLock(); |
422 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
422 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
423 |
|
Thread t = new Thread(new Runnable() { |
424 |
|
public void run() { |
425 |
|
try { |
428 |
|
lock.unlock(); |
429 |
|
} |
430 |
|
catch(InterruptedException e) { |
431 |
< |
threadFail("unexpected exception"); |
431 |
> |
threadUnexpectedException(); |
432 |
|
} |
433 |
|
} |
434 |
|
}); |
443 |
|
assertFalse(t.isAlive()); |
444 |
|
} |
445 |
|
catch (Exception ex) { |
446 |
< |
fail("unexpected exception"); |
446 |
> |
unexpectedException(); |
447 |
|
} |
448 |
|
} |
449 |
|
|
450 |
+ |
/** |
451 |
+ |
* hasWaiters returns true when a thread is waiting, else false |
452 |
+ |
*/ |
453 |
|
public void testHasWaiters() { |
454 |
|
final ReentrantLock lock = new ReentrantLock(); |
455 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
455 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
456 |
|
Thread t = new Thread(new Runnable() { |
457 |
|
public void run() { |
458 |
|
try { |
463 |
|
lock.unlock(); |
464 |
|
} |
465 |
|
catch(InterruptedException e) { |
466 |
< |
threadFail("unexpected exception"); |
466 |
> |
threadUnexpectedException(); |
467 |
|
} |
468 |
|
} |
469 |
|
}); |
485 |
|
assertFalse(t.isAlive()); |
486 |
|
} |
487 |
|
catch (Exception ex) { |
488 |
< |
fail("unexpected exception"); |
488 |
> |
unexpectedException(); |
489 |
|
} |
490 |
|
} |
491 |
|
|
492 |
+ |
/** |
493 |
+ |
* getWaitQueueLength returns number of waiting threads |
494 |
+ |
*/ |
495 |
|
public void testGetWaitQueueLength() { |
496 |
|
final ReentrantLock lock = new ReentrantLock(); |
497 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
497 |
> |
final AbstractQueuedSynchronizer.ConditionObject c = lock.newCondition(); |
498 |
|
Thread t1 = new Thread(new Runnable() { |
499 |
|
public void run() { |
500 |
|
try { |
505 |
|
lock.unlock(); |
506 |
|
} |
507 |
|
catch(InterruptedException e) { |
508 |
< |
threadFail("unexpected exception"); |
508 |
> |
threadUnexpectedException(); |
509 |
|
} |
510 |
|
} |
511 |
|
}); |
520 |
|
lock.unlock(); |
521 |
|
} |
522 |
|
catch(InterruptedException e) { |
523 |
< |
threadFail("unexpected exception"); |
523 |
> |
threadUnexpectedException(); |
524 |
|
} |
525 |
|
} |
526 |
|
}); |
546 |
|
assertFalse(t2.isAlive()); |
547 |
|
} |
548 |
|
catch (Exception ex) { |
549 |
< |
fail("unexpected exception"); |
489 |
< |
} |
490 |
< |
} |
491 |
< |
|
492 |
< |
public void testGetWaitingThreads() { |
493 |
< |
final MyReentrantLock lock = new MyReentrantLock(); |
494 |
< |
final MyReentrantLock.MyCondition c = (MyReentrantLock.MyCondition)lock.newCondition(); |
495 |
< |
Thread t1 = new Thread(new Runnable() { |
496 |
< |
public void run() { |
497 |
< |
try { |
498 |
< |
lock.lock(); |
499 |
< |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
500 |
< |
c.await(); |
501 |
< |
lock.unlock(); |
502 |
< |
} |
503 |
< |
catch(InterruptedException e) { |
504 |
< |
threadFail("unexpected exception"); |
505 |
< |
} |
506 |
< |
} |
507 |
< |
}); |
508 |
< |
|
509 |
< |
Thread t2 = new Thread(new Runnable() { |
510 |
< |
public void run() { |
511 |
< |
try { |
512 |
< |
lock.lock(); |
513 |
< |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
514 |
< |
c.await(); |
515 |
< |
lock.unlock(); |
516 |
< |
} |
517 |
< |
catch(InterruptedException e) { |
518 |
< |
threadFail("unexpected exception"); |
519 |
< |
} |
520 |
< |
} |
521 |
< |
}); |
522 |
< |
|
523 |
< |
try { |
524 |
< |
lock.lock(); |
525 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
526 |
< |
lock.unlock(); |
527 |
< |
t1.start(); |
528 |
< |
Thread.sleep(SHORT_DELAY_MS); |
529 |
< |
t2.start(); |
530 |
< |
Thread.sleep(SHORT_DELAY_MS); |
531 |
< |
lock.lock(); |
532 |
< |
assertTrue(c.hasWaiters()); |
533 |
< |
assertTrue(c.getWaitingThreads().contains(t1)); |
534 |
< |
assertTrue(c.getWaitingThreads().contains(t2)); |
535 |
< |
c.signalAll(); |
536 |
< |
lock.unlock(); |
537 |
< |
Thread.sleep(SHORT_DELAY_MS); |
538 |
< |
lock.lock(); |
539 |
< |
assertFalse(c.hasWaiters()); |
540 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
541 |
< |
lock.unlock(); |
542 |
< |
t1.join(SHORT_DELAY_MS); |
543 |
< |
t2.join(SHORT_DELAY_MS); |
544 |
< |
assertFalse(t1.isAlive()); |
545 |
< |
assertFalse(t2.isAlive()); |
546 |
< |
} |
547 |
< |
catch (Exception ex) { |
548 |
< |
fail("unexpected exception"); |
549 |
> |
unexpectedException(); |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
+ |
/** |
554 |
+ |
* awaitUninterruptibly doesn't abort on interrupt |
555 |
+ |
*/ |
556 |
|
public void testAwaitUninterruptibly() { |
557 |
|
final ReentrantLock lock = new ReentrantLock(); |
558 |
|
final Condition c = lock.newCondition(); |
571 |
|
lock.lock(); |
572 |
|
c.signal(); |
573 |
|
lock.unlock(); |
574 |
+ |
assert(t.isInterrupted()); |
575 |
|
t.join(SHORT_DELAY_MS); |
576 |
|
assertFalse(t.isAlive()); |
577 |
|
} |
578 |
|
catch (Exception ex) { |
579 |
< |
fail("unexpected exception"); |
579 |
> |
unexpectedException(); |
580 |
|
} |
581 |
|
} |
582 |
|
|
583 |
+ |
/** |
584 |
+ |
* await is interruptible |
585 |
+ |
*/ |
586 |
|
public void testAwait_Interrupt() { |
587 |
|
final ReentrantLock lock = new ReentrantLock(); |
588 |
|
final Condition c = lock.newCondition(); |
592 |
|
lock.lock(); |
593 |
|
c.await(); |
594 |
|
lock.unlock(); |
595 |
< |
threadFail("should throw"); |
595 |
> |
threadShouldThrow(); |
596 |
|
} |
597 |
|
catch(InterruptedException success) { |
598 |
|
} |
607 |
|
assertFalse(t.isAlive()); |
608 |
|
} |
609 |
|
catch (Exception ex) { |
610 |
< |
fail("unexpected exception"); |
610 |
> |
unexpectedException(); |
611 |
|
} |
612 |
|
} |
613 |
|
|
614 |
+ |
/** |
615 |
+ |
* awaitNanos is interruptible |
616 |
+ |
*/ |
617 |
|
public void testAwaitNanos_Interrupt() { |
618 |
|
final ReentrantLock lock = new ReentrantLock(); |
619 |
|
final Condition c = lock.newCondition(); |
623 |
|
lock.lock(); |
624 |
|
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
625 |
|
lock.unlock(); |
626 |
< |
threadFail("should throw"); |
626 |
> |
threadShouldThrow(); |
627 |
|
} |
628 |
|
catch(InterruptedException success) { |
629 |
|
} |
638 |
|
assertFalse(t.isAlive()); |
639 |
|
} |
640 |
|
catch (Exception ex) { |
641 |
< |
fail("unexpected exception"); |
641 |
> |
unexpectedException(); |
642 |
|
} |
643 |
|
} |
644 |
|
|
645 |
+ |
/** |
646 |
+ |
* awaitUntil is interruptible |
647 |
+ |
*/ |
648 |
|
public void testAwaitUntil_Interrupt() { |
649 |
|
final ReentrantLock lock = new ReentrantLock(); |
650 |
|
final Condition c = lock.newCondition(); |
655 |
|
java.util.Date d = new java.util.Date(); |
656 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
657 |
|
lock.unlock(); |
658 |
< |
threadFail("should throw"); |
658 |
> |
threadShouldThrow(); |
659 |
|
} |
660 |
|
catch(InterruptedException success) { |
661 |
|
} |
670 |
|
assertFalse(t.isAlive()); |
671 |
|
} |
672 |
|
catch (Exception ex) { |
673 |
< |
fail("unexpected exception"); |
673 |
> |
unexpectedException(); |
674 |
|
} |
675 |
|
} |
676 |
|
|
677 |
+ |
/** |
678 |
+ |
* signalAll wakes up all threads |
679 |
+ |
*/ |
680 |
|
public void testSignalAll() { |
681 |
|
final ReentrantLock lock = new ReentrantLock(); |
682 |
|
final Condition c = lock.newCondition(); |
688 |
|
lock.unlock(); |
689 |
|
} |
690 |
|
catch(InterruptedException e) { |
691 |
< |
threadFail("unexpected exception"); |
691 |
> |
threadUnexpectedException(); |
692 |
|
} |
693 |
|
} |
694 |
|
}); |
701 |
|
lock.unlock(); |
702 |
|
} |
703 |
|
catch(InterruptedException e) { |
704 |
< |
threadFail("unexpected exception"); |
704 |
> |
threadUnexpectedException(); |
705 |
|
} |
706 |
|
} |
707 |
|
}); |
719 |
|
assertFalse(t2.isAlive()); |
720 |
|
} |
721 |
|
catch (Exception ex) { |
722 |
< |
fail("unexpected exception"); |
722 |
> |
unexpectedException(); |
723 |
|
} |
724 |
|
} |
725 |
|
|
726 |
+ |
/** |
727 |
+ |
* A serialized lock deserializes as unlocked |
728 |
+ |
*/ |
729 |
|
public void testSerialization() { |
730 |
|
ReentrantLock l = new ReentrantLock(); |
731 |
|
l.lock(); |
744 |
|
r.unlock(); |
745 |
|
} catch(Exception e){ |
746 |
|
e.printStackTrace(); |
747 |
< |
fail("unexpected exception"); |
747 |
> |
unexpectedException(); |
748 |
|
} |
749 |
|
} |
750 |
|
|