8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.locks.*; |
10 |
|
import java.util.concurrent.*; |
11 |
+ |
import java.util.*; |
12 |
|
import java.io.*; |
13 |
|
|
14 |
< |
public class ReentrantLockTest extends TestCase { |
14 |
< |
static int HOLD_COUNT_TEST_LIMIT = 20; |
15 |
< |
|
14 |
> |
public class ReentrantLockTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
19 |
– |
|
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(ReentrantLockTest.class); |
20 |
|
} |
21 |
|
|
22 |
< |
private static long SHORT_DELAY_MS = 100; |
23 |
< |
private static long MEDIUM_DELAY_MS = 1000; |
24 |
< |
private static long LONG_DELAY_MS = 10000; |
22 |
> |
static int HOLD_COUNT_TEST_LIMIT = 20; |
23 |
> |
|
24 |
> |
class InterruptibleLockRunnable implements Runnable { |
25 |
> |
final ReentrantLock lock; |
26 |
> |
InterruptibleLockRunnable(ReentrantLock l) { lock = l; } |
27 |
> |
public void run(){ |
28 |
> |
try{ |
29 |
> |
lock.lockInterruptibly(); |
30 |
> |
} catch(InterruptedException success){} |
31 |
> |
} |
32 |
> |
} |
33 |
> |
|
34 |
> |
// Same, except must interrupt |
35 |
> |
class InterruptedLockRunnable implements Runnable { |
36 |
> |
final ReentrantLock lock; |
37 |
> |
InterruptedLockRunnable(ReentrantLock l) { lock = l; } |
38 |
> |
public void run(){ |
39 |
> |
try{ |
40 |
> |
lock.lockInterruptibly(); |
41 |
> |
threadFail("should throw"); |
42 |
> |
} catch(InterruptedException success){} |
43 |
> |
} |
44 |
> |
} |
45 |
> |
|
46 |
> |
/** |
47 |
> |
* To expose protected methods |
48 |
> |
*/ |
49 |
> |
static class MyReentrantLock extends ReentrantLock { |
50 |
> |
MyReentrantLock() { super(); } |
51 |
> |
public Collection<Thread> getQueuedThreads() { |
52 |
> |
return super.getQueuedThreads(); |
53 |
> |
} |
54 |
> |
public ConditionObject newCondition() { |
55 |
> |
return new MyCondition(this); |
56 |
> |
} |
57 |
> |
|
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 |
|
/* |
68 |
< |
* Unlocks an unlocked lock, throws Illegal Monitor State |
30 |
< |
* |
68 |
> |
* Unlocking an unlocked lock throws IllegalMonitorStateException |
69 |
|
*/ |
32 |
– |
|
70 |
|
public void testIllegalMonitorStateException(){ |
71 |
|
ReentrantLock rl = new ReentrantLock(); |
72 |
|
try{ |
74 |
|
fail("Should of thown Illegal Monitor State Exception"); |
75 |
|
|
76 |
|
} catch(IllegalMonitorStateException success){} |
40 |
– |
|
41 |
– |
|
77 |
|
} |
78 |
< |
|
78 |
> |
|
79 |
|
/* |
80 |
< |
* makes a lock, locks it, tries to aquire the lock in another thread |
46 |
< |
* interrupts that thread and waits for an interrupted Exception to |
47 |
< |
* be thrown. |
80 |
> |
* lockInterruptibly is interruptible. |
81 |
|
*/ |
49 |
– |
|
82 |
|
public void testInterruptedException(){ |
83 |
|
final ReentrantLock lock = new ReentrantLock(); |
84 |
|
lock.lock(); |
85 |
< |
Thread t = new Thread(new Runnable() { |
86 |
< |
public void run(){ |
87 |
< |
try{ |
88 |
< |
lock.lockInterruptibly(); |
89 |
< |
fail("should throw"); |
90 |
< |
} catch(InterruptedException success){} |
91 |
< |
} |
92 |
< |
}); |
93 |
< |
t.start(); |
62 |
< |
t.interrupt(); |
63 |
< |
lock.unlock(); |
85 |
> |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
86 |
> |
try { |
87 |
> |
t.start(); |
88 |
> |
t.interrupt(); |
89 |
> |
lock.unlock(); |
90 |
> |
t.join(); |
91 |
> |
} catch(Exception e){ |
92 |
> |
fail("unexpected exception"); |
93 |
> |
} |
94 |
|
} |
95 |
|
|
96 |
|
/* |
97 |
< |
* tests for interrupted exception on a timed wait |
68 |
< |
* |
97 |
> |
* getLockQueueLength reports number of waiting threads |
98 |
|
*/ |
99 |
< |
|
99 |
> |
public void testgetLockQueueLength(){ |
100 |
> |
final ReentrantLock lock = new ReentrantLock(); |
101 |
> |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
102 |
> |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
103 |
> |
try { |
104 |
> |
assertEquals(0, lock.getLockQueueLength()); |
105 |
> |
lock.lock(); |
106 |
> |
t1.start(); |
107 |
> |
Thread.sleep(SHORT_DELAY_MS); |
108 |
> |
assertEquals(1, lock.getLockQueueLength()); |
109 |
> |
t2.start(); |
110 |
> |
Thread.sleep(SHORT_DELAY_MS); |
111 |
> |
assertEquals(2, lock.getLockQueueLength()); |
112 |
> |
t1.interrupt(); |
113 |
> |
Thread.sleep(SHORT_DELAY_MS); |
114 |
> |
assertEquals(1, lock.getLockQueueLength()); |
115 |
> |
lock.unlock(); |
116 |
> |
Thread.sleep(SHORT_DELAY_MS); |
117 |
> |
assertEquals(0, lock.getLockQueueLength()); |
118 |
> |
t1.join(); |
119 |
> |
t2.join(); |
120 |
> |
} catch(Exception e){ |
121 |
> |
fail("unexpected exception"); |
122 |
> |
} |
123 |
> |
} |
124 |
|
|
125 |
+ |
/* |
126 |
+ |
* getQueuedThreads includes waiting threads |
127 |
+ |
*/ |
128 |
+ |
public void testGetQueuedThreads(){ |
129 |
+ |
final MyReentrantLock lock = new MyReentrantLock(); |
130 |
+ |
Thread t1 = new Thread(new InterruptedLockRunnable(lock)); |
131 |
+ |
Thread t2 = new Thread(new InterruptibleLockRunnable(lock)); |
132 |
+ |
try { |
133 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
134 |
+ |
lock.lock(); |
135 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
136 |
+ |
t1.start(); |
137 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
138 |
+ |
assertTrue(lock.getQueuedThreads().contains(t1)); |
139 |
+ |
t2.start(); |
140 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
141 |
+ |
assertTrue(lock.getQueuedThreads().contains(t1)); |
142 |
+ |
assertTrue(lock.getQueuedThreads().contains(t2)); |
143 |
+ |
t1.interrupt(); |
144 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
145 |
+ |
assertFalse(lock.getQueuedThreads().contains(t1)); |
146 |
+ |
assertTrue(lock.getQueuedThreads().contains(t2)); |
147 |
+ |
lock.unlock(); |
148 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
149 |
+ |
assertTrue(lock.getQueuedThreads().isEmpty()); |
150 |
+ |
t1.join(); |
151 |
+ |
t2.join(); |
152 |
+ |
} catch(Exception e){ |
153 |
+ |
fail("unexpected exception"); |
154 |
+ |
} |
155 |
+ |
} |
156 |
+ |
|
157 |
+ |
|
158 |
+ |
/* |
159 |
+ |
* timed trylock is interruptible. |
160 |
+ |
*/ |
161 |
|
public void testInterruptedException2(){ |
162 |
|
final ReentrantLock lock = new ReentrantLock(); |
163 |
|
lock.lock(); |
164 |
|
Thread t = new Thread(new Runnable() { |
165 |
|
public void run(){ |
166 |
|
try{ |
167 |
< |
lock.tryLock(1000,TimeUnit.MILLISECONDS); |
168 |
< |
fail("should throw"); |
167 |
> |
lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS); |
168 |
> |
threadFail("should throw"); |
169 |
|
} catch(InterruptedException success){} |
170 |
|
} |
171 |
|
}); |
172 |
< |
t.start(); |
173 |
< |
t.interrupt(); |
172 |
> |
try { |
173 |
> |
t.start(); |
174 |
> |
t.interrupt(); |
175 |
> |
} catch(Exception e){ |
176 |
> |
fail("unexpected exception"); |
177 |
> |
} |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
+ |
/** |
182 |
+ |
* Trylock on a locked lock fails |
183 |
+ |
*/ |
184 |
|
public void testTryLockWhenLocked() { |
185 |
|
final ReentrantLock lock = new ReentrantLock(); |
186 |
|
lock.lock(); |
187 |
|
Thread t = new Thread(new Runnable() { |
188 |
|
public void run(){ |
189 |
< |
assertFalse(lock.tryLock()); |
189 |
> |
threadAssertFalse(lock.tryLock()); |
190 |
|
} |
191 |
|
}); |
192 |
|
try { |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
+ |
/** |
202 |
+ |
* Timed Trylock on a locked lock times out |
203 |
+ |
*/ |
204 |
|
public void testTryLock_Timeout(){ |
205 |
|
final ReentrantLock lock = new ReentrantLock(); |
206 |
|
lock.lock(); |
207 |
|
Thread t = new Thread(new Runnable() { |
208 |
|
public void run(){ |
209 |
|
try { |
210 |
< |
assertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
210 |
> |
threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS)); |
211 |
|
} catch (Exception ex) { |
212 |
< |
fail("unexpected exception"); |
212 |
> |
threadFail("unexpected exception"); |
213 |
|
} |
214 |
|
} |
215 |
|
}); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
+ |
/** |
226 |
+ |
* getHoldCount returns number of recursive holds |
227 |
+ |
*/ |
228 |
|
public void testGetHoldCount() { |
229 |
|
ReentrantLock lock = new ReentrantLock(); |
230 |
|
for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) { |
238 |
|
} |
239 |
|
|
240 |
|
|
241 |
< |
|
242 |
< |
|
241 |
> |
/** |
242 |
> |
* isLocked is true when locked and false when not |
243 |
> |
*/ |
244 |
|
public void testIsLocked() { |
245 |
|
final ReentrantLock lock = new ReentrantLock(); |
246 |
|
lock.lock(); |
251 |
|
public void run() { |
252 |
|
lock.lock(); |
253 |
|
try { |
254 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
254 |
> |
Thread.sleep(SMALL_DELAY_MS); |
255 |
|
} |
256 |
< |
catch(Exception e) {} |
256 |
> |
catch(Exception e) { |
257 |
> |
threadFail("unexpected exception"); |
258 |
> |
} |
259 |
|
lock.unlock(); |
260 |
|
} |
261 |
|
}); |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
+ |
/** |
275 |
+ |
* lockInterruptibly succeeds when unlocked, else is interruptible |
276 |
+ |
*/ |
277 |
|
public void testLockInterruptibly() { |
278 |
|
final ReentrantLock lock = new ReentrantLock(); |
279 |
|
try { |
281 |
|
} catch(Exception e) { |
282 |
|
fail("unexpected exception"); |
283 |
|
} |
284 |
< |
Thread t = new Thread(new Runnable() { |
177 |
< |
public void run() { |
178 |
< |
try { |
179 |
< |
lock.lockInterruptibly(); |
180 |
< |
fail("should throw"); |
181 |
< |
} |
182 |
< |
catch(InterruptedException e) {} |
183 |
< |
} |
184 |
< |
}); |
284 |
> |
Thread t = new Thread(new InterruptedLockRunnable(lock)); |
285 |
|
try { |
286 |
|
t.start(); |
287 |
|
t.interrupt(); |
340 |
|
final Condition c = lock.newCondition(); |
341 |
|
try { |
342 |
|
lock.lock(); |
343 |
< |
assertFalse(c.await(10, TimeUnit.MILLISECONDS)); |
343 |
> |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
344 |
|
lock.unlock(); |
345 |
|
} |
346 |
|
catch (Exception ex) { |
364 |
|
|
365 |
|
public void testAwait() { |
366 |
|
final ReentrantLock lock = new ReentrantLock(); |
367 |
< |
final Condition c = lock.newCondition(); |
367 |
> |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
368 |
> |
Thread t = new Thread(new Runnable() { |
369 |
> |
public void run() { |
370 |
> |
try { |
371 |
> |
lock.lock(); |
372 |
> |
c.await(); |
373 |
> |
lock.unlock(); |
374 |
> |
} |
375 |
> |
catch(InterruptedException e) { |
376 |
> |
threadFail("unexpected exception"); |
377 |
> |
} |
378 |
> |
} |
379 |
> |
}); |
380 |
> |
|
381 |
> |
try { |
382 |
> |
t.start(); |
383 |
> |
Thread.sleep(SHORT_DELAY_MS); |
384 |
> |
lock.lock(); |
385 |
> |
c.signal(); |
386 |
> |
lock.unlock(); |
387 |
> |
t.join(SHORT_DELAY_MS); |
388 |
> |
assertFalse(t.isAlive()); |
389 |
> |
} |
390 |
> |
catch (Exception ex) { |
391 |
> |
fail("unexpected exception"); |
392 |
> |
} |
393 |
> |
} |
394 |
> |
|
395 |
> |
public void testHasWaiters() { |
396 |
> |
final ReentrantLock lock = new ReentrantLock(); |
397 |
> |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
398 |
|
Thread t = new Thread(new Runnable() { |
399 |
|
public void run() { |
400 |
|
try { |
401 |
|
lock.lock(); |
402 |
+ |
threadAssertFalse(c.hasWaiters()); |
403 |
+ |
threadAssertEquals(0, c.getWaitQueueLength()); |
404 |
|
c.await(); |
405 |
|
lock.unlock(); |
406 |
|
} |
407 |
|
catch(InterruptedException e) { |
408 |
< |
fail("unexpected exception"); |
408 |
> |
threadFail("unexpected exception"); |
409 |
|
} |
410 |
|
} |
411 |
|
}); |
414 |
|
t.start(); |
415 |
|
Thread.sleep(SHORT_DELAY_MS); |
416 |
|
lock.lock(); |
417 |
+ |
assertTrue(c.hasWaiters()); |
418 |
+ |
assertEquals(1, c.getWaitQueueLength()); |
419 |
|
c.signal(); |
420 |
|
lock.unlock(); |
421 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
422 |
+ |
lock.lock(); |
423 |
+ |
assertFalse(c.hasWaiters()); |
424 |
+ |
assertEquals(0, c.getWaitQueueLength()); |
425 |
+ |
lock.unlock(); |
426 |
|
t.join(SHORT_DELAY_MS); |
427 |
|
assertFalse(t.isAlive()); |
428 |
|
} |
431 |
|
} |
432 |
|
} |
433 |
|
|
434 |
+ |
public void testGetWaitQueueLength() { |
435 |
+ |
final ReentrantLock lock = new ReentrantLock(); |
436 |
+ |
final ReentrantLock.ConditionObject c = lock.newCondition(); |
437 |
+ |
Thread t1 = new Thread(new Runnable() { |
438 |
+ |
public void run() { |
439 |
+ |
try { |
440 |
+ |
lock.lock(); |
441 |
+ |
threadAssertFalse(c.hasWaiters()); |
442 |
+ |
threadAssertEquals(0, c.getWaitQueueLength()); |
443 |
+ |
c.await(); |
444 |
+ |
lock.unlock(); |
445 |
+ |
} |
446 |
+ |
catch(InterruptedException e) { |
447 |
+ |
threadFail("unexpected exception"); |
448 |
+ |
} |
449 |
+ |
} |
450 |
+ |
}); |
451 |
+ |
|
452 |
+ |
Thread t2 = new Thread(new Runnable() { |
453 |
+ |
public void run() { |
454 |
+ |
try { |
455 |
+ |
lock.lock(); |
456 |
+ |
threadAssertTrue(c.hasWaiters()); |
457 |
+ |
threadAssertEquals(1, c.getWaitQueueLength()); |
458 |
+ |
c.await(); |
459 |
+ |
lock.unlock(); |
460 |
+ |
} |
461 |
+ |
catch(InterruptedException e) { |
462 |
+ |
threadFail("unexpected exception"); |
463 |
+ |
} |
464 |
+ |
} |
465 |
+ |
}); |
466 |
+ |
|
467 |
+ |
try { |
468 |
+ |
t1.start(); |
469 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
470 |
+ |
t2.start(); |
471 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
472 |
+ |
lock.lock(); |
473 |
+ |
assertTrue(c.hasWaiters()); |
474 |
+ |
assertEquals(2, c.getWaitQueueLength()); |
475 |
+ |
c.signalAll(); |
476 |
+ |
lock.unlock(); |
477 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
478 |
+ |
lock.lock(); |
479 |
+ |
assertFalse(c.hasWaiters()); |
480 |
+ |
assertEquals(0, c.getWaitQueueLength()); |
481 |
+ |
lock.unlock(); |
482 |
+ |
t1.join(SHORT_DELAY_MS); |
483 |
+ |
t2.join(SHORT_DELAY_MS); |
484 |
+ |
assertFalse(t1.isAlive()); |
485 |
+ |
assertFalse(t2.isAlive()); |
486 |
+ |
} |
487 |
+ |
catch (Exception ex) { |
488 |
+ |
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 |
+ |
} |
550 |
+ |
} |
551 |
+ |
|
552 |
|
public void testAwaitUninterruptibly() { |
553 |
|
final ReentrantLock lock = new ReentrantLock(); |
554 |
|
final Condition c = lock.newCondition(); |
584 |
|
lock.lock(); |
585 |
|
c.await(); |
586 |
|
lock.unlock(); |
587 |
< |
fail("should throw"); |
587 |
> |
threadFail("should throw"); |
588 |
|
} |
589 |
|
catch(InterruptedException success) { |
590 |
|
} |
612 |
|
lock.lock(); |
613 |
|
c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000); |
614 |
|
lock.unlock(); |
615 |
< |
fail("should throw"); |
615 |
> |
threadFail("should throw"); |
616 |
|
} |
617 |
|
catch(InterruptedException success) { |
618 |
|
} |
641 |
|
java.util.Date d = new java.util.Date(); |
642 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
643 |
|
lock.unlock(); |
644 |
< |
fail("should throw"); |
644 |
> |
threadFail("should throw"); |
645 |
|
} |
646 |
|
catch(InterruptedException success) { |
647 |
|
} |
671 |
|
lock.unlock(); |
672 |
|
} |
673 |
|
catch(InterruptedException e) { |
674 |
< |
fail("unexpected exception"); |
674 |
> |
threadFail("unexpected exception"); |
675 |
|
} |
676 |
|
} |
677 |
|
}); |
684 |
|
lock.unlock(); |
685 |
|
} |
686 |
|
catch(InterruptedException e) { |
687 |
< |
fail("unexpected exception"); |
687 |
> |
threadFail("unexpected exception"); |
688 |
|
} |
689 |
|
} |
690 |
|
}); |