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.9 by dl, Mon Nov 3 13:50:07 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 64 | Line 69 | public class ReentrantLockTest extends J
69  
70      }
71  
72 <    /*
72 >    /**
73 >     * Constructor sets given fairness
74 >     */
75 >    public void testConstructor() {
76 >        ReentrantLock rl = new ReentrantLock();
77 >        assertFalse(rl.isFair());
78 >        ReentrantLock r2 = new ReentrantLock(true);
79 >        assertTrue(r2.isFair());
80 >    }
81 >
82 >    /**
83 >     * locking an unlocked lock succeeds
84 >     */
85 >    public void testLock() {
86 >        ReentrantLock rl = new ReentrantLock();
87 >        rl.lock();
88 >        assertTrue(rl.isLocked());
89 >        rl.unlock();
90 >    }
91 >
92 >    /**
93 >     * locking an unlocked fair lock succeeds
94 >     */
95 >    public void testFairLock() {
96 >        ReentrantLock rl = new ReentrantLock(true);
97 >        rl.lock();
98 >        assertTrue(rl.isLocked());
99 >        rl.unlock();
100 >    }
101 >
102 >    /**
103       * Unlocking an unlocked lock throws IllegalMonitorStateException
104       */
105 <    public void testIllegalMonitorStateException(){
105 >    public void testUnlock_IllegalMonitorStateException() {
106          ReentrantLock rl = new ReentrantLock();
107 <        try{
107 >        try {
108              rl.unlock();
109 <            fail("Should of thown Illegal Monitor State Exception");
109 >            shouldThrow();
110  
111          } catch(IllegalMonitorStateException success){}
112      }
113  
114 <    /*
115 <     * lockInterruptibly is interruptible.
114 >    /**
115 >     * trylock on an unlocked lock succeeds
116       */
117 <    public void testInterruptedException(){
118 <        final ReentrantLock lock = new ReentrantLock();
119 <        lock.lock();
120 <        Thread t = new Thread(new InterruptedLockRunnable(lock));
121 <        try {
122 <            t.start();
123 <            t.interrupt();
89 <            lock.unlock();
90 <            t.join();
91 <        } catch(Exception e){
92 <            fail("unexpected exception");
93 <        }
94 <    }
117 >    public void testTryLock() {
118 >        ReentrantLock rl = new ReentrantLock();
119 >        assertTrue(rl.tryLock());
120 >        assertTrue(rl.isLocked());
121 >        rl.unlock();
122 >    }
123 >
124  
125 <    /*
126 <     * getLockQueueLength reports number of waiting threads
125 >    /**
126 >     * getQueueLength reports number of waiting threads
127       */
128 <    public void testgetLockQueueLength(){
128 >    public void testGetQueueLength() {
129          final ReentrantLock lock = new ReentrantLock();
130          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
131          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
132          try {
133 <            assertEquals(0, lock.getLockQueueLength());
133 >            assertEquals(0, lock.getQueueLength());
134              lock.lock();
135              t1.start();
136              Thread.sleep(SHORT_DELAY_MS);
137 <            assertEquals(1, lock.getLockQueueLength());
137 >            assertEquals(1, lock.getQueueLength());
138              t2.start();
139              Thread.sleep(SHORT_DELAY_MS);
140 <            assertEquals(2, lock.getLockQueueLength());
140 >            assertEquals(2, lock.getQueueLength());
141              t1.interrupt();
142              Thread.sleep(SHORT_DELAY_MS);
143 <            assertEquals(1, lock.getLockQueueLength());
143 >            assertEquals(1, lock.getQueueLength());
144              lock.unlock();
145              Thread.sleep(SHORT_DELAY_MS);
146 <            assertEquals(0, lock.getLockQueueLength());
146 >            assertEquals(0, lock.getQueueLength());
147              t1.join();
148              t2.join();
149          } catch(Exception e){
150 <            fail("unexpected exception");
150 >            unexpectedException();
151          }
152      }
153  
154 <    /*
154 >    /**
155       * getQueuedThreads includes waiting threads
156       */
157 <    public void testGetQueuedThreads(){
158 <        final MyReentrantLock lock = new MyReentrantLock();
157 >    public void testGetQueuedThreads() {
158 >        final PublicReentrantLock lock = new PublicReentrantLock();
159          Thread t1 = new Thread(new InterruptedLockRunnable(lock));
160          Thread t2 = new Thread(new InterruptibleLockRunnable(lock));
161          try {
# Line 150 | Line 179 | public class ReentrantLockTest extends J
179              t1.join();
180              t2.join();
181          } catch(Exception e){
182 <            fail("unexpected exception");
182 >            unexpectedException();
183          }
184      }
185  
186  
187 <    /*
187 >    /**
188       * timed trylock is interruptible.
189       */
190 <    public void testInterruptedException2(){
190 >    public void testInterruptedException2() {
191          final ReentrantLock lock = new ReentrantLock();
192          lock.lock();
193          Thread t = new Thread(new Runnable() {
194 <                public void run(){
195 <                    try{
194 >                public void run() {
195 >                    try {
196                          lock.tryLock(MEDIUM_DELAY_MS,TimeUnit.MILLISECONDS);
197 <                        threadFail("should throw");
197 >                        threadShouldThrow();
198                      } catch(InterruptedException success){}
199                  }
200              });
# Line 173 | Line 202 | public class ReentrantLockTest extends J
202              t.start();
203              t.interrupt();
204          } catch(Exception e){
205 <            fail("unexpected exception");
205 >            unexpectedException();
206          }
207      }
208  
# Line 185 | Line 214 | public class ReentrantLockTest extends J
214          final ReentrantLock lock = new ReentrantLock();
215          lock.lock();
216          Thread t = new Thread(new Runnable() {
217 <                public void run(){
217 >                public void run() {
218                      threadAssertFalse(lock.tryLock());
219                  }
220              });
# Line 194 | Line 223 | public class ReentrantLockTest extends J
223              t.join();
224              lock.unlock();
225          } catch(Exception e){
226 <            fail("unexpected exception");
226 >            unexpectedException();
227          }
228      }
229  
230      /**
231 <     * Timed Trylock on a locked lock times out
231 >     * Timed trylock on a locked lock times out
232       */
233 <    public void testTryLock_Timeout(){
233 >    public void testTryLock_Timeout() {
234          final ReentrantLock lock = new ReentrantLock();
235          lock.lock();
236          Thread t = new Thread(new Runnable() {
237 <                public void run(){
237 >                public void run() {
238                      try {
239                          threadAssertFalse(lock.tryLock(1, TimeUnit.MILLISECONDS));
240                      } catch (Exception ex) {
241 <                        threadFail("unexpected exception");
241 >                        threadUnexpectedException();
242                      }
243                  }
244              });
# Line 218 | Line 247 | public class ReentrantLockTest extends J
247              t.join();
248              lock.unlock();
249          } catch(Exception e){
250 <            fail("unexpected exception");
250 >            unexpectedException();
251          }
252      }
253      
# Line 227 | Line 256 | public class ReentrantLockTest extends J
256       */
257      public void testGetHoldCount() {
258          ReentrantLock lock = new ReentrantLock();
259 <        for(int i = 1; i <= ReentrantLockTest.HOLD_COUNT_TEST_LIMIT;i++) {
259 >        for(int i = 1; i <= SIZE; i++) {
260              lock.lock();
261              assertEquals(i,lock.getHoldCount());
262          }
263 <        for(int i = ReentrantLockTest.HOLD_COUNT_TEST_LIMIT; i > 0; i--) {
263 >        for(int i = SIZE; i > 0; i--) {
264              lock.unlock();
265              assertEquals(i-1,lock.getHoldCount());
266          }
# Line 254 | Line 283 | public class ReentrantLockTest extends J
283                          Thread.sleep(SMALL_DELAY_MS);
284                      }
285                      catch(Exception e) {
286 <                        threadFail("unexpected exception");
286 >                        threadUnexpectedException();
287                      }
288                      lock.unlock();
289                  }
290              });
291 <        try{
291 >        try {
292              t.start();
293              Thread.sleep(SHORT_DELAY_MS);
294              assertTrue(lock.isLocked());
295              t.join();
296              assertFalse(lock.isLocked());
297          } catch(Exception e){
298 <            fail("unexpected exception");
298 >            unexpectedException();
299          }
300      }
301  
302  
303      /**
304 +     * lockInterruptibly is interruptible.
305 +     */
306 +    public void testLockInterruptibly1() {
307 +        final ReentrantLock lock = new ReentrantLock();
308 +        lock.lock();
309 +        Thread t = new Thread(new InterruptedLockRunnable(lock));
310 +        try {
311 +            t.start();
312 +            t.interrupt();
313 +            lock.unlock();
314 +            t.join();
315 +        } catch(Exception e){
316 +            unexpectedException();
317 +        }
318 +    }
319 +
320 +    /**
321       * lockInterruptibly succeeds when unlocked, else is interruptible
322       */
323 <    public void testLockInterruptibly() {
323 >    public void testLockInterruptibly2() {
324          final ReentrantLock lock = new ReentrantLock();
325          try {
326              lock.lockInterruptibly();
327          } catch(Exception e) {
328 <            fail("unexpected exception");
328 >            unexpectedException();
329          }
330          Thread t = new Thread(new InterruptedLockRunnable(lock));
331          try {
# Line 289 | Line 335 | public class ReentrantLockTest extends J
335              assertTrue(lock.isHeldByCurrentThread());
336              t.join();
337          } catch(Exception e){
338 <            fail("unexpected exception");
338 >            unexpectedException();
339          }
340      }
341  
342 +    /**
343 +     * Calling await without holding lock throws IllegalMonitorStateException
344 +     */
345      public void testAwait_IllegalMonitor() {
346          final ReentrantLock lock = new ReentrantLock();
347          final Condition c = lock.newCondition();
348          try {
349              c.await();
350 <            fail("should throw");
350 >            shouldThrow();
351          }
352          catch (IllegalMonitorStateException success) {
353          }
354          catch (Exception ex) {
355 <            fail("should throw IMSE");
355 >            unexpectedException();
356          }
357      }
358  
359 +    /**
360 +     * Calling signal without holding lock throws IllegalMonitorStateException
361 +     */
362      public void testSignal_IllegalMonitor() {
363          final ReentrantLock lock = new ReentrantLock();
364          final Condition c = lock.newCondition();
365          try {
366              c.signal();
367 <            fail("should throw");
367 >            shouldThrow();
368          }
369          catch (IllegalMonitorStateException success) {
370          }
371          catch (Exception ex) {
372 <            fail("should throw IMSE");
372 >            unexpectedException();
373          }
374      }
375  
376 +    /**
377 +     * awaitNanos without a signal times out
378 +     */
379      public void testAwaitNanos_Timeout() {
380          final ReentrantLock lock = new ReentrantLock();
381          final Condition c = lock.newCondition();
# Line 331 | Line 386 | public class ReentrantLockTest extends J
386              lock.unlock();
387          }
388          catch (Exception ex) {
389 <            fail("unexpected exception");
389 >            unexpectedException();
390          }
391      }
392  
393 +    /**
394 +     *  timed await without a signal times out
395 +     */
396      public void testAwait_Timeout() {
397          final ReentrantLock lock = new ReentrantLock();
398          final Condition c = lock.newCondition();
# Line 344 | Line 402 | public class ReentrantLockTest extends J
402              lock.unlock();
403          }
404          catch (Exception ex) {
405 <            fail("unexpected exception");
405 >            unexpectedException();
406          }
407      }
408  
409 +    /**
410 +     * awaitUntil without a signal times out
411 +     */
412      public void testAwaitUntil_Timeout() {
413          final ReentrantLock lock = new ReentrantLock();
414          final Condition c = lock.newCondition();
# Line 358 | Line 419 | public class ReentrantLockTest extends J
419              lock.unlock();
420          }
421          catch (Exception ex) {
422 <            fail("unexpected exception");
422 >            unexpectedException();
423          }
424      }
425  
426 +    /**
427 +     * await returns when signalled
428 +     */
429      public void testAwait() {
430          final ReentrantLock lock = new ReentrantLock();
431          final ReentrantLock.ConditionObject c = lock.newCondition();
# Line 373 | Line 437 | public class ReentrantLockTest extends J
437                          lock.unlock();
438                      }
439                      catch(InterruptedException e) {
440 <                        threadFail("unexpected exception");
440 >                        threadUnexpectedException();
441                      }
442                  }
443              });
# Line 388 | Line 452 | public class ReentrantLockTest extends J
452              assertFalse(t.isAlive());
453          }
454          catch (Exception ex) {
455 <            fail("unexpected exception");
455 >            unexpectedException();
456          }
457      }
458  
459 +    /**
460 +     * hasWaiters returns true when a thread is waiting, else false
461 +     */
462      public void testHasWaiters() {
463          final ReentrantLock lock = new ReentrantLock();
464          final ReentrantLock.ConditionObject c = lock.newCondition();
# Line 405 | Line 472 | public class ReentrantLockTest extends J
472                          lock.unlock();
473                      }
474                      catch(InterruptedException e) {
475 <                        threadFail("unexpected exception");
475 >                        threadUnexpectedException();
476                      }
477                  }
478              });
# Line 427 | Line 494 | public class ReentrantLockTest extends J
494              assertFalse(t.isAlive());
495          }
496          catch (Exception ex) {
497 <            fail("unexpected exception");
497 >            unexpectedException();
498          }
499      }
500  
501 +    /**
502 +     * getWaitQueueLength returns number of waiting threads
503 +     */
504      public void testGetWaitQueueLength() {
505          final ReentrantLock lock = new ReentrantLock();
506          final ReentrantLock.ConditionObject c = lock.newCondition();
# Line 444 | Line 514 | public class ReentrantLockTest extends J
514                          lock.unlock();
515                      }
516                      catch(InterruptedException e) {
517 <                        threadFail("unexpected exception");
517 >                        threadUnexpectedException();
518                      }
519                  }
520              });
# Line 459 | Line 529 | public class ReentrantLockTest extends J
529                          lock.unlock();
530                      }
531                      catch(InterruptedException e) {
532 <                        threadFail("unexpected exception");
532 >                        threadUnexpectedException();
533                      }
534                  }
535              });
# Line 485 | Line 555 | public class ReentrantLockTest extends J
555              assertFalse(t2.isAlive());
556          }
557          catch (Exception ex) {
558 <            fail("unexpected exception");
558 >            unexpectedException();
559          }
560      }
561  
562 +    /**
563 +     * getWaitingThreads returns only and all waiting threads
564 +     */
565      public void testGetWaitingThreads() {
566 <        final MyReentrantLock lock = new MyReentrantLock();    
567 <        final MyReentrantLock.MyCondition c = (MyReentrantLock.MyCondition)lock.newCondition();
566 >        final PublicReentrantLock lock = new PublicReentrantLock();    
567 >        final PublicReentrantLock.PublicCondition c = (PublicReentrantLock.PublicCondition)lock.newCondition();
568          Thread t1 = new Thread(new Runnable() {
569                  public void run() {
570                      try {
# Line 501 | Line 574 | public class ReentrantLockTest extends J
574                          lock.unlock();
575                      }
576                      catch(InterruptedException e) {
577 <                        threadFail("unexpected exception");
577 >                        threadUnexpectedException();
578                      }
579                  }
580              });
# Line 515 | Line 588 | public class ReentrantLockTest extends J
588                          lock.unlock();
589                      }
590                      catch(InterruptedException e) {
591 <                        threadFail("unexpected exception");
591 >                        threadUnexpectedException();
592                      }
593                  }
594              });
# Line 545 | Line 618 | public class ReentrantLockTest extends J
618              assertFalse(t2.isAlive());
619          }
620          catch (Exception ex) {
621 <            fail("unexpected exception");
621 >            unexpectedException();
622          }
623      }
624  
625 +    /**
626 +     * awaitUninterruptibly doesn't abort on interrupt
627 +     */
628      public void testAwaitUninterruptibly() {
629          final ReentrantLock lock = new ReentrantLock();
630          final Condition c = lock.newCondition();
# Line 567 | Line 643 | public class ReentrantLockTest extends J
643              lock.lock();
644              c.signal();
645              lock.unlock();
646 +            assert(t.isInterrupted());
647              t.join(SHORT_DELAY_MS);
648              assertFalse(t.isAlive());
649          }
650          catch (Exception ex) {
651 <            fail("unexpected exception");
651 >            unexpectedException();
652          }
653      }
654  
655 +    /**
656 +     * await is interruptible
657 +     */
658      public void testAwait_Interrupt() {
659          final ReentrantLock lock = new ReentrantLock();
660          final Condition c = lock.newCondition();
# Line 584 | Line 664 | public class ReentrantLockTest extends J
664                          lock.lock();
665                          c.await();
666                          lock.unlock();
667 <                        threadFail("should throw");
667 >                        threadShouldThrow();
668                      }
669                      catch(InterruptedException success) {
670                      }
# Line 599 | Line 679 | public class ReentrantLockTest extends J
679              assertFalse(t.isAlive());
680          }
681          catch (Exception ex) {
682 <            fail("unexpected exception");
682 >            unexpectedException();
683          }
684      }
685  
686 +    /**
687 +     * awaitNanos is interruptible
688 +     */
689      public void testAwaitNanos_Interrupt() {
690          final ReentrantLock lock = new ReentrantLock();
691          final Condition c = lock.newCondition();
# Line 612 | Line 695 | public class ReentrantLockTest extends J
695                          lock.lock();
696                          c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
697                          lock.unlock();
698 <                        threadFail("should throw");
698 >                        threadShouldThrow();
699                      }
700                      catch(InterruptedException success) {
701                      }
# Line 627 | Line 710 | public class ReentrantLockTest extends J
710              assertFalse(t.isAlive());
711          }
712          catch (Exception ex) {
713 <            fail("unexpected exception");
713 >            unexpectedException();
714          }
715      }
716  
717 +    /**
718 +     * awaitUntil is interruptible
719 +     */
720      public void testAwaitUntil_Interrupt() {
721          final ReentrantLock lock = new ReentrantLock();
722          final Condition c = lock.newCondition();
# Line 641 | Line 727 | public class ReentrantLockTest extends J
727                          java.util.Date d = new java.util.Date();
728                          c.awaitUntil(new java.util.Date(d.getTime() + 10000));
729                          lock.unlock();
730 <                        threadFail("should throw");
730 >                        threadShouldThrow();
731                      }
732                      catch(InterruptedException success) {
733                      }
# Line 656 | Line 742 | public class ReentrantLockTest extends J
742              assertFalse(t.isAlive());
743          }
744          catch (Exception ex) {
745 <            fail("unexpected exception");
745 >            unexpectedException();
746          }
747      }
748  
749 +    /**
750 +     * signalAll wakes up all threads
751 +     */
752      public void testSignalAll() {
753          final ReentrantLock lock = new ReentrantLock();
754          final Condition c = lock.newCondition();
# Line 671 | Line 760 | public class ReentrantLockTest extends J
760                          lock.unlock();
761                      }
762                      catch(InterruptedException e) {
763 <                        threadFail("unexpected exception");
763 >                        threadUnexpectedException();
764                      }
765                  }
766              });
# Line 684 | Line 773 | public class ReentrantLockTest extends J
773                          lock.unlock();
774                      }
775                      catch(InterruptedException e) {
776 <                        threadFail("unexpected exception");
776 >                        threadUnexpectedException();
777                      }
778                  }
779              });
# Line 702 | Line 791 | public class ReentrantLockTest extends J
791              assertFalse(t2.isAlive());
792          }
793          catch (Exception ex) {
794 <            fail("unexpected exception");
794 >            unexpectedException();
795          }
796      }
797  
798 +    /**
799 +     * A serialized lock deserializes as unlocked
800 +     */
801      public void testSerialization() {
802          ReentrantLock l = new ReentrantLock();
803          l.lock();
# Line 724 | Line 816 | public class ReentrantLockTest extends J
816              r.unlock();
817          } catch(Exception e){
818              e.printStackTrace();
819 <            fail("unexpected exception");
819 >            unexpectedException();
820          }
821      }
822  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines