ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.62
Committed: Fri Jul 3 01:25:15 2015 UTC (8 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.61: +2 -4 lines
Log Message:
use delayedDate(); use timeoutMillis()

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