59 |
|
} |
60 |
|
|
61 |
|
/** |
62 |
+ |
* Checks that condition c has no waiters. |
63 |
+ |
*/ |
64 |
+ |
void assertHasNoWaiters(PublicReentrantLock lock, Condition c) { |
65 |
+ |
assertHasWaiters(lock, c, new Thread[] {}); |
66 |
+ |
} |
67 |
+ |
|
68 |
+ |
/** |
69 |
+ |
* Checks that condition c has exactly the given waiter threads. |
70 |
+ |
*/ |
71 |
+ |
void assertHasWaiters(PublicReentrantLock lock, Condition c, |
72 |
+ |
Thread... threads) { |
73 |
+ |
lock.lock(); |
74 |
+ |
assertEquals(threads.length > 0, lock.hasWaiters(c)); |
75 |
+ |
assertEquals(threads.length, lock.getWaitQueueLength(c)); |
76 |
+ |
assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty()); |
77 |
+ |
assertEquals(threads.length, lock.getWaitingThreads(c).size()); |
78 |
+ |
assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)), |
79 |
+ |
new HashSet<Thread>(Arrays.asList(threads))); |
80 |
+ |
lock.unlock(); |
81 |
+ |
} |
82 |
+ |
|
83 |
+ |
/** |
84 |
|
* Constructor sets given fairness |
85 |
|
*/ |
86 |
|
public void testConstructor() { |
757 |
|
* await is interruptible |
758 |
|
*/ |
759 |
|
public void testAwait_Interrupt() throws InterruptedException { |
760 |
< |
final ReentrantLock lock = new ReentrantLock(); |
760 |
> |
final PublicReentrantLock lock = new PublicReentrantLock(); |
761 |
|
final Condition c = lock.newCondition(); |
762 |
+ |
final CountDownLatch locked = new CountDownLatch(1); |
763 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
764 |
|
public void realRun() throws InterruptedException { |
765 |
|
lock.lock(); |
766 |
< |
c.await(); |
766 |
> |
assertTrue(lock.isLocked()); |
767 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
768 |
> |
assertHasNoWaiters(lock, c); |
769 |
> |
locked.countDown(); |
770 |
> |
try { |
771 |
> |
c.await(); |
772 |
> |
} finally { |
773 |
> |
assertTrue(lock.isLocked()); |
774 |
> |
assertTrue(lock.isHeldByCurrentThread()); |
775 |
> |
assertHasNoWaiters(lock, c); |
776 |
> |
lock.unlock(); |
777 |
> |
assertFalse(Thread.interrupted()); |
778 |
> |
} |
779 |
|
}}); |
780 |
|
|
781 |
< |
delay(SHORT_DELAY_MS); |
781 |
> |
locked.await(); |
782 |
> |
assertHasWaiters(lock, c, t); |
783 |
|
t.interrupt(); |
784 |
|
awaitTermination(t); |
785 |
+ |
assertFalse(lock.isLocked()); |
786 |
|
} |
787 |
|
|
788 |
|
/** |