ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
Revision: 1.26
Committed: Mon Nov 2 20:28:31 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +70 -70 lines
Log Message:
whitespace

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.26 junit.textui.TestRunner.run (suite());
18 dl 1.1 }
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 jsr166 1.26 public Collection<Thread> getQueuedThreads() {
58     return super.getQueuedThreads();
59 dl 1.5 }
60 jsr166 1.26 public Collection<Thread> getWaitingThreads(Condition c) {
61     return super.getWaitingThreads(c);
62 dl 1.13 }
63    
64 dl 1.5
65     }
66    
67 dl 1.8 /**
68 dl 1.9 * Constructor sets given fairness
69     */
70 jsr166 1.26 public void testConstructor() {
71 dl 1.9 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 jsr166 1.26 public void testLock() {
81 dl 1.6 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 jsr166 1.26 public void testFairLock() {
91 dl 1.6 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 jsr166 1.26 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 jsr166 1.26 public void testTryLock() {
113 dl 1.6 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 jsr166 1.26 public void testhasQueuedThreads() {
124 dl 1.13 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 jsr166 1.26 }
148 dl 1.13
149     /**
150 dl 1.7 * getQueueLength reports number of waiting threads
151 dl 1.1 */
152 jsr166 1.26 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 jsr166 1.26 }
177 dl 1.5
178 dl 1.8 /**
179 dl 1.16 * getQueueLength reports number of waiting threads
180     */
181 jsr166 1.26 public void testGetQueueLength_fair() {
182 dl 1.16 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 jsr166 1.26 }
206 dl 1.16
207     /**
208 dl 1.19 * hasQueuedThread(null) throws NPE
209     */
210 jsr166 1.26 public void testHasQueuedThreadNPE() {
211 dl 1.19 final ReentrantLock sync = new ReentrantLock();
212     try {
213     sync.hasQueuedThread(null);
214     shouldThrow();
215     } catch (NullPointerException success) {
216     }
217     }
218    
219     /**
220     * hasQueuedThread reports whether a thread is queued.
221     */
222 jsr166 1.26 public void testHasQueuedThread() {
223 dl 1.19 final ReentrantLock sync = new ReentrantLock();
224     Thread t1 = new Thread(new InterruptedLockRunnable(sync));
225     Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
226     try {
227     assertFalse(sync.hasQueuedThread(t1));
228     assertFalse(sync.hasQueuedThread(t2));
229     sync.lock();
230     t1.start();
231     Thread.sleep(SHORT_DELAY_MS);
232     assertTrue(sync.hasQueuedThread(t1));
233     t2.start();
234     Thread.sleep(SHORT_DELAY_MS);
235     assertTrue(sync.hasQueuedThread(t1));
236     assertTrue(sync.hasQueuedThread(t2));
237     t1.interrupt();
238     Thread.sleep(SHORT_DELAY_MS);
239     assertFalse(sync.hasQueuedThread(t1));
240     assertTrue(sync.hasQueuedThread(t2));
241     sync.unlock();
242     Thread.sleep(SHORT_DELAY_MS);
243     assertFalse(sync.hasQueuedThread(t1));
244     Thread.sleep(SHORT_DELAY_MS);
245     assertFalse(sync.hasQueuedThread(t2));
246     t1.join();
247     t2.join();
248     } catch(Exception e){
249     unexpectedException();
250     }
251 jsr166 1.26 }
252 dl 1.19
253    
254     /**
255 dl 1.5 * getQueuedThreads includes waiting threads
256     */
257 jsr166 1.26 public void testGetQueuedThreads() {
258 dl 1.6 final PublicReentrantLock lock = new PublicReentrantLock();
259 dl 1.5 Thread t1 = new Thread(new InterruptedLockRunnable(lock));
260     Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
261     try {
262     assertTrue(lock.getQueuedThreads().isEmpty());
263     lock.lock();
264     assertTrue(lock.getQueuedThreads().isEmpty());
265     t1.start();
266     Thread.sleep(SHORT_DELAY_MS);
267     assertTrue(lock.getQueuedThreads().contains(t1));
268     t2.start();
269     Thread.sleep(SHORT_DELAY_MS);
270     assertTrue(lock.getQueuedThreads().contains(t1));
271     assertTrue(lock.getQueuedThreads().contains(t2));
272     t1.interrupt();
273     Thread.sleep(SHORT_DELAY_MS);
274     assertFalse(lock.getQueuedThreads().contains(t1));
275     assertTrue(lock.getQueuedThreads().contains(t2));
276     lock.unlock();
277     Thread.sleep(SHORT_DELAY_MS);
278     assertTrue(lock.getQueuedThreads().isEmpty());
279     t1.join();
280     t2.join();
281     } catch(Exception e){
282 dl 1.6 unexpectedException();
283 dl 1.5 }
284 jsr166 1.26 }
285 dl 1.5
286 dl 1.1
287 dl 1.8 /**
288 dl 1.15 * timed tryLock is interruptible.
289 dl 1.5 */
290 jsr166 1.26 public void testInterruptedException2() {
291 dl 1.1 final ReentrantLock lock = new ReentrantLock();
292     lock.lock();
293     Thread t = new Thread(new Runnable() {
294 dl 1.6 public void run() {
295     try {
296 dl 1.4 lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
297 dl 1.6 threadShouldThrow();
298 dl 1.3 } catch(InterruptedException success){}
299 dl 1.1 }
300     });
301 dl 1.4 try {
302     t.start();
303     t.interrupt();
304     } catch(Exception e){
305 dl 1.6 unexpectedException();
306 dl 1.4 }
307 dl 1.1 }
308    
309 dl 1.3
310 dl 1.5 /**
311 dl 1.15 * TryLock on a locked lock fails
312 dl 1.5 */
313 jsr166 1.26 public void testTryLockWhenLocked() {
314 dl 1.3 final ReentrantLock lock = new ReentrantLock();
315     lock.lock();
316     Thread t = new Thread(new Runnable() {
317 dl 1.6 public void run() {
318 dl 1.4 threadAssertFalse(lock.tryLock());
319 dl 1.3 }
320     });
321     try {
322     t.start();
323     t.join();
324     lock.unlock();
325     } catch(Exception e){
326 dl 1.6 unexpectedException();
327 dl 1.3 }
328 jsr166 1.26 }
329 dl 1.3
330 dl 1.5 /**
331 dl 1.15 * Timed tryLock on a locked lock times out
332 dl 1.5 */
333 jsr166 1.26 public void testTryLock_Timeout() {
334 dl 1.3 final ReentrantLock lock = new ReentrantLock();
335     lock.lock();
336     Thread t = new Thread(new Runnable() {
337 dl 1.6 public void run() {
338 dl 1.3 try {
339 dl 1.4 threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
340 dl 1.3 } catch (Exception ex) {
341 dl 1.6 threadUnexpectedException();
342 dl 1.3 }
343     }
344     });
345     try {
346     t.start();
347     t.join();
348     lock.unlock();
349     } catch(Exception e){
350 dl 1.6 unexpectedException();
351 dl 1.3 }
352 jsr166 1.26 }
353    
354 dl 1.5 /**
355     * getHoldCount returns number of recursive holds
356     */
357 dl 1.1 public void testGetHoldCount() {
358     ReentrantLock lock = new ReentrantLock();
359 dl 1.6 for(int i = 1; i <= SIZE; i++) {
360 dl 1.1 lock.lock();
361     assertEquals(i,lock.getHoldCount());
362     }
363 dl 1.6 for(int i = SIZE; i > 0; i--) {
364 dl 1.1 lock.unlock();
365     assertEquals(i-1,lock.getHoldCount());
366     }
367     }
368 jsr166 1.26
369    
370 dl 1.5 /**
371     * isLocked is true when locked and false when not
372     */
373 dl 1.1 public void testIsLocked() {
374     final ReentrantLock lock = new ReentrantLock();
375     lock.lock();
376     assertTrue(lock.isLocked());
377     lock.unlock();
378     assertFalse(lock.isLocked());
379 jsr166 1.26 Thread t = new Thread(new Runnable() {
380 dl 1.1 public void run() {
381     lock.lock();
382     try {
383 dl 1.4 Thread.sleep(SMALL_DELAY_MS);
384 dl 1.1 }
385 dl 1.4 catch(Exception e) {
386 dl 1.6 threadUnexpectedException();
387 dl 1.4 }
388 dl 1.1 lock.unlock();
389     }
390     });
391 dl 1.6 try {
392 dl 1.1 t.start();
393     Thread.sleep(SHORT_DELAY_MS);
394     assertTrue(lock.isLocked());
395     t.join();
396     assertFalse(lock.isLocked());
397     } catch(Exception e){
398 dl 1.6 unexpectedException();
399 dl 1.1 }
400     }
401    
402    
403 dl 1.8 /**
404 dl 1.6 * lockInterruptibly is interruptible.
405     */
406 jsr166 1.26 public void testLockInterruptibly1() {
407 dl 1.6 final ReentrantLock lock = new ReentrantLock();
408     lock.lock();
409     Thread t = new Thread(new InterruptedLockRunnable(lock));
410     try {
411     t.start();
412 dl 1.24 Thread.sleep(SHORT_DELAY_MS);
413 dl 1.6 t.interrupt();
414 dl 1.25 Thread.sleep(SHORT_DELAY_MS);
415 dl 1.6 lock.unlock();
416     t.join();
417     } catch(Exception e){
418     unexpectedException();
419     }
420 jsr166 1.26 }
421 dl 1.6
422 dl 1.5 /**
423     * lockInterruptibly succeeds when unlocked, else is interruptible
424     */
425 dl 1.6 public void testLockInterruptibly2() {
426 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
427 dl 1.3 try {
428     lock.lockInterruptibly();
429     } catch(Exception e) {
430 dl 1.6 unexpectedException();
431 dl 1.3 }
432 dl 1.5 Thread t = new Thread(new InterruptedLockRunnable(lock));
433 dl 1.3 try {
434     t.start();
435     t.interrupt();
436     assertTrue(lock.isLocked());
437     assertTrue(lock.isHeldByCurrentThread());
438     t.join();
439     } catch(Exception e){
440 dl 1.6 unexpectedException();
441 dl 1.3 }
442 dl 1.1 }
443 dl 1.2
444 dl 1.6 /**
445     * Calling await without holding lock throws IllegalMonitorStateException
446     */
447 dl 1.2 public void testAwait_IllegalMonitor() {
448 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
449 dl 1.2 final Condition c = lock.newCondition();
450     try {
451     c.await();
452 dl 1.6 shouldThrow();
453 dl 1.2 }
454     catch (IllegalMonitorStateException success) {
455     }
456     catch (Exception ex) {
457 dl 1.6 unexpectedException();
458 dl 1.2 }
459     }
460    
461 dl 1.6 /**
462     * Calling signal without holding lock throws IllegalMonitorStateException
463     */
464 dl 1.2 public void testSignal_IllegalMonitor() {
465 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
466 dl 1.2 final Condition c = lock.newCondition();
467     try {
468     c.signal();
469 dl 1.6 shouldThrow();
470 dl 1.2 }
471     catch (IllegalMonitorStateException success) {
472     }
473     catch (Exception ex) {
474 dl 1.6 unexpectedException();
475 dl 1.2 }
476     }
477    
478 dl 1.6 /**
479     * awaitNanos without a signal times out
480     */
481 dl 1.2 public void testAwaitNanos_Timeout() {
482 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
483 dl 1.2 final Condition c = lock.newCondition();
484     try {
485     lock.lock();
486     long t = c.awaitNanos(100);
487     assertTrue(t <= 0);
488     lock.unlock();
489     }
490     catch (Exception ex) {
491 dl 1.6 unexpectedException();
492 dl 1.2 }
493     }
494    
495 dl 1.6 /**
496     * timed await without a signal times out
497     */
498 dl 1.2 public void testAwait_Timeout() {
499 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
500 dl 1.2 final Condition c = lock.newCondition();
501     try {
502     lock.lock();
503 dl 1.20 c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
504 dl 1.2 lock.unlock();
505     }
506     catch (Exception ex) {
507 dl 1.6 unexpectedException();
508 dl 1.2 }
509     }
510    
511 dl 1.6 /**
512     * awaitUntil without a signal times out
513     */
514 dl 1.2 public void testAwaitUntil_Timeout() {
515 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
516 dl 1.2 final Condition c = lock.newCondition();
517     try {
518     lock.lock();
519     java.util.Date d = new java.util.Date();
520 dl 1.20 c.awaitUntil(new java.util.Date(d.getTime() + 10));
521 dl 1.2 lock.unlock();
522     }
523     catch (Exception ex) {
524 dl 1.6 unexpectedException();
525 dl 1.2 }
526     }
527    
528 dl 1.6 /**
529     * await returns when signalled
530     */
531 dl 1.2 public void testAwait() {
532 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
533 dl 1.13 final Condition c = lock.newCondition();
534 jsr166 1.26 Thread t = new Thread(new Runnable() {
535 dl 1.5 public void run() {
536     try {
537     lock.lock();
538     c.await();
539     lock.unlock();
540     }
541     catch(InterruptedException e) {
542 dl 1.6 threadUnexpectedException();
543 dl 1.5 }
544     }
545     });
546    
547     try {
548     t.start();
549     Thread.sleep(SHORT_DELAY_MS);
550     lock.lock();
551     c.signal();
552     lock.unlock();
553     t.join(SHORT_DELAY_MS);
554     assertFalse(t.isAlive());
555     }
556     catch (Exception ex) {
557 dl 1.6 unexpectedException();
558 dl 1.5 }
559     }
560    
561 dl 1.6 /**
562 dl 1.14 * hasWaiters throws NPE if null
563     */
564     public void testHasWaitersNPE() {
565     final ReentrantLock lock = new ReentrantLock();
566     try {
567     lock.hasWaiters(null);
568     shouldThrow();
569     } catch (NullPointerException success) {
570     } catch (Exception ex) {
571     unexpectedException();
572     }
573     }
574    
575     /**
576     * getWaitQueueLength throws NPE if null
577     */
578     public void testGetWaitQueueLengthNPE() {
579     final ReentrantLock lock = new ReentrantLock();
580     try {
581     lock.getWaitQueueLength(null);
582     shouldThrow();
583     } catch (NullPointerException success) {
584     } catch (Exception ex) {
585     unexpectedException();
586     }
587     }
588    
589    
590     /**
591     * getWaitingThreads throws NPE if null
592     */
593     public void testGetWaitingThreadsNPE() {
594     final PublicReentrantLock lock = new PublicReentrantLock();
595     try {
596     lock.getWaitingThreads(null);
597     shouldThrow();
598     } catch (NullPointerException success) {
599     } catch (Exception ex) {
600     unexpectedException();
601     }
602     }
603    
604    
605     /**
606 dl 1.13 * hasWaiters throws IAE if not owned
607     */
608     public void testHasWaitersIAE() {
609     final ReentrantLock lock = new ReentrantLock();
610     final Condition c = (lock.newCondition());
611     final ReentrantLock lock2 = new ReentrantLock();
612     try {
613     lock2.hasWaiters(c);
614     shouldThrow();
615     } catch (IllegalArgumentException success) {
616     } catch (Exception ex) {
617     unexpectedException();
618     }
619     }
620    
621     /**
622     * hasWaiters throws IMSE if not locked
623     */
624     public void testHasWaitersIMSE() {
625     final ReentrantLock lock = new ReentrantLock();
626     final Condition c = (lock.newCondition());
627     try {
628     lock.hasWaiters(c);
629     shouldThrow();
630     } catch (IllegalMonitorStateException success) {
631     } catch (Exception ex) {
632     unexpectedException();
633     }
634     }
635    
636    
637     /**
638     * getWaitQueueLength throws IAE if not owned
639     */
640     public void testGetWaitQueueLengthIAE() {
641     final ReentrantLock lock = new ReentrantLock();
642     final Condition c = (lock.newCondition());
643     final ReentrantLock lock2 = new ReentrantLock();
644     try {
645     lock2.getWaitQueueLength(c);
646     shouldThrow();
647     } catch (IllegalArgumentException success) {
648     } catch (Exception ex) {
649     unexpectedException();
650     }
651     }
652    
653     /**
654     * getWaitQueueLength throws IMSE if not locked
655     */
656     public void testGetWaitQueueLengthIMSE() {
657     final ReentrantLock lock = new ReentrantLock();
658     final Condition c = (lock.newCondition());
659     try {
660     lock.getWaitQueueLength(c);
661     shouldThrow();
662     } catch (IllegalMonitorStateException success) {
663     } catch (Exception ex) {
664     unexpectedException();
665     }
666     }
667    
668    
669     /**
670     * getWaitingThreads throws IAE if not owned
671     */
672     public void testGetWaitingThreadsIAE() {
673 jsr166 1.26 final PublicReentrantLock lock = new PublicReentrantLock();
674 dl 1.13 final Condition c = (lock.newCondition());
675 jsr166 1.26 final PublicReentrantLock lock2 = new PublicReentrantLock();
676 dl 1.13 try {
677     lock2.getWaitingThreads(c);
678     shouldThrow();
679     } catch (IllegalArgumentException success) {
680     } catch (Exception ex) {
681     unexpectedException();
682     }
683     }
684    
685     /**
686     * getWaitingThreads throws IMSE if not locked
687     */
688     public void testGetWaitingThreadsIMSE() {
689 jsr166 1.26 final PublicReentrantLock lock = new PublicReentrantLock();
690 dl 1.13 final Condition c = (lock.newCondition());
691     try {
692     lock.getWaitingThreads(c);
693     shouldThrow();
694     } catch (IllegalMonitorStateException success) {
695     } catch (Exception ex) {
696     unexpectedException();
697     }
698     }
699    
700    
701    
702     /**
703 dl 1.6 * hasWaiters returns true when a thread is waiting, else false
704     */
705 dl 1.5 public void testHasWaiters() {
706 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
707 dl 1.13 final Condition c = lock.newCondition();
708 jsr166 1.26 Thread t = new Thread(new Runnable() {
709 dl 1.2 public void run() {
710     try {
711     lock.lock();
712 dl 1.13 threadAssertFalse(lock.hasWaiters(c));
713     threadAssertEquals(0, lock.getWaitQueueLength(c));
714 dl 1.2 c.await();
715     lock.unlock();
716     }
717     catch(InterruptedException e) {
718 dl 1.6 threadUnexpectedException();
719 dl 1.2 }
720     }
721     });
722    
723     try {
724     t.start();
725     Thread.sleep(SHORT_DELAY_MS);
726     lock.lock();
727 dl 1.13 assertTrue(lock.hasWaiters(c));
728     assertEquals(1, lock.getWaitQueueLength(c));
729 dl 1.2 c.signal();
730     lock.unlock();
731 dl 1.5 Thread.sleep(SHORT_DELAY_MS);
732     lock.lock();
733 dl 1.13 assertFalse(lock.hasWaiters(c));
734     assertEquals(0, lock.getWaitQueueLength(c));
735 dl 1.5 lock.unlock();
736 dl 1.2 t.join(SHORT_DELAY_MS);
737     assertFalse(t.isAlive());
738 dl 1.5 }
739     catch (Exception ex) {
740 dl 1.6 unexpectedException();
741 dl 1.5 }
742     }
743    
744 dl 1.6 /**
745     * getWaitQueueLength returns number of waiting threads
746     */
747 dl 1.5 public void testGetWaitQueueLength() {
748 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
749 dl 1.13 final Condition c = lock.newCondition();
750 jsr166 1.26 Thread t1 = new Thread(new Runnable() {
751 dl 1.13 public void run() {
752     try {
753     lock.lock();
754     threadAssertFalse(lock.hasWaiters(c));
755     threadAssertEquals(0, lock.getWaitQueueLength(c));
756     c.await();
757     lock.unlock();
758     }
759     catch(InterruptedException e) {
760     threadUnexpectedException();
761     }
762     }
763     });
764    
765 jsr166 1.26 Thread t2 = new Thread(new Runnable() {
766 dl 1.13 public void run() {
767     try {
768     lock.lock();
769     threadAssertTrue(lock.hasWaiters(c));
770     threadAssertEquals(1, lock.getWaitQueueLength(c));
771     c.await();
772     lock.unlock();
773     }
774     catch(InterruptedException e) {
775     threadUnexpectedException();
776     }
777     }
778     });
779    
780     try {
781     t1.start();
782     Thread.sleep(SHORT_DELAY_MS);
783     t2.start();
784     Thread.sleep(SHORT_DELAY_MS);
785     lock.lock();
786     assertTrue(lock.hasWaiters(c));
787     assertEquals(2, lock.getWaitQueueLength(c));
788     c.signalAll();
789     lock.unlock();
790     Thread.sleep(SHORT_DELAY_MS);
791     lock.lock();
792     assertFalse(lock.hasWaiters(c));
793     assertEquals(0, lock.getWaitQueueLength(c));
794     lock.unlock();
795     t1.join(SHORT_DELAY_MS);
796     t2.join(SHORT_DELAY_MS);
797     assertFalse(t1.isAlive());
798     assertFalse(t2.isAlive());
799     }
800     catch (Exception ex) {
801     unexpectedException();
802     }
803     }
804    
805     /**
806     * getWaitingThreads returns only and all waiting threads
807     */
808     public void testGetWaitingThreads() {
809 jsr166 1.26 final PublicReentrantLock lock = new PublicReentrantLock();
810 dl 1.13 final Condition c = lock.newCondition();
811 jsr166 1.26 Thread t1 = new Thread(new Runnable() {
812 dl 1.5 public void run() {
813     try {
814     lock.lock();
815 dl 1.13 threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
816 dl 1.5 c.await();
817     lock.unlock();
818     }
819     catch(InterruptedException e) {
820 dl 1.6 threadUnexpectedException();
821 dl 1.5 }
822     }
823     });
824    
825 jsr166 1.26 Thread t2 = new Thread(new Runnable() {
826 dl 1.5 public void run() {
827     try {
828     lock.lock();
829 dl 1.13 threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
830 dl 1.5 c.await();
831     lock.unlock();
832     }
833     catch(InterruptedException e) {
834 dl 1.6 threadUnexpectedException();
835 dl 1.5 }
836     }
837     });
838    
839     try {
840 dl 1.13 lock.lock();
841     assertTrue(lock.getWaitingThreads(c).isEmpty());
842     lock.unlock();
843 dl 1.5 t1.start();
844     Thread.sleep(SHORT_DELAY_MS);
845     t2.start();
846     Thread.sleep(SHORT_DELAY_MS);
847     lock.lock();
848 dl 1.13 assertTrue(lock.hasWaiters(c));
849     assertTrue(lock.getWaitingThreads(c).contains(t1));
850     assertTrue(lock.getWaitingThreads(c).contains(t2));
851 dl 1.5 c.signalAll();
852     lock.unlock();
853     Thread.sleep(SHORT_DELAY_MS);
854     lock.lock();
855 dl 1.13 assertFalse(lock.hasWaiters(c));
856     assertTrue(lock.getWaitingThreads(c).isEmpty());
857 dl 1.5 lock.unlock();
858     t1.join(SHORT_DELAY_MS);
859     t2.join(SHORT_DELAY_MS);
860     assertFalse(t1.isAlive());
861     assertFalse(t2.isAlive());
862 dl 1.2 }
863     catch (Exception ex) {
864 dl 1.6 unexpectedException();
865 dl 1.2 }
866     }
867 dl 1.13
868 dl 1.22 /** A helper class for uninterruptible wait tests */
869     class UninterruptableThread extends Thread {
870     private ReentrantLock lock;
871     private Condition c;
872 jsr166 1.26
873 dl 1.22 public volatile boolean canAwake = false;
874     public volatile boolean interrupted = false;
875     public volatile boolean lockStarted = false;
876 jsr166 1.26
877 dl 1.22 public UninterruptableThread(ReentrantLock lock, Condition c) {
878     this.lock = lock;
879     this.c = c;
880     }
881 jsr166 1.26
882 dl 1.22 public synchronized void run() {
883     lock.lock();
884     lockStarted = true;
885 jsr166 1.26
886 dl 1.22 while (!canAwake) {
887     c.awaitUninterruptibly();
888     }
889 jsr166 1.26
890 dl 1.22 interrupted = isInterrupted();
891     lock.unlock();
892     }
893     }
894 dl 1.2
895 dl 1.6 /**
896     * awaitUninterruptibly doesn't abort on interrupt
897     */
898 dl 1.2 public void testAwaitUninterruptibly() {
899 dl 1.22 final ReentrantLock lock = new ReentrantLock();
900 dl 1.2 final Condition c = lock.newCondition();
901 dl 1.22 UninterruptableThread thread = new UninterruptableThread(lock, c);
902 dl 1.2
903     try {
904 dl 1.22 thread.start();
905    
906     while (!thread.lockStarted) {
907     Thread.sleep(100);
908     }
909    
910 dl 1.2 lock.lock();
911 dl 1.22 try {
912     thread.interrupt();
913     thread.canAwake = true;
914     c.signal();
915     } finally {
916     lock.unlock();
917     }
918    
919     thread.join();
920     assertTrue(thread.interrupted);
921     assertFalse(thread.isAlive());
922     } catch (Exception ex) {
923 dl 1.6 unexpectedException();
924 dl 1.2 }
925     }
926    
927 dl 1.6 /**
928     * await is interruptible
929     */
930 dl 1.2 public void testAwait_Interrupt() {
931 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
932 dl 1.2 final Condition c = lock.newCondition();
933 jsr166 1.26 Thread t = new Thread(new Runnable() {
934 dl 1.2 public void run() {
935     try {
936     lock.lock();
937     c.await();
938     lock.unlock();
939 dl 1.6 threadShouldThrow();
940 dl 1.2 }
941     catch(InterruptedException success) {
942     }
943     }
944     });
945    
946     try {
947     t.start();
948     Thread.sleep(SHORT_DELAY_MS);
949     t.interrupt();
950     t.join(SHORT_DELAY_MS);
951     assertFalse(t.isAlive());
952     }
953     catch (Exception ex) {
954 dl 1.6 unexpectedException();
955 dl 1.2 }
956     }
957    
958 dl 1.6 /**
959     * awaitNanos is interruptible
960     */
961 dl 1.2 public void testAwaitNanos_Interrupt() {
962 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
963 dl 1.2 final Condition c = lock.newCondition();
964 jsr166 1.26 Thread t = new Thread(new Runnable() {
965 dl 1.2 public void run() {
966     try {
967     lock.lock();
968 dl 1.17 c.awaitNanos(1000 * 1000 * 1000); // 1 sec
969 dl 1.2 lock.unlock();
970 dl 1.6 threadShouldThrow();
971 dl 1.2 }
972     catch(InterruptedException success) {
973     }
974     }
975     });
976    
977     try {
978     t.start();
979     Thread.sleep(SHORT_DELAY_MS);
980     t.interrupt();
981     t.join(SHORT_DELAY_MS);
982     assertFalse(t.isAlive());
983     }
984     catch (Exception ex) {
985 dl 1.6 unexpectedException();
986 dl 1.2 }
987     }
988    
989 dl 1.6 /**
990     * awaitUntil is interruptible
991     */
992 dl 1.2 public void testAwaitUntil_Interrupt() {
993 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
994 dl 1.2 final Condition c = lock.newCondition();
995 jsr166 1.26 Thread t = new Thread(new Runnable() {
996 dl 1.2 public void run() {
997     try {
998     lock.lock();
999     java.util.Date d = new java.util.Date();
1000     c.awaitUntil(new java.util.Date(d.getTime() + 10000));
1001     lock.unlock();
1002 dl 1.6 threadShouldThrow();
1003 dl 1.2 }
1004     catch(InterruptedException success) {
1005     }
1006     }
1007     });
1008    
1009     try {
1010     t.start();
1011     Thread.sleep(SHORT_DELAY_MS);
1012     t.interrupt();
1013     t.join(SHORT_DELAY_MS);
1014     assertFalse(t.isAlive());
1015     }
1016     catch (Exception ex) {
1017 dl 1.6 unexpectedException();
1018 dl 1.2 }
1019     }
1020    
1021 dl 1.6 /**
1022     * signalAll wakes up all threads
1023     */
1024 dl 1.2 public void testSignalAll() {
1025 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
1026 dl 1.2 final Condition c = lock.newCondition();
1027 jsr166 1.26 Thread t1 = new Thread(new Runnable() {
1028 dl 1.2 public void run() {
1029     try {
1030     lock.lock();
1031     c.await();
1032     lock.unlock();
1033     }
1034     catch(InterruptedException e) {
1035 dl 1.6 threadUnexpectedException();
1036 dl 1.2 }
1037     }
1038     });
1039    
1040 jsr166 1.26 Thread t2 = new Thread(new Runnable() {
1041 dl 1.2 public void run() {
1042     try {
1043     lock.lock();
1044     c.await();
1045     lock.unlock();
1046     }
1047     catch(InterruptedException e) {
1048 dl 1.6 threadUnexpectedException();
1049 dl 1.2 }
1050     }
1051     });
1052    
1053     try {
1054     t1.start();
1055     t2.start();
1056     Thread.sleep(SHORT_DELAY_MS);
1057     lock.lock();
1058     c.signalAll();
1059     lock.unlock();
1060     t1.join(SHORT_DELAY_MS);
1061     t2.join(SHORT_DELAY_MS);
1062     assertFalse(t1.isAlive());
1063     assertFalse(t2.isAlive());
1064     }
1065     catch (Exception ex) {
1066 dl 1.6 unexpectedException();
1067 dl 1.3 }
1068     }
1069    
1070 dl 1.6 /**
1071 dl 1.23 * await after multiple reentrant locking preserves lock count
1072     */
1073     public void testAwaitLockCount() {
1074 jsr166 1.26 final ReentrantLock lock = new ReentrantLock();
1075 dl 1.23 final Condition c = lock.newCondition();
1076 jsr166 1.26 Thread t1 = new Thread(new Runnable() {
1077 dl 1.23 public void run() {
1078     try {
1079     lock.lock();
1080     threadAssertEquals(1, lock.getHoldCount());
1081     c.await();
1082     threadAssertEquals(1, lock.getHoldCount());
1083     lock.unlock();
1084     }
1085     catch(InterruptedException e) {
1086     threadUnexpectedException();
1087     }
1088     }
1089     });
1090    
1091 jsr166 1.26 Thread t2 = new Thread(new Runnable() {
1092 dl 1.23 public void run() {
1093     try {
1094     lock.lock();
1095     lock.lock();
1096     threadAssertEquals(2, lock.getHoldCount());
1097     c.await();
1098     threadAssertEquals(2, lock.getHoldCount());
1099     lock.unlock();
1100     lock.unlock();
1101     }
1102     catch(InterruptedException e) {
1103     threadUnexpectedException();
1104     }
1105     }
1106     });
1107    
1108     try {
1109     t1.start();
1110     t2.start();
1111     Thread.sleep(SHORT_DELAY_MS);
1112     lock.lock();
1113     c.signalAll();
1114     lock.unlock();
1115     t1.join(SHORT_DELAY_MS);
1116     t2.join(SHORT_DELAY_MS);
1117     assertFalse(t1.isAlive());
1118     assertFalse(t2.isAlive());
1119     }
1120     catch (Exception ex) {
1121     unexpectedException();
1122     }
1123     }
1124    
1125     /**
1126 dl 1.6 * A serialized lock deserializes as unlocked
1127     */
1128 dl 1.3 public void testSerialization() {
1129     ReentrantLock l = new ReentrantLock();
1130     l.lock();
1131     l.unlock();
1132    
1133     try {
1134     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1135     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1136     out.writeObject(l);
1137     out.close();
1138    
1139     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1140     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1141     ReentrantLock r = (ReentrantLock) in.readObject();
1142     r.lock();
1143     r.unlock();
1144     } catch(Exception e){
1145     e.printStackTrace();
1146 dl 1.6 unexpectedException();
1147 dl 1.2 }
1148     }
1149 dl 1.1
1150 dl 1.18 /**
1151     * toString indicates current lock state
1152     */
1153     public void testToString() {
1154     ReentrantLock lock = new ReentrantLock();
1155     String us = lock.toString();
1156     assertTrue(us.indexOf("Unlocked") >= 0);
1157     lock.lock();
1158     String ls = lock.toString();
1159     assertTrue(ls.indexOf("Locked") >= 0);
1160     }
1161    
1162 dl 1.1 }