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 |
|
} |
60 |
< |
public ConditionObject newCondition() { |
61 |
< |
return new MyCondition(this); |
60 |
> |
public Collection<Thread> getWaitingThreads(Condition c) { |
61 |
> |
return super.getWaitingThreads(c); |
62 |
|
} |
63 |
|
|
58 |
– |
static class MyCondition extends ReentrantLock.ConditionObject { |
59 |
– |
MyCondition(MyReentrantLock l) { super(l); } |
60 |
– |
public Collection<Thread> getWaitingThreads() { |
61 |
– |
return super.getWaitingThreads(); |
62 |
– |
} |
63 |
– |
} |
64 |
|
|
65 |
|
} |
66 |
|
|
67 |
< |
/* |
67 |
> |
/** |
68 |
> |
* Constructor sets given fairness |
69 |
> |
*/ |
70 |
> |
public void testConstructor() { |
71 |
> |
ReentrantLock rl = new ReentrantLock(); |
72 |
> |
assertFalse(rl.isFair()); |
73 |
> |
ReentrantLock r2 = new ReentrantLock(true); |
74 |
> |
assertTrue(r2.isFair()); |
75 |
> |
} |
76 |
> |
|
77 |
> |
/** |
78 |
> |
* locking an unlocked lock succeeds |
79 |
> |
*/ |
80 |
> |
public void testLock() { |
81 |
> |
ReentrantLock rl = new ReentrantLock(); |
82 |
> |
rl.lock(); |
83 |
> |
assertTrue(rl.isLocked()); |
84 |
> |
rl.unlock(); |
85 |
> |
} |
86 |
> |
|
87 |
> |
/** |
88 |
> |
* locking an unlocked fair lock succeeds |
89 |
> |
*/ |
90 |
> |
public void testFairLock() { |
91 |
> |
ReentrantLock rl = new ReentrantLock(true); |
92 |
> |
rl.lock(); |
93 |
> |
assertTrue(rl.isLocked()); |
94 |
> |
rl.unlock(); |
95 |
> |
} |
96 |
> |
|
97 |
> |
/** |
98 |
|
* Unlocking an unlocked lock throws IllegalMonitorStateException |
99 |
|
*/ |
100 |
< |
public void testIllegalMonitorStateException(){ |
100 |
> |
public void testUnlock_IllegalMonitorStateException() { |
101 |
|
ReentrantLock rl = new ReentrantLock(); |
102 |
< |
try{ |
102 |
> |
try { |
103 |
|
rl.unlock(); |
104 |
< |
fail("Should of thown Illegal Monitor State Exception"); |
104 |
> |
shouldThrow(); |
105 |
|
|
106 |
|
} catch(IllegalMonitorStateException success){} |
107 |
|
} |
108 |
|
|
109 |
< |
/* |
110 |
< |
* lockInterruptibly is interruptible. |
109 |
> |
/** |
110 |
> |
* trylock on an unlocked lock succeeds |
111 |
> |
*/ |
112 |
> |
public void testTryLock() { |
113 |
> |
ReentrantLock rl = new ReentrantLock(); |
114 |
> |
assertTrue(rl.tryLock()); |
115 |
> |
assertTrue(rl.isLocked()); |
116 |
> |
rl.unlock(); |
117 |
> |
} |
118 |
> |
|
119 |
> |
|
120 |
> |
/** |
121 |
> |
* hasQueuedThreads reports whether there are waiting threads |
122 |
|
*/ |
123 |
< |
public void testInterruptedException(){ |
123 |
> |
public void testhasQueuedThreads() { |
124 |
|
final ReentrantLock lock = new ReentrantLock(); |
125 |
< |
lock.lock(); |
126 |
< |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
125 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
126 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
127 |
|
try { |
128 |
< |
t.start(); |
129 |
< |
t.interrupt(); |
128 |
> |
assertFalse(lock.hasQueuedThreads()); |
129 |
> |
lock.lock(); |
130 |
> |
t1.start(); |
131 |
> |
Thread.sleep(SHORT_DELAY_MS); |
132 |
> |
assertTrue(lock.hasQueuedThreads()); |
133 |
> |
t2.start(); |
134 |
> |
Thread.sleep(SHORT_DELAY_MS); |
135 |
> |
assertTrue(lock.hasQueuedThreads()); |
136 |
> |
t1.interrupt(); |
137 |
> |
Thread.sleep(SHORT_DELAY_MS); |
138 |
> |
assertTrue(lock.hasQueuedThreads()); |
139 |
|
lock.unlock(); |
140 |
< |
t.join(); |
140 |
> |
Thread.sleep(SHORT_DELAY_MS); |
141 |
> |
assertFalse(lock.hasQueuedThreads()); |
142 |
> |
t1.join(); |
143 |
> |
t2.join(); |
144 |
|
} catch(Exception e){ |
145 |
< |
fail("unexpected exception"); |
145 |
> |
unexpectedException(); |
146 |
|
} |
147 |
|
} |
148 |
|
|
149 |
< |
/* |
150 |
< |
* getLockQueueLength reports number of waiting threads |
149 |
> |
/** |
150 |
> |
* getQueueLength reports number of waiting threads |
151 |
|
*/ |
152 |
< |
public void testgetLockQueueLength(){ |
152 |
> |
public void testGetQueueLength() { |
153 |
|
final ReentrantLock lock = new ReentrantLock(); |
154 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
155 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
156 |
|
try { |
157 |
< |
assertEquals(0, lock.getLockQueueLength()); |
157 |
> |
assertEquals(0, lock.getQueueLength()); |
158 |
|
lock.lock(); |
159 |
|
t1.start(); |
160 |
|
Thread.sleep(SHORT_DELAY_MS); |
161 |
< |
assertEquals(1, lock.getLockQueueLength()); |
161 |
> |
assertEquals(1, lock.getQueueLength()); |
162 |
|
t2.start(); |
163 |
|
Thread.sleep(SHORT_DELAY_MS); |
164 |
< |
assertEquals(2, lock.getLockQueueLength()); |
164 |
> |
assertEquals(2, lock.getQueueLength()); |
165 |
|
t1.interrupt(); |
166 |
|
Thread.sleep(SHORT_DELAY_MS); |
167 |
< |
assertEquals(1, lock.getLockQueueLength()); |
167 |
> |
assertEquals(1, lock.getQueueLength()); |
168 |
|
lock.unlock(); |
169 |
|
Thread.sleep(SHORT_DELAY_MS); |
170 |
< |
assertEquals(0, lock.getLockQueueLength()); |
170 |
> |
assertEquals(0, lock.getQueueLength()); |
171 |
|
t1.join(); |
172 |
|
t2.join(); |
173 |
|
} catch(Exception e){ |
174 |
< |
fail("unexpected exception"); |
174 |
> |
unexpectedException(); |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
< |
/* |
178 |
> |
/** |
179 |
|
* getQueuedThreads includes waiting threads |
180 |
|
*/ |
181 |
< |
public void testGetQueuedThreads(){ |
182 |
< |
final MyReentrantLock lock = new MyReentrantLock(); |
181 |
> |
public void testGetQueuedThreads() { |
182 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
183 |
|
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
184 |
|
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
185 |
|
try { |
203 |
|
t1.join(); |
204 |
|
t2.join(); |
205 |
|
} catch(Exception e){ |
206 |
< |
fail("unexpected exception"); |
206 |
> |
unexpectedException(); |
207 |
|
} |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
< |
/* |
211 |
> |
/** |
212 |
|
* timed trylock is interruptible. |
213 |
|
*/ |
214 |
< |
public void testInterruptedException2(){ |
214 |
> |
public void testInterruptedException2() { |
215 |
|
final ReentrantLock lock = new ReentrantLock(); |
216 |
|
lock.lock(); |
217 |
|
Thread t = new Thread(new Runnable() { |
218 |
< |
public void run(){ |
219 |
< |
try{ |
218 |
> |
public void run() { |
219 |
> |
try { |
220 |
|
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
221 |
< |
threadFail("should throw"); |
221 |
> |
threadShouldThrow(); |
222 |
|
} catch(InterruptedException success){} |
223 |
|
} |
224 |
|
}); |
226 |
|
t.start(); |
227 |
|
t.interrupt(); |
228 |
|
} catch(Exception e){ |
229 |
< |
fail("unexpected exception"); |
229 |
> |
unexpectedException(); |
230 |
|
} |
231 |
|
} |
232 |
|
|
238 |
|
final ReentrantLock lock = new ReentrantLock(); |
239 |
|
lock.lock(); |
240 |
|
Thread t = new Thread(new Runnable() { |
241 |
< |
public void run(){ |
241 |
> |
public void run() { |
242 |
|
threadAssertFalse(lock.tryLock()); |
243 |
|
} |
244 |
|
}); |
247 |
|
t.join(); |
248 |
|
lock.unlock(); |
249 |
|
} catch(Exception e){ |
250 |
< |
fail("unexpected exception"); |
250 |
> |
unexpectedException(); |
251 |
|
} |
252 |
|
} |
253 |
|
|
254 |
|
/** |
255 |
< |
* Timed Trylock on a locked lock times out |
255 |
> |
* Timed trylock on a locked lock times out |
256 |
|
*/ |
257 |
< |
public void testTryLock_Timeout(){ |
257 |
> |
public void testTryLock_Timeout() { |
258 |
|
final ReentrantLock lock = new ReentrantLock(); |
259 |
|
lock.lock(); |
260 |
|
Thread t = new Thread(new Runnable() { |
261 |
< |
public void run(){ |
261 |
> |
public void run() { |
262 |
|
try { |
263 |
|
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
264 |
|
} catch (Exception ex) { |
265 |
< |
threadFail("unexpected exception"); |
265 |
> |
threadUnexpectedException(); |
266 |
|
} |
267 |
|
} |
268 |
|
}); |
271 |
|
t.join(); |
272 |
|
lock.unlock(); |
273 |
|
} catch(Exception e){ |
274 |
< |
fail("unexpected exception"); |
274 |
> |
unexpectedException(); |
275 |
|
} |
276 |
|
} |
277 |
|
|
280 |
|
*/ |
281 |
|
public void testGetHoldCount() { |
282 |
|
ReentrantLock lock = new ReentrantLock(); |
283 |
< |
for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) { |
283 |
> |
for(int i = 1; i <= SIZE; i++) { |
284 |
|
lock.lock(); |
285 |
|
assertEquals(i,lock.getHoldCount()); |
286 |
|
} |
287 |
< |
for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) { |
287 |
> |
for(int i = SIZE; i > 0; i--) { |
288 |
|
lock.unlock(); |
289 |
|
assertEquals(i-1,lock.getHoldCount()); |
290 |
|
} |
307 |
|
Thread.sleep(SMALL_DELAY_MS); |
308 |
|
} |
309 |
|
catch(Exception e) { |
310 |
< |
threadFail("unexpected exception"); |
310 |
> |
threadUnexpectedException(); |
311 |
|
} |
312 |
|
lock.unlock(); |
313 |
|
} |
314 |
|
}); |
315 |
< |
try{ |
315 |
> |
try { |
316 |
|
t.start(); |
317 |
|
Thread.sleep(SHORT_DELAY_MS); |
318 |
|
assertTrue(lock.isLocked()); |
319 |
|
t.join(); |
320 |
|
assertFalse(lock.isLocked()); |
321 |
|
} catch(Exception e){ |
322 |
< |
fail("unexpected exception"); |
322 |
> |
unexpectedException(); |
323 |
|
} |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
/** |
328 |
+ |
* lockInterruptibly is interruptible. |
329 |
+ |
*/ |
330 |
+ |
public void testLockInterruptibly1() { |
331 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
332 |
+ |
lock.lock(); |
333 |
+ |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
334 |
+ |
try { |
335 |
+ |
t.start(); |
336 |
+ |
t.interrupt(); |
337 |
+ |
lock.unlock(); |
338 |
+ |
t.join(); |
339 |
+ |
} catch(Exception e){ |
340 |
+ |
unexpectedException(); |
341 |
+ |
} |
342 |
+ |
} |
343 |
+ |
|
344 |
+ |
/** |
345 |
|
* lockInterruptibly succeeds when unlocked, else is interruptible |
346 |
|
*/ |
347 |
< |
public void testLockInterruptibly() { |
347 |
> |
public void testLockInterruptibly2() { |
348 |
|
final ReentrantLock lock = new ReentrantLock(); |
349 |
|
try { |
350 |
|
lock.lockInterruptibly(); |
351 |
|
} catch(Exception e) { |
352 |
< |
fail("unexpected exception"); |
352 |
> |
unexpectedException(); |
353 |
|
} |
354 |
|
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
355 |
|
try { |
359 |
|
assertTrue(lock.isHeldByCurrentThread()); |
360 |
|
t.join(); |
361 |
|
} catch(Exception e){ |
362 |
< |
fail("unexpected exception"); |
362 |
> |
unexpectedException(); |
363 |
|
} |
364 |
|
} |
365 |
|
|
366 |
+ |
/** |
367 |
+ |
* Calling await without holding lock throws IllegalMonitorStateException |
368 |
+ |
*/ |
369 |
|
public void testAwait_IllegalMonitor() { |
370 |
|
final ReentrantLock lock = new ReentrantLock(); |
371 |
|
final Condition c = lock.newCondition(); |
372 |
|
try { |
373 |
|
c.await(); |
374 |
< |
fail("should throw"); |
374 |
> |
shouldThrow(); |
375 |
|
} |
376 |
|
catch (IllegalMonitorStateException success) { |
377 |
|
} |
378 |
|
catch (Exception ex) { |
379 |
< |
fail("should throw IMSE"); |
379 |
> |
unexpectedException(); |
380 |
|
} |
381 |
|
} |
382 |
|
|
383 |
+ |
/** |
384 |
+ |
* Calling signal without holding lock throws IllegalMonitorStateException |
385 |
+ |
*/ |
386 |
|
public void testSignal_IllegalMonitor() { |
387 |
|
final ReentrantLock lock = new ReentrantLock(); |
388 |
|
final Condition c = lock.newCondition(); |
389 |
|
try { |
390 |
|
c.signal(); |
391 |
< |
fail("should throw"); |
391 |
> |
shouldThrow(); |
392 |
|
} |
393 |
|
catch (IllegalMonitorStateException success) { |
394 |
|
} |
395 |
|
catch (Exception ex) { |
396 |
< |
fail("should throw IMSE"); |
396 |
> |
unexpectedException(); |
397 |
|
} |
398 |
|
} |
399 |
|
|
400 |
+ |
/** |
401 |
+ |
* awaitNanos without a signal times out |
402 |
+ |
*/ |
403 |
|
public void testAwaitNanos_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 |
+ |
* timed await without a signal times out |
419 |
+ |
*/ |
420 |
|
public void testAwait_Timeout() { |
421 |
|
final ReentrantLock lock = new ReentrantLock(); |
422 |
|
final Condition c = lock.newCondition(); |
426 |
|
lock.unlock(); |
427 |
|
} |
428 |
|
catch (Exception ex) { |
429 |
< |
fail("unexpected exception"); |
429 |
> |
unexpectedException(); |
430 |
|
} |
431 |
|
} |
432 |
|
|
433 |
+ |
/** |
434 |
+ |
* awaitUntil without a signal times out |
435 |
+ |
*/ |
436 |
|
public void testAwaitUntil_Timeout() { |
437 |
|
final ReentrantLock lock = new ReentrantLock(); |
438 |
|
final Condition c = lock.newCondition(); |
443 |
|
lock.unlock(); |
444 |
|
} |
445 |
|
catch (Exception ex) { |
446 |
< |
fail("unexpected exception"); |
446 |
> |
unexpectedException(); |
447 |
|
} |
448 |
|
} |
449 |
|
|
450 |
+ |
/** |
451 |
+ |
* await returns when signalled |
452 |
+ |
*/ |
453 |
|
public void testAwait() { |
454 |
|
final ReentrantLock lock = new ReentrantLock(); |
455 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
455 |
> |
final Condition c = lock.newCondition(); |
456 |
|
Thread t = new Thread(new Runnable() { |
457 |
|
public void run() { |
458 |
|
try { |
461 |
|
lock.unlock(); |
462 |
|
} |
463 |
|
catch(InterruptedException e) { |
464 |
< |
threadFail("unexpected exception"); |
464 |
> |
threadUnexpectedException(); |
465 |
|
} |
466 |
|
} |
467 |
|
}); |
476 |
|
assertFalse(t.isAlive()); |
477 |
|
} |
478 |
|
catch (Exception ex) { |
479 |
< |
fail("unexpected exception"); |
479 |
> |
unexpectedException(); |
480 |
> |
} |
481 |
> |
} |
482 |
> |
|
483 |
> |
/** |
484 |
> |
* hasWaiters throws IAE if not owned |
485 |
> |
*/ |
486 |
> |
public void testHasWaitersIAE() { |
487 |
> |
final ReentrantLock lock = new ReentrantLock(); |
488 |
> |
final Condition c = (lock.newCondition()); |
489 |
> |
final ReentrantLock lock2 = new ReentrantLock(); |
490 |
> |
try { |
491 |
> |
lock2.hasWaiters(c); |
492 |
> |
shouldThrow(); |
493 |
> |
} catch (IllegalArgumentException success) { |
494 |
> |
} catch (Exception ex) { |
495 |
> |
unexpectedException(); |
496 |
> |
} |
497 |
> |
} |
498 |
> |
|
499 |
> |
/** |
500 |
> |
* hasWaiters throws IMSE if not locked |
501 |
> |
*/ |
502 |
> |
public void testHasWaitersIMSE() { |
503 |
> |
final ReentrantLock lock = new ReentrantLock(); |
504 |
> |
final Condition c = (lock.newCondition()); |
505 |
> |
try { |
506 |
> |
lock.hasWaiters(c); |
507 |
> |
shouldThrow(); |
508 |
> |
} catch (IllegalMonitorStateException success) { |
509 |
> |
} catch (Exception ex) { |
510 |
> |
unexpectedException(); |
511 |
> |
} |
512 |
> |
} |
513 |
> |
|
514 |
> |
|
515 |
> |
/** |
516 |
> |
* getWaitQueueLength throws IAE if not owned |
517 |
> |
*/ |
518 |
> |
public void testGetWaitQueueLengthIAE() { |
519 |
> |
final ReentrantLock lock = new ReentrantLock(); |
520 |
> |
final Condition c = (lock.newCondition()); |
521 |
> |
final ReentrantLock lock2 = new ReentrantLock(); |
522 |
> |
try { |
523 |
> |
lock2.getWaitQueueLength(c); |
524 |
> |
shouldThrow(); |
525 |
> |
} catch (IllegalArgumentException success) { |
526 |
> |
} catch (Exception ex) { |
527 |
> |
unexpectedException(); |
528 |
> |
} |
529 |
> |
} |
530 |
> |
|
531 |
> |
/** |
532 |
> |
* getWaitQueueLength throws IMSE if not locked |
533 |
> |
*/ |
534 |
> |
public void testGetWaitQueueLengthIMSE() { |
535 |
> |
final ReentrantLock lock = new ReentrantLock(); |
536 |
> |
final Condition c = (lock.newCondition()); |
537 |
> |
try { |
538 |
> |
lock.getWaitQueueLength(c); |
539 |
> |
shouldThrow(); |
540 |
> |
} catch (IllegalMonitorStateException success) { |
541 |
> |
} catch (Exception ex) { |
542 |
> |
unexpectedException(); |
543 |
|
} |
544 |
|
} |
545 |
|
|
546 |
+ |
|
547 |
+ |
/** |
548 |
+ |
* getWaitingThreads throws IAE if not owned |
549 |
+ |
*/ |
550 |
+ |
public void testGetWaitingThreadsIAE() { |
551 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
552 |
+ |
final Condition c = (lock.newCondition()); |
553 |
+ |
final PublicReentrantLock lock2 = new PublicReentrantLock(); |
554 |
+ |
try { |
555 |
+ |
lock2.getWaitingThreads(c); |
556 |
+ |
shouldThrow(); |
557 |
+ |
} catch (IllegalArgumentException success) { |
558 |
+ |
} catch (Exception ex) { |
559 |
+ |
unexpectedException(); |
560 |
+ |
} |
561 |
+ |
} |
562 |
+ |
|
563 |
+ |
/** |
564 |
+ |
* getWaitingThreads throws IMSE if not locked |
565 |
+ |
*/ |
566 |
+ |
public void testGetWaitingThreadsIMSE() { |
567 |
+ |
final PublicReentrantLock lock = new PublicReentrantLock(); |
568 |
+ |
final Condition c = (lock.newCondition()); |
569 |
+ |
try { |
570 |
+ |
lock.getWaitingThreads(c); |
571 |
+ |
shouldThrow(); |
572 |
+ |
} catch (IllegalMonitorStateException success) { |
573 |
+ |
} catch (Exception ex) { |
574 |
+ |
unexpectedException(); |
575 |
+ |
} |
576 |
+ |
} |
577 |
+ |
|
578 |
+ |
|
579 |
+ |
|
580 |
+ |
/** |
581 |
+ |
* hasWaiters returns true when a thread is waiting, else false |
582 |
+ |
*/ |
583 |
|
public void testHasWaiters() { |
584 |
|
final ReentrantLock lock = new ReentrantLock(); |
585 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
585 |
> |
final Condition c = lock.newCondition(); |
586 |
|
Thread t = new Thread(new Runnable() { |
587 |
|
public void run() { |
588 |
|
try { |
589 |
|
lock.lock(); |
590 |
< |
threadAssertFalse(c.hasWaiters()); |
591 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
590 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
591 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
592 |
|
c.await(); |
593 |
|
lock.unlock(); |
594 |
|
} |
595 |
|
catch(InterruptedException e) { |
596 |
< |
threadFail("unexpected exception"); |
596 |
> |
threadUnexpectedException(); |
597 |
|
} |
598 |
|
} |
599 |
|
}); |
602 |
|
t.start(); |
603 |
|
Thread.sleep(SHORT_DELAY_MS); |
604 |
|
lock.lock(); |
605 |
< |
assertTrue(c.hasWaiters()); |
606 |
< |
assertEquals(1, c.getWaitQueueLength()); |
605 |
> |
assertTrue(lock.hasWaiters(c)); |
606 |
> |
assertEquals(1, lock.getWaitQueueLength(c)); |
607 |
|
c.signal(); |
608 |
|
lock.unlock(); |
609 |
|
Thread.sleep(SHORT_DELAY_MS); |
610 |
|
lock.lock(); |
611 |
< |
assertFalse(c.hasWaiters()); |
612 |
< |
assertEquals(0, c.getWaitQueueLength()); |
611 |
> |
assertFalse(lock.hasWaiters(c)); |
612 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
613 |
|
lock.unlock(); |
614 |
|
t.join(SHORT_DELAY_MS); |
615 |
|
assertFalse(t.isAlive()); |
616 |
|
} |
617 |
|
catch (Exception ex) { |
618 |
< |
fail("unexpected exception"); |
618 |
> |
unexpectedException(); |
619 |
|
} |
620 |
|
} |
621 |
|
|
622 |
+ |
/** |
623 |
+ |
* getWaitQueueLength returns number of waiting threads |
624 |
+ |
*/ |
625 |
|
public void testGetWaitQueueLength() { |
626 |
|
final ReentrantLock lock = new ReentrantLock(); |
627 |
< |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
627 |
> |
final Condition c = lock.newCondition(); |
628 |
|
Thread t1 = new Thread(new Runnable() { |
629 |
|
public void run() { |
630 |
|
try { |
631 |
|
lock.lock(); |
632 |
< |
threadAssertFalse(c.hasWaiters()); |
633 |
< |
threadAssertEquals(0, c.getWaitQueueLength()); |
632 |
> |
threadAssertFalse(lock.hasWaiters(c)); |
633 |
> |
threadAssertEquals(0, lock.getWaitQueueLength(c)); |
634 |
|
c.await(); |
635 |
|
lock.unlock(); |
636 |
|
} |
637 |
|
catch(InterruptedException e) { |
638 |
< |
threadFail("unexpected exception"); |
638 |
> |
threadUnexpectedException(); |
639 |
|
} |
640 |
|
} |
641 |
|
}); |
644 |
|
public void run() { |
645 |
|
try { |
646 |
|
lock.lock(); |
647 |
< |
threadAssertTrue(c.hasWaiters()); |
648 |
< |
threadAssertEquals(1, c.getWaitQueueLength()); |
647 |
> |
threadAssertTrue(lock.hasWaiters(c)); |
648 |
> |
threadAssertEquals(1, lock.getWaitQueueLength(c)); |
649 |
|
c.await(); |
650 |
|
lock.unlock(); |
651 |
|
} |
652 |
|
catch(InterruptedException e) { |
653 |
< |
threadFail("unexpected exception"); |
653 |
> |
threadUnexpectedException(); |
654 |
|
} |
655 |
|
} |
656 |
|
}); |
661 |
|
t2.start(); |
662 |
|
Thread.sleep(SHORT_DELAY_MS); |
663 |
|
lock.lock(); |
664 |
< |
assertTrue(c.hasWaiters()); |
665 |
< |
assertEquals(2, c.getWaitQueueLength()); |
664 |
> |
assertTrue(lock.hasWaiters(c)); |
665 |
> |
assertEquals(2, lock.getWaitQueueLength(c)); |
666 |
|
c.signalAll(); |
667 |
|
lock.unlock(); |
668 |
|
Thread.sleep(SHORT_DELAY_MS); |
669 |
|
lock.lock(); |
670 |
< |
assertFalse(c.hasWaiters()); |
671 |
< |
assertEquals(0, c.getWaitQueueLength()); |
670 |
> |
assertFalse(lock.hasWaiters(c)); |
671 |
> |
assertEquals(0, lock.getWaitQueueLength(c)); |
672 |
|
lock.unlock(); |
673 |
|
t1.join(SHORT_DELAY_MS); |
674 |
|
t2.join(SHORT_DELAY_MS); |
676 |
|
assertFalse(t2.isAlive()); |
677 |
|
} |
678 |
|
catch (Exception ex) { |
679 |
< |
fail("unexpected exception"); |
679 |
> |
unexpectedException(); |
680 |
|
} |
681 |
|
} |
682 |
|
|
683 |
+ |
/** |
684 |
+ |
* getWaitingThreads returns only and all waiting threads |
685 |
+ |
*/ |
686 |
|
public void testGetWaitingThreads() { |
687 |
< |
final MyReentrantLock lock = new MyReentrantLock(); |
688 |
< |
final MyReentrantLock.MyCondition c = (MyReentrantLock.MyCondition)lock.newCondition(); |
687 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
688 |
> |
final Condition c = lock.newCondition(); |
689 |
|
Thread t1 = new Thread(new Runnable() { |
690 |
|
public void run() { |
691 |
|
try { |
692 |
|
lock.lock(); |
693 |
< |
threadAssertTrue(c.getWaitingThreads().isEmpty()); |
693 |
> |
threadAssertTrue(lock.getWaitingThreads(c).isEmpty()); |
694 |
|
c.await(); |
695 |
|
lock.unlock(); |
696 |
|
} |
697 |
|
catch(InterruptedException e) { |
698 |
< |
threadFail("unexpected exception"); |
698 |
> |
threadUnexpectedException(); |
699 |
|
} |
700 |
|
} |
701 |
|
}); |
704 |
|
public void run() { |
705 |
|
try { |
706 |
|
lock.lock(); |
707 |
< |
threadAssertFalse(c.getWaitingThreads().isEmpty()); |
707 |
> |
threadAssertFalse(lock.getWaitingThreads(c).isEmpty()); |
708 |
|
c.await(); |
709 |
|
lock.unlock(); |
710 |
|
} |
711 |
|
catch(InterruptedException e) { |
712 |
< |
threadFail("unexpected exception"); |
712 |
> |
threadUnexpectedException(); |
713 |
|
} |
714 |
|
} |
715 |
|
}); |
716 |
|
|
717 |
|
try { |
718 |
|
lock.lock(); |
719 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
719 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
720 |
|
lock.unlock(); |
721 |
|
t1.start(); |
722 |
|
Thread.sleep(SHORT_DELAY_MS); |
723 |
|
t2.start(); |
724 |
|
Thread.sleep(SHORT_DELAY_MS); |
725 |
|
lock.lock(); |
726 |
< |
assertTrue(c.hasWaiters()); |
727 |
< |
assertTrue(c.getWaitingThreads().contains(t1)); |
728 |
< |
assertTrue(c.getWaitingThreads().contains(t2)); |
726 |
> |
assertTrue(lock.hasWaiters(c)); |
727 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t1)); |
728 |
> |
assertTrue(lock.getWaitingThreads(c).contains(t2)); |
729 |
|
c.signalAll(); |
730 |
|
lock.unlock(); |
731 |
|
Thread.sleep(SHORT_DELAY_MS); |
732 |
|
lock.lock(); |
733 |
< |
assertFalse(c.hasWaiters()); |
734 |
< |
assertTrue(c.getWaitingThreads().isEmpty()); |
733 |
> |
assertFalse(lock.hasWaiters(c)); |
734 |
> |
assertTrue(lock.getWaitingThreads(c).isEmpty()); |
735 |
|
lock.unlock(); |
736 |
|
t1.join(SHORT_DELAY_MS); |
737 |
|
t2.join(SHORT_DELAY_MS); |
739 |
|
assertFalse(t2.isAlive()); |
740 |
|
} |
741 |
|
catch (Exception ex) { |
742 |
< |
fail("unexpected exception"); |
742 |
> |
unexpectedException(); |
743 |
|
} |
744 |
|
} |
745 |
|
|
746 |
+ |
|
747 |
+ |
|
748 |
+ |
/** |
749 |
+ |
* awaitUninterruptibly doesn't abort on interrupt |
750 |
+ |
*/ |
751 |
|
public void testAwaitUninterruptibly() { |
752 |
|
final ReentrantLock lock = new ReentrantLock(); |
753 |
|
final Condition c = lock.newCondition(); |
766 |
|
lock.lock(); |
767 |
|
c.signal(); |
768 |
|
lock.unlock(); |
769 |
+ |
assert(t.isInterrupted()); |
770 |
|
t.join(SHORT_DELAY_MS); |
771 |
|
assertFalse(t.isAlive()); |
772 |
|
} |
773 |
|
catch (Exception ex) { |
774 |
< |
fail("unexpected exception"); |
774 |
> |
unexpectedException(); |
775 |
|
} |
776 |
|
} |
777 |
|
|
778 |
+ |
/** |
779 |
+ |
* await is interruptible |
780 |
+ |
*/ |
781 |
|
public void testAwait_Interrupt() { |
782 |
|
final ReentrantLock lock = new ReentrantLock(); |
783 |
|
final Condition c = lock.newCondition(); |
787 |
|
lock.lock(); |
788 |
|
c.await(); |
789 |
|
lock.unlock(); |
790 |
< |
threadFail("should throw"); |
790 |
> |
threadShouldThrow(); |
791 |
|
} |
792 |
|
catch(InterruptedException success) { |
793 |
|
} |
802 |
|
assertFalse(t.isAlive()); |
803 |
|
} |
804 |
|
catch (Exception ex) { |
805 |
< |
fail("unexpected exception"); |
805 |
> |
unexpectedException(); |
806 |
|
} |
807 |
|
} |
808 |
|
|
809 |
+ |
/** |
810 |
+ |
* awaitNanos is interruptible |
811 |
+ |
*/ |
812 |
|
public void testAwaitNanos_Interrupt() { |
813 |
|
final ReentrantLock lock = new ReentrantLock(); |
814 |
|
final Condition c = lock.newCondition(); |
818 |
|
lock.lock(); |
819 |
|
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
820 |
|
lock.unlock(); |
821 |
< |
threadFail("should throw"); |
821 |
> |
threadShouldThrow(); |
822 |
|
} |
823 |
|
catch(InterruptedException success) { |
824 |
|
} |
833 |
|
assertFalse(t.isAlive()); |
834 |
|
} |
835 |
|
catch (Exception ex) { |
836 |
< |
fail("unexpected exception"); |
836 |
> |
unexpectedException(); |
837 |
|
} |
838 |
|
} |
839 |
|
|
840 |
+ |
/** |
841 |
+ |
* awaitUntil is interruptible |
842 |
+ |
*/ |
843 |
|
public void testAwaitUntil_Interrupt() { |
844 |
|
final ReentrantLock lock = new ReentrantLock(); |
845 |
|
final Condition c = lock.newCondition(); |
850 |
|
java.util.Date d = new java.util.Date(); |
851 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
852 |
|
lock.unlock(); |
853 |
< |
threadFail("should throw"); |
853 |
> |
threadShouldThrow(); |
854 |
|
} |
855 |
|
catch(InterruptedException success) { |
856 |
|
} |
865 |
|
assertFalse(t.isAlive()); |
866 |
|
} |
867 |
|
catch (Exception ex) { |
868 |
< |
fail("unexpected exception"); |
868 |
> |
unexpectedException(); |
869 |
|
} |
870 |
|
} |
871 |
|
|
872 |
+ |
/** |
873 |
+ |
* signalAll wakes up all threads |
874 |
+ |
*/ |
875 |
|
public void testSignalAll() { |
876 |
|
final ReentrantLock lock = new ReentrantLock(); |
877 |
|
final Condition c = lock.newCondition(); |
883 |
|
lock.unlock(); |
884 |
|
} |
885 |
|
catch(InterruptedException e) { |
886 |
< |
threadFail("unexpected exception"); |
886 |
> |
threadUnexpectedException(); |
887 |
|
} |
888 |
|
} |
889 |
|
}); |
896 |
|
lock.unlock(); |
897 |
|
} |
898 |
|
catch(InterruptedException e) { |
899 |
< |
threadFail("unexpected exception"); |
899 |
> |
threadUnexpectedException(); |
900 |
|
} |
901 |
|
} |
902 |
|
}); |
914 |
|
assertFalse(t2.isAlive()); |
915 |
|
} |
916 |
|
catch (Exception ex) { |
917 |
< |
fail("unexpected exception"); |
917 |
> |
unexpectedException(); |
918 |
|
} |
919 |
|
} |
920 |
|
|
921 |
+ |
/** |
922 |
+ |
* A serialized lock deserializes as unlocked |
923 |
+ |
*/ |
924 |
|
public void testSerialization() { |
925 |
|
ReentrantLock l = new ReentrantLock(); |
926 |
|
l.lock(); |
939 |
|
r.unlock(); |
940 |
|
} catch(Exception e){ |
941 |
|
e.printStackTrace(); |
942 |
< |
fail("unexpected exception"); |
942 |
> |
unexpectedException(); |
943 |
|
} |
944 |
|
} |
945 |
|
|