ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.55
Committed: Wed Dec 31 20:34:16 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.54: +2 -0 lines
Log Message:
add default clause to switch-on-enums

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