ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.16
Committed: Wed Jan 7 01:02:17 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.15: +29 -0 lines
Log Message:
Added and fised testcases

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