ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.45
Committed: Sat May 7 19:03:26 2011 UTC (13 years ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.44: +292 -193 lines
Log Message:
Improve ReentrantLock and ReentrantReadWriteLock tests

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     assertLockedBy(lock, Thread.currentThread());
69     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     throw new AssertionError("timed out");
82     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.43 * Checks that condition c has no waiters.
112     */
113     void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
114     assertHasWaiters(lock, c, new Thread[] {});
115     }
116    
117     /**
118     * Checks that condition c has exactly the given waiter threads.
119     */
120     void assertHasWaiters(PublicReentrantLock lock, Condition c,
121     Thread... threads) {
122     lock.lock();
123     assertEquals(threads.length > 0, lock.hasWaiters(c));
124     assertEquals(threads.length, lock.getWaitQueueLength(c));
125     assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
126     assertEquals(threads.length, lock.getWaitingThreads(c).size());
127     assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
128     new HashSet<Thread>(Arrays.asList(threads)));
129     lock.unlock();
130     }
131    
132     /**
133 jsr166 1.45 * Constructor sets given fairness, and is in unlocked state
134 dl 1.9 */
135 jsr166 1.26 public void testConstructor() {
136 jsr166 1.45 PublicReentrantLock lock;
137    
138     lock = new PublicReentrantLock();
139     assertFalse(lock.isFair());
140     assertNotLocked(lock);
141    
142     lock = new PublicReentrantLock(true);
143     assertTrue(lock.isFair());
144     assertNotLocked(lock);
145    
146     lock = new PublicReentrantLock(false);
147     assertFalse(lock.isFair());
148     assertNotLocked(lock);
149 dl 1.9 }
150    
151     /**
152 dl 1.6 * locking an unlocked lock succeeds
153     */
154 jsr166 1.26 public void testLock() {
155 jsr166 1.45 PublicReentrantLock lock = new PublicReentrantLock();
156     lock.lock();
157     assertLockedBy(lock, Thread.currentThread());
158     releaseLock(lock);
159 dl 1.6 }
160    
161 dl 1.8 /**
162 dl 1.6 * locking an unlocked fair lock succeeds
163     */
164 jsr166 1.26 public void testFairLock() {
165 jsr166 1.45 PublicReentrantLock lock = new PublicReentrantLock(true);
166     lock.lock();
167     assertLockedBy(lock, Thread.currentThread());
168     releaseLock(lock);
169 dl 1.6 }
170    
171 dl 1.8 /**
172 dl 1.5 * Unlocking an unlocked lock throws IllegalMonitorStateException
173 dl 1.1 */
174 jsr166 1.26 public void testUnlock_IllegalMonitorStateException() {
175 jsr166 1.45 ReentrantLock lock = new ReentrantLock();
176 jsr166 1.30 try {
177 jsr166 1.45 lock.unlock();
178 jsr166 1.30 shouldThrow();
179     } catch (IllegalMonitorStateException success) {}
180 dl 1.5 }
181 dl 1.1
182 dl 1.8 /**
183 dl 1.15 * tryLock on an unlocked lock succeeds
184 dl 1.1 */
185 jsr166 1.26 public void testTryLock() {
186 jsr166 1.45 PublicReentrantLock lock = new PublicReentrantLock();
187     assertTrue(lock.tryLock());
188     assertLockedBy(lock, Thread.currentThread());
189     releaseLock(lock);
190 dl 1.6 }
191    
192 dl 1.8 /**
193 dl 1.13 * hasQueuedThreads reports whether there are waiting threads
194     */
195 jsr166 1.45 public void testHasQueuedThreads() throws InterruptedException {
196     final PublicReentrantLock lock = new PublicReentrantLock();
197 dl 1.13 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
198     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
199 jsr166 1.29 assertFalse(lock.hasQueuedThreads());
200     lock.lock();
201 jsr166 1.45 assertFalse(lock.hasQueuedThreads());
202 jsr166 1.29 t1.start();
203 jsr166 1.45 waitForQueuedThread(lock, t1);
204 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
205     t2.start();
206 jsr166 1.45 waitForQueuedThread(lock, t2);
207 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
208     t1.interrupt();
209 jsr166 1.45 awaitTermination(t1);
210 jsr166 1.29 assertTrue(lock.hasQueuedThreads());
211     lock.unlock();
212 jsr166 1.45 awaitTermination(t2);
213 jsr166 1.29 assertFalse(lock.hasQueuedThreads());
214 jsr166 1.26 }
215 dl 1.13
216     /**
217 dl 1.7 * getQueueLength reports number of waiting threads
218 dl 1.1 */
219 jsr166 1.29 public void testGetQueueLength() throws InterruptedException {
220 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
221 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
222     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
223 jsr166 1.29 assertEquals(0, lock.getQueueLength());
224     lock.lock();
225     t1.start();
226 jsr166 1.45 waitForQueuedThread(lock, t1);
227 jsr166 1.29 assertEquals(1, lock.getQueueLength());
228     t2.start();
229 jsr166 1.45 waitForQueuedThread(lock, t2);
230 jsr166 1.29 assertEquals(2, lock.getQueueLength());
231     t1.interrupt();
232 jsr166 1.45 awaitTermination(t1);
233 jsr166 1.29 assertEquals(1, lock.getQueueLength());
234     lock.unlock();
235 jsr166 1.45 awaitTermination(t2);
236 jsr166 1.29 assertEquals(0, lock.getQueueLength());
237 jsr166 1.26 }
238 dl 1.5
239 dl 1.8 /**
240 dl 1.16 * getQueueLength reports number of waiting threads
241     */
242 jsr166 1.29 public void testGetQueueLength_fair() throws InterruptedException {
243 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock(true);
244 dl 1.16 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
245     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
246 jsr166 1.29 assertEquals(0, lock.getQueueLength());
247     lock.lock();
248     t1.start();
249 jsr166 1.45 waitForQueuedThread(lock, t1);
250 jsr166 1.29 assertEquals(1, lock.getQueueLength());
251     t2.start();
252 jsr166 1.45 waitForQueuedThread(lock, t2);
253 jsr166 1.29 assertEquals(2, lock.getQueueLength());
254     t1.interrupt();
255 jsr166 1.45 awaitTermination(t1);
256 jsr166 1.29 assertEquals(1, lock.getQueueLength());
257     lock.unlock();
258 jsr166 1.45 awaitTermination(t2);
259 jsr166 1.29 assertEquals(0, lock.getQueueLength());
260 jsr166 1.26 }
261 dl 1.16
262     /**
263 dl 1.19 * hasQueuedThread(null) throws NPE
264     */
265 jsr166 1.26 public void testHasQueuedThreadNPE() {
266 jsr166 1.45 final ReentrantLock lock = new ReentrantLock();
267 dl 1.19 try {
268 jsr166 1.45 lock.hasQueuedThread(null);
269 dl 1.19 shouldThrow();
270 jsr166 1.29 } catch (NullPointerException success) {}
271 dl 1.19 }
272    
273     /**
274     * hasQueuedThread reports whether a thread is queued.
275     */
276 jsr166 1.29 public void testHasQueuedThread() throws InterruptedException {
277 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
278     Thread t1 = new Thread(new InterruptedLockRunnable(lock));
279     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
280     assertFalse(lock.hasQueuedThread(t1));
281     assertFalse(lock.hasQueuedThread(t2));
282     lock.lock();
283 jsr166 1.29 t1.start();
284 jsr166 1.45 waitForQueuedThread(lock, t1);
285     assertTrue(lock.hasQueuedThread(t1));
286     assertFalse(lock.hasQueuedThread(t2));
287 jsr166 1.29 t2.start();
288 jsr166 1.45 waitForQueuedThread(lock, t2);
289     assertTrue(lock.hasQueuedThread(t1));
290     assertTrue(lock.hasQueuedThread(t2));
291 jsr166 1.29 t1.interrupt();
292 jsr166 1.42 awaitTermination(t1);
293 jsr166 1.45 assertFalse(lock.hasQueuedThread(t1));
294     assertTrue(lock.hasQueuedThread(t2));
295     lock.unlock();
296 jsr166 1.42 awaitTermination(t2);
297 jsr166 1.45 assertFalse(lock.hasQueuedThread(t1));
298     assertFalse(lock.hasQueuedThread(t2));
299 jsr166 1.26 }
300 dl 1.19
301     /**
302 dl 1.5 * getQueuedThreads includes waiting threads
303     */
304 jsr166 1.29 public void testGetQueuedThreads() throws InterruptedException {
305 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
306 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
307     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
308 jsr166 1.29 assertTrue(lock.getQueuedThreads().isEmpty());
309     lock.lock();
310     assertTrue(lock.getQueuedThreads().isEmpty());
311     t1.start();
312 jsr166 1.45 waitForQueuedThread(lock, t1);
313     assertEquals(1, lock.getQueuedThreads().size());
314 jsr166 1.29 assertTrue(lock.getQueuedThreads().contains(t1));
315     t2.start();
316 jsr166 1.45 waitForQueuedThread(lock, t2);
317     assertEquals(2, lock.getQueuedThreads().size());
318 jsr166 1.29 assertTrue(lock.getQueuedThreads().contains(t1));
319     assertTrue(lock.getQueuedThreads().contains(t2));
320     t1.interrupt();
321 jsr166 1.45 awaitTermination(t1);
322 jsr166 1.29 assertFalse(lock.getQueuedThreads().contains(t1));
323     assertTrue(lock.getQueuedThreads().contains(t2));
324 jsr166 1.45 assertEquals(1, lock.getQueuedThreads().size());
325 jsr166 1.29 lock.unlock();
326 jsr166 1.45 awaitTermination(t2);
327 jsr166 1.29 assertTrue(lock.getQueuedThreads().isEmpty());
328 jsr166 1.26 }
329 dl 1.5
330 dl 1.8 /**
331 dl 1.15 * timed tryLock is interruptible.
332 dl 1.5 */
333 jsr166 1.45 public void testTryLock_Interrupted() throws InterruptedException {
334     final PublicReentrantLock lock = new PublicReentrantLock();
335 jsr166 1.30 lock.lock();
336 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
337 jsr166 1.29 public void realRun() throws InterruptedException {
338 jsr166 1.45 lock.tryLock(2 * LONG_DELAY_MS, MILLISECONDS);
339 jsr166 1.29 }});
340    
341 jsr166 1.45 waitForQueuedThread(lock, t);
342 jsr166 1.29 t.interrupt();
343 jsr166 1.42 awaitTermination(t);
344 jsr166 1.45 releaseLock(lock);
345 dl 1.1 }
346    
347 dl 1.5 /**
348 jsr166 1.45 * tryLock on a locked lock fails
349 dl 1.5 */
350 jsr166 1.29 public void testTryLockWhenLocked() throws InterruptedException {
351 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
352 jsr166 1.30 lock.lock();
353 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
354 jsr166 1.29 public void realRun() {
355 jsr166 1.37 assertFalse(lock.tryLock());
356 jsr166 1.29 }});
357    
358 jsr166 1.42 awaitTermination(t);
359 jsr166 1.45 releaseLock(lock);
360 jsr166 1.26 }
361 dl 1.3
362 dl 1.5 /**
363 dl 1.15 * Timed tryLock on a locked lock times out
364 dl 1.5 */
365 jsr166 1.29 public void testTryLock_Timeout() throws InterruptedException {
366 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
367 jsr166 1.30 lock.lock();
368 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
369 jsr166 1.29 public void realRun() throws InterruptedException {
370 jsr166 1.45 long startTime = System.nanoTime();
371     long timeoutMillis = 10;
372     assertFalse(lock.tryLock(timeoutMillis, MILLISECONDS));
373     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
374 jsr166 1.29 }});
375    
376 jsr166 1.42 awaitTermination(t);
377 jsr166 1.45 releaseLock(lock);
378 jsr166 1.26 }
379    
380 dl 1.5 /**
381     * getHoldCount returns number of recursive holds
382     */
383 dl 1.1 public void testGetHoldCount() {
384 jsr166 1.30 ReentrantLock lock = new ReentrantLock();
385     for (int i = 1; i <= SIZE; i++) {
386     lock.lock();
387     assertEquals(i, lock.getHoldCount());
388     }
389     for (int i = SIZE; i > 0; i--) {
390     lock.unlock();
391     assertEquals(i-1, lock.getHoldCount());
392     }
393 dl 1.1 }
394 jsr166 1.26
395 dl 1.5 /**
396     * isLocked is true when locked and false when not
397     */
398 jsr166 1.45 public void testIsLocked() throws Exception {
399 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
400 jsr166 1.45 assertFalse(lock.isLocked());
401 jsr166 1.30 lock.lock();
402     assertTrue(lock.isLocked());
403 jsr166 1.45 lock.lock();
404     assertTrue(lock.isLocked());
405     lock.unlock();
406     assertTrue(lock.isLocked());
407 jsr166 1.30 lock.unlock();
408     assertFalse(lock.isLocked());
409 jsr166 1.45 final CyclicBarrier barrier = new CyclicBarrier(2);
410 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
411 jsr166 1.45 public void realRun() throws Exception {
412 jsr166 1.29 lock.lock();
413 jsr166 1.45 assertTrue(lock.isLocked());
414     barrier.await();
415     barrier.await();
416 jsr166 1.29 lock.unlock();
417     }});
418    
419 jsr166 1.45 barrier.await();
420 jsr166 1.29 assertTrue(lock.isLocked());
421 jsr166 1.45 barrier.await();
422 jsr166 1.42 awaitTermination(t);
423 jsr166 1.29 assertFalse(lock.isLocked());
424 dl 1.1 }
425    
426 dl 1.8 /**
427 dl 1.6 * lockInterruptibly is interruptible.
428     */
429 jsr166 1.45 public void testLockInterruptibly_Interrupted() throws InterruptedException {
430     final PublicReentrantLock lock = new PublicReentrantLock();
431 jsr166 1.30 lock.lock();
432 jsr166 1.41 Thread t = newStartedThread(new InterruptedLockRunnable(lock));
433 jsr166 1.45 waitForQueuedThread(lock, t);
434 jsr166 1.29 t.interrupt();
435 jsr166 1.42 awaitTermination(t);
436 jsr166 1.45 releaseLock(lock);
437 jsr166 1.26 }
438 dl 1.6
439 dl 1.5 /**
440     * lockInterruptibly succeeds when unlocked, else is interruptible
441     */
442 jsr166 1.45 public void testLockInterruptibly_Interrupted2() throws InterruptedException {
443     final PublicReentrantLock lock = new PublicReentrantLock();
444 jsr166 1.29 lock.lockInterruptibly();
445 jsr166 1.41 Thread t = newStartedThread(new InterruptedLockRunnable(lock));
446 jsr166 1.45 waitForQueuedThread(lock, t);
447 jsr166 1.29 t.interrupt();
448     assertTrue(lock.isLocked());
449     assertTrue(lock.isHeldByCurrentThread());
450 jsr166 1.42 awaitTermination(t);
451 jsr166 1.45 releaseLock(lock);
452 dl 1.1 }
453 dl 1.2
454 dl 1.6 /**
455     * Calling await without holding lock throws IllegalMonitorStateException
456     */
457 jsr166 1.29 public void testAwait_IllegalMonitor() throws InterruptedException {
458 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
459 dl 1.2 final Condition c = lock.newCondition();
460     try {
461     c.await();
462 dl 1.6 shouldThrow();
463 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
464 jsr166 1.45 try {
465     c.await(LONG_DELAY_MS, MILLISECONDS);
466     shouldThrow();
467     } catch (IllegalMonitorStateException success) {}
468     try {
469     c.awaitNanos(100);
470     shouldThrow();
471     } catch (IllegalMonitorStateException success) {}
472     try {
473     c.awaitUninterruptibly();
474     shouldThrow();
475     } catch (IllegalMonitorStateException success) {}
476 dl 1.2 }
477    
478 dl 1.6 /**
479     * Calling signal without holding lock throws IllegalMonitorStateException
480     */
481 dl 1.2 public void testSignal_IllegalMonitor() {
482 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
483 dl 1.2 final Condition c = lock.newCondition();
484     try {
485     c.signal();
486 dl 1.6 shouldThrow();
487 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
488 dl 1.2 }
489    
490 dl 1.6 /**
491     * awaitNanos without a signal times out
492     */
493 jsr166 1.29 public void testAwaitNanos_Timeout() throws InterruptedException {
494 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
495 dl 1.2 final Condition c = lock.newCondition();
496 jsr166 1.29 lock.lock();
497 jsr166 1.45 long startTime = System.nanoTime();
498     long timeoutMillis = 10;
499     long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
500     long nanosRemaining = c.awaitNanos(timeoutNanos);
501     assertTrue(nanosRemaining <= 0);
502     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
503 jsr166 1.29 lock.unlock();
504 dl 1.2 }
505    
506 dl 1.6 /**
507 jsr166 1.38 * timed await without a signal times out
508 dl 1.6 */
509 jsr166 1.29 public void testAwait_Timeout() throws InterruptedException {
510 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
511 dl 1.2 final Condition c = lock.newCondition();
512 jsr166 1.29 lock.lock();
513 jsr166 1.45 long startTime = System.nanoTime();
514     long timeoutMillis = 10;
515     assertFalse(c.await(timeoutMillis, MILLISECONDS));
516     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
517 jsr166 1.29 lock.unlock();
518 dl 1.2 }
519    
520 dl 1.6 /**
521     * awaitUntil without a signal times out
522     */
523 jsr166 1.29 public void testAwaitUntil_Timeout() throws InterruptedException {
524 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
525 dl 1.2 final Condition c = lock.newCondition();
526 jsr166 1.29 lock.lock();
527 jsr166 1.45 long startTime = System.nanoTime();
528     long timeoutMillis = 10;
529 jsr166 1.29 java.util.Date d = new java.util.Date();
530 jsr166 1.45 assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + timeoutMillis)));
531     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
532 jsr166 1.29 lock.unlock();
533 dl 1.2 }
534    
535 dl 1.6 /**
536     * await returns when signalled
537     */
538 jsr166 1.29 public void testAwait() throws InterruptedException {
539 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
540 dl 1.13 final Condition c = lock.newCondition();
541 jsr166 1.45 final CountDownLatch locked = new CountDownLatch(1);
542 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
543 jsr166 1.29 public void realRun() throws InterruptedException {
544     lock.lock();
545 jsr166 1.45 locked.countDown();
546 jsr166 1.29 c.await();
547     lock.unlock();
548     }});
549 dl 1.5
550 jsr166 1.45 locked.await();
551 jsr166 1.29 lock.lock();
552 jsr166 1.45 assertHasWaiters(lock, c, t);
553 jsr166 1.29 c.signal();
554 jsr166 1.45 assertHasNoWaiters(lock, c);
555     assertTrue(t.isAlive());
556 jsr166 1.29 lock.unlock();
557 jsr166 1.42 awaitTermination(t);
558 dl 1.5 }
559    
560 dl 1.6 /**
561 dl 1.14 * hasWaiters throws NPE if null
562     */
563     public void testHasWaitersNPE() {
564 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
565 dl 1.14 try {
566     lock.hasWaiters(null);
567     shouldThrow();
568 jsr166 1.29 } catch (NullPointerException success) {}
569 dl 1.14 }
570    
571     /**
572     * getWaitQueueLength throws NPE if null
573     */
574     public void testGetWaitQueueLengthNPE() {
575 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
576 dl 1.14 try {
577     lock.getWaitQueueLength(null);
578     shouldThrow();
579 jsr166 1.29 } catch (NullPointerException success) {}
580 dl 1.14 }
581    
582     /**
583     * getWaitingThreads throws NPE if null
584     */
585     public void testGetWaitingThreadsNPE() {
586 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
587 dl 1.14 try {
588     lock.getWaitingThreads(null);
589     shouldThrow();
590 jsr166 1.29 } catch (NullPointerException success) {}
591 dl 1.14 }
592    
593     /**
594 jsr166 1.45 * hasWaiters throws IllegalArgumentException if not owned
595 dl 1.13 */
596     public void testHasWaitersIAE() {
597 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
598 jsr166 1.29 final Condition c = lock.newCondition();
599 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
600 dl 1.13 try {
601     lock2.hasWaiters(c);
602     shouldThrow();
603 jsr166 1.29 } catch (IllegalArgumentException success) {}
604 dl 1.13 }
605    
606     /**
607 jsr166 1.45 * hasWaiters throws IllegalMonitorStateException if not locked
608 dl 1.13 */
609     public void testHasWaitersIMSE() {
610 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
611 jsr166 1.29 final Condition c = lock.newCondition();
612 dl 1.13 try {
613     lock.hasWaiters(c);
614     shouldThrow();
615 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
616 dl 1.13 }
617    
618     /**
619 jsr166 1.45 * getWaitQueueLength throws IllegalArgumentException if not owned
620 dl 1.13 */
621     public void testGetWaitQueueLengthIAE() {
622 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
623 jsr166 1.29 final Condition c = lock.newCondition();
624 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
625 dl 1.13 try {
626     lock2.getWaitQueueLength(c);
627     shouldThrow();
628 jsr166 1.29 } catch (IllegalArgumentException success) {}
629 dl 1.13 }
630    
631     /**
632 jsr166 1.45 * getWaitQueueLength throws IllegalMonitorStateException if not locked
633 dl 1.13 */
634     public void testGetWaitQueueLengthIMSE() {
635 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
636 jsr166 1.29 final Condition c = lock.newCondition();
637 dl 1.13 try {
638     lock.getWaitQueueLength(c);
639     shouldThrow();
640 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
641 dl 1.13 }
642    
643     /**
644 jsr166 1.45 * getWaitingThreads throws IllegalArgumentException if not owned
645 dl 1.13 */
646     public void testGetWaitingThreadsIAE() {
647 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
648 jsr166 1.29 final Condition c = lock.newCondition();
649 jsr166 1.30 final PublicReentrantLock lock2 = new PublicReentrantLock();
650 dl 1.13 try {
651     lock2.getWaitingThreads(c);
652     shouldThrow();
653 jsr166 1.29 } catch (IllegalArgumentException success) {}
654 dl 1.13 }
655    
656     /**
657 jsr166 1.45 * getWaitingThreads throws IllegalMonitorStateException if not locked
658 dl 1.13 */
659     public void testGetWaitingThreadsIMSE() {
660 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
661 jsr166 1.29 final Condition c = lock.newCondition();
662 dl 1.13 try {
663     lock.getWaitingThreads(c);
664     shouldThrow();
665 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
666 dl 1.13 }
667    
668     /**
669 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
670     */
671 jsr166 1.29 public void testHasWaiters() throws InterruptedException {
672 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
673 dl 1.13 final Condition c = lock.newCondition();
674 jsr166 1.45 final CountDownLatch locked = new CountDownLatch(1);
675 jsr166 1.41 Thread t = newStartedThread(new CheckedRunnable() {
676 jsr166 1.29 public void realRun() throws InterruptedException {
677     lock.lock();
678 jsr166 1.45 assertHasNoWaiters(lock, c);
679 jsr166 1.37 assertFalse(lock.hasWaiters(c));
680 jsr166 1.45 locked.countDown();
681 jsr166 1.29 c.await();
682 jsr166 1.45 assertHasNoWaiters(lock, c);
683     assertFalse(lock.hasWaiters(c));
684 jsr166 1.29 lock.unlock();
685     }});
686 dl 1.2
687 jsr166 1.45 locked.await();
688 jsr166 1.29 lock.lock();
689 jsr166 1.45 assertHasWaiters(lock, c, t);
690 jsr166 1.29 assertTrue(lock.hasWaiters(c));
691     c.signal();
692 jsr166 1.45 assertHasNoWaiters(lock, c);
693 jsr166 1.29 assertFalse(lock.hasWaiters(c));
694     lock.unlock();
695 jsr166 1.42 awaitTermination(t);
696 jsr166 1.45 assertHasNoWaiters(lock, c);
697 dl 1.5 }
698    
699 dl 1.6 /**
700     * getWaitQueueLength returns number of waiting threads
701     */
702 jsr166 1.29 public void testGetWaitQueueLength() throws InterruptedException {
703 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
704 dl 1.13 final Condition c = lock.newCondition();
705 jsr166 1.45 final CountDownLatch locked1 = new CountDownLatch(1);
706     final CountDownLatch locked2 = new CountDownLatch(1);
707     Thread t1 = new Thread(new CheckedRunnable() {
708 jsr166 1.29 public void realRun() throws InterruptedException {
709     lock.lock();
710 jsr166 1.37 assertFalse(lock.hasWaiters(c));
711     assertEquals(0, lock.getWaitQueueLength(c));
712 jsr166 1.45 locked1.countDown();
713 jsr166 1.29 c.await();
714     lock.unlock();
715     }});
716 dl 1.13
717 jsr166 1.45 Thread t2 = new Thread(new CheckedRunnable() {
718 jsr166 1.29 public void realRun() throws InterruptedException {
719     lock.lock();
720 jsr166 1.37 assertTrue(lock.hasWaiters(c));
721     assertEquals(1, lock.getWaitQueueLength(c));
722 jsr166 1.45 locked2.countDown();
723 jsr166 1.29 c.await();
724     lock.unlock();
725     }});
726    
727     lock.lock();
728 jsr166 1.45 assertEquals(0, lock.getWaitQueueLength(c));
729     lock.unlock();
730    
731     t1.start();
732     locked1.await();
733    
734     lock.lock();
735     assertHasWaiters(lock, c, t1);
736     assertEquals(1, lock.getWaitQueueLength(c));
737     lock.unlock();
738    
739     t2.start();
740     locked2.await();
741    
742     lock.lock();
743     assertHasWaiters(lock, c, t1, t2);
744 jsr166 1.29 assertEquals(2, lock.getWaitQueueLength(c));
745     c.signalAll();
746 jsr166 1.45 assertHasNoWaiters(lock, c);
747 jsr166 1.29 lock.unlock();
748 jsr166 1.45
749 jsr166 1.42 awaitTermination(t1);
750     awaitTermination(t2);
751 jsr166 1.45
752     assertHasNoWaiters(lock, c);
753 dl 1.13 }
754    
755     /**
756     * getWaitingThreads returns only and all waiting threads
757     */
758 jsr166 1.29 public void testGetWaitingThreads() throws InterruptedException {
759 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
760 dl 1.13 final Condition c = lock.newCondition();
761 jsr166 1.45 final CountDownLatch locked1 = new CountDownLatch(1);
762     final CountDownLatch locked2 = new CountDownLatch(1);
763 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
764 jsr166 1.29 public void realRun() throws InterruptedException {
765     lock.lock();
766 jsr166 1.37 assertTrue(lock.getWaitingThreads(c).isEmpty());
767 jsr166 1.45 locked1.countDown();
768 jsr166 1.29 c.await();
769     lock.unlock();
770     }});
771    
772 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
773 jsr166 1.29 public void realRun() throws InterruptedException {
774     lock.lock();
775 jsr166 1.37 assertFalse(lock.getWaitingThreads(c).isEmpty());
776 jsr166 1.45 locked2.countDown();
777 jsr166 1.29 c.await();
778     lock.unlock();
779     }});
780 dl 1.5
781 jsr166 1.29 lock.lock();
782     assertTrue(lock.getWaitingThreads(c).isEmpty());
783     lock.unlock();
784 jsr166 1.45
785 jsr166 1.29 t1.start();
786 jsr166 1.45 locked1.await();
787    
788     lock.lock();
789     assertHasWaiters(lock, c, t1);
790     assertTrue(lock.getWaitingThreads(c).contains(t1));
791     assertFalse(lock.getWaitingThreads(c).contains(t2));
792     assertEquals(1, lock.getWaitingThreads(c).size());
793     lock.unlock();
794    
795 jsr166 1.29 t2.start();
796 jsr166 1.45 locked2.await();
797    
798 jsr166 1.29 lock.lock();
799 jsr166 1.45 assertHasWaiters(lock, c, t1, t2);
800 jsr166 1.29 assertTrue(lock.getWaitingThreads(c).contains(t1));
801     assertTrue(lock.getWaitingThreads(c).contains(t2));
802 jsr166 1.45 assertEquals(2, lock.getWaitingThreads(c).size());
803 jsr166 1.29 c.signalAll();
804 jsr166 1.45 assertHasNoWaiters(lock, c);
805 jsr166 1.29 lock.unlock();
806 jsr166 1.45
807 jsr166 1.42 awaitTermination(t1);
808     awaitTermination(t2);
809 jsr166 1.26
810 jsr166 1.45 assertHasNoWaiters(lock, c);
811 dl 1.22 }
812 dl 1.2
813 dl 1.6 /**
814     * awaitUninterruptibly doesn't abort on interrupt
815     */
816 jsr166 1.29 public void testAwaitUninterruptibly() throws InterruptedException {
817 dl 1.22 final ReentrantLock lock = new ReentrantLock();
818 dl 1.2 final Condition c = lock.newCondition();
819 jsr166 1.45 final CountDownLatch locked = new CountDownLatch(1);
820     Thread t = newStartedThread(new CheckedRunnable() {
821     public void realRun() {
822     lock.lock();
823     locked.countDown();
824     c.awaitUninterruptibly();
825     assertTrue(Thread.interrupted());
826     lock.unlock();
827     }});
828 dl 1.22
829 jsr166 1.45 locked.await();
830     lock.lock();
831     lock.unlock();
832     t.interrupt();
833     long timeoutMillis = 10;
834     assertThreadJoinTimesOut(t, timeoutMillis);
835 jsr166 1.29 lock.lock();
836 jsr166 1.45 c.signal();
837     lock.unlock();
838     awaitTermination(t);
839 dl 1.2 }
840    
841 dl 1.6 /**
842     * await is interruptible
843     */
844 jsr166 1.29 public void testAwait_Interrupt() throws InterruptedException {
845 jsr166 1.43 final PublicReentrantLock lock = new PublicReentrantLock();
846 dl 1.2 final Condition c = lock.newCondition();
847 jsr166 1.43 final CountDownLatch locked = new CountDownLatch(1);
848 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
849 jsr166 1.30 public void realRun() throws InterruptedException {
850 jsr166 1.29 lock.lock();
851 jsr166 1.43 assertTrue(lock.isLocked());
852     assertTrue(lock.isHeldByCurrentThread());
853     assertHasNoWaiters(lock, c);
854     locked.countDown();
855     try {
856     c.await();
857     } finally {
858     assertTrue(lock.isLocked());
859     assertTrue(lock.isHeldByCurrentThread());
860     assertHasNoWaiters(lock, c);
861     lock.unlock();
862     assertFalse(Thread.interrupted());
863     }
864 jsr166 1.29 }});
865    
866 jsr166 1.43 locked.await();
867     assertHasWaiters(lock, c, t);
868 jsr166 1.29 t.interrupt();
869 jsr166 1.42 awaitTermination(t);
870 jsr166 1.43 assertFalse(lock.isLocked());
871 dl 1.2 }
872    
873 dl 1.6 /**
874     * awaitNanos is interruptible
875     */
876 jsr166 1.29 public void testAwaitNanos_Interrupt() throws InterruptedException {
877 jsr166 1.44 final PublicReentrantLock lock = new PublicReentrantLock();
878 dl 1.2 final Condition c = lock.newCondition();
879 jsr166 1.44 final CountDownLatch locked = new CountDownLatch(1);
880 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
881 jsr166 1.30 public void realRun() throws InterruptedException {
882 jsr166 1.29 lock.lock();
883 jsr166 1.44 assertTrue(lock.isLocked());
884     assertTrue(lock.isHeldByCurrentThread());
885     assertHasNoWaiters(lock, c);
886     locked.countDown();
887     try {
888 jsr166 1.45 c.awaitNanos(MILLISECONDS.toNanos(2 * LONG_DELAY_MS));
889 jsr166 1.44 } finally {
890     assertTrue(lock.isLocked());
891     assertTrue(lock.isHeldByCurrentThread());
892     assertHasNoWaiters(lock, c);
893     lock.unlock();
894     assertFalse(Thread.interrupted());
895     }
896 jsr166 1.29 }});
897    
898 jsr166 1.44 locked.await();
899     assertHasWaiters(lock, c, t);
900 jsr166 1.29 t.interrupt();
901 jsr166 1.42 awaitTermination(t);
902 jsr166 1.44 assertFalse(lock.isLocked());
903 dl 1.2 }
904    
905 dl 1.6 /**
906     * awaitUntil is interruptible
907     */
908 jsr166 1.29 public void testAwaitUntil_Interrupt() throws InterruptedException {
909 jsr166 1.44 final PublicReentrantLock lock = new PublicReentrantLock();
910 dl 1.2 final Condition c = lock.newCondition();
911 jsr166 1.44 final CountDownLatch locked = new CountDownLatch(1);
912 jsr166 1.41 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
913 jsr166 1.30 public void realRun() throws InterruptedException {
914 jsr166 1.29 lock.lock();
915 jsr166 1.44 assertTrue(lock.isLocked());
916     assertTrue(lock.isHeldByCurrentThread());
917     assertHasNoWaiters(lock, c);
918     locked.countDown();
919 jsr166 1.29 java.util.Date d = new java.util.Date();
920 jsr166 1.44 try {
921 jsr166 1.45 c.awaitUntil(new java.util.Date(d.getTime() + 2 * LONG_DELAY_MS));
922 jsr166 1.44 } finally {
923     assertTrue(lock.isLocked());
924     assertTrue(lock.isHeldByCurrentThread());
925     assertHasNoWaiters(lock, c);
926     lock.unlock();
927     assertFalse(Thread.interrupted());
928     }
929 jsr166 1.29 }});
930    
931 jsr166 1.44 locked.await();
932     assertHasWaiters(lock, c, t);
933 jsr166 1.29 t.interrupt();
934 jsr166 1.42 awaitTermination(t);
935 jsr166 1.44 assertFalse(lock.isLocked());
936 dl 1.2 }
937    
938 dl 1.6 /**
939     * signalAll wakes up all threads
940     */
941 jsr166 1.29 public void testSignalAll() throws InterruptedException {
942 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
943 dl 1.2 final Condition c = lock.newCondition();
944 jsr166 1.45 final CountDownLatch locked = new CountDownLatch(2);
945 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
946 jsr166 1.29 public void realRun() throws InterruptedException {
947     lock.lock();
948 jsr166 1.45 locked.countDown();
949 jsr166 1.29 c.await();
950     lock.unlock();
951     }});
952    
953 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
954 jsr166 1.29 public void realRun() throws InterruptedException {
955     lock.lock();
956 jsr166 1.45 locked.countDown();
957 jsr166 1.29 c.await();
958     lock.unlock();
959     }});
960 dl 1.2
961 jsr166 1.45 locked.await();
962 jsr166 1.29 lock.lock();
963 jsr166 1.45 assertHasWaiters(lock, c, t1, t2);
964 jsr166 1.29 c.signalAll();
965 jsr166 1.45 assertHasNoWaiters(lock, c);
966 jsr166 1.29 lock.unlock();
967 jsr166 1.42 awaitTermination(t1);
968     awaitTermination(t2);
969 dl 1.3 }
970    
971 dl 1.6 /**
972 dl 1.23 * await after multiple reentrant locking preserves lock count
973     */
974 jsr166 1.29 public void testAwaitLockCount() throws InterruptedException {
975 jsr166 1.45 final PublicReentrantLock lock = new PublicReentrantLock();
976 dl 1.23 final Condition c = lock.newCondition();
977 jsr166 1.45 final CountDownLatch locked = new CountDownLatch(2);
978 jsr166 1.41 Thread t1 = newStartedThread(new CheckedRunnable() {
979 jsr166 1.29 public void realRun() throws InterruptedException {
980     lock.lock();
981 jsr166 1.45 assertLockedBy(lock, Thread.currentThread());
982 jsr166 1.37 assertEquals(1, lock.getHoldCount());
983 jsr166 1.45 locked.countDown();
984 jsr166 1.29 c.await();
985 jsr166 1.45 assertLockedBy(lock, Thread.currentThread());
986 jsr166 1.37 assertEquals(1, lock.getHoldCount());
987 jsr166 1.29 lock.unlock();
988     }});
989 dl 1.23
990 jsr166 1.41 Thread t2 = newStartedThread(new CheckedRunnable() {
991 jsr166 1.29 public void realRun() throws InterruptedException {
992     lock.lock();
993     lock.lock();
994 jsr166 1.45 assertLockedBy(lock, Thread.currentThread());
995 jsr166 1.37 assertEquals(2, lock.getHoldCount());
996 jsr166 1.45 locked.countDown();
997 jsr166 1.29 c.await();
998 jsr166 1.45 assertLockedBy(lock, Thread.currentThread());
999 jsr166 1.37 assertEquals(2, lock.getHoldCount());
1000 jsr166 1.29 lock.unlock();
1001     lock.unlock();
1002     }});
1003    
1004 jsr166 1.45 locked.await();
1005 jsr166 1.29 lock.lock();
1006 jsr166 1.45 assertHasWaiters(lock, c, t1, t2);
1007     assertEquals(1, lock.getHoldCount());
1008 jsr166 1.29 c.signalAll();
1009 jsr166 1.45 assertHasNoWaiters(lock, c);
1010 jsr166 1.29 lock.unlock();
1011 jsr166 1.42 awaitTermination(t1);
1012     awaitTermination(t2);
1013 dl 1.23 }
1014    
1015     /**
1016 dl 1.6 * A serialized lock deserializes as unlocked
1017     */
1018 jsr166 1.29 public void testSerialization() throws Exception {
1019 dl 1.3 ReentrantLock l = new ReentrantLock();
1020     l.lock();
1021     l.unlock();
1022    
1023 jsr166 1.29 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1024     ObjectOutputStream out =
1025     new ObjectOutputStream(new BufferedOutputStream(bout));
1026     out.writeObject(l);
1027     out.close();
1028 jsr166 1.35
1029 jsr166 1.29 ByteArrayInputStream bin =
1030     new ByteArrayInputStream(bout.toByteArray());
1031     ObjectInputStream in =
1032     new ObjectInputStream(new BufferedInputStream(bin));
1033     ReentrantLock r = (ReentrantLock) in.readObject();
1034     r.lock();
1035     r.unlock();
1036 dl 1.2 }
1037 dl 1.1
1038 dl 1.18 /**
1039     * toString indicates current lock state
1040     */
1041     public void testToString() {
1042     ReentrantLock lock = new ReentrantLock();
1043     String us = lock.toString();
1044     assertTrue(us.indexOf("Unlocked") >= 0);
1045     lock.lock();
1046     String ls = lock.toString();
1047     assertTrue(ls.indexOf("Locked") >= 0);
1048     }
1049 dl 1.1 }