ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.49
Committed: Tue May 24 23:40:14 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.48: +7 -7 lines
Log Message:
pretty up testToString

File Contents

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