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.6 by dl, Sat Sep 20 18:20:08 2003 UTC

# Line 19 | Line 19 | public class ReentrantLockTest extends J
19          return new TestSuite(ReentrantLockTest.class);
20      }
21  
22 <    static int HOLD_COUNT_TEST_LIMIT = 20;
23 <
22 >    /**
23 >     * A runnable calling lockInterruptibly
24 >     */
25      class InterruptibleLockRunnable implements Runnable {
26          final ReentrantLock lock;
27          InterruptibleLockRunnable(ReentrantLock l) { lock = l; }
28 <        public void run(){
29 <            try{
28 >        public void run() {
29 >            try {
30                  lock.lockInterruptibly();
31              } catch(InterruptedException success){}
32          }
33      }
34  
35 <    // Same, except must interrupt
35 >
36 >    /**
37 >     * A runnable calling lockInterruptibly that expects to be
38 >     * interrupted
39 >     */
40      class InterruptedLockRunnable implements Runnable {
41          final ReentrantLock lock;
42          InterruptedLockRunnable(ReentrantLock l) { lock = l; }
43 <        public void run(){
44 <            try{
43 >        public void run() {
44 >            try {
45                  lock.lockInterruptibly();
46 <                threadFail("should throw");
46 >                threadShouldThrow();
47              } catch(InterruptedException success){}
48          }
49      }
50  
51      /**
52 <     * To expose protected methods
52 >     * Subclass to expose protected methods
53       */
54 <    static class MyReentrantLock extends ReentrantLock {
55 <        MyReentrantLock() { super(); }
54 >    static class PublicReentrantLock extends ReentrantLock {
55 >        PublicReentrantLock() { super(); }
56          public Collection<Thread> getQueuedThreads() {
57              return super.getQueuedThreads();
58          }
59          public ConditionObject newCondition() {
60 <            return new MyCondition(this);
60 >            return new PublicCondition(this);
61          }
62  
63 <        static class MyCondition extends ReentrantLock.ConditionObject {
64 <            MyCondition(MyReentrantLock l) { super(l); }
63 >        static class PublicCondition extends ReentrantLock.ConditionObject {
64 >            PublicCondition(PublicReentrantLock l) { super(l); }
65              public Collection<Thread> getWaitingThreads() {
66                  return super.getWaitingThreads();
67              }
# Line 65 | Line 70 | public class ReentrantLockTest extends J
70      }
71  
72      /*
73 +     * locking an unlocked lock succeeds
74 +     */
75 +    public void testLock() {
76 +        ReentrantLock rl = new ReentrantLock();
77 +        rl.lock();
78 +        assertTrue(rl.isLocked());
79 +        rl.unlock();
80 +    }
81 +
82 +    /*
83 +     * locking an unlocked fair lock succeeds
84 +     */
85 +    public void testFairLock() {
86 +        ReentrantLock rl = new ReentrantLock(true);
87 +        rl.lock();
88 +        assertTrue(rl.isLocked());
89 +        rl.unlock();
90 +    }
91 +
92 +    /*
93       * Unlocking an unlocked lock throws IllegalMonitorStateException
94       */
95 <    public void testIllegalMonitorStateException(){
95 >    public void testUnlock_IllegalMonitorStateException() {
96          ReentrantLock rl = new ReentrantLock();
97 <        try{
97 >        try {
98              rl.unlock();
99 <            fail("Should of thown Illegal Monitor State Exception");
99 >            shouldThrow();
100  
101          } catch(IllegalMonitorStateException success){}
102      }
103  
104      /*
105 <     * lockInterruptibly is interruptible.
105 >     * trylock on an unlocked lock succeeds
106       */
107 <    public void testInterruptedException(){
108 <        final ReentrantLock lock = new ReentrantLock();
109 <        lock.lock();
110 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
111 <        try {
112 <            t.start();
113 <            t.interrupt();
89 <            lock.unlock();
90 <            t.join();
91 <        } catch(Exception e){
92 <            fail("unexpected exception");
93 <        }
94 <    }
107 >    public void testTryLock() {
108 >        ReentrantLock rl = new ReentrantLock();
109 >        assertTrue(rl.tryLock());
110 >        assertTrue(rl.isLocked());
111 >        rl.unlock();
112 >    }
113 >
114  
115      /*
116       * getLockQueueLength reports number of waiting threads
117       */
118 <    public void testgetLockQueueLength(){
118 >    public void testGetLockQueueLength() {
119          final ReentrantLock lock = new ReentrantLock();
120          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
121          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
# Line 118 | Line 137 | public class ReentrantLockTest extends J
137              t1.join();
138              t2.join();
139          } catch(Exception e){
140 <            fail("unexpected exception");
140 >            unexpectedException();
141          }
142      }
143  
144      /*
145       * getQueuedThreads includes waiting threads
146       */
147 <    public void testGetQueuedThreads(){
148 <        final MyReentrantLock lock = new MyReentrantLock();
147 >    public void testGetQueuedThreads() {
148 >        final PublicReentrantLock lock = new PublicReentrantLock();
149          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
150          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
151          try {
# Line 150 | Line 169 | public class ReentrantLockTest extends J
169              t1.join();
170              t2.join();
171          } catch(Exception e){
172 <            fail("unexpected exception");
172 >            unexpectedException();
173          }
174      }
175  
# Line 158 | Line 177 | public class ReentrantLockTest extends J
177      /*
178       * timed trylock is interruptible.
179       */
180 <    public void testInterruptedException2(){
180 >    public void testInterruptedException2() {
181          final ReentrantLock lock = new ReentrantLock();
182          lock.lock();
183          Thread t = new Thread(new Runnable() {
184 <                public void run(){
185 <                    try{
184 >                public void run() {
185 >                    try {
186                          lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
187 <                        threadFail("should throw");
187 >                        threadShouldThrow();
188                      } catch(InterruptedException success){}
189                  }
190              });
# Line 173 | Line 192 | public class ReentrantLockTest extends J
192              t.start();
193              t.interrupt();
194          } catch(Exception e){
195 <            fail("unexpected exception");
195 >            unexpectedException();
196          }
197      }
198  
# Line 185 | Line 204 | public class ReentrantLockTest extends J
204          final ReentrantLock lock = new ReentrantLock();
205          lock.lock();
206          Thread t = new Thread(new Runnable() {
207 <                public void run(){
207 >                public void run() {
208                      threadAssertFalse(lock.tryLock());
209                  }
210              });
# Line 194 | Line 213 | public class ReentrantLockTest extends J
213              t.join();
214              lock.unlock();
215          } catch(Exception e){
216 <            fail("unexpected exception");
216 >            unexpectedException();
217          }
218      }
219  
220      /**
221 <     * Timed Trylock on a locked lock times out
221 >     * Timed trylock on a locked lock times out
222       */
223 <    public void testTryLock_Timeout(){
223 >    public void testTryLock_Timeout() {
224          final ReentrantLock lock = new ReentrantLock();
225          lock.lock();
226          Thread t = new Thread(new Runnable() {
227 <                public void run(){
227 >                public void run() {
228                      try {
229                          threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
230                      } catch (Exception ex) {
231 <                        threadFail("unexpected exception");
231 >                        threadUnexpectedException();
232                      }
233                  }
234              });
# Line 218 | Line 237 | public class ReentrantLockTest extends J
237              t.join();
238              lock.unlock();
239          } catch(Exception e){
240 <            fail("unexpected exception");
240 >            unexpectedException();
241          }
242      }
243      
# Line 227 | Line 246 | public class ReentrantLockTest extends J
246       */
247      public void testGetHoldCount() {
248          ReentrantLock lock = new ReentrantLock();
249 <        for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) {
249 >        for(int i = 1; i <= SIZE; i++) {
250              lock.lock();
251              assertEquals(i,lock.getHoldCount());
252          }
253 <        for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) {
253 >        for(int i = SIZE; i > 0; i--) {
254              lock.unlock();
255              assertEquals(i-1,lock.getHoldCount());
256          }
# Line 254 | Line 273 | public class ReentrantLockTest extends J
273                          Thread.sleep(SMALL_DELAY_MS);
274                      }
275                      catch(Exception e) {
276 <                        threadFail("unexpected exception");
276 >                        threadUnexpectedException();
277                      }
278                      lock.unlock();
279                  }
280              });
281 <        try{
281 >        try {
282              t.start();
283              Thread.sleep(SHORT_DELAY_MS);
284              assertTrue(lock.isLocked());
285              t.join();
286              assertFalse(lock.isLocked());
287          } catch(Exception e){
288 <            fail("unexpected exception");
288 >            unexpectedException();
289          }
290      }
291  
292  
293 +    /*
294 +     * lockInterruptibly is interruptible.
295 +     */
296 +    public void testLockInterruptibly1() {
297 +        final ReentrantLock lock = new ReentrantLock();
298 +        lock.lock();
299 +        Thread t = new Thread(new InterruptedLockRunnable(lock));
300 +        try {
301 +            t.start();
302 +            t.interrupt();
303 +            lock.unlock();
304 +            t.join();
305 +        } catch(Exception e){
306 +            unexpectedException();
307 +        }
308 +    }
309 +
310      /**
311       * lockInterruptibly succeeds when unlocked, else is interruptible
312       */
313 <    public void testLockInterruptibly() {
313 >    public void testLockInterruptibly2() {
314          final ReentrantLock lock = new ReentrantLock();
315          try {
316              lock.lockInterruptibly();
317          } catch(Exception e) {
318 <            fail("unexpected exception");
318 >            unexpectedException();
319          }
320          Thread t = new Thread(new InterruptedLockRunnable(lock));
321          try {
# Line 289 | Line 325 | public class ReentrantLockTest extends J
325              assertTrue(lock.isHeldByCurrentThread());
326              t.join();
327          } catch(Exception e){
328 <            fail("unexpected exception");
328 >            unexpectedException();
329          }
330      }
331  
332 +    /**
333 +     * Calling await without holding lock throws IllegalMonitorStateException
334 +     */
335      public void testAwait_IllegalMonitor() {
336          final ReentrantLock lock = new ReentrantLock();
337          final Condition c = lock.newCondition();
338          try {
339              c.await();
340 <            fail("should throw");
340 >            shouldThrow();
341          }
342          catch (IllegalMonitorStateException success) {
343          }
344          catch (Exception ex) {
345 <            fail("should throw IMSE");
345 >            unexpectedException();
346          }
347      }
348  
349 +    /**
350 +     * Calling signal without holding lock throws IllegalMonitorStateException
351 +     */
352      public void testSignal_IllegalMonitor() {
353          final ReentrantLock lock = new ReentrantLock();
354          final Condition c = lock.newCondition();
355          try {
356              c.signal();
357 <            fail("should throw");
357 >            shouldThrow();
358          }
359          catch (IllegalMonitorStateException success) {
360          }
361          catch (Exception ex) {
362 <            fail("should throw IMSE");
362 >            unexpectedException();
363          }
364      }
365  
366 +    /**
367 +     * awaitNanos without a signal times out
368 +     */
369      public void testAwaitNanos_Timeout() {
370          final ReentrantLock lock = new ReentrantLock();
371          final Condition c = lock.newCondition();
# Line 331 | Line 376 | public class ReentrantLockTest extends J
376              lock.unlock();
377          }
378          catch (Exception ex) {
379 <            fail("unexpected exception");
379 >            unexpectedException();
380          }
381      }
382  
383 +    /**
384 +     *  timed await without a signal times out
385 +     */
386      public void testAwait_Timeout() {
387          final ReentrantLock lock = new ReentrantLock();
388          final Condition c = lock.newCondition();
# Line 344 | Line 392 | public class ReentrantLockTest extends J
392              lock.unlock();
393          }
394          catch (Exception ex) {
395 <            fail("unexpected exception");
395 >            unexpectedException();
396          }
397      }
398  
399 +    /**
400 +     * awaitUntil without a signal times out
401 +     */
402      public void testAwaitUntil_Timeout() {
403          final ReentrantLock lock = new ReentrantLock();
404          final Condition c = lock.newCondition();
# Line 358 | Line 409 | public class ReentrantLockTest extends J
409              lock.unlock();
410          }
411          catch (Exception ex) {
412 <            fail("unexpected exception");
412 >            unexpectedException();
413          }
414      }
415  
416 +    /**
417 +     * await returns when signalled
418 +     */
419      public void testAwait() {
420          final ReentrantLock lock = new ReentrantLock();
421          final ReentrantLock.ConditionObject c = lock.newCondition();
# Line 373 | Line 427 | public class ReentrantLockTest extends J
427                          lock.unlock();
428                      }
429                      catch(InterruptedException e) {
430 <                        threadFail("unexpected exception");
430 >                        threadUnexpectedException();
431                      }
432                  }
433              });
# Line 388 | Line 442 | public class ReentrantLockTest extends J
442              assertFalse(t.isAlive());
443          }
444          catch (Exception ex) {
445 <            fail("unexpected exception");
445 >            unexpectedException();
446          }
447      }
448  
449 +    /**
450 +     * hasWaiters returns true when a thread is waiting, else false
451 +     */
452      public void testHasWaiters() {
453          final ReentrantLock lock = new ReentrantLock();
454          final ReentrantLock.ConditionObject c = lock.newCondition();
# Line 405 | Line 462 | public class ReentrantLockTest extends J
462                          lock.unlock();
463                      }
464                      catch(InterruptedException e) {
465 <                        threadFail("unexpected exception");
465 >                        threadUnexpectedException();
466                      }
467                  }
468              });
# Line 427 | Line 484 | public class ReentrantLockTest extends J
484              assertFalse(t.isAlive());
485          }
486          catch (Exception ex) {
487 <            fail("unexpected exception");
487 >            unexpectedException();
488          }
489      }
490  
491 +    /**
492 +     * getWaitQueueLength returns number of waiting threads
493 +     */
494      public void testGetWaitQueueLength() {
495          final ReentrantLock lock = new ReentrantLock();
496          final ReentrantLock.ConditionObject c = lock.newCondition();
# Line 444 | Line 504 | public class ReentrantLockTest extends J
504                          lock.unlock();
505                      }
506                      catch(InterruptedException e) {
507 <                        threadFail("unexpected exception");
507 >                        threadUnexpectedException();
508                      }
509                  }
510              });
# Line 459 | Line 519 | public class ReentrantLockTest extends J
519                          lock.unlock();
520                      }
521                      catch(InterruptedException e) {
522 <                        threadFail("unexpected exception");
522 >                        threadUnexpectedException();
523                      }
524                  }
525              });
# Line 485 | Line 545 | public class ReentrantLockTest extends J
545              assertFalse(t2.isAlive());
546          }
547          catch (Exception ex) {
548 <            fail("unexpected exception");
548 >            unexpectedException();
549          }
550      }
551  
552 +    /**
553 +     * getWaitingThreads returns only and all waiting threads
554 +     */
555      public void testGetWaitingThreads() {
556 <        final MyReentrantLock lock = new MyReentrantLock();    
557 <        final MyReentrantLock.MyCondition c = (MyReentrantLock.MyCondition)lock.newCondition();
556 >        final PublicReentrantLock lock = new PublicReentrantLock();    
557 >        final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition();
558          Thread t1 = new Thread(new Runnable() {
559                  public void run() {
560                      try {
# Line 501 | Line 564 | public class ReentrantLockTest extends J
564                          lock.unlock();
565                      }
566                      catch(InterruptedException e) {
567 <                        threadFail("unexpected exception");
567 >                        threadUnexpectedException();
568                      }
569                  }
570              });
# Line 515 | Line 578 | public class ReentrantLockTest extends J
578                          lock.unlock();
579                      }
580                      catch(InterruptedException e) {
581 <                        threadFail("unexpected exception");
581 >                        threadUnexpectedException();
582                      }
583                  }
584              });
# Line 545 | Line 608 | public class ReentrantLockTest extends J
608              assertFalse(t2.isAlive());
609          }
610          catch (Exception ex) {
611 <            fail("unexpected exception");
611 >            unexpectedException();
612          }
613      }
614  
615 +    /**
616 +     * awaitUninterruptibly doesn't abort on interrupt
617 +     */
618      public void testAwaitUninterruptibly() {
619          final ReentrantLock lock = new ReentrantLock();
620          final Condition c = lock.newCondition();
# Line 567 | Line 633 | public class ReentrantLockTest extends J
633              lock.lock();
634              c.signal();
635              lock.unlock();
636 +            assert(t.isInterrupted());
637              t.join(SHORT_DELAY_MS);
638              assertFalse(t.isAlive());
639          }
640          catch (Exception ex) {
641 <            fail("unexpected exception");
641 >            unexpectedException();
642          }
643      }
644  
645 +    /**
646 +     * await is interruptible
647 +     */
648      public void testAwait_Interrupt() {
649          final ReentrantLock lock = new ReentrantLock();
650          final Condition c = lock.newCondition();
# Line 584 | Line 654 | public class ReentrantLockTest extends J
654                          lock.lock();
655                          c.await();
656                          lock.unlock();
657 <                        threadFail("should throw");
657 >                        threadShouldThrow();
658                      }
659                      catch(InterruptedException success) {
660                      }
# Line 599 | Line 669 | public class ReentrantLockTest extends J
669              assertFalse(t.isAlive());
670          }
671          catch (Exception ex) {
672 <            fail("unexpected exception");
672 >            unexpectedException();
673          }
674      }
675  
676 +    /**
677 +     * awaitNanos is interruptible
678 +     */
679      public void testAwaitNanos_Interrupt() {
680          final ReentrantLock lock = new ReentrantLock();
681          final Condition c = lock.newCondition();
# Line 612 | Line 685 | public class ReentrantLockTest extends J
685                          lock.lock();
686                          c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
687                          lock.unlock();
688 <                        threadFail("should throw");
688 >                        threadShouldThrow();
689                      }
690                      catch(InterruptedException success) {
691                      }
# Line 627 | Line 700 | public class ReentrantLockTest extends J
700              assertFalse(t.isAlive());
701          }
702          catch (Exception ex) {
703 <            fail("unexpected exception");
703 >            unexpectedException();
704          }
705      }
706  
707 +    /**
708 +     * awaitUntil is interruptible
709 +     */
710      public void testAwaitUntil_Interrupt() {
711          final ReentrantLock lock = new ReentrantLock();
712          final Condition c = lock.newCondition();
# Line 641 | Line 717 | public class ReentrantLockTest extends J
717                          java.util.Date d = new java.util.Date();
718                          c.awaitUntil(new java.util.Date(d.getTime() + 10000));
719                          lock.unlock();
720 <                        threadFail("should throw");
720 >                        threadShouldThrow();
721                      }
722                      catch(InterruptedException success) {
723                      }
# Line 656 | Line 732 | public class ReentrantLockTest extends J
732              assertFalse(t.isAlive());
733          }
734          catch (Exception ex) {
735 <            fail("unexpected exception");
735 >            unexpectedException();
736          }
737      }
738  
739 +    /**
740 +     * signalAll wakes up all threads
741 +     */
742      public void testSignalAll() {
743          final ReentrantLock lock = new ReentrantLock();
744          final Condition c = lock.newCondition();
# Line 671 | Line 750 | public class ReentrantLockTest extends J
750                          lock.unlock();
751                      }
752                      catch(InterruptedException e) {
753 <                        threadFail("unexpected exception");
753 >                        threadUnexpectedException();
754                      }
755                  }
756              });
# Line 684 | Line 763 | public class ReentrantLockTest extends J
763                          lock.unlock();
764                      }
765                      catch(InterruptedException e) {
766 <                        threadFail("unexpected exception");
766 >                        threadUnexpectedException();
767                      }
768                  }
769              });
# Line 702 | Line 781 | public class ReentrantLockTest extends J
781              assertFalse(t2.isAlive());
782          }
783          catch (Exception ex) {
784 <            fail("unexpected exception");
784 >            unexpectedException();
785          }
786      }
787  
788 +    /**
789 +     * A serialized lock deserializes as unlocked
790 +     */
791      public void testSerialization() {
792          ReentrantLock l = new ReentrantLock();
793          l.lock();
# Line 724 | Line 806 | public class ReentrantLockTest extends J
806              r.unlock();
807          } catch(Exception e){
808              e.printStackTrace();
809 <            fail("unexpected exception");
809 >            unexpectedException();
810          }
811      }
812  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines