ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.30
Committed: Sat Nov 21 02:07:27 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.29: +87 -87 lines
Log Message:
untabify

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     * http://creativecommons.org/licenses/publicdomain
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 dl 1.5 import java.util.*;
13 dl 1.3 import java.io.*;
14 dl 1.1
15 dl 1.4 public class ReentrantLockTest extends JSR166TestCase {
16 dl 1.1 public static void main(String[] args) {
17 jsr166 1.30 junit.textui.TestRunner.run (suite());
18 dl 1.1 }
19     public static Test suite() {
20 jsr166 1.30 return new TestSuite(ReentrantLockTest.class);
21 dl 1.1 }
22    
23 dl 1.6 /**
24     * A runnable calling lockInterruptibly
25     */
26 jsr166 1.29 class InterruptibleLockRunnable extends CheckedRunnable {
27 dl 1.5 final ReentrantLock lock;
28     InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
29 jsr166 1.29 public void realRun() throws InterruptedException {
30     lock.lockInterruptibly();
31 dl 1.5 }
32     }
33    
34 dl 1.6
35     /**
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.26 public Collection<Thread> getQueuedThreads() {
53     return super.getQueuedThreads();
54 dl 1.5 }
55 jsr166 1.26 public Collection<Thread> getWaitingThreads(Condition c) {
56     return super.getWaitingThreads(c);
57 dl 1.13 }
58    
59 dl 1.5
60     }
61    
62 dl 1.8 /**
63 dl 1.9 * Constructor sets given fairness
64     */
65 jsr166 1.26 public void testConstructor() {
66 jsr166 1.29 assertFalse(new ReentrantLock().isFair());
67     assertFalse(new ReentrantLock(false).isFair());
68     assertTrue(new ReentrantLock(true).isFair());
69 dl 1.9 }
70    
71     /**
72 dl 1.6 * locking an unlocked lock succeeds
73     */
74 jsr166 1.26 public void testLock() {
75 jsr166 1.30 ReentrantLock rl = new ReentrantLock();
76 dl 1.6 rl.lock();
77     assertTrue(rl.isLocked());
78     rl.unlock();
79 jsr166 1.29 assertFalse(rl.isLocked());
80 dl 1.6 }
81    
82 dl 1.8 /**
83 dl 1.6 * locking an unlocked fair lock succeeds
84     */
85 jsr166 1.26 public void testFairLock() {
86 jsr166 1.30 ReentrantLock rl = new ReentrantLock(true);
87 dl 1.6 rl.lock();
88     assertTrue(rl.isLocked());
89     rl.unlock();
90     }
91    
92 dl 1.8 /**
93 dl 1.5 * Unlocking an unlocked lock throws IllegalMonitorStateException
94 dl 1.1 */
95 jsr166 1.26 public void testUnlock_IllegalMonitorStateException() {
96 jsr166 1.30 ReentrantLock rl = new ReentrantLock();
97     try {
98     rl.unlock();
99     shouldThrow();
100     } catch (IllegalMonitorStateException success) {}
101 dl 1.5 }
102 dl 1.1
103 dl 1.8 /**
104 dl 1.15 * tryLock on an unlocked lock succeeds
105 dl 1.1 */
106 jsr166 1.26 public void testTryLock() {
107 jsr166 1.30 ReentrantLock rl = new ReentrantLock();
108 dl 1.6 assertTrue(rl.tryLock());
109     assertTrue(rl.isLocked());
110     rl.unlock();
111     }
112    
113 dl 1.1
114 dl 1.8 /**
115 dl 1.13 * hasQueuedThreads reports whether there are waiting threads
116     */
117 jsr166 1.29 public void testhasQueuedThreads() throws InterruptedException {
118 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
119 dl 1.13 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
120     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
121 jsr166 1.29 assertFalse(lock.hasQueuedThreads());
122     lock.lock();
123     t1.start();
124     Thread.sleep(SHORT_DELAY_MS);
125     assertTrue(lock.hasQueuedThreads());
126     t2.start();
127     Thread.sleep(SHORT_DELAY_MS);
128     assertTrue(lock.hasQueuedThreads());
129     t1.interrupt();
130     Thread.sleep(SHORT_DELAY_MS);
131     assertTrue(lock.hasQueuedThreads());
132     lock.unlock();
133     Thread.sleep(SHORT_DELAY_MS);
134     assertFalse(lock.hasQueuedThreads());
135     t1.join();
136     t2.join();
137 jsr166 1.26 }
138 dl 1.13
139     /**
140 dl 1.7 * getQueueLength reports number of waiting threads
141 dl 1.1 */
142 jsr166 1.29 public void testGetQueueLength() throws InterruptedException {
143 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
144 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
145     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
146 jsr166 1.29 assertEquals(0, lock.getQueueLength());
147     lock.lock();
148     t1.start();
149     Thread.sleep(SHORT_DELAY_MS);
150     assertEquals(1, lock.getQueueLength());
151     t2.start();
152     Thread.sleep(SHORT_DELAY_MS);
153     assertEquals(2, lock.getQueueLength());
154     t1.interrupt();
155     Thread.sleep(SHORT_DELAY_MS);
156     assertEquals(1, lock.getQueueLength());
157     lock.unlock();
158     Thread.sleep(SHORT_DELAY_MS);
159     assertEquals(0, lock.getQueueLength());
160     t1.join();
161     t2.join();
162 jsr166 1.26 }
163 dl 1.5
164 dl 1.8 /**
165 dl 1.16 * getQueueLength reports number of waiting threads
166     */
167 jsr166 1.29 public void testGetQueueLength_fair() throws InterruptedException {
168 jsr166 1.30 final ReentrantLock lock = new ReentrantLock(true);
169 dl 1.16 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
170     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
171 jsr166 1.29 assertEquals(0, lock.getQueueLength());
172     lock.lock();
173     t1.start();
174     Thread.sleep(SHORT_DELAY_MS);
175     assertEquals(1, lock.getQueueLength());
176     t2.start();
177     Thread.sleep(SHORT_DELAY_MS);
178     assertEquals(2, lock.getQueueLength());
179     t1.interrupt();
180     Thread.sleep(SHORT_DELAY_MS);
181     assertEquals(1, lock.getQueueLength());
182     lock.unlock();
183     Thread.sleep(SHORT_DELAY_MS);
184     assertEquals(0, lock.getQueueLength());
185     t1.join();
186     t2.join();
187 jsr166 1.26 }
188 dl 1.16
189     /**
190 dl 1.19 * hasQueuedThread(null) throws NPE
191     */
192 jsr166 1.26 public void testHasQueuedThreadNPE() {
193 jsr166 1.30 final ReentrantLock sync = new ReentrantLock();
194 dl 1.19 try {
195     sync.hasQueuedThread(null);
196     shouldThrow();
197 jsr166 1.29 } catch (NullPointerException success) {}
198 dl 1.19 }
199    
200     /**
201     * hasQueuedThread reports whether a thread is queued.
202     */
203 jsr166 1.29 public void testHasQueuedThread() throws InterruptedException {
204 jsr166 1.30 final ReentrantLock sync = new ReentrantLock();
205 dl 1.19 Thread t1 = new Thread(new InterruptedLockRunnable(sync));
206     Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
207 jsr166 1.29 assertFalse(sync.hasQueuedThread(t1));
208     assertFalse(sync.hasQueuedThread(t2));
209     sync.lock();
210     t1.start();
211     Thread.sleep(SHORT_DELAY_MS);
212     assertTrue(sync.hasQueuedThread(t1));
213     t2.start();
214     Thread.sleep(SHORT_DELAY_MS);
215     assertTrue(sync.hasQueuedThread(t1));
216     assertTrue(sync.hasQueuedThread(t2));
217     t1.interrupt();
218     Thread.sleep(SHORT_DELAY_MS);
219     assertFalse(sync.hasQueuedThread(t1));
220     assertTrue(sync.hasQueuedThread(t2));
221     sync.unlock();
222     Thread.sleep(SHORT_DELAY_MS);
223     assertFalse(sync.hasQueuedThread(t1));
224     Thread.sleep(SHORT_DELAY_MS);
225     assertFalse(sync.hasQueuedThread(t2));
226     t1.join();
227     t2.join();
228 jsr166 1.26 }
229 dl 1.19
230    
231     /**
232 dl 1.5 * getQueuedThreads includes waiting threads
233     */
234 jsr166 1.29 public void testGetQueuedThreads() throws InterruptedException {
235 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
236 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
237     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
238 jsr166 1.29 assertTrue(lock.getQueuedThreads().isEmpty());
239     lock.lock();
240     assertTrue(lock.getQueuedThreads().isEmpty());
241     t1.start();
242     Thread.sleep(SHORT_DELAY_MS);
243     assertTrue(lock.getQueuedThreads().contains(t1));
244     t2.start();
245     Thread.sleep(SHORT_DELAY_MS);
246     assertTrue(lock.getQueuedThreads().contains(t1));
247     assertTrue(lock.getQueuedThreads().contains(t2));
248     t1.interrupt();
249     Thread.sleep(SHORT_DELAY_MS);
250     assertFalse(lock.getQueuedThreads().contains(t1));
251     assertTrue(lock.getQueuedThreads().contains(t2));
252     lock.unlock();
253     Thread.sleep(SHORT_DELAY_MS);
254     assertTrue(lock.getQueuedThreads().isEmpty());
255     t1.join();
256     t2.join();
257 jsr166 1.26 }
258 dl 1.5
259 dl 1.1
260 dl 1.8 /**
261 dl 1.15 * timed tryLock is interruptible.
262 dl 1.5 */
263 jsr166 1.29 public void testInterruptedException2() throws InterruptedException {
264 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
265     lock.lock();
266     Thread t = new Thread(new CheckedInterruptedRunnable() {
267 jsr166 1.29 public void realRun() throws InterruptedException {
268     lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
269     }});
270    
271     t.start();
272     t.interrupt();
273     t.join();
274 dl 1.1 }
275    
276 dl 1.3
277 dl 1.5 /**
278 dl 1.15 * TryLock on a locked lock fails
279 dl 1.5 */
280 jsr166 1.29 public void testTryLockWhenLocked() throws InterruptedException {
281 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
282     lock.lock();
283     Thread t = new Thread(new CheckedRunnable() {
284 jsr166 1.29 public void realRun() {
285     threadAssertFalse(lock.tryLock());
286     }});
287    
288     t.start();
289     t.join();
290     lock.unlock();
291 jsr166 1.26 }
292 dl 1.3
293 dl 1.5 /**
294 dl 1.15 * Timed tryLock on a locked lock times out
295 dl 1.5 */
296 jsr166 1.29 public void testTryLock_Timeout() throws InterruptedException {
297 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
298     lock.lock();
299     Thread t = new Thread(new CheckedRunnable() {
300 jsr166 1.29 public void realRun() throws InterruptedException {
301     threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
302     }});
303    
304     t.start();
305     t.join();
306     lock.unlock();
307 jsr166 1.26 }
308    
309 dl 1.5 /**
310     * getHoldCount returns number of recursive holds
311     */
312 dl 1.1 public void testGetHoldCount() {
313 jsr166 1.30 ReentrantLock lock = new ReentrantLock();
314     for (int i = 1; i <= SIZE; i++) {
315     lock.lock();
316     assertEquals(i, lock.getHoldCount());
317     }
318     for (int i = SIZE; i > 0; i--) {
319     lock.unlock();
320     assertEquals(i-1, lock.getHoldCount());
321     }
322 dl 1.1 }
323 jsr166 1.26
324    
325 dl 1.5 /**
326     * isLocked is true when locked and false when not
327     */
328 jsr166 1.29 public void testIsLocked() throws InterruptedException {
329 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
330     lock.lock();
331     assertTrue(lock.isLocked());
332     lock.unlock();
333     assertFalse(lock.isLocked());
334     Thread t = new Thread(new CheckedRunnable() {
335 jsr166 1.29 public void realRun() throws InterruptedException {
336     lock.lock();
337     Thread.sleep(SMALL_DELAY_MS);
338     lock.unlock();
339     }});
340    
341     t.start();
342     Thread.sleep(SHORT_DELAY_MS);
343     assertTrue(lock.isLocked());
344     t.join();
345     assertFalse(lock.isLocked());
346 dl 1.1 }
347    
348    
349 dl 1.8 /**
350 dl 1.6 * lockInterruptibly is interruptible.
351     */
352 jsr166 1.29 public void testLockInterruptibly1() throws InterruptedException {
353 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
354     lock.lock();
355     Thread t = new Thread(new InterruptedLockRunnable(lock));
356 jsr166 1.29 t.start();
357     Thread.sleep(SHORT_DELAY_MS);
358     t.interrupt();
359     Thread.sleep(SHORT_DELAY_MS);
360     lock.unlock();
361     t.join();
362 jsr166 1.26 }
363 dl 1.6
364 dl 1.5 /**
365     * lockInterruptibly succeeds when unlocked, else is interruptible
366     */
367 jsr166 1.29 public void testLockInterruptibly2() throws InterruptedException {
368 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
369 jsr166 1.29 lock.lockInterruptibly();
370 jsr166 1.30 Thread t = new Thread(new InterruptedLockRunnable(lock));
371 jsr166 1.29 t.start();
372     t.interrupt();
373     assertTrue(lock.isLocked());
374     assertTrue(lock.isHeldByCurrentThread());
375     t.join();
376 dl 1.1 }
377 dl 1.2
378 dl 1.6 /**
379     * Calling await without holding lock throws IllegalMonitorStateException
380     */
381 jsr166 1.29 public void testAwait_IllegalMonitor() throws InterruptedException {
382 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
383 dl 1.2 final Condition c = lock.newCondition();
384     try {
385     c.await();
386 dl 1.6 shouldThrow();
387 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
388 dl 1.2 }
389    
390 dl 1.6 /**
391     * Calling signal without holding lock throws IllegalMonitorStateException
392     */
393 dl 1.2 public void testSignal_IllegalMonitor() {
394 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
395 dl 1.2 final Condition c = lock.newCondition();
396     try {
397     c.signal();
398 dl 1.6 shouldThrow();
399 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
400 dl 1.2 }
401    
402 dl 1.6 /**
403     * awaitNanos without a signal times out
404     */
405 jsr166 1.29 public void testAwaitNanos_Timeout() throws InterruptedException {
406 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
407 dl 1.2 final Condition c = lock.newCondition();
408 jsr166 1.29 lock.lock();
409     long t = c.awaitNanos(100);
410     assertTrue(t <= 0);
411     lock.unlock();
412 dl 1.2 }
413    
414 dl 1.6 /**
415     * timed await without a signal times out
416     */
417 jsr166 1.29 public void testAwait_Timeout() throws InterruptedException {
418 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
419 dl 1.2 final Condition c = lock.newCondition();
420 jsr166 1.29 lock.lock();
421     assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
422     lock.unlock();
423 dl 1.2 }
424    
425 dl 1.6 /**
426     * awaitUntil without a signal times out
427     */
428 jsr166 1.29 public void testAwaitUntil_Timeout() throws InterruptedException {
429 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
430 dl 1.2 final Condition c = lock.newCondition();
431 jsr166 1.29 lock.lock();
432     java.util.Date d = new java.util.Date();
433     assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
434     lock.unlock();
435 dl 1.2 }
436    
437 dl 1.6 /**
438     * await returns when signalled
439     */
440 jsr166 1.29 public void testAwait() throws InterruptedException {
441 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
442 dl 1.13 final Condition c = lock.newCondition();
443 jsr166 1.30 Thread t = new Thread(new CheckedRunnable() {
444 jsr166 1.29 public void realRun() throws InterruptedException {
445     lock.lock();
446     c.await();
447     lock.unlock();
448     }});
449 dl 1.5
450 jsr166 1.29 t.start();
451     Thread.sleep(SHORT_DELAY_MS);
452     lock.lock();
453     c.signal();
454     lock.unlock();
455     t.join(SHORT_DELAY_MS);
456     assertFalse(t.isAlive());
457 dl 1.5 }
458    
459 dl 1.6 /**
460 dl 1.14 * hasWaiters throws NPE if null
461     */
462     public void testHasWaitersNPE() {
463 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
464 dl 1.14 try {
465     lock.hasWaiters(null);
466     shouldThrow();
467 jsr166 1.29 } catch (NullPointerException success) {}
468 dl 1.14 }
469    
470     /**
471     * getWaitQueueLength throws NPE if null
472     */
473     public void testGetWaitQueueLengthNPE() {
474 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
475 dl 1.14 try {
476     lock.getWaitQueueLength(null);
477     shouldThrow();
478 jsr166 1.29 } catch (NullPointerException success) {}
479 dl 1.14 }
480    
481    
482     /**
483     * getWaitingThreads throws NPE if null
484     */
485     public void testGetWaitingThreadsNPE() {
486 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
487 dl 1.14 try {
488     lock.getWaitingThreads(null);
489     shouldThrow();
490 jsr166 1.29 } catch (NullPointerException success) {}
491 dl 1.14 }
492    
493    
494     /**
495 dl 1.13 * hasWaiters throws IAE if not owned
496     */
497     public void testHasWaitersIAE() {
498 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
499 jsr166 1.29 final Condition c = lock.newCondition();
500 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
501 dl 1.13 try {
502     lock2.hasWaiters(c);
503     shouldThrow();
504 jsr166 1.29 } catch (IllegalArgumentException success) {}
505 dl 1.13 }
506    
507     /**
508     * hasWaiters throws IMSE if not locked
509     */
510     public void testHasWaitersIMSE() {
511 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
512 jsr166 1.29 final Condition c = lock.newCondition();
513 dl 1.13 try {
514     lock.hasWaiters(c);
515     shouldThrow();
516 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
517 dl 1.13 }
518    
519    
520     /**
521     * getWaitQueueLength throws IAE if not owned
522     */
523     public void testGetWaitQueueLengthIAE() {
524 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
525 jsr166 1.29 final Condition c = lock.newCondition();
526 jsr166 1.30 final ReentrantLock lock2 = new ReentrantLock();
527 dl 1.13 try {
528     lock2.getWaitQueueLength(c);
529     shouldThrow();
530 jsr166 1.29 } catch (IllegalArgumentException success) {}
531 dl 1.13 }
532    
533     /**
534     * getWaitQueueLength throws IMSE if not locked
535     */
536     public void testGetWaitQueueLengthIMSE() {
537 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
538 jsr166 1.29 final Condition c = lock.newCondition();
539 dl 1.13 try {
540     lock.getWaitQueueLength(c);
541     shouldThrow();
542 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
543 dl 1.13 }
544    
545    
546     /**
547     * getWaitingThreads throws IAE if not owned
548     */
549     public void testGetWaitingThreadsIAE() {
550 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
551 jsr166 1.29 final Condition c = lock.newCondition();
552 jsr166 1.30 final PublicReentrantLock lock2 = new PublicReentrantLock();
553 dl 1.13 try {
554     lock2.getWaitingThreads(c);
555     shouldThrow();
556 jsr166 1.29 } catch (IllegalArgumentException success) {}
557 dl 1.13 }
558    
559     /**
560     * getWaitingThreads throws IMSE if not locked
561     */
562     public void testGetWaitingThreadsIMSE() {
563 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
564 jsr166 1.29 final Condition c = lock.newCondition();
565 dl 1.13 try {
566     lock.getWaitingThreads(c);
567     shouldThrow();
568 jsr166 1.29 } catch (IllegalMonitorStateException success) {}
569 dl 1.13 }
570    
571    
572     /**
573 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
574     */
575 jsr166 1.29 public void testHasWaiters() throws InterruptedException {
576 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
577 dl 1.13 final Condition c = lock.newCondition();
578 jsr166 1.30 Thread t = new Thread(new CheckedRunnable() {
579 jsr166 1.29 public void realRun() throws InterruptedException {
580     lock.lock();
581     threadAssertFalse(lock.hasWaiters(c));
582     threadAssertEquals(0, lock.getWaitQueueLength(c));
583     c.await();
584     lock.unlock();
585     }});
586 dl 1.2
587 jsr166 1.29 t.start();
588     Thread.sleep(SHORT_DELAY_MS);
589     lock.lock();
590     assertTrue(lock.hasWaiters(c));
591     assertEquals(1, lock.getWaitQueueLength(c));
592     c.signal();
593     lock.unlock();
594     Thread.sleep(SHORT_DELAY_MS);
595     lock.lock();
596     assertFalse(lock.hasWaiters(c));
597     assertEquals(0, lock.getWaitQueueLength(c));
598     lock.unlock();
599     t.join(SHORT_DELAY_MS);
600     assertFalse(t.isAlive());
601 dl 1.5 }
602    
603 dl 1.6 /**
604     * getWaitQueueLength returns number of waiting threads
605     */
606 jsr166 1.29 public void testGetWaitQueueLength() throws InterruptedException {
607 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
608 dl 1.13 final Condition c = lock.newCondition();
609 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
610 jsr166 1.29 public void realRun() throws InterruptedException {
611     lock.lock();
612     threadAssertFalse(lock.hasWaiters(c));
613     threadAssertEquals(0, lock.getWaitQueueLength(c));
614     c.await();
615     lock.unlock();
616     }});
617 dl 1.13
618 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
619 jsr166 1.29 public void realRun() throws InterruptedException {
620     lock.lock();
621     threadAssertTrue(lock.hasWaiters(c));
622     threadAssertEquals(1, lock.getWaitQueueLength(c));
623     c.await();
624     lock.unlock();
625     }});
626    
627     t1.start();
628     Thread.sleep(SHORT_DELAY_MS);
629     t2.start();
630     Thread.sleep(SHORT_DELAY_MS);
631     lock.lock();
632     assertTrue(lock.hasWaiters(c));
633     assertEquals(2, lock.getWaitQueueLength(c));
634     c.signalAll();
635     lock.unlock();
636     Thread.sleep(SHORT_DELAY_MS);
637     lock.lock();
638     assertFalse(lock.hasWaiters(c));
639     assertEquals(0, lock.getWaitQueueLength(c));
640     lock.unlock();
641     t1.join(SHORT_DELAY_MS);
642     t2.join(SHORT_DELAY_MS);
643     assertFalse(t1.isAlive());
644     assertFalse(t2.isAlive());
645 dl 1.13 }
646    
647     /**
648     * getWaitingThreads returns only and all waiting threads
649     */
650 jsr166 1.29 public void testGetWaitingThreads() throws InterruptedException {
651 jsr166 1.30 final PublicReentrantLock lock = new PublicReentrantLock();
652 dl 1.13 final Condition c = lock.newCondition();
653 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
654 jsr166 1.29 public void realRun() throws InterruptedException {
655     lock.lock();
656     threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
657     c.await();
658     lock.unlock();
659     }});
660    
661 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
662 jsr166 1.29 public void realRun() throws InterruptedException {
663     lock.lock();
664     threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
665     c.await();
666     lock.unlock();
667     }});
668 dl 1.5
669 jsr166 1.29 lock.lock();
670     assertTrue(lock.getWaitingThreads(c).isEmpty());
671     lock.unlock();
672     t1.start();
673     Thread.sleep(SHORT_DELAY_MS);
674     t2.start();
675     Thread.sleep(SHORT_DELAY_MS);
676     lock.lock();
677     assertTrue(lock.hasWaiters(c));
678     assertTrue(lock.getWaitingThreads(c).contains(t1));
679     assertTrue(lock.getWaitingThreads(c).contains(t2));
680     c.signalAll();
681     lock.unlock();
682     Thread.sleep(SHORT_DELAY_MS);
683     lock.lock();
684     assertFalse(lock.hasWaiters(c));
685     assertTrue(lock.getWaitingThreads(c).isEmpty());
686     lock.unlock();
687     t1.join(SHORT_DELAY_MS);
688     t2.join(SHORT_DELAY_MS);
689     assertFalse(t1.isAlive());
690     assertFalse(t2.isAlive());
691 dl 1.2 }
692 dl 1.13
693 dl 1.22 /** A helper class for uninterruptible wait tests */
694 jsr166 1.29 class UninterruptibleThread extends Thread {
695 dl 1.22 private ReentrantLock lock;
696     private Condition c;
697 jsr166 1.26
698 dl 1.22 public volatile boolean canAwake = false;
699     public volatile boolean interrupted = false;
700     public volatile boolean lockStarted = false;
701 jsr166 1.26
702 jsr166 1.29 public UninterruptibleThread(ReentrantLock lock, Condition c) {
703 dl 1.22 this.lock = lock;
704     this.c = c;
705     }
706 jsr166 1.26
707 dl 1.22 public synchronized void run() {
708     lock.lock();
709     lockStarted = true;
710 jsr166 1.26
711 dl 1.22 while (!canAwake) {
712     c.awaitUninterruptibly();
713     }
714 jsr166 1.26
715 dl 1.22 interrupted = isInterrupted();
716     lock.unlock();
717     }
718     }
719 dl 1.2
720 dl 1.6 /**
721     * awaitUninterruptibly doesn't abort on interrupt
722     */
723 jsr166 1.29 public void testAwaitUninterruptibly() throws InterruptedException {
724 dl 1.22 final ReentrantLock lock = new ReentrantLock();
725 dl 1.2 final Condition c = lock.newCondition();
726 jsr166 1.29 UninterruptibleThread thread = new UninterruptibleThread(lock, c);
727 dl 1.2
728 jsr166 1.29 thread.start();
729 dl 1.22
730 jsr166 1.29 while (!thread.lockStarted) {
731     Thread.sleep(100);
732     }
733 dl 1.22
734 jsr166 1.29 lock.lock();
735     try {
736     thread.interrupt();
737     thread.canAwake = true;
738     c.signal();
739     } finally {
740     lock.unlock();
741     }
742 dl 1.22
743 jsr166 1.29 thread.join();
744     assertTrue(thread.interrupted);
745     assertFalse(thread.isAlive());
746 dl 1.2 }
747    
748 dl 1.6 /**
749     * await is interruptible
750     */
751 jsr166 1.29 public void testAwait_Interrupt() throws InterruptedException {
752 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
753 dl 1.2 final Condition c = lock.newCondition();
754 jsr166 1.30 Thread t = new Thread(new CheckedInterruptedRunnable() {
755     public void realRun() throws InterruptedException {
756 jsr166 1.29 lock.lock();
757     c.await();
758     }});
759    
760     t.start();
761     Thread.sleep(SHORT_DELAY_MS);
762     t.interrupt();
763     t.join(SHORT_DELAY_MS);
764     assertFalse(t.isAlive());
765 dl 1.2 }
766    
767 dl 1.6 /**
768     * awaitNanos is interruptible
769     */
770 jsr166 1.29 public void testAwaitNanos_Interrupt() throws InterruptedException {
771 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
772 dl 1.2 final Condition c = lock.newCondition();
773 jsr166 1.30 Thread t = new Thread(new CheckedInterruptedRunnable() {
774     public void realRun() throws InterruptedException {
775 jsr166 1.29 lock.lock();
776     c.awaitNanos(1000 * 1000 * 1000); // 1 sec
777     }});
778    
779     t.start();
780     Thread.sleep(SHORT_DELAY_MS);
781     t.interrupt();
782     t.join(SHORT_DELAY_MS);
783     assertFalse(t.isAlive());
784 dl 1.2 }
785    
786 dl 1.6 /**
787     * awaitUntil is interruptible
788     */
789 jsr166 1.29 public void testAwaitUntil_Interrupt() throws InterruptedException {
790 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
791 dl 1.2 final Condition c = lock.newCondition();
792 jsr166 1.30 Thread t = new Thread(new CheckedInterruptedRunnable() {
793     public void realRun() throws InterruptedException {
794 jsr166 1.29 lock.lock();
795     java.util.Date d = new java.util.Date();
796     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
797     }});
798    
799     t.start();
800     Thread.sleep(SHORT_DELAY_MS);
801     t.interrupt();
802     t.join(SHORT_DELAY_MS);
803     assertFalse(t.isAlive());
804 dl 1.2 }
805    
806 dl 1.6 /**
807     * signalAll wakes up all threads
808     */
809 jsr166 1.29 public void testSignalAll() throws InterruptedException {
810 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
811 dl 1.2 final Condition c = lock.newCondition();
812 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
813 jsr166 1.29 public void realRun() throws InterruptedException {
814     lock.lock();
815     c.await();
816     lock.unlock();
817     }});
818    
819 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
820 jsr166 1.29 public void realRun() throws InterruptedException {
821     lock.lock();
822     c.await();
823     lock.unlock();
824     }});
825 dl 1.2
826 jsr166 1.29 t1.start();
827     t2.start();
828     Thread.sleep(SHORT_DELAY_MS);
829     lock.lock();
830     c.signalAll();
831     lock.unlock();
832     t1.join(SHORT_DELAY_MS);
833     t2.join(SHORT_DELAY_MS);
834     assertFalse(t1.isAlive());
835     assertFalse(t2.isAlive());
836 dl 1.3 }
837    
838 dl 1.6 /**
839 dl 1.23 * await after multiple reentrant locking preserves lock count
840     */
841 jsr166 1.29 public void testAwaitLockCount() throws InterruptedException {
842 jsr166 1.30 final ReentrantLock lock = new ReentrantLock();
843 dl 1.23 final Condition c = lock.newCondition();
844 jsr166 1.30 Thread t1 = new Thread(new CheckedRunnable() {
845 jsr166 1.29 public void realRun() throws InterruptedException {
846     lock.lock();
847     threadAssertEquals(1, lock.getHoldCount());
848     c.await();
849     threadAssertEquals(1, lock.getHoldCount());
850     lock.unlock();
851     }});
852 dl 1.23
853 jsr166 1.30 Thread t2 = new Thread(new CheckedRunnable() {
854 jsr166 1.29 public void realRun() throws InterruptedException {
855     lock.lock();
856     lock.lock();
857     threadAssertEquals(2, lock.getHoldCount());
858     c.await();
859     threadAssertEquals(2, lock.getHoldCount());
860     lock.unlock();
861     lock.unlock();
862     }});
863    
864     t1.start();
865     t2.start();
866     Thread.sleep(SHORT_DELAY_MS);
867     lock.lock();
868     c.signalAll();
869     lock.unlock();
870     t1.join(SHORT_DELAY_MS);
871     t2.join(SHORT_DELAY_MS);
872     assertFalse(t1.isAlive());
873     assertFalse(t2.isAlive());
874 dl 1.23 }
875    
876     /**
877 dl 1.6 * A serialized lock deserializes as unlocked
878     */
879 jsr166 1.29 public void testSerialization() throws Exception {
880 dl 1.3 ReentrantLock l = new ReentrantLock();
881     l.lock();
882     l.unlock();
883    
884 jsr166 1.29 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
885     ObjectOutputStream out =
886     new ObjectOutputStream(new BufferedOutputStream(bout));
887     out.writeObject(l);
888     out.close();
889    
890     ByteArrayInputStream bin =
891     new ByteArrayInputStream(bout.toByteArray());
892     ObjectInputStream in =
893     new ObjectInputStream(new BufferedInputStream(bin));
894     ReentrantLock r = (ReentrantLock) in.readObject();
895     r.lock();
896     r.unlock();
897 dl 1.2 }
898 dl 1.1
899 dl 1.18 /**
900     * toString indicates current lock state
901     */
902     public void testToString() {
903     ReentrantLock lock = new ReentrantLock();
904     String us = lock.toString();
905     assertTrue(us.indexOf("Unlocked") >= 0);
906     lock.lock();
907     String ls = lock.toString();
908     assertTrue(ls.indexOf("Locked") >= 0);
909     }
910    
911 dl 1.1 }