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

Comparing jsr166/src/test/tck/ReentrantReadWriteLockTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.4 by dl, Sat Sep 20 18:20:08 2003 UTC

# Line 18 | Line 18 | public class ReentrantReadWriteLockTest
18          return new TestSuite(ReentrantReadWriteLockTest.class);
19      }
20  
21    static int HOLD_COUNT_TEST_LIMIT = 20;
21  
22      /*
23 <     * Unlocks an unlocked lock, throws Illegal Monitor State
25 <     *
23 >     * write-locking and read-locking an unlocked lock succeed
24       */
25 <    public void testIllegalMonitorStateException(){
25 >    public void testLock() {
26          ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
27 <        try{
28 <            rl.writeLock().unlock();
29 <            fail("Should of thown Illegal Monitor State Exception");
27 >        rl.writeLock().lock();
28 >        rl.writeLock().unlock();
29 >        rl.readLock().lock();
30 >        rl.readLock().unlock();
31 >    }
32 >
33  
34 <        }catch(IllegalMonitorStateException success){}
34 >    /*
35 >     * locking an unlocked fair lock succeeds
36 >     */
37 >    public void testFairLock() {
38 >        ReentrantReadWriteLock rl = new ReentrantReadWriteLock(true);
39 >        rl.writeLock().lock();
40 >        rl.writeLock().unlock();
41 >        rl.readLock().lock();
42 >        rl.readLock().unlock();
43      }
44  
45 +    /**
46 +     * write-unlocking an unlocked lock throws IllegalMonitorStateException
47 +     */
48 +    public void testUnlock_IllegalMonitorStateException() {
49 +        ReentrantReadWriteLock rl = new ReentrantReadWriteLock();
50 +        try {
51 +            rl.writeLock().unlock();
52 +            shouldThrow();
53 +        } catch(IllegalMonitorStateException success){}
54 +    }
55  
56  
57 <    public void testInterruptedException(){
57 >    /**
58 >     * write-lockInterruptibly is interruptible
59 >     */
60 >    public void testWriteLockInterruptibly_Interrupted() {
61          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
62          lock.writeLock().lock();
63          Thread t = new Thread(new Runnable() {
64 <                public void run(){
65 <                    try{
64 >                public void run() {
65 >                    try {
66                          lock.writeLock().lockInterruptibly();
67 <                        threadFail("should throw");
68 <                    }catch(InterruptedException success){}
67 >                        threadShouldThrow();
68 >                    } catch(InterruptedException success){}
69                  }
70              });
71          try {
# Line 52 | Line 74 | public class ReentrantReadWriteLockTest
74              lock.writeLock().unlock();
75              t.join();
76          } catch(Exception e){
77 <            fail("unexpected exception");
77 >            unexpectedException();
78          }
79      }
80  
81 <    public void testInterruptedException2(){
81 >    /**
82 >     * timed write-trylock is interruptible
83 >     */
84 >    public void testWriteTryLock_Interrupted() {
85          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
86          lock.writeLock().lock();
87          Thread t = new Thread(new Runnable() {
88 <                public void run(){
89 <                    try{
88 >                public void run() {
89 >                    try {
90                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
91 <                        threadFail("should throw");
92 <                    }catch(InterruptedException success){}
91 >                        threadShouldThrow();
92 >                    } catch(InterruptedException success){}
93                  }
94              });
95          try {
# Line 73 | Line 98 | public class ReentrantReadWriteLockTest
98              lock.writeLock().unlock();
99              t.join();
100          } catch(Exception e){
101 <            fail("unexpected exception");
101 >            unexpectedException();
102          }
103      }
104  
105 <    public void testInterruptedException3(){
105 >    /**
106 >     * read-lockInterruptibly is interruptible
107 >     */
108 >    public void testReadLockInterruptibly_Interrupted() {
109          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
110          lock.writeLock().lock();
111          Thread t = new Thread(new Runnable() {
112 <                public void run(){
113 <                    try{
112 >                public void run() {
113 >                    try {
114                          lock.readLock().lockInterruptibly();
115 <                        threadFail("should throw");
116 <                    }catch(InterruptedException success){}
115 >                        threadShouldThrow();
116 >                    } catch(InterruptedException success){}
117                  }
118              });
119          try {
# Line 94 | Line 122 | public class ReentrantReadWriteLockTest
122              lock.writeLock().unlock();
123              t.join();
124          } catch(Exception e){
125 <            fail("unexpected exception");
125 >            unexpectedException();
126          }
127      }
128  
129 <    public void testInterruptedException4(){
129 >    /**
130 >     * timed read-trylock is interruptible
131 >     */
132 >    public void testReadTryLock_Interrupted() {
133          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
134          lock.writeLock().lock();
135          Thread t = new Thread(new Runnable() {
136 <                public void run(){
137 <                    try{
136 >                public void run() {
137 >                    try {
138                          lock.readLock().tryLock(1000,TimeUnit.MILLISECONDS);
139 <                        threadFail("should throw");
140 <                    }catch(InterruptedException success){}
139 >                        threadShouldThrow();
140 >                    } catch(InterruptedException success){}
141                  }
142              });
143          try {
# Line 114 | Line 145 | public class ReentrantReadWriteLockTest
145              t.interrupt();
146              t.join();
147          } catch(Exception e){
148 <            fail("unexpected exception");
148 >            unexpectedException();
149          }
150      }
151  
152      
153 <    public void testTryLockWhenLocked() {
153 >    /**
154 >     * write-trylock fails if locked
155 >     */
156 >    public void testWriteTryLockWhenLocked() {
157          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
158          lock.writeLock().lock();
159          Thread t = new Thread(new Runnable() {
160 <                public void run(){
160 >                public void run() {
161                      threadAssertFalse(lock.writeLock().tryLock());
162                  }
163              });
# Line 132 | Line 166 | public class ReentrantReadWriteLockTest
166              t.join();
167              lock.writeLock().unlock();
168          } catch(Exception e){
169 <            fail("unexpected exception");
169 >            unexpectedException();
170          }
171      }
172  
173 <    public void testTryLockWhenLocked2() {
173 >    /**
174 >     * read-trylock fails if locked
175 >     */
176 >    public void testReadTryLockWhenLocked() {
177          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
178          lock.writeLock().lock();
179          Thread t = new Thread(new Runnable() {
180 <                public void run(){
180 >                public void run() {
181                      threadAssertFalse(lock.readLock().tryLock());
182                  }
183              });
# Line 149 | Line 186 | public class ReentrantReadWriteLockTest
186              t.join();
187              lock.writeLock().unlock();
188          } catch(Exception e){
189 <            fail("unexpected exception");
189 >            unexpectedException();
190          }
191      }
192  
193 +    /**
194 +     * Multiple threads can hold a read lock when not write-locked
195 +     */
196      public void testMultipleReadLocks() {
197          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
198          lock.readLock().lock();
199          Thread t = new Thread(new Runnable() {
200 <                public void run(){
200 >                public void run() {
201                      threadAssertTrue(lock.readLock().tryLock());
202                      lock.readLock().unlock();
203                  }
# Line 167 | Line 207 | public class ReentrantReadWriteLockTest
207              t.join();
208              lock.readLock().unlock();
209          } catch(Exception e){
210 <            fail("unexpected exception");
210 >            unexpectedException();
211          }
212      }
213  
214 +    /**
215 +     * A writelock succeeds after reading threads unlock
216 +     */
217      public void testWriteAfterMultipleReadLocks() {
218          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
219          lock.readLock().lock();
220          Thread t1 = new Thread(new Runnable() {
221 <                public void run(){
221 >                public void run() {
222                      lock.readLock().lock();
223                      lock.readLock().unlock();
224                  }
225              });
226          Thread t2 = new Thread(new Runnable() {
227 <                public void run(){
227 >                public void run() {
228                      lock.writeLock().lock();
229                      lock.writeLock().unlock();
230                  }
# Line 198 | Line 241 | public class ReentrantReadWriteLockTest
241              assertTrue(!t2.isAlive());
242            
243          } catch(Exception e){
244 <            fail("unexpected exception");
244 >            unexpectedException();
245          }
246      }
247  
248 +    /**
249 +     * Readlocks succeed after a writing thread unlocks
250 +     */
251      public void testReadAfterWriteLock() {
252          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
253          lock.writeLock().lock();
254          Thread t1 = new Thread(new Runnable() {
255 <                public void run(){
255 >                public void run() {
256                      lock.readLock().lock();
257                      lock.readLock().unlock();
258                  }
259              });
260          Thread t2 = new Thread(new Runnable() {
261 <                public void run(){
261 >                public void run() {
262                      lock.readLock().lock();
263                      lock.readLock().unlock();
264                  }
# Line 229 | Line 275 | public class ReentrantReadWriteLockTest
275              assertTrue(!t2.isAlive());
276            
277          } catch(Exception e){
278 <            fail("unexpected exception");
278 >            unexpectedException();
279          }
280      }
281  
282  
283 +    /**
284 +     * Read trylock succeeds if readlocked but not writelocked
285 +     */
286      public void testTryLockWhenReadLocked() {
287          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
288          lock.readLock().lock();
289          Thread t = new Thread(new Runnable() {
290 <                public void run(){
290 >                public void run() {
291                      threadAssertTrue(lock.readLock().tryLock());
292                      lock.readLock().unlock();
293                  }
# Line 248 | Line 297 | public class ReentrantReadWriteLockTest
297              t.join();
298              lock.readLock().unlock();
299          } catch(Exception e){
300 <            fail("unexpected exception");
300 >            unexpectedException();
301          }
302      }
303  
304      
305  
306 +    /**
307 +     * write trylock fails when readlocked
308 +     */
309      public void testWriteTryLockWhenReadLocked() {
310          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
311          lock.readLock().lock();
312          Thread t = new Thread(new Runnable() {
313 <                public void run(){
313 >                public void run() {
314                      threadAssertFalse(lock.writeLock().tryLock());
315                  }
316              });
# Line 267 | Line 319 | public class ReentrantReadWriteLockTest
319              t.join();
320              lock.readLock().unlock();
321          } catch(Exception e){
322 <            fail("unexpected exception");
322 >            unexpectedException();
323          }
324      }
325  
326      
327  
328 <    public void testTryLock_Timeout(){
328 >    /**
329 >     * write timed trylock times out if locked
330 >     */
331 >    public void testWriteTryLock_Timeout() {
332          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
333          lock.writeLock().lock();
334          Thread t = new Thread(new Runnable() {
335 <                public void run(){
335 >                public void run() {
336                      try {
337                          threadAssertFalse(lock.writeLock().tryLock(1, TimeUnit.MILLISECONDS));
338                      } catch (Exception ex) {
339 <                        threadFail("unexpected exception");
339 >                        threadUnexpectedException();
340                      }
341                  }
342              });
# Line 290 | Line 345 | public class ReentrantReadWriteLockTest
345              t.join();
346              lock.writeLock().unlock();
347          } catch(Exception e){
348 <            fail("unexpected exception");
348 >            unexpectedException();
349          }
350      }
351  
352 <    public void testTryLock_Timeout2(){
352 >    /**
353 >     * read timed trylock times out if write-locked
354 >     */
355 >    public void testReadTryLock_Timeout() {
356          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
357          lock.writeLock().lock();
358          Thread t = new Thread(new Runnable() {
359 <                public void run(){
359 >                public void run() {
360                      try {
361                          threadAssertFalse(lock.readLock().tryLock(1, TimeUnit.MILLISECONDS));
362                      } catch (Exception ex) {
363 <                        threadFail("unexpected exception");
363 >                        threadUnexpectedException();
364                      }
365                  }
366              });
# Line 311 | Line 369 | public class ReentrantReadWriteLockTest
369              t.join();
370              lock.writeLock().unlock();
371          } catch(Exception e){
372 <            fail("unexpected exception");
372 >            unexpectedException();
373          }
374      }
375  
376  
377 <    public void testLockInterruptibly() {
377 >    /**
378 >     * write lockInterruptibly succeeds if lock free else is interruptible
379 >     */
380 >    public void testWriteLockInterruptibly() {
381          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
382          try {
383              lock.writeLock().lockInterruptibly();
384          } catch(Exception e) {
385 <            fail("unexpected exception");
385 >            unexpectedException();
386          }
387          Thread t = new Thread(new Runnable() {
388                  public void run() {
389                      try {
390                          lock.writeLock().lockInterruptibly();
391 <                        threadFail("should throw");
391 >                        threadShouldThrow();
392                      }
393                      catch(InterruptedException success) {
394                      }
# Line 339 | Line 400 | public class ReentrantReadWriteLockTest
400              t.join();
401              lock.writeLock().unlock();
402          } catch(Exception e){
403 <            fail("unexpected exception");
403 >            unexpectedException();
404          }
405      }
406  
407 <    public void testLockInterruptibly2() {
407 >    /**
408 >     *  read lockInterruptibly succeeds if lock free else is interruptible
409 >     */
410 >    public void testReadLockInterruptibly() {
411          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
412          try {
413              lock.writeLock().lockInterruptibly();
414          } catch(Exception e) {
415 <            fail("unexpected exception");
415 >            unexpectedException();
416          }
417          Thread t = new Thread(new Runnable() {
418                  public void run() {
419                      try {
420                          lock.readLock().lockInterruptibly();
421 <                        threadFail("should throw");
421 >                        threadShouldThrow();
422                      }
423                      catch(InterruptedException success) {
424                      }
# Line 366 | Line 430 | public class ReentrantReadWriteLockTest
430              t.join();
431              lock.writeLock().unlock();
432          } catch(Exception e){
433 <            fail("unexpected exception");
433 >            unexpectedException();
434          }
435      }
436  
437 +    /**
438 +     * Calling await without holding lock throws IllegalMonitorStateException
439 +     */
440      public void testAwait_IllegalMonitor() {
441          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
442          final Condition c = lock.writeLock().newCondition();
443          try {
444              c.await();
445 <            fail("should throw");
445 >            shouldThrow();
446          }
447          catch (IllegalMonitorStateException success) {
448          }
449          catch (Exception ex) {
450 <            fail("should throw IMSE");
450 >            shouldThrow();
451          }
452      }
453  
454 +    /**
455 +     * Calling signal without holding lock throws IllegalMonitorStateException
456 +     */
457      public void testSignal_IllegalMonitor() {
458          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
459          final Condition c = lock.writeLock().newCondition();
460          try {
461              c.signal();
462 <            fail("should throw");
462 >            shouldThrow();
463          }
464          catch (IllegalMonitorStateException success) {
465          }
466          catch (Exception ex) {
467 <            fail("should throw IMSE");
467 >            unexpectedException();
468          }
469      }
470  
471 +    /**
472 +     * awaitNanos without a signal times out
473 +     */
474      public void testAwaitNanos_Timeout() {
475          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
476          final Condition c = lock.writeLock().newCondition();
# Line 408 | Line 481 | public class ReentrantReadWriteLockTest
481              lock.writeLock().unlock();
482          }
483          catch (Exception ex) {
484 <            fail("unexpected exception");
484 >            unexpectedException();
485          }
486      }
487  
488 +
489 +    /**
490 +     *  timed await without a signal times out
491 +     */
492      public void testAwait_Timeout() {
493          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
494          final Condition c = lock.writeLock().newCondition();
# Line 421 | Line 498 | public class ReentrantReadWriteLockTest
498              lock.writeLock().unlock();
499          }
500          catch (Exception ex) {
501 <            fail("unexpected exception");
501 >            unexpectedException();
502          }
503      }
504  
505 +    /**
506 +     * awaitUntil without a signal times out
507 +     */
508      public void testAwaitUntil_Timeout() {
509          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
510          final Condition c = lock.writeLock().newCondition();
# Line 435 | Line 515 | public class ReentrantReadWriteLockTest
515              lock.writeLock().unlock();
516          }
517          catch (Exception ex) {
518 <            fail("unexpected exception");
518 >            unexpectedException();
519          }
520      }
521  
522 +    /**
523 +     * await returns when signalled
524 +     */
525      public void testAwait() {
526          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
527          final Condition c = lock.writeLock().newCondition();
# Line 450 | Line 533 | public class ReentrantReadWriteLockTest
533                          lock.writeLock().unlock();
534                      }
535                      catch(InterruptedException e) {
536 <                        threadFail("unexpected exception");
536 >                        threadUnexpectedException();
537                      }
538                  }
539              });
# Line 465 | Line 548 | public class ReentrantReadWriteLockTest
548              assertFalse(t.isAlive());
549          }
550          catch (Exception ex) {
551 <            fail("unexpected exception");
551 >            unexpectedException();
552          }
553      }
554  
555 +    /**
556 +     * awaitUninterruptibly doesn't abort on interrupt
557 +     */
558      public void testAwaitUninterruptibly() {
559          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
560          final Condition c = lock.writeLock().newCondition();
# Line 487 | Line 573 | public class ReentrantReadWriteLockTest
573              lock.writeLock().lock();
574              c.signal();
575              lock.writeLock().unlock();
576 +            assert(t.isInterrupted());
577              t.join(SHORT_DELAY_MS);
578              assertFalse(t.isAlive());
579          }
580          catch (Exception ex) {
581 <            fail("unexpected exception");
581 >            unexpectedException();
582          }
583      }
584  
585 +    /**
586 +     * await is interruptible
587 +     */
588      public void testAwait_Interrupt() {
589          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
590          final Condition c = lock.writeLock().newCondition();
# Line 504 | Line 594 | public class ReentrantReadWriteLockTest
594                          lock.writeLock().lock();
595                          c.await();
596                          lock.writeLock().unlock();
597 <                        threadFail("should throw");
597 >                        threadShouldThrow();
598                      }
599                      catch(InterruptedException success) {
600                      }
# Line 519 | Line 609 | public class ReentrantReadWriteLockTest
609              assertFalse(t.isAlive());
610          }
611          catch (Exception ex) {
612 <            fail("unexpected exception");
612 >            unexpectedException();
613          }
614      }
615  
616 +    /**
617 +     * awaitNanos is interruptible
618 +     */
619      public void testAwaitNanos_Interrupt() {
620          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
621          final Condition c = lock.writeLock().newCondition();
# Line 532 | Line 625 | public class ReentrantReadWriteLockTest
625                          lock.writeLock().lock();
626                          c.awaitNanos(SHORT_DELAY_MS * 2 * 1000000);
627                          lock.writeLock().unlock();
628 <                        threadFail("should throw");
628 >                        threadShouldThrow();
629                      }
630                      catch(InterruptedException success) {
631                      }
# Line 547 | Line 640 | public class ReentrantReadWriteLockTest
640              assertFalse(t.isAlive());
641          }
642          catch (Exception ex) {
643 <            fail("unexpected exception");
643 >            unexpectedException();
644          }
645      }
646  
647 +    /**
648 +     * awaitUntil is interruptible
649 +     */
650      public void testAwaitUntil_Interrupt() {
651          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
652          final Condition c = lock.writeLock().newCondition();
# Line 561 | Line 657 | public class ReentrantReadWriteLockTest
657                          java.util.Date d = new java.util.Date();
658                          c.awaitUntil(new java.util.Date(d.getTime() + 10000));
659                          lock.writeLock().unlock();
660 <                        threadFail("should throw");
660 >                        threadShouldThrow();
661                      }
662                      catch(InterruptedException success) {
663                      }
# Line 576 | Line 672 | public class ReentrantReadWriteLockTest
672              assertFalse(t.isAlive());
673          }
674          catch (Exception ex) {
675 <            fail("unexpected exception");
675 >            unexpectedException();
676          }
677      }
678  
679 +    /**
680 +     * signalAll wakes up all threads
681 +     */
682      public void testSignalAll() {
683          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
684          final Condition c = lock.writeLock().newCondition();
# Line 591 | Line 690 | public class ReentrantReadWriteLockTest
690                          lock.writeLock().unlock();
691                      }
692                      catch(InterruptedException e) {
693 <                        threadFail("unexpected exception");
693 >                        threadUnexpectedException();
694                      }
695                  }
696              });
# Line 604 | Line 703 | public class ReentrantReadWriteLockTest
703                          lock.writeLock().unlock();
704                      }
705                      catch(InterruptedException e) {
706 <                        threadFail("unexpected exception");
706 >                        threadUnexpectedException();
707                      }
708                  }
709              });
# Line 622 | Line 721 | public class ReentrantReadWriteLockTest
721              assertFalse(t2.isAlive());
722          }
723          catch (Exception ex) {
724 <            fail("unexpected exception");
724 >            unexpectedException();
725          }
726      }
727  
728 +    /**
729 +     * A serialized lock deserializes as unlocked
730 +     */
731      public void testSerialization() {
732          ReentrantReadWriteLock l = new ReentrantReadWriteLock();
733          l.readLock().lock();
# Line 644 | Line 746 | public class ReentrantReadWriteLockTest
746              r.readLock().unlock();
747          } catch(Exception e){
748              e.printStackTrace();
749 <            fail("unexpected exception");
749 >            unexpectedException();
750          }
751      }
752  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines