ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ReentrantLockTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ReentrantLockTest.java (file contents):
Revision 1.5 by dl, Sat Sep 20 00:31:57 2003 UTC vs.
Revision 1.18 by dl, Sat Jan 10 01:41:59 2004 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * 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   */
8  
9   import junit.framework.*;
# Line 19 | Line 20 | public class ReentrantLockTest extends J
20          return new TestSuite(ReentrantLockTest.class);
21      }
22  
23 <    static int HOLD_COUNT_TEST_LIMIT = 20;
24 <
23 >    /**
24 >     * A runnable calling lockInterruptibly
25 >     */
26      class InterruptibleLockRunnable implements Runnable {
27          final ReentrantLock lock;
28          InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
29 <        public void run(){
30 <            try{
29 >        public void run() {
30 >            try {
31                  lock.lockInterruptibly();
32              } catch(InterruptedException success){}
33          }
34      }
35  
36 <    // Same, except must interrupt
36 >
37 >    /**
38 >     * A runnable calling lockInterruptibly that expects to be
39 >     * interrupted
40 >     */
41      class InterruptedLockRunnable implements Runnable {
42          final ReentrantLock lock;
43          InterruptedLockRunnable(ReentrantLock l) { lock = l; }
44 <        public void run(){
45 <            try{
44 >        public void run() {
45 >            try {
46                  lock.lockInterruptibly();
47 <                threadFail("should throw");
47 >                threadShouldThrow();
48              } catch(InterruptedException success){}
49          }
50      }
51  
52      /**
53 <     * To expose protected methods
53 >     * Subclass to expose protected methods
54       */
55 <    static class MyReentrantLock extends ReentrantLock {
56 <        MyReentrantLock() { super(); }
55 >    static class PublicReentrantLock extends ReentrantLock {
56 >        PublicReentrantLock() { super(); }
57          public Collection<Thread> getQueuedThreads() {
58              return super.getQueuedThreads();
59          }
60 <        public ConditionObject newCondition() {
61 <            return new MyCondition(this);
60 >        public Collection<Thread> getWaitingThreads(Condition c) {
61 >            return super.getWaitingThreads(c);
62          }
63  
58        static class MyCondition extends ReentrantLock.ConditionObject {
59            MyCondition(MyReentrantLock l) { super(l); }
60            public Collection<Thread> getWaitingThreads() {
61                return super.getWaitingThreads();
62            }
63        }
64  
65      }
66  
67 <    /*
67 >    /**
68 >     * 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 >     * 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 >    /**
88 >     * 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 >    /**
98       * Unlocking an unlocked lock throws IllegalMonitorStateException
99       */
100 <    public void testIllegalMonitorStateException(){
100 >    public void testUnlock_IllegalMonitorStateException() {
101          ReentrantLock rl = new ReentrantLock();
102 <        try{
102 >        try {
103              rl.unlock();
104 <            fail("Should of thown Illegal Monitor State Exception");
104 >            shouldThrow();
105  
106          } catch(IllegalMonitorStateException success){}
107      }
108  
109 <    /*
110 <     * lockInterruptibly is interruptible.
109 >    /**
110 >     * tryLock on an unlocked lock succeeds
111 >     */
112 >    public void testTryLock() {
113 >        ReentrantLock rl = new ReentrantLock();
114 >        assertTrue(rl.tryLock());
115 >        assertTrue(rl.isLocked());
116 >        rl.unlock();
117 >    }
118 >
119 >
120 >    /**
121 >     * hasQueuedThreads reports whether there are waiting threads
122       */
123 <    public void testInterruptedException(){
123 >    public void testhasQueuedThreads() {
124          final ReentrantLock lock = new ReentrantLock();
125 <        lock.lock();
126 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
125 >        Thread t1 = new Thread(new InterruptedLockRunnable(lock));
126 >        Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
127          try {
128 <            t.start();
129 <            t.interrupt();
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 <            t.join();
140 >            Thread.sleep(SHORT_DELAY_MS);
141 >            assertFalse(lock.hasQueuedThreads());
142 >            t1.join();
143 >            t2.join();
144          } catch(Exception e){
145 <            fail("unexpected exception");
145 >            unexpectedException();
146          }
147      }
148  
149 <    /*
150 <     * getLockQueueLength reports number of waiting threads
149 >    /**
150 >     * getQueueLength reports number of waiting threads
151       */
152 <    public void testgetLockQueueLength(){
152 >    public void testGetQueueLength() {
153          final ReentrantLock lock = new ReentrantLock();
154          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
155          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
156          try {
157 <            assertEquals(0, lock.getLockQueueLength());
157 >            assertEquals(0, lock.getQueueLength());
158 >            lock.lock();
159 >            t1.start();
160 >            Thread.sleep(SHORT_DELAY_MS);
161 >            assertEquals(1, lock.getQueueLength());
162 >            t2.start();
163 >            Thread.sleep(SHORT_DELAY_MS);
164 >            assertEquals(2, lock.getQueueLength());
165 >            t1.interrupt();
166 >            Thread.sleep(SHORT_DELAY_MS);
167 >            assertEquals(1, lock.getQueueLength());
168 >            lock.unlock();
169 >            Thread.sleep(SHORT_DELAY_MS);
170 >            assertEquals(0, lock.getQueueLength());
171 >            t1.join();
172 >            t2.join();
173 >        } catch(Exception e){
174 >            unexpectedException();
175 >        }
176 >    }
177 >
178 >    /**
179 >     * 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.getLockQueueLength());
190 >            assertEquals(1, lock.getQueueLength());
191              t2.start();
192              Thread.sleep(SHORT_DELAY_MS);
193 <            assertEquals(2, lock.getLockQueueLength());
193 >            assertEquals(2, lock.getQueueLength());
194              t1.interrupt();
195              Thread.sleep(SHORT_DELAY_MS);
196 <            assertEquals(1, lock.getLockQueueLength());
196 >            assertEquals(1, lock.getQueueLength());
197              lock.unlock();
198              Thread.sleep(SHORT_DELAY_MS);
199 <            assertEquals(0, lock.getLockQueueLength());
199 >            assertEquals(0, lock.getQueueLength());
200              t1.join();
201              t2.join();
202          } catch(Exception e){
203 <            fail("unexpected exception");
203 >            unexpectedException();
204          }
205      }
206  
207 <    /*
207 >    /**
208       * getQueuedThreads includes waiting threads
209       */
210 <    public void testGetQueuedThreads(){
211 <        final MyReentrantLock lock = new MyReentrantLock();
210 >    public void testGetQueuedThreads() {
211 >        final PublicReentrantLock lock = new PublicReentrantLock();
212          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
213          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
214          try {
# Line 150 | Line 232 | public class ReentrantLockTest extends J
232              t1.join();
233              t2.join();
234          } catch(Exception e){
235 <            fail("unexpected exception");
235 >            unexpectedException();
236          }
237      }
238  
239  
240 <    /*
241 <     * timed trylock is interruptible.
240 >    /**
241 >     * timed tryLock is interruptible.
242       */
243 <    public void testInterruptedException2(){
243 >    public void testInterruptedException2() {
244          final ReentrantLock lock = new ReentrantLock();
245          lock.lock();
246          Thread t = new Thread(new Runnable() {
247 <                public void run(){
248 <                    try{
247 >                public void run() {
248 >                    try {
249                          lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
250 <                        threadFail("should throw");
250 >                        threadShouldThrow();
251                      } catch(InterruptedException success){}
252                  }
253              });
# Line 173 | Line 255 | public class ReentrantLockTest extends J
255              t.start();
256              t.interrupt();
257          } catch(Exception e){
258 <            fail("unexpected exception");
258 >            unexpectedException();
259          }
260      }
261  
262  
263      /**
264 <     * Trylock on a locked lock fails
264 >     * TryLock on a locked lock fails
265       */
266      public void testTryLockWhenLocked() {
267          final ReentrantLock lock = new ReentrantLock();
268          lock.lock();
269          Thread t = new Thread(new Runnable() {
270 <                public void run(){
270 >                public void run() {
271                      threadAssertFalse(lock.tryLock());
272                  }
273              });
# Line 194 | Line 276 | public class ReentrantLockTest extends J
276              t.join();
277              lock.unlock();
278          } catch(Exception e){
279 <            fail("unexpected exception");
279 >            unexpectedException();
280          }
281      }
282  
283      /**
284 <     * Timed Trylock on a locked lock times out
284 >     * Timed tryLock on a locked lock times out
285       */
286 <    public void testTryLock_Timeout(){
286 >    public void testTryLock_Timeout() {
287          final ReentrantLock lock = new ReentrantLock();
288          lock.lock();
289          Thread t = new Thread(new Runnable() {
290 <                public void run(){
290 >                public void run() {
291                      try {
292                          threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
293                      } catch (Exception ex) {
294 <                        threadFail("unexpected exception");
294 >                        threadUnexpectedException();
295                      }
296                  }
297              });
# Line 218 | Line 300 | public class ReentrantLockTest extends J
300              t.join();
301              lock.unlock();
302          } catch(Exception e){
303 <            fail("unexpected exception");
303 >            unexpectedException();
304          }
305      }
306      
# Line 227 | Line 309 | public class ReentrantLockTest extends J
309       */
310      public void testGetHoldCount() {
311          ReentrantLock lock = new ReentrantLock();
312 <        for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) {
312 >        for(int i = 1; i <= SIZE; i++) {
313              lock.lock();
314              assertEquals(i,lock.getHoldCount());
315          }
316 <        for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) {
316 >        for(int i = SIZE; i > 0; i--) {
317              lock.unlock();
318              assertEquals(i-1,lock.getHoldCount());
319          }
# Line 254 | Line 336 | public class ReentrantLockTest extends J
336                          Thread.sleep(SMALL_DELAY_MS);
337                      }
338                      catch(Exception e) {
339 <                        threadFail("unexpected exception");
339 >                        threadUnexpectedException();
340                      }
341                      lock.unlock();
342                  }
343              });
344 <        try{
344 >        try {
345              t.start();
346              Thread.sleep(SHORT_DELAY_MS);
347              assertTrue(lock.isLocked());
348              t.join();
349              assertFalse(lock.isLocked());
350          } catch(Exception e){
351 <            fail("unexpected exception");
351 >            unexpectedException();
352          }
353      }
354  
355  
356      /**
357 +     * 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 +    /**
374       * lockInterruptibly succeeds when unlocked, else is interruptible
375       */
376 <    public void testLockInterruptibly() {
376 >    public void testLockInterruptibly2() {
377          final ReentrantLock lock = new ReentrantLock();
378          try {
379              lock.lockInterruptibly();
380          } catch(Exception e) {
381 <            fail("unexpected exception");
381 >            unexpectedException();
382          }
383          Thread t = new Thread(new InterruptedLockRunnable(lock));
384          try {
# Line 289 | Line 388 | public class ReentrantLockTest extends J
388              assertTrue(lock.isHeldByCurrentThread());
389              t.join();
390          } catch(Exception e){
391 <            fail("unexpected exception");
391 >            unexpectedException();
392          }
393      }
394  
395 +    /**
396 +     * Calling await without holding lock throws IllegalMonitorStateException
397 +     */
398      public void testAwait_IllegalMonitor() {
399          final ReentrantLock lock = new ReentrantLock();
400          final Condition c = lock.newCondition();
401          try {
402              c.await();
403 <            fail("should throw");
403 >            shouldThrow();
404          }
405          catch (IllegalMonitorStateException success) {
406          }
407          catch (Exception ex) {
408 <            fail("should throw IMSE");
408 >            unexpectedException();
409          }
410      }
411  
412 +    /**
413 +     * Calling signal without holding lock throws IllegalMonitorStateException
414 +     */
415      public void testSignal_IllegalMonitor() {
416          final ReentrantLock lock = new ReentrantLock();
417          final Condition c = lock.newCondition();
418          try {
419              c.signal();
420 <            fail("should throw");
420 >            shouldThrow();
421          }
422          catch (IllegalMonitorStateException success) {
423          }
424          catch (Exception ex) {
425 <            fail("should throw IMSE");
425 >            unexpectedException();
426          }
427      }
428  
429 +    /**
430 +     * awaitNanos without a signal times out
431 +     */
432      public void testAwaitNanos_Timeout() {
433          final ReentrantLock lock = new ReentrantLock();
434          final Condition c = lock.newCondition();
# Line 331 | Line 439 | public class ReentrantLockTest extends J
439              lock.unlock();
440          }
441          catch (Exception ex) {
442 <            fail("unexpected exception");
442 >            unexpectedException();
443          }
444      }
445  
446 +    /**
447 +     *  timed await without a signal times out
448 +     */
449      public void testAwait_Timeout() {
450          final ReentrantLock lock = new ReentrantLock();
451          final Condition c = lock.newCondition();
# Line 344 | Line 455 | public class ReentrantLockTest extends J
455              lock.unlock();
456          }
457          catch (Exception ex) {
458 <            fail("unexpected exception");
458 >            unexpectedException();
459          }
460      }
461  
462 +    /**
463 +     * awaitUntil without a signal times out
464 +     */
465      public void testAwaitUntil_Timeout() {
466          final ReentrantLock lock = new ReentrantLock();
467          final Condition c = lock.newCondition();
# Line 358 | Line 472 | public class ReentrantLockTest extends J
472              lock.unlock();
473          }
474          catch (Exception ex) {
475 <            fail("unexpected exception");
475 >            unexpectedException();
476          }
477      }
478  
479 +    /**
480 +     * await returns when signalled
481 +     */
482      public void testAwait() {
483          final ReentrantLock lock = new ReentrantLock();
484 <        final ReentrantLock.ConditionObject c = lock.newCondition();
484 >        final Condition c = lock.newCondition();
485          Thread t = new Thread(new Runnable() {
486                  public void run() {
487                      try {
# Line 373 | Line 490 | public class ReentrantLockTest extends J
490                          lock.unlock();
491                      }
492                      catch(InterruptedException e) {
493 <                        threadFail("unexpected exception");
493 >                        threadUnexpectedException();
494                      }
495                  }
496              });
# Line 388 | Line 505 | public class ReentrantLockTest extends J
505              assertFalse(t.isAlive());
506          }
507          catch (Exception ex) {
508 <            fail("unexpected exception");
508 >            unexpectedException();
509 >        }
510 >    }
511 >
512 >    /**
513 >     * 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 >     * 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 +     * hasWaiters returns true when a thread is waiting, else false
655 +     */
656      public void testHasWaiters() {
657          final ReentrantLock lock = new ReentrantLock();
658 <        final ReentrantLock.ConditionObject c = lock.newCondition();
658 >        final Condition c = lock.newCondition();
659          Thread t = new Thread(new Runnable() {
660                  public void run() {
661                      try {
662                          lock.lock();
663 <                        threadAssertFalse(c.hasWaiters());
664 <                        threadAssertEquals(0, c.getWaitQueueLength());
663 >                        threadAssertFalse(lock.hasWaiters(c));
664 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
665                          c.await();
666                          lock.unlock();
667                      }
668                      catch(InterruptedException e) {
669 <                        threadFail("unexpected exception");
669 >                        threadUnexpectedException();
670                      }
671                  }
672              });
# Line 414 | Line 675 | public class ReentrantLockTest extends J
675              t.start();
676              Thread.sleep(SHORT_DELAY_MS);
677              lock.lock();
678 <            assertTrue(c.hasWaiters());
679 <            assertEquals(1, c.getWaitQueueLength());
678 >            assertTrue(lock.hasWaiters(c));
679 >            assertEquals(1, lock.getWaitQueueLength(c));
680              c.signal();
681              lock.unlock();
682              Thread.sleep(SHORT_DELAY_MS);
683              lock.lock();
684 <            assertFalse(c.hasWaiters());
685 <            assertEquals(0, c.getWaitQueueLength());
684 >            assertFalse(lock.hasWaiters(c));
685 >            assertEquals(0, lock.getWaitQueueLength(c));
686              lock.unlock();
687              t.join(SHORT_DELAY_MS);
688              assertFalse(t.isAlive());
689          }
690          catch (Exception ex) {
691 <            fail("unexpected exception");
691 >            unexpectedException();
692          }
693      }
694  
695 +    /**
696 +     * getWaitQueueLength returns number of waiting threads
697 +     */
698      public void testGetWaitQueueLength() {
699          final ReentrantLock lock = new ReentrantLock();
700 <        final ReentrantLock.ConditionObject c = lock.newCondition();
700 >        final Condition c = lock.newCondition();
701          Thread t1 = new Thread(new Runnable() {
702                  public void run() {
703                      try {
704                          lock.lock();
705 <                        threadAssertFalse(c.hasWaiters());
706 <                        threadAssertEquals(0, c.getWaitQueueLength());
705 >                        threadAssertFalse(lock.hasWaiters(c));
706 >                        threadAssertEquals(0, lock.getWaitQueueLength(c));
707                          c.await();
708                          lock.unlock();
709                      }
710                      catch(InterruptedException e) {
711 <                        threadFail("unexpected exception");
711 >                        threadUnexpectedException();
712                      }
713                  }
714              });
# Line 453 | Line 717 | public class ReentrantLockTest extends J
717                  public void run() {
718                      try {
719                          lock.lock();
720 <                        threadAssertTrue(c.hasWaiters());
721 <                        threadAssertEquals(1, c.getWaitQueueLength());
720 >                        threadAssertTrue(lock.hasWaiters(c));
721 >                        threadAssertEquals(1, lock.getWaitQueueLength(c));
722                          c.await();
723                          lock.unlock();
724                      }
725                      catch(InterruptedException e) {
726 <                        threadFail("unexpected exception");
726 >                        threadUnexpectedException();
727                      }
728                  }
729              });
# Line 470 | Line 734 | public class ReentrantLockTest extends J
734              t2.start();
735              Thread.sleep(SHORT_DELAY_MS);
736              lock.lock();
737 <            assertTrue(c.hasWaiters());
738 <            assertEquals(2, c.getWaitQueueLength());
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(c.hasWaiters());
744 <            assertEquals(0, c.getWaitQueueLength());
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);
# Line 485 | Line 749 | public class ReentrantLockTest extends J
749              assertFalse(t2.isAlive());
750          }
751          catch (Exception ex) {
752 <            fail("unexpected exception");
752 >            unexpectedException();
753          }
754      }
755  
756 +    /**
757 +     * getWaitingThreads returns only and all waiting threads
758 +     */
759      public void testGetWaitingThreads() {
760 <        final MyReentrantLock lock = new MyReentrantLock();    
761 <        final MyReentrantLock.MyCondition c = (MyReentrantLock.MyCondition)lock.newCondition();
760 >        final PublicReentrantLock lock = new PublicReentrantLock();    
761 >        final Condition c = lock.newCondition();
762          Thread t1 = new Thread(new Runnable() {
763                  public void run() {
764                      try {
765                          lock.lock();
766 <                        threadAssertTrue(c.getWaitingThreads().isEmpty());
766 >                        threadAssertTrue(lock.getWaitingThreads(c).isEmpty());
767                          c.await();
768                          lock.unlock();
769                      }
770                      catch(InterruptedException e) {
771 <                        threadFail("unexpected exception");
771 >                        threadUnexpectedException();
772                      }
773                  }
774              });
# Line 510 | Line 777 | public class ReentrantLockTest extends J
777                  public void run() {
778                      try {
779                          lock.lock();
780 <                        threadAssertFalse(c.getWaitingThreads().isEmpty());
780 >                        threadAssertFalse(lock.getWaitingThreads(c).isEmpty());
781                          c.await();
782                          lock.unlock();
783                      }
784                      catch(InterruptedException e) {
785 <                        threadFail("unexpected exception");
785 >                        threadUnexpectedException();
786                      }
787                  }
788              });
789  
790          try {
791              lock.lock();
792 <            assertTrue(c.getWaitingThreads().isEmpty());
792 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
793              lock.unlock();
794              t1.start();
795              Thread.sleep(SHORT_DELAY_MS);
796              t2.start();
797              Thread.sleep(SHORT_DELAY_MS);
798              lock.lock();
799 <            assertTrue(c.hasWaiters());
800 <            assertTrue(c.getWaitingThreads().contains(t1));
801 <            assertTrue(c.getWaitingThreads().contains(t2));
799 >            assertTrue(lock.hasWaiters(c));
800 >            assertTrue(lock.getWaitingThreads(c).contains(t1));
801 >            assertTrue(lock.getWaitingThreads(c).contains(t2));
802              c.signalAll();
803              lock.unlock();
804              Thread.sleep(SHORT_DELAY_MS);
805              lock.lock();
806 <            assertFalse(c.hasWaiters());
807 <            assertTrue(c.getWaitingThreads().isEmpty());
806 >            assertFalse(lock.hasWaiters(c));
807 >            assertTrue(lock.getWaitingThreads(c).isEmpty());
808              lock.unlock();
809              t1.join(SHORT_DELAY_MS);
810              t2.join(SHORT_DELAY_MS);
# Line 545 | Line 812 | public class ReentrantLockTest extends J
812              assertFalse(t2.isAlive());
813          }
814          catch (Exception ex) {
815 <            fail("unexpected exception");
815 >            unexpectedException();
816          }
817      }
818  
819 +
820 +
821 +    /**
822 +     * awaitUninterruptibly doesn't abort on interrupt
823 +     */
824      public void testAwaitUninterruptibly() {
825          final ReentrantLock lock = new ReentrantLock();
826          final Condition c = lock.newCondition();
# Line 567 | Line 839 | public class ReentrantLockTest extends J
839              lock.lock();
840              c.signal();
841              lock.unlock();
842 +            assert(t.isInterrupted());
843              t.join(SHORT_DELAY_MS);
844              assertFalse(t.isAlive());
845          }
846          catch (Exception ex) {
847 <            fail("unexpected exception");
847 >            unexpectedException();
848          }
849      }
850  
851 +    /**
852 +     * await is interruptible
853 +     */
854      public void testAwait_Interrupt() {
855          final ReentrantLock lock = new ReentrantLock();
856          final Condition c = lock.newCondition();
# Line 584 | Line 860 | public class ReentrantLockTest extends J
860                          lock.lock();
861                          c.await();
862                          lock.unlock();
863 <                        threadFail("should throw");
863 >                        threadShouldThrow();
864                      }
865                      catch(InterruptedException success) {
866                      }
# Line 599 | Line 875 | public class ReentrantLockTest extends J
875              assertFalse(t.isAlive());
876          }
877          catch (Exception ex) {
878 <            fail("unexpected exception");
878 >            unexpectedException();
879          }
880      }
881  
882 +    /**
883 +     * awaitNanos is interruptible
884 +     */
885      public void testAwaitNanos_Interrupt() {
886          final ReentrantLock lock = new ReentrantLock();
887          final Condition c = lock.newCondition();
# Line 610 | Line 889 | public class ReentrantLockTest extends J
889                  public void run() {
890                      try {
891                          lock.lock();
892 <                        c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
892 >                        c.awaitNanos(1000 * 1000 * 1000); // 1 sec
893                          lock.unlock();
894 <                        threadFail("should throw");
894 >                        threadShouldThrow();
895                      }
896                      catch(InterruptedException success) {
897                      }
# Line 627 | Line 906 | public class ReentrantLockTest extends J
906              assertFalse(t.isAlive());
907          }
908          catch (Exception ex) {
909 <            fail("unexpected exception");
909 >            unexpectedException();
910          }
911      }
912  
913 +    /**
914 +     * awaitUntil is interruptible
915 +     */
916      public void testAwaitUntil_Interrupt() {
917          final ReentrantLock lock = new ReentrantLock();
918          final Condition c = lock.newCondition();
# Line 641 | Line 923 | public class ReentrantLockTest extends J
923                          java.util.Date d = new java.util.Date();
924                          c.awaitUntil(new java.util.Date(d.getTime() + 10000));
925                          lock.unlock();
926 <                        threadFail("should throw");
926 >                        threadShouldThrow();
927                      }
928                      catch(InterruptedException success) {
929                      }
# Line 656 | Line 938 | public class ReentrantLockTest extends J
938              assertFalse(t.isAlive());
939          }
940          catch (Exception ex) {
941 <            fail("unexpected exception");
941 >            unexpectedException();
942          }
943      }
944  
945 +    /**
946 +     * signalAll wakes up all threads
947 +     */
948      public void testSignalAll() {
949          final ReentrantLock lock = new ReentrantLock();
950          final Condition c = lock.newCondition();
# Line 671 | Line 956 | public class ReentrantLockTest extends J
956                          lock.unlock();
957                      }
958                      catch(InterruptedException e) {
959 <                        threadFail("unexpected exception");
959 >                        threadUnexpectedException();
960                      }
961                  }
962              });
# Line 684 | Line 969 | public class ReentrantLockTest extends J
969                          lock.unlock();
970                      }
971                      catch(InterruptedException e) {
972 <                        threadFail("unexpected exception");
972 >                        threadUnexpectedException();
973                      }
974                  }
975              });
# Line 702 | Line 987 | public class ReentrantLockTest extends J
987              assertFalse(t2.isAlive());
988          }
989          catch (Exception ex) {
990 <            fail("unexpected exception");
990 >            unexpectedException();
991          }
992      }
993  
994 +    /**
995 +     * A serialized lock deserializes as unlocked
996 +     */
997      public void testSerialization() {
998          ReentrantLock l = new ReentrantLock();
999          l.lock();
# Line 724 | Line 1012 | public class ReentrantLockTest extends J
1012              r.unlock();
1013          } catch(Exception e){
1014              e.printStackTrace();
1015 <            fail("unexpected exception");
1015 >            unexpectedException();
1016          }
1017      }
1018  
1019 +    /**
1020 +     * toString indicates current lock state
1021 +     */
1022 +    public void testToString() {
1023 +        ReentrantLock lock = new ReentrantLock();
1024 +        String us = lock.toString();
1025 +        assertTrue(us.indexOf("Unlocked") >= 0);
1026 +        lock.lock();
1027 +        String ls = lock.toString();
1028 +        assertTrue(ls.indexOf("Locked") >= 0);
1029 +    }
1030 +
1031   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines