ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.53
Committed: Wed Dec 31 05:04:04 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.52: +1 -1 lines
Log Message:
delete stray semicolons

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.12 * 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 jsr166 1.39 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.26 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10 jsr166 1.50 import java.util.concurrent.locks.Condition;
11     import java.util.concurrent.locks.ReentrantLock;
12     import java.util.concurrent.CountDownLatch;
13     import java.util.concurrent.CyclicBarrier;
14 jsr166 1.31 import static java.util.concurrent.TimeUnit.MILLISECONDS;
15 dl 1.5 import java.util.*;
16 dl 1.1
17 dl 1.4 public class ReentrantLockTest extends JSR166TestCase {
18 dl 1.1 public static void main(String[] args) {
19 jsr166 1.36 junit.textui.TestRunner.run(suite());
20 dl 1.1 }
21     public static Test suite() {
22 jsr166 1.30 return new TestSuite(ReentrantLockTest.class);
23 dl 1.1 }
24    
25 dl 1.6 /**
26     * A runnable calling lockInterruptibly
27     */
28 jsr166 1.29 class InterruptibleLockRunnable extends CheckedRunnable {
29 dl 1.5 final ReentrantLock lock;
30     InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
31 jsr166 1.29 public void realRun() throws InterruptedException {
32     lock.lockInterruptibly();
33 dl 1.5 }
34     }
35    
36 dl 1.6 /**
37     * A runnable calling lockInterruptibly that expects to be
38     * interrupted
39     */
40 jsr166 1.29 class InterruptedLockRunnable extends CheckedInterruptedRunnable {
41 dl 1.5 final ReentrantLock lock;
42     InterruptedLockRunnable(ReentrantLock l) { lock = l; }
43 jsr166 1.29 public void realRun() throws InterruptedException {
44     lock.lockInterruptibly();
45 dl 1.5 }
46     }
47    
48     /**
49 dl 1.6 * Subclass to expose protected methods
50 dl 1.5 */
51 dl 1.6 static class PublicReentrantLock extends ReentrantLock {
52     PublicReentrantLock() { super(); }
53 jsr166 1.45 PublicReentrantLock(boolean fair) { super(fair); }
54     public Thread getOwner() {
55     return super.getOwner();
56     }
57 jsr166 1.26 public Collection<Thread> getQueuedThreads() {
58     return super.getQueuedThreads();
59 dl 1.5 }
60 jsr166 1.26 public Collection<Thread> getWaitingThreads(Condition c) {
61     return super.getWaitingThreads(c);
62 dl 1.13 }
63 dl 1.5 }
64    
65 dl 1.8 /**
66 jsr166 1.45 * Releases write lock, checking that it had a hold count of 1.
67     */
68     void releaseLock(PublicReentrantLock lock) {
69 jsr166 1.46 assertLockedByMoi(lock);
70 jsr166 1.45 lock.unlock();
71     assertFalse(lock.isHeldByCurrentThread());
72     assertNotLocked(lock);
73     }
74    
75     /**
76     * Spin-waits until lock.hasQueuedThread(t) becomes true.
77     */
78     void waitForQueuedThread(PublicReentrantLock lock, Thread t) {
79     long startTime = System.nanoTime();
80     while (!lock.hasQueuedThread(t)) {
81     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
82 jsr166 1.47 throw new AssertionFailedError("timed out");
83 jsr166 1.45 Thread.yield();
84     }
85     assertTrue(t.isAlive());
86 jsr166 1.52 assertNotSame(t, lock.getOwner());
87 jsr166 1.45 }
88    
89     /**
90     * Checks that lock is not locked.
91     */
92     void assertNotLocked(PublicReentrantLock lock) {
93     assertFalse(lock.isLocked());
94     assertFalse(lock.isHeldByCurrentThread());
95     assertNull(lock.getOwner());
96     assertEquals(0, lock.getHoldCount());
97     }
98    
99     /**
100     * Checks that lock is locked by the given thread.
101     */
102     void assertLockedBy(PublicReentrantLock lock, Thread t) {
103     assertTrue(lock.isLocked());
104     assertSame(t, lock.getOwner());
105     assertEquals(t == Thread.currentThread(),
106     lock.isHeldByCurrentThread());
107     assertEquals(t == Thread.currentThread(),
108     lock.getHoldCount() > 0);
109     }
110    
111     /**
112 jsr166 1.46 * Checks that lock is locked by the current thread.
113     */
114     void assertLockedByMoi(PublicReentrantLock lock) {
115     assertLockedBy(lock, Thread.currentThread());
116     }
117    
118     /**
119 jsr166 1.43 * Checks that condition c has no waiters.
120     */
121     void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
122     assertHasWaiters(lock, c, new Thread[] {});
123     }
124    
125     /**
126     * Checks that condition c has exactly the given waiter threads.
127     */
128     void assertHasWaiters(PublicReentrantLock lock, Condition c,
129     Thread... threads) {
130     lock.lock();
131     assertEquals(threads.length > 0, lock.hasWaiters(c));
132     assertEquals(threads.length, lock.getWaitQueueLength(c));
133     assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
134     assertEquals(threads.length, lock.getWaitingThreads(c).size());
135     assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
136     new HashSet<Thread>(Arrays.asList(threads)));
137     lock.unlock();
138     }
139    
140 jsr166 1.53 enum AwaitMethod { await, awaitTimed, awaitNanos, awaitUntil }
141 jsr166 1.46
142     /**
143     * Awaits condition using the specified AwaitMethod.
144     */
145     void await(Condition c, AwaitMethod awaitMethod)
146     throws InterruptedException {
147 jsr166 1.48 long timeoutMillis = 2 * LONG_DELAY_MS;
148 jsr166 1.46 switch (awaitMethod) {
149     case await:
150     c.await();
151     break;
152 jsr166 1.48 case awaitTimed:
153     assertTrue(c.await(timeoutMillis, MILLISECONDS));
154     break;
155 jsr166 1.46 case awaitNanos:
156 jsr166 1.48 long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
157     long nanosRemaining = c.awaitNanos(nanosTimeout);
158 jsr166 1.46 assertTrue(nanosRemaining > 0);
159     break;
160     case awaitUntil:
161 jsr166 1.48 assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
162 jsr166 1.46 break;
163     }
164     }
165    
166 jsr166 1.43 /**
167 jsr166 1.45 * Constructor sets given fairness, and is in unlocked state
168 dl 1.9 */
169 jsr166 1.26 public void testConstructor() {
170 jsr166 1.45 PublicReentrantLock lock;
171    
172     lock = new PublicReentrantLock();
173     assertFalse(lock.isFair());
174     assertNotLocked(lock);
175    
176     lock = new PublicReentrantLock(true);
177     assertTrue(lock.isFair());
178     assertNotLocked(lock);
179    
180     lock = new PublicReentrantLock(false);
181     assertFalse(lock.isFair());
182     assertNotLocked(lock);
183 dl 1.9 }
184    
185     /**
186 dl 1.6 * locking an unlocked lock succeeds
187     */
188 jsr166 1.46 public void testLock() { testLock(false); }
189     public void testLock_fair() { testLock(true); }
190     public void testLock(boolean fair) {
191     PublicReentrantLock lock = new PublicReentrantLock(fair);
192 jsr166 1.45 lock.lock();
193 jsr166 1.46 assertLockedByMoi(lock);
194 jsr166 1.45 releaseLock(lock);
195 dl 1.6 }
196    
197 dl 1.8 /**
198 dl 1.5 * Unlocking an unlocked lock throws IllegalMonitorStateException
199 dl 1.1 */
200 jsr166 1.46 public void testUnlock_IMSE() { testUnlock_IMSE(false); }
201     public void testUnlock_IMSE_fair() { testUnlock_IMSE(true); }
202     public void testUnlock_IMSE(boolean fair) {
203     ReentrantLock lock = new ReentrantLock(fair);
204 jsr166 1.30 try {
205 jsr166 1.45 lock.unlock();
206 jsr166 1.30 shouldThrow();
207     } catch (IllegalMonitorStateException success) {}
208 dl 1.5 }
209 dl 1.1
210 dl 1.8 /**
211 dl 1.15 * tryLock on an unlocked lock succeeds
212 dl 1.1 */
213 jsr166 1.46 public void testTryLock() { testTryLock(false); }
214     public void testTryLock_fair() { testTryLock(true); }
215     public void testTryLock(boolean fair) {
216     PublicReentrantLock lock = new PublicReentrantLock(fair);
217     assertTrue(lock.tryLock());
218     assertLockedByMoi(lock);
219 jsr166 1.45 assertTrue(lock.tryLock());
220 jsr166 1.46 assertLockedByMoi(lock);
221     lock.unlock();
222 jsr166 1.45 releaseLock(lock);
223 dl 1.6 }
224    
225 dl 1.8 /**
226 dl 1.13 * hasQueuedThreads reports whether there are waiting threads
227     */
228 jsr166 1.46 public void testHasQueuedThreads() { testHasQueuedThreads(false); }
229     public void testHasQueuedThreads_fair() { testHasQueuedThreads(true); }
230     public void testHasQueuedThreads(boolean fair) {
231     final PublicReentrantLock lock = new PublicReentrantLock(fair);
232 dl 1.13 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
233     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
234 jsr166 1.29 assertFalse(lock.hasQueuedThreads());
235     lock.lock();
236 jsr166 1.45 assertFalse(lock.hasQueuedThreads());
237 jsr166 1.29 t1.start();
238 jsr166 1.45 waitForQueuedThread(lock, t1);
239 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
240     t2.start();
241 jsr166 1.45 waitForQueuedThread(lock, t2);
242 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
243     t1.interrupt();
244 jsr166 1.45 awaitTermination(t1);
245 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
246     lock.unlock();
247 jsr166 1.45 awaitTermination(t2);
248 jsr166 1.29 assertFalse(lock.hasQueuedThreads());
249 jsr166 1.26 }
250 dl 1.13
251     /**
252 dl 1.7 * getQueueLength reports number of waiting threads
253 dl 1.1 */
254 jsr166 1.46 public void testGetQueueLength() { testGetQueueLength(false); }
255     public void testGetQueueLength_fair() { testGetQueueLength(true); }
256     public void testGetQueueLength(boolean fair) {
257     final PublicReentrantLock lock = new PublicReentrantLock(fair);
258 dl 1.16 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
259     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
260 jsr166 1.29 assertEquals(0, lock.getQueueLength());
261     lock.lock();
262     t1.start();
263 jsr166 1.45 waitForQueuedThread(lock, t1);
264 jsr166 1.29 assertEquals(1, lock.getQueueLength());
265     t2.start();
266 jsr166 1.45 waitForQueuedThread(lock, t2);
267 jsr166 1.29 assertEquals(2, lock.getQueueLength());
268     t1.interrupt();
269 jsr166 1.45 awaitTermination(t1);
270 jsr166 1.29 assertEquals(1, lock.getQueueLength());
271     lock.unlock();
272 jsr166 1.45 awaitTermination(t2);
273 jsr166 1.29 assertEquals(0, lock.getQueueLength());
274 jsr166 1.26 }
275 dl 1.16
276     /**
277 dl 1.19 * hasQueuedThread(null) throws NPE
278     */
279 jsr166 1.46 public void testHasQueuedThreadNPE() { testHasQueuedThreadNPE(false); }
280     public void testHasQueuedThreadNPE_fair() { testHasQueuedThreadNPE(true); }
281     public void testHasQueuedThreadNPE(boolean fair) {
282     final ReentrantLock lock = new ReentrantLock(fair);
283 dl 1.19 try {
284 jsr166 1.45 lock.hasQueuedThread(null);
285 dl 1.19 shouldThrow();
286 jsr166 1.29 } catch (NullPointerException success) {}
287 dl 1.19 }
288    
289     /**
290 jsr166 1.49 * hasQueuedThread reports whether a thread is queued
291 dl 1.19 */
292 jsr166 1.46 public void testHasQueuedThread() { testHasQueuedThread(false); }
293     public void testHasQueuedThread_fair() { testHasQueuedThread(true); }
294     public void testHasQueuedThread(boolean fair) {
295     final PublicReentrantLock lock = new PublicReentrantLock(fair);
296 jsr166 1.45 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
297     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
298     assertFalse(lock.hasQueuedThread(t1));
299     assertFalse(lock.hasQueuedThread(t2));
300     lock.lock();
301 jsr166 1.29 t1.start();
302 jsr166 1.45 waitForQueuedThread(lock, t1);
303     assertTrue(lock.hasQueuedThread(t1));
304     assertFalse(lock.hasQueuedThread(t2));
305 jsr166 1.29 t2.start();
306 jsr166 1.45 waitForQueuedThread(lock, t2);
307     assertTrue(lock.hasQueuedThread(t1));
308     assertTrue(lock.hasQueuedThread(t2));
309 jsr166 1.29 t1.interrupt();
310 jsr166 1.42 awaitTermination(t1);
311 jsr166 1.45 assertFalse(lock.hasQueuedThread(t1));
312     assertTrue(lock.hasQueuedThread(t2));
313     lock.unlock();
314 jsr166 1.42 awaitTermination(t2);
315 jsr166 1.45 assertFalse(lock.hasQueuedThread(t1));
316     assertFalse(lock.hasQueuedThread(t2));
317 jsr166 1.26 }
318 dl 1.19
319     /**
320 dl 1.5 * getQueuedThreads includes waiting threads
321     */
322 jsr166 1.46 public void testGetQueuedThreads() { testGetQueuedThreads(false); }
323     public void testGetQueuedThreads_fair() { testGetQueuedThreads(true); }
324     public void testGetQueuedThreads(boolean fair) {
325     final PublicReentrantLock lock = new PublicReentrantLock(fair);
326 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
327     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
328 jsr166 1.29 assertTrue(lock.getQueuedThreads().isEmpty());
329     lock.lock();
330     assertTrue(lock.getQueuedThreads().isEmpty());
331     t1.start();
332 jsr166 1.45 waitForQueuedThread(lock, t1);
333     assertEquals(1, lock.getQueuedThreads().size());
334 jsr166 1.29 assertTrue(lock.getQueuedThreads().contains(t1));
335     t2.start();
336 jsr166 1.45 waitForQueuedThread(lock, t2);
337     assertEquals(2, lock.getQueuedThreads().size());
338 jsr166 1.29 assertTrue(lock.getQueuedThreads().contains(t1));
339     assertTrue(lock.getQueuedThreads().contains(t2));
340     t1.interrupt();
341 jsr166 1.45 awaitTermination(t1);
342 jsr166 1.29 assertFalse(lock.getQueuedThreads().contains(t1));
343     assertTrue(lock.getQueuedThreads().contains(t2));
344 jsr166 1.45 assertEquals(1, lock.getQueuedThreads().size());
345 jsr166 1.29 lock.unlock();
346 jsr166 1.45 awaitTermination(t2);
347 jsr166 1.29 assertTrue(lock.getQueuedThreads().isEmpty());
348 jsr166 1.26 }
349 dl 1.5
350 dl 1.8 /**
351 jsr166 1.49 * timed tryLock is interruptible
352 dl 1.5 */
353 jsr166 1.46 public void testTryLock_Interruptible() { testTryLock_Interruptible(false); }
354     public void testTryLock_Interruptible_fair() { testTryLock_Interruptible(true); }
355     public void testTryLock_Interruptible(boolean fair) {
356     final PublicReentrantLock lock = new PublicReentrantLock(fair);
357 jsr166 1.30 lock.lock();
358 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
359 jsr166 1.29 public void realRun() throws InterruptedException {
360 jsr166 1.45 lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
361 jsr166 1.29 }});
362    
363 jsr166 1.45 waitForQueuedThread(lock, t);
364 jsr166 1.29 t.interrupt();
365 jsr166 1.42 awaitTermination(t);
366 jsr166 1.45 releaseLock(lock);
367 dl 1.1 }
368    
369 dl 1.5 /**
370 jsr166 1.45 * tryLock on a locked lock fails
371 dl 1.5 */
372 jsr166 1.46 public void testTryLockWhenLocked() { testTryLockWhenLocked(false); }
373     public void testTryLockWhenLocked_fair() { testTryLockWhenLocked(true); }
374     public void testTryLockWhenLocked(boolean fair) {
375     final PublicReentrantLock lock = new PublicReentrantLock(fair);
376 jsr166 1.30 lock.lock();
377 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
378 jsr166 1.29 public void realRun() {
379 jsr166 1.37 assertFalse(lock.tryLock());
380 jsr166 1.29 }});
381    
382 jsr166 1.42 awaitTermination(t);
383 jsr166 1.45 releaseLock(lock);
384 jsr166 1.26 }
385 dl 1.3
386 dl 1.5 /**
387 dl 1.15 * Timed tryLock on a locked lock times out
388 dl 1.5 */
389 jsr166 1.46 public void testTryLock_Timeout() { testTryLock_Timeout(false); }
390     public void testTryLock_Timeout_fair() { testTryLock_Timeout(true); }
391     public void testTryLock_Timeout(boolean fair) {
392     final PublicReentrantLock lock = new PublicReentrantLock(fair);
393 jsr166 1.30 lock.lock();
394 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
395 jsr166 1.29 public void realRun() throws InterruptedException {
396 jsr166 1.45 long startTime = System.nanoTime();
397     long timeoutMillis = 10;
398     assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
399     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
400 jsr166 1.29 }});
401    
402 jsr166 1.42 awaitTermination(t);
403 jsr166 1.45 releaseLock(lock);
404 jsr166 1.26 }
405    
406 dl 1.5 /**
407     * getHoldCount returns number of recursive holds
408     */
409 jsr166 1.46 public void testGetHoldCount() { testGetHoldCount(false); }
410     public void testGetHoldCount_fair() { testGetHoldCount(true); }
411     public void testGetHoldCount(boolean fair) {
412     ReentrantLock lock = new ReentrantLock(fair);
413 jsr166 1.30 for (int i = 1; i <= SIZE; i++) {
414     lock.lock();
415     assertEquals(i, lock.getHoldCount());
416     }
417     for (int i = SIZE; i > 0; i--) {
418     lock.unlock();
419     assertEquals(i-1, lock.getHoldCount());
420     }
421 dl 1.1 }
422 jsr166 1.26
423 dl 1.5 /**
424     * isLocked is true when locked and false when not
425     */
426 jsr166 1.46 public void testIsLocked() { testIsLocked(false); }
427     public void testIsLocked_fair() { testIsLocked(true); }
428     public void testIsLocked(boolean fair) {
429     try {
430     final ReentrantLock lock = new ReentrantLock(fair);
431     assertFalse(lock.isLocked());
432     lock.lock();
433     assertTrue(lock.isLocked());
434     lock.lock();
435     assertTrue(lock.isLocked());
436     lock.unlock();
437     assertTrue(lock.isLocked());
438     lock.unlock();
439     assertFalse(lock.isLocked());
440     final CyclicBarrier barrier = new CyclicBarrier(2);
441     Thread t = newStartedThread(new CheckedRunnable() {
442     public void realRun() throws Exception {
443     lock.lock();
444     assertTrue(lock.isLocked());
445     barrier.await();
446     barrier.await();
447     lock.unlock();
448     }});
449    
450     barrier.await();
451     assertTrue(lock.isLocked());
452     barrier.await();
453     awaitTermination(t);
454     assertFalse(lock.isLocked());
455     } catch (Exception e) {
456     threadUnexpectedException(e);
457     }
458 jsr166 1.26 }
459 dl 1.6
460 dl 1.5 /**
461     * lockInterruptibly succeeds when unlocked, else is interruptible
462     */
463 jsr166 1.46 public void testLockInterruptibly() { testLockInterruptibly(false); }
464     public void testLockInterruptibly_fair() { testLockInterruptibly(true); }
465     public void testLockInterruptibly(boolean fair) {
466     final PublicReentrantLock lock = new PublicReentrantLock(fair);
467     try {
468     lock.lockInterruptibly();
469     } catch (InterruptedException ie) {
470     threadUnexpectedException(ie);
471     }
472     assertLockedByMoi(lock);
473 jsr166 1.41 Thread t = newStartedThread(new InterruptedLockRunnable(lock));
474 jsr166 1.45 waitForQueuedThread(lock, t);
475 jsr166 1.29 t.interrupt();
476     assertTrue(lock.isLocked());
477     assertTrue(lock.isHeldByCurrentThread());
478 jsr166 1.42 awaitTermination(t);
479 jsr166 1.45 releaseLock(lock);
480 dl 1.1 }
481 dl 1.2
482 dl 1.6 /**
483     * Calling await without holding lock throws IllegalMonitorStateException
484     */
485 jsr166 1.46 public void testAwait_IMSE() { testAwait_IMSE(false); }
486     public void testAwait_IMSE_fair() { testAwait_IMSE(true); }
487     public void testAwait_IMSE(boolean fair) {
488     final ReentrantLock lock = new ReentrantLock(fair);
489 dl 1.2 final Condition c = lock.newCondition();
490 jsr166 1.48 for (AwaitMethod awaitMethod : AwaitMethod.values()) {
491     long startTime = System.nanoTime();
492 jsr166 1.46 try {
493 jsr166 1.48 await(c, awaitMethod);
494 jsr166 1.46 shouldThrow();
495 jsr166 1.48 } catch (IllegalMonitorStateException success) {
496     } catch (InterruptedException e) { threadUnexpectedException(e); }
497     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
498 jsr166 1.46 }
499 dl 1.2 }
500    
501 dl 1.6 /**
502     * Calling signal without holding lock throws IllegalMonitorStateException
503     */
504 jsr166 1.46 public void testSignal_IMSE() { testSignal_IMSE(false); }
505     public void testSignal_IMSE_fair() { testSignal_IMSE(true); }
506     public void testSignal_IMSE(boolean fair) {
507     final ReentrantLock lock = new ReentrantLock(fair);
508 dl 1.2 final Condition c = lock.newCondition();
509     try {
510     c.signal();
511 dl 1.6 shouldThrow();
512 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
513 dl 1.2 }
514    
515 dl 1.6 /**
516     * awaitNanos without a signal times out
517     */
518 jsr166 1.46 public void testAwaitNanos_Timeout() { testAwaitNanos_Timeout(false); }
519     public void testAwaitNanos_Timeout_fair() { testAwaitNanos_Timeout(true); }
520     public void testAwaitNanos_Timeout(boolean fair) {
521     try {
522     final ReentrantLock lock = new ReentrantLock(fair);
523     final Condition c = lock.newCondition();
524     lock.lock();
525     long startTime = System.nanoTime();
526     long timeoutMillis = 10;
527     long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
528     long nanosRemaining = c.awaitNanos(timeoutNanos);
529     assertTrue(nanosRemaining <= 0);
530     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
531     lock.unlock();
532     } catch (InterruptedException e) {
533     threadUnexpectedException(e);
534     }
535 dl 1.2 }
536    
537 dl 1.6 /**
538 jsr166 1.38 * timed await without a signal times out
539 dl 1.6 */
540 jsr166 1.46 public void testAwait_Timeout() { testAwait_Timeout(false); }
541     public void testAwait_Timeout_fair() { testAwait_Timeout(true); }
542     public void testAwait_Timeout(boolean fair) {
543     try {
544     final ReentrantLock lock = new ReentrantLock(fair);
545     final Condition c = lock.newCondition();
546     lock.lock();
547     long startTime = System.nanoTime();
548     long timeoutMillis = 10;
549     assertFalse(c.await(timeoutMillis, MILLISECONDS));
550     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
551     lock.unlock();
552     } catch (InterruptedException e) {
553     threadUnexpectedException(e);
554     }
555 dl 1.2 }
556    
557 dl 1.6 /**
558     * awaitUntil without a signal times out
559     */
560 jsr166 1.46 public void testAwaitUntil_Timeout() { testAwaitUntil_Timeout(false); }
561     public void testAwaitUntil_Timeout_fair() { testAwaitUntil_Timeout(true); }
562     public void testAwaitUntil_Timeout(boolean fair) {
563     try {
564     final ReentrantLock lock = new ReentrantLock(fair);
565     final Condition c = lock.newCondition();
566     lock.lock();
567     long startTime = System.nanoTime();
568     long timeoutMillis = 10;
569     java.util.Date d = new java.util.Date();
570     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
571     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
572     lock.unlock();
573     } catch (InterruptedException e) {
574     threadUnexpectedException(e);
575     }
576 dl 1.2 }
577    
578 dl 1.6 /**
579     * await returns when signalled
580     */
581 jsr166 1.46 public void testAwait() { testAwait(false); }
582     public void testAwait_fair() { testAwait(true); }
583     public void testAwait(boolean fair) {
584     final PublicReentrantLock lock = new PublicReentrantLock(fair);
585 dl 1.13 final Condition c = lock.newCondition();
586 jsr166 1.45 final CountDownLatch locked = new CountDownLatch(1);
587 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
588 jsr166 1.29 public void realRun() throws InterruptedException {
589     lock.lock();
590 jsr166 1.45 locked.countDown();
591 jsr166 1.29 c.await();
592     lock.unlock();
593     }});
594 dl 1.5
595 jsr166 1.46 await(locked);
596 jsr166 1.29 lock.lock();
597 jsr166 1.45 assertHasWaiters(lock, c, t);
598 jsr166 1.29 c.signal();
599 jsr166 1.45 assertHasNoWaiters(lock, c);
600     assertTrue(t.isAlive());
601 jsr166 1.29 lock.unlock();
602 jsr166 1.42 awaitTermination(t);
603 dl 1.5 }
604    
605 dl 1.6 /**
606 dl 1.14 * hasWaiters throws NPE if null
607     */
608 jsr166 1.46 public void testHasWaitersNPE() { testHasWaitersNPE(false); }
609     public void testHasWaitersNPE_fair() { testHasWaitersNPE(true); }
610     public void testHasWaitersNPE(boolean fair) {
611     final ReentrantLock lock = new ReentrantLock(fair);
612 dl 1.14 try {
613     lock.hasWaiters(null);
614     shouldThrow();
615 jsr166 1.29 } catch (NullPointerException success) {}
616 dl 1.14 }
617    
618     /**
619     * getWaitQueueLength throws NPE if null
620     */
621 jsr166 1.46 public void testGetWaitQueueLengthNPE() { testGetWaitQueueLengthNPE(false); }
622     public void testGetWaitQueueLengthNPE_fair() { testGetWaitQueueLengthNPE(true); }
623     public void testGetWaitQueueLengthNPE(boolean fair) {
624     final ReentrantLock lock = new ReentrantLock(fair);
625 dl 1.14 try {
626     lock.getWaitQueueLength(null);
627     shouldThrow();
628 jsr166 1.29 } catch (NullPointerException success) {}
629 dl 1.14 }
630    
631     /**
632     * getWaitingThreads throws NPE if null
633     */
634 jsr166 1.46 public void testGetWaitingThreadsNPE() { testGetWaitingThreadsNPE(false); }
635     public void testGetWaitingThreadsNPE_fair() { testGetWaitingThreadsNPE(true); }
636     public void testGetWaitingThreadsNPE(boolean fair) {
637     final PublicReentrantLock lock = new PublicReentrantLock(fair);
638 dl 1.14 try {
639     lock.getWaitingThreads(null);
640     shouldThrow();
641 jsr166 1.29 } catch (NullPointerException success) {}
642 dl 1.14 }
643    
644     /**
645 jsr166 1.45 * hasWaiters throws IllegalArgumentException if not owned
646 dl 1.13 */
647 jsr166 1.46 public void testHasWaitersIAE() { testHasWaitersIAE(false); }
648     public void testHasWaitersIAE_fair() { testHasWaitersIAE(true); }
649     public void testHasWaitersIAE(boolean fair) {
650     final ReentrantLock lock = new ReentrantLock(fair);
651 jsr166 1.29 final Condition c = lock.newCondition();
652 jsr166 1.46 final ReentrantLock lock2 = new ReentrantLock(fair);
653 dl 1.13 try {
654     lock2.hasWaiters(c);
655     shouldThrow();
656 jsr166 1.29 } catch (IllegalArgumentException success) {}
657 dl 1.13 }
658    
659     /**
660 jsr166 1.45 * hasWaiters throws IllegalMonitorStateException if not locked
661 dl 1.13 */
662 jsr166 1.46 public void testHasWaitersIMSE() { testHasWaitersIMSE(false); }
663     public void testHasWaitersIMSE_fair() { testHasWaitersIMSE(true); }
664     public void testHasWaitersIMSE(boolean fair) {
665     final ReentrantLock lock = new ReentrantLock(fair);
666 jsr166 1.29 final Condition c = lock.newCondition();
667 dl 1.13 try {
668     lock.hasWaiters(c);
669     shouldThrow();
670 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
671 dl 1.13 }
672    
673     /**
674 jsr166 1.45 * getWaitQueueLength throws IllegalArgumentException if not owned
675 dl 1.13 */
676 jsr166 1.46 public void testGetWaitQueueLengthIAE() { testGetWaitQueueLengthIAE(false); }
677     public void testGetWaitQueueLengthIAE_fair() { testGetWaitQueueLengthIAE(true); }
678     public void testGetWaitQueueLengthIAE(boolean fair) {
679     final ReentrantLock lock = new ReentrantLock(fair);
680 jsr166 1.29 final Condition c = lock.newCondition();
681 jsr166 1.46 final ReentrantLock lock2 = new ReentrantLock(fair);
682 dl 1.13 try {
683     lock2.getWaitQueueLength(c);
684     shouldThrow();
685 jsr166 1.29 } catch (IllegalArgumentException success) {}
686 dl 1.13 }
687    
688     /**
689 jsr166 1.45 * getWaitQueueLength throws IllegalMonitorStateException if not locked
690 dl 1.13 */
691 jsr166 1.46 public void testGetWaitQueueLengthIMSE() { testGetWaitQueueLengthIMSE(false); }
692     public void testGetWaitQueueLengthIMSE_fair() { testGetWaitQueueLengthIMSE(true); }
693     public void testGetWaitQueueLengthIMSE(boolean fair) {
694     final ReentrantLock lock = new ReentrantLock(fair);
695 jsr166 1.29 final Condition c = lock.newCondition();
696 dl 1.13 try {
697     lock.getWaitQueueLength(c);
698     shouldThrow();
699 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
700 dl 1.13 }
701    
702     /**
703 jsr166 1.45 * getWaitingThreads throws IllegalArgumentException if not owned
704 dl 1.13 */
705 jsr166 1.46 public void testGetWaitingThreadsIAE() { testGetWaitingThreadsIAE(false); }
706     public void testGetWaitingThreadsIAE_fair() { testGetWaitingThreadsIAE(true); }
707     public void testGetWaitingThreadsIAE(boolean fair) {
708     final PublicReentrantLock lock = new PublicReentrantLock(fair);
709 jsr166 1.29 final Condition c = lock.newCondition();
710 jsr166 1.46 final PublicReentrantLock lock2 = new PublicReentrantLock(fair);
711 dl 1.13 try {
712     lock2.getWaitingThreads(c);
713     shouldThrow();
714 jsr166 1.29 } catch (IllegalArgumentException success) {}
715 dl 1.13 }
716    
717     /**
718 jsr166 1.45 * getWaitingThreads throws IllegalMonitorStateException if not locked
719 dl 1.13 */
720 jsr166 1.46 public void testGetWaitingThreadsIMSE() { testGetWaitingThreadsIMSE(false); }
721     public void testGetWaitingThreadsIMSE_fair() { testGetWaitingThreadsIMSE(true); }
722     public void testGetWaitingThreadsIMSE(boolean fair) {
723     final PublicReentrantLock lock = new PublicReentrantLock(fair);
724 jsr166 1.29 final Condition c = lock.newCondition();
725 dl 1.13 try {
726     lock.getWaitingThreads(c);
727     shouldThrow();
728 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
729 dl 1.13 }
730    
731     /**
732 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
733     */
734 jsr166 1.46 public void testHasWaiters() { testHasWaiters(false); }
735     public void testHasWaiters_fair() { testHasWaiters(true); }
736     public void testHasWaiters(boolean fair) {
737     final PublicReentrantLock lock = new PublicReentrantLock(fair);
738 dl 1.13 final Condition c = lock.newCondition();
739 jsr166 1.51 final CountDownLatch pleaseSignal = new CountDownLatch(1);
740 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
741 jsr166 1.29 public void realRun() throws InterruptedException {
742     lock.lock();
743 jsr166 1.45 assertHasNoWaiters(lock, c);
744 jsr166 1.37 assertFalse(lock.hasWaiters(c));
745 jsr166 1.51 pleaseSignal.countDown();
746 jsr166 1.29 c.await();
747 jsr166 1.45 assertHasNoWaiters(lock, c);
748     assertFalse(lock.hasWaiters(c));
749 jsr166 1.29 lock.unlock();
750     }});
751 dl 1.2
752 jsr166 1.51 await(pleaseSignal);
753 jsr166 1.29 lock.lock();
754 jsr166 1.45 assertHasWaiters(lock, c, t);
755 jsr166 1.29 assertTrue(lock.hasWaiters(c));
756     c.signal();
757 jsr166 1.45 assertHasNoWaiters(lock, c);
758 jsr166 1.29 assertFalse(lock.hasWaiters(c));
759     lock.unlock();
760 jsr166 1.42 awaitTermination(t);
761 jsr166 1.45 assertHasNoWaiters(lock, c);
762 dl 1.5 }
763    
764 dl 1.6 /**
765     * getWaitQueueLength returns number of waiting threads
766     */
767 jsr166 1.46 public void testGetWaitQueueLength() { testGetWaitQueueLength(false); }
768     public void testGetWaitQueueLength_fair() { testGetWaitQueueLength(true); }
769     public void testGetWaitQueueLength(boolean fair) {
770     final PublicReentrantLock lock = new PublicReentrantLock(fair);
771 dl 1.13 final Condition c = lock.newCondition();
772 jsr166 1.45 final CountDownLatch locked1 = new CountDownLatch(1);
773     final CountDownLatch locked2 = new CountDownLatch(1);
774     Thread t1 = new Thread(new CheckedRunnable() {
775 jsr166 1.29 public void realRun() throws InterruptedException {
776     lock.lock();
777 jsr166 1.37 assertFalse(lock.hasWaiters(c));
778     assertEquals(0, lock.getWaitQueueLength(c));
779 jsr166 1.45 locked1.countDown();
780 jsr166 1.29 c.await();
781     lock.unlock();
782     }});
783 dl 1.13
784 jsr166 1.45 Thread t2 = new Thread(new CheckedRunnable() {
785 jsr166 1.29 public void realRun() throws InterruptedException {
786     lock.lock();
787 jsr166 1.37 assertTrue(lock.hasWaiters(c));
788     assertEquals(1, lock.getWaitQueueLength(c));
789 jsr166 1.45 locked2.countDown();
790 jsr166 1.29 c.await();
791     lock.unlock();
792     }});
793    
794     lock.lock();
795 jsr166 1.45 assertEquals(0, lock.getWaitQueueLength(c));
796     lock.unlock();
797    
798     t1.start();
799 jsr166 1.46 await(locked1);
800 jsr166 1.45
801     lock.lock();
802     assertHasWaiters(lock, c, t1);
803     assertEquals(1, lock.getWaitQueueLength(c));
804     lock.unlock();
805    
806     t2.start();
807 jsr166 1.46 await(locked2);
808 jsr166 1.45
809     lock.lock();
810     assertHasWaiters(lock, c, t1, t2);
811 jsr166 1.29 assertEquals(2, lock.getWaitQueueLength(c));
812     c.signalAll();
813 jsr166 1.45 assertHasNoWaiters(lock, c);
814 jsr166 1.29 lock.unlock();
815 jsr166 1.45
816 jsr166 1.42 awaitTermination(t1);
817     awaitTermination(t2);
818 jsr166 1.45
819     assertHasNoWaiters(lock, c);
820 dl 1.13 }
821    
822     /**
823     * getWaitingThreads returns only and all waiting threads
824     */
825 jsr166 1.46 public void testGetWaitingThreads() { testGetWaitingThreads(false); }
826     public void testGetWaitingThreads_fair() { testGetWaitingThreads(true); }
827     public void testGetWaitingThreads(boolean fair) {
828     final PublicReentrantLock lock = new PublicReentrantLock(fair);
829 dl 1.13 final Condition c = lock.newCondition();
830 jsr166 1.45 final CountDownLatch locked1 = new CountDownLatch(1);
831     final CountDownLatch locked2 = new CountDownLatch(1);
832 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
833 jsr166 1.29 public void realRun() throws InterruptedException {
834     lock.lock();
835 jsr166 1.37 assertTrue(lock.getWaitingThreads(c).isEmpty());
836 jsr166 1.45 locked1.countDown();
837 jsr166 1.29 c.await();
838     lock.unlock();
839     }});
840    
841 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
842 jsr166 1.29 public void realRun() throws InterruptedException {
843     lock.lock();
844 jsr166 1.37 assertFalse(lock.getWaitingThreads(c).isEmpty());
845 jsr166 1.45 locked2.countDown();
846 jsr166 1.29 c.await();
847     lock.unlock();
848     }});
849 dl 1.5
850 jsr166 1.29 lock.lock();
851     assertTrue(lock.getWaitingThreads(c).isEmpty());
852     lock.unlock();
853 jsr166 1.45
854 jsr166 1.29 t1.start();
855 jsr166 1.46 await(locked1);
856 jsr166 1.45
857     lock.lock();
858     assertHasWaiters(lock, c, t1);
859     assertTrue(lock.getWaitingThreads(c).contains(t1));
860     assertFalse(lock.getWaitingThreads(c).contains(t2));
861     assertEquals(1, lock.getWaitingThreads(c).size());
862     lock.unlock();
863    
864 jsr166 1.29 t2.start();
865 jsr166 1.46 await(locked2);
866 jsr166 1.45
867 jsr166 1.29 lock.lock();
868 jsr166 1.45 assertHasWaiters(lock, c, t1, t2);
869 jsr166 1.29 assertTrue(lock.getWaitingThreads(c).contains(t1));
870     assertTrue(lock.getWaitingThreads(c).contains(t2));
871 jsr166 1.45 assertEquals(2, lock.getWaitingThreads(c).size());
872 jsr166 1.29 c.signalAll();
873 jsr166 1.45 assertHasNoWaiters(lock, c);
874 jsr166 1.29 lock.unlock();
875 jsr166 1.45
876 jsr166 1.42 awaitTermination(t1);
877     awaitTermination(t2);
878 jsr166 1.26
879 jsr166 1.45 assertHasNoWaiters(lock, c);
880 dl 1.22 }
881 dl 1.2
882 dl 1.6 /**
883 jsr166 1.51 * awaitUninterruptibly is uninterruptible
884 dl 1.6 */
885 jsr166 1.46 public void testAwaitUninterruptibly() { testAwaitUninterruptibly(false); }
886     public void testAwaitUninterruptibly_fair() { testAwaitUninterruptibly(true); }
887     public void testAwaitUninterruptibly(boolean fair) {
888     final ReentrantLock lock = new ReentrantLock(fair);
889 dl 1.2 final Condition c = lock.newCondition();
890 jsr166 1.51 final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
891    
892     Thread t1 = newStartedThread(new CheckedRunnable() {
893     public void realRun() {
894     // Interrupt before awaitUninterruptibly
895     lock.lock();
896     pleaseInterrupt.countDown();
897     Thread.currentThread().interrupt();
898     c.awaitUninterruptibly();
899     assertTrue(Thread.interrupted());
900     lock.unlock();
901     }});
902    
903     Thread t2 = newStartedThread(new CheckedRunnable() {
904 jsr166 1.45 public void realRun() {
905 jsr166 1.51 // Interrupt during awaitUninterruptibly
906 jsr166 1.45 lock.lock();
907 jsr166 1.51 pleaseInterrupt.countDown();
908 jsr166 1.45 c.awaitUninterruptibly();
909     assertTrue(Thread.interrupted());
910     lock.unlock();
911     }});
912 dl 1.22
913 jsr166 1.51 await(pleaseInterrupt);
914 jsr166 1.45 lock.lock();
915     lock.unlock();
916 jsr166 1.51 t2.interrupt();
917    
918     assertThreadStaysAlive(t1);
919     assertTrue(t2.isAlive());
920    
921 jsr166 1.29 lock.lock();
922 jsr166 1.51 c.signalAll();
923 jsr166 1.45 lock.unlock();
924 jsr166 1.51
925     awaitTermination(t1);
926     awaitTermination(t2);
927 dl 1.2 }
928    
929 dl 1.6 /**
930 jsr166 1.46 * await/awaitNanos/awaitUntil is interruptible
931 dl 1.6 */
932 jsr166 1.46 public void testInterruptible_await() { testInterruptible(false, AwaitMethod.await); }
933     public void testInterruptible_await_fair() { testInterruptible(true, AwaitMethod.await); }
934 jsr166 1.48 public void testInterruptible_awaitTimed() { testInterruptible(false, AwaitMethod.awaitTimed); }
935     public void testInterruptible_awaitTimed_fair() { testInterruptible(true, AwaitMethod.awaitTimed); }
936 jsr166 1.46 public void testInterruptible_awaitNanos() { testInterruptible(false, AwaitMethod.awaitNanos); }
937     public void testInterruptible_awaitNanos_fair() { testInterruptible(true, AwaitMethod.awaitNanos); }
938     public void testInterruptible_awaitUntil() { testInterruptible(false, AwaitMethod.awaitUntil); }
939     public void testInterruptible_awaitUntil_fair() { testInterruptible(true, AwaitMethod.awaitUntil); }
940     public void testInterruptible(boolean fair, final AwaitMethod awaitMethod) {
941     final PublicReentrantLock lock =
942     new PublicReentrantLock(fair);
943 dl 1.2 final Condition c = lock.newCondition();
944 jsr166 1.51 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
945 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
946 jsr166 1.30 public void realRun() throws InterruptedException {
947 jsr166 1.29 lock.lock();
948 jsr166 1.46 assertLockedByMoi(lock);
949 jsr166 1.43 assertHasNoWaiters(lock, c);
950 jsr166 1.51 pleaseInterrupt.countDown();
951 jsr166 1.43 try {
952 jsr166 1.46 await(c, awaitMethod);
953 jsr166 1.43 } finally {
954 jsr166 1.46 assertLockedByMoi(lock);
955 jsr166 1.43 assertHasNoWaiters(lock, c);
956     lock.unlock();
957     assertFalse(Thread.interrupted());
958     }
959 jsr166 1.29 }});
960    
961 jsr166 1.51 await(pleaseInterrupt);
962 jsr166 1.43 assertHasWaiters(lock, c, t);
963 jsr166 1.29 t.interrupt();
964 jsr166 1.42 awaitTermination(t);
965 jsr166 1.46 assertNotLocked(lock);
966 dl 1.2 }
967    
968 dl 1.6 /**
969 jsr166 1.46 * signalAll wakes up all threads
970 dl 1.6 */
971 jsr166 1.46 public void testSignalAll_await() { testSignalAll(false, AwaitMethod.await); }
972     public void testSignalAll_await_fair() { testSignalAll(true, AwaitMethod.await); }
973 jsr166 1.48 public void testSignalAll_awaitTimed() { testSignalAll(false, AwaitMethod.awaitTimed); }
974     public void testSignalAll_awaitTimed_fair() { testSignalAll(true, AwaitMethod.awaitTimed); }
975 jsr166 1.46 public void testSignalAll_awaitNanos() { testSignalAll(false, AwaitMethod.awaitNanos); }
976     public void testSignalAll_awaitNanos_fair() { testSignalAll(true, AwaitMethod.awaitNanos); }
977     public void testSignalAll_awaitUntil() { testSignalAll(false, AwaitMethod.awaitUntil); }
978     public void testSignalAll_awaitUntil_fair() { testSignalAll(true, AwaitMethod.awaitUntil); }
979     public void testSignalAll(boolean fair, final AwaitMethod awaitMethod) {
980     final PublicReentrantLock lock = new PublicReentrantLock(fair);
981 dl 1.2 final Condition c = lock.newCondition();
982 jsr166 1.51 final CountDownLatch pleaseSignal = new CountDownLatch(2);
983 jsr166 1.46 class Awaiter extends CheckedRunnable {
984 jsr166 1.30 public void realRun() throws InterruptedException {
985 jsr166 1.29 lock.lock();
986 jsr166 1.51 pleaseSignal.countDown();
987 jsr166 1.46 await(c, awaitMethod);
988     lock.unlock();
989     }
990     }
991 jsr166 1.29
992 jsr166 1.46 Thread t1 = newStartedThread(new Awaiter());
993     Thread t2 = newStartedThread(new Awaiter());
994 dl 1.2
995 jsr166 1.51 await(pleaseSignal);
996 jsr166 1.46 lock.lock();
997     assertHasWaiters(lock, c, t1, t2);
998     c.signalAll();
999     assertHasNoWaiters(lock, c);
1000     lock.unlock();
1001     awaitTermination(t1);
1002     awaitTermination(t2);
1003 dl 1.2 }
1004    
1005 dl 1.6 /**
1006 jsr166 1.49 * signal wakes up waiting threads in FIFO order
1007 dl 1.6 */
1008 jsr166 1.46 public void testSignalWakesFifo() { testSignalWakesFifo(false); }
1009     public void testSignalWakesFifo_fair() { testSignalWakesFifo(true); }
1010     public void testSignalWakesFifo(boolean fair) {
1011     final PublicReentrantLock lock =
1012     new PublicReentrantLock(fair);
1013 dl 1.2 final Condition c = lock.newCondition();
1014 jsr166 1.46 final CountDownLatch locked1 = new CountDownLatch(1);
1015     final CountDownLatch locked2 = new CountDownLatch(1);
1016 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
1017 jsr166 1.29 public void realRun() throws InterruptedException {
1018     lock.lock();
1019 jsr166 1.46 locked1.countDown();
1020 jsr166 1.29 c.await();
1021     lock.unlock();
1022     }});
1023    
1024 jsr166 1.46 await(locked1);
1025    
1026 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
1027 jsr166 1.29 public void realRun() throws InterruptedException {
1028     lock.lock();
1029 jsr166 1.46 locked2.countDown();
1030 jsr166 1.29 c.await();
1031     lock.unlock();
1032     }});
1033 dl 1.2
1034 jsr166 1.46 await(locked2);
1035    
1036 jsr166 1.29 lock.lock();
1037 jsr166 1.45 assertHasWaiters(lock, c, t1, t2);
1038 jsr166 1.46 assertFalse(lock.hasQueuedThreads());
1039     c.signal();
1040     assertHasWaiters(lock, c, t2);
1041     assertTrue(lock.hasQueuedThread(t1));
1042     assertFalse(lock.hasQueuedThread(t2));
1043     c.signal();
1044 jsr166 1.45 assertHasNoWaiters(lock, c);
1045 jsr166 1.46 assertTrue(lock.hasQueuedThread(t1));
1046     assertTrue(lock.hasQueuedThread(t2));
1047 jsr166 1.29 lock.unlock();
1048 jsr166 1.42 awaitTermination(t1);
1049     awaitTermination(t2);
1050 dl 1.3 }
1051    
1052 dl 1.6 /**
1053 dl 1.23 * await after multiple reentrant locking preserves lock count
1054     */
1055 jsr166 1.46 public void testAwaitLockCount() { testAwaitLockCount(false); }
1056     public void testAwaitLockCount_fair() { testAwaitLockCount(true); }
1057     public void testAwaitLockCount(boolean fair) {
1058     final PublicReentrantLock lock = new PublicReentrantLock(fair);
1059 dl 1.23 final Condition c = lock.newCondition();
1060 jsr166 1.51 final CountDownLatch pleaseSignal = new CountDownLatch(2);
1061 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
1062 jsr166 1.29 public void realRun() throws InterruptedException {
1063     lock.lock();
1064 jsr166 1.46 assertLockedByMoi(lock);
1065 jsr166 1.37 assertEquals(1, lock.getHoldCount());
1066 jsr166 1.51 pleaseSignal.countDown();
1067 jsr166 1.29 c.await();
1068 jsr166 1.46 assertLockedByMoi(lock);
1069 jsr166 1.37 assertEquals(1, lock.getHoldCount());
1070 jsr166 1.29 lock.unlock();
1071     }});
1072 dl 1.23
1073 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
1074 jsr166 1.29 public void realRun() throws InterruptedException {
1075     lock.lock();
1076     lock.lock();
1077 jsr166 1.46 assertLockedByMoi(lock);
1078 jsr166 1.37 assertEquals(2, lock.getHoldCount());
1079 jsr166 1.51 pleaseSignal.countDown();
1080 jsr166 1.29 c.await();
1081 jsr166 1.46 assertLockedByMoi(lock);
1082 jsr166 1.37 assertEquals(2, lock.getHoldCount());
1083 jsr166 1.29 lock.unlock();
1084     lock.unlock();
1085     }});
1086    
1087 jsr166 1.51 await(pleaseSignal);
1088 jsr166 1.29 lock.lock();
1089 jsr166 1.45 assertHasWaiters(lock, c, t1, t2);
1090     assertEquals(1, lock.getHoldCount());
1091 jsr166 1.29 c.signalAll();
1092 jsr166 1.45 assertHasNoWaiters(lock, c);
1093 jsr166 1.29 lock.unlock();
1094 jsr166 1.42 awaitTermination(t1);
1095     awaitTermination(t2);
1096 dl 1.23 }
1097    
1098     /**
1099 dl 1.6 * A serialized lock deserializes as unlocked
1100     */
1101 jsr166 1.46 public void testSerialization() { testSerialization(false); }
1102     public void testSerialization_fair() { testSerialization(true); }
1103     public void testSerialization(boolean fair) {
1104     ReentrantLock lock = new ReentrantLock(fair);
1105     lock.lock();
1106    
1107     ReentrantLock clone = serialClone(lock);
1108     assertEquals(lock.isFair(), clone.isFair());
1109     assertTrue(lock.isLocked());
1110     assertFalse(clone.isLocked());
1111     assertEquals(1, lock.getHoldCount());
1112     assertEquals(0, clone.getHoldCount());
1113     clone.lock();
1114     clone.lock();
1115     assertTrue(clone.isLocked());
1116     assertEquals(2, clone.getHoldCount());
1117     assertEquals(1, lock.getHoldCount());
1118     clone.unlock();
1119     clone.unlock();
1120     assertTrue(lock.isLocked());
1121     assertFalse(clone.isLocked());
1122 dl 1.2 }
1123 dl 1.1
1124 dl 1.18 /**
1125     * toString indicates current lock state
1126     */
1127 jsr166 1.46 public void testToString() { testToString(false); }
1128     public void testToString_fair() { testToString(true); }
1129     public void testToString(boolean fair) {
1130     ReentrantLock lock = new ReentrantLock(fair);
1131 jsr166 1.49 assertTrue(lock.toString().contains("Unlocked"));
1132 dl 1.18 lock.lock();
1133 jsr166 1.49 assertTrue(lock.toString().contains("Locked"));
1134     lock.unlock();
1135     assertTrue(lock.toString().contains("Unlocked"));
1136 dl 1.18 }
1137 dl 1.1 }