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.13 by dl, Sun Dec 28 21:56:18 2003 UTC vs.
Revision 1.25 by dl, Sun Jan 29 21:17:38 2006 UTC

# Line 84 | Line 84 | public class ReentrantReadWriteLockTest
84          rl.writeLock().lock();
85          assertTrue(rl.isWriteLocked());
86          assertTrue(rl.isWriteLockedByCurrentThread());
87 +        assertTrue(rl.writeLock().isHeldByCurrentThread());
88          assertEquals(0, rl.getReadLockCount());
89          rl.writeLock().unlock();
90          assertFalse(rl.isWriteLocked());
91          assertFalse(rl.isWriteLockedByCurrentThread());
92 +        assertFalse(rl.writeLock().isHeldByCurrentThread());
93          assertEquals(0, rl.getReadLockCount());
94          rl.readLock().lock();
95          assertFalse(rl.isWriteLocked());
# Line 108 | Line 110 | public class ReentrantReadWriteLockTest
110          rl.writeLock().lock();
111          assertTrue(rl.isWriteLocked());
112          assertTrue(rl.isWriteLockedByCurrentThread());
113 +        assertTrue(rl.writeLock().isHeldByCurrentThread());
114          assertEquals(0, rl.getReadLockCount());
115          rl.writeLock().unlock();
116          assertFalse(rl.isWriteLocked());
117          assertFalse(rl.isWriteLockedByCurrentThread());
118 +        assertFalse(rl.writeLock().isHeldByCurrentThread());
119          assertEquals(0, rl.getReadLockCount());
120          rl.readLock().lock();
121          assertFalse(rl.isWriteLocked());
# Line 126 | Line 130 | public class ReentrantReadWriteLockTest
130      /**
131       * getWriteHoldCount returns number of recursive holds
132       */
133 <    public void testGetHoldCount() {
133 >    public void testGetWriteHoldCount() {
134          ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
135          for(int i = 1; i <= SIZE; i++) {
136              lock.writeLock().lock();
# Line 137 | Line 141 | public class ReentrantReadWriteLockTest
141              assertEquals(i-1,lock.getWriteHoldCount());
142          }
143      }
144 +
145 +    /**
146 +     * WriteLock.getHoldCount returns number of recursive holds
147 +     */
148 +    public void testGetHoldCount() {
149 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
150 +        for(int i = 1; i <= SIZE; i++) {
151 +            lock.writeLock().lock();
152 +            assertEquals(i,lock.writeLock().getHoldCount());
153 +        }
154 +        for(int i = SIZE; i > 0; i--) {
155 +            lock.writeLock().unlock();
156 +            assertEquals(i-1,lock.writeLock().getHoldCount());
157 +        }
158 +    }
159 +
160 +    /**
161 +     * getReadHoldCount returns number of recursive holds
162 +     */
163 +    public void testGetReadHoldCount() {
164 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
165 +        for(int i = 1; i <= SIZE; i++) {
166 +            lock.readLock().lock();
167 +            assertEquals(i,lock.getReadHoldCount());
168 +        }
169 +        for(int i = SIZE; i > 0; i--) {
170 +            lock.readLock().unlock();
171 +            assertEquals(i-1,lock.getReadHoldCount());
172 +        }
173 +    }
174      
175  
176      /**
# Line 156 | Line 190 | public class ReentrantReadWriteLockTest
190       */
191      public void testWriteLockInterruptibly_Interrupted() {
192          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
159        lock.writeLock().lock();
193          Thread t = new Thread(new Runnable() {
194                  public void run() {
195                      try {
196                          lock.writeLock().lockInterruptibly();
197 <                        threadShouldThrow();
197 >                        lock.writeLock().unlock();
198 >                        lock.writeLock().lockInterruptibly();
199 >                        lock.writeLock().unlock();
200                      } catch(InterruptedException success){}
201                  }
202              });
203          try {
204 +            lock.writeLock().lock();
205              t.start();
206              t.interrupt();
207              lock.writeLock().unlock();
# Line 176 | Line 212 | public class ReentrantReadWriteLockTest
212      }
213  
214      /**
215 <     * timed write-trylock is interruptible
215 >     * timed write-tryLock is interruptible
216       */
217      public void testWriteTryLock_Interrupted() {
218          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 185 | Line 221 | public class ReentrantReadWriteLockTest
221                  public void run() {
222                      try {
223                          lock.writeLock().tryLock(1000,TimeUnit.MILLISECONDS);
188                        threadShouldThrow();
224                      } catch(InterruptedException success){}
225                  }
226              });
# Line 209 | Line 244 | public class ReentrantReadWriteLockTest
244                  public void run() {
245                      try {
246                          lock.readLock().lockInterruptibly();
212                        threadShouldThrow();
247                      } catch(InterruptedException success){}
248                  }
249              });
# Line 224 | Line 258 | public class ReentrantReadWriteLockTest
258      }
259  
260      /**
261 <     * timed read-trylock is interruptible
261 >     * timed read-tryLock is interruptible
262       */
263      public void testReadTryLock_Interrupted() {
264          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 248 | Line 282 | public class ReentrantReadWriteLockTest
282  
283      
284      /**
285 <     * write-trylock fails if locked
285 >     * write-tryLock fails if locked
286       */
287      public void testWriteTryLockWhenLocked() {
288          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 268 | Line 302 | public class ReentrantReadWriteLockTest
302      }
303  
304      /**
305 <     * read-trylock fails if locked
305 >     * read-tryLock fails if locked
306       */
307      public void testReadTryLockWhenLocked() {
308          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 376 | Line 410 | public class ReentrantReadWriteLockTest
410          }
411      }
412  
413 +    /**
414 +     * Read trylock succeeds if write locked by current thread
415 +     */
416 +    public void testReadHoldingWriteLock() {
417 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
418 +        lock.writeLock().lock();
419 +        assertTrue(lock.readLock().tryLock());
420 +        lock.readLock().unlock();
421 +        lock.writeLock().unlock();
422 +    }
423  
424      /**
425 <     * Read trylock succeeds if readlocked but not writelocked
425 >     * Read lock succeeds if write locked by current thread even if
426 >     * other threads are waiting for readlock
427 >     */
428 >    public void testReadHoldingWriteLock2() {
429 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
430 >        lock.writeLock().lock();
431 >        Thread t1 = new Thread(new Runnable() {
432 >                public void run() {
433 >                    lock.readLock().lock();
434 >                    lock.readLock().unlock();
435 >                }
436 >            });
437 >        Thread t2 = new Thread(new Runnable() {
438 >                public void run() {
439 >                    lock.readLock().lock();
440 >                    lock.readLock().unlock();
441 >                }
442 >            });
443 >
444 >        try {
445 >            t1.start();
446 >            t2.start();
447 >            lock.readLock().lock();
448 >            lock.readLock().unlock();
449 >            Thread.sleep(SHORT_DELAY_MS);
450 >            lock.readLock().lock();
451 >            lock.readLock().unlock();
452 >            lock.writeLock().unlock();
453 >            t1.join(MEDIUM_DELAY_MS);
454 >            t2.join(MEDIUM_DELAY_MS);
455 >            assertTrue(!t1.isAlive());
456 >            assertTrue(!t2.isAlive());
457 >          
458 >        } catch(Exception e){
459 >            unexpectedException();
460 >        }
461 >    }
462 >
463 >    /**
464 >     *  Read lock succeeds if write locked by current thread even if
465 >     * other threads are waiting for writelock
466 >     */
467 >    public void testReadHoldingWriteLock3() {
468 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
469 >        lock.writeLock().lock();
470 >        Thread t1 = new Thread(new Runnable() {
471 >                public void run() {
472 >                    lock.writeLock().lock();
473 >                    lock.writeLock().unlock();
474 >                }
475 >            });
476 >        Thread t2 = new Thread(new Runnable() {
477 >                public void run() {
478 >                    lock.writeLock().lock();
479 >                    lock.writeLock().unlock();
480 >                }
481 >            });
482 >
483 >        try {
484 >            t1.start();
485 >            t2.start();
486 >            lock.readLock().lock();
487 >            lock.readLock().unlock();
488 >            Thread.sleep(SHORT_DELAY_MS);
489 >            lock.readLock().lock();
490 >            lock.readLock().unlock();
491 >            lock.writeLock().unlock();
492 >            t1.join(MEDIUM_DELAY_MS);
493 >            t2.join(MEDIUM_DELAY_MS);
494 >            assertTrue(!t1.isAlive());
495 >            assertTrue(!t2.isAlive());
496 >          
497 >        } catch(Exception e){
498 >            unexpectedException();
499 >        }
500 >    }
501 >
502 >
503 >    /**
504 >     *  Write lock succeeds if write locked by current thread even if
505 >     * other threads are waiting for writelock
506 >     */
507 >    public void testWriteHoldingWriteLock4() {
508 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
509 >        lock.writeLock().lock();
510 >        Thread t1 = new Thread(new Runnable() {
511 >                public void run() {
512 >                    lock.writeLock().lock();
513 >                    lock.writeLock().unlock();
514 >                }
515 >            });
516 >        Thread t2 = new Thread(new Runnable() {
517 >                public void run() {
518 >                    lock.writeLock().lock();
519 >                    lock.writeLock().unlock();
520 >                }
521 >            });
522 >
523 >        try {
524 >            t1.start();
525 >            t2.start();
526 >            lock.writeLock().lock();
527 >            lock.writeLock().unlock();
528 >            Thread.sleep(SHORT_DELAY_MS);
529 >            lock.writeLock().lock();
530 >            lock.writeLock().unlock();
531 >            lock.writeLock().unlock();
532 >            t1.join(MEDIUM_DELAY_MS);
533 >            t2.join(MEDIUM_DELAY_MS);
534 >            assertTrue(!t1.isAlive());
535 >            assertTrue(!t2.isAlive());
536 >          
537 >        } catch(Exception e){
538 >            unexpectedException();
539 >        }
540 >    }
541 >
542 >
543 >    /**
544 >     * Fair Read trylock succeeds if write locked by current thread
545 >     */
546 >    public void testReadHoldingWriteLockFair() {
547 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
548 >        lock.writeLock().lock();
549 >        assertTrue(lock.readLock().tryLock());
550 >        lock.readLock().unlock();
551 >        lock.writeLock().unlock();
552 >    }
553 >
554 >    /**
555 >     * Fair Read lock succeeds if write locked by current thread even if
556 >     * other threads are waiting for readlock
557 >     */
558 >    public void testReadHoldingWriteLockFair2() {
559 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
560 >        lock.writeLock().lock();
561 >        Thread t1 = new Thread(new Runnable() {
562 >                public void run() {
563 >                    lock.readLock().lock();
564 >                    lock.readLock().unlock();
565 >                }
566 >            });
567 >        Thread t2 = new Thread(new Runnable() {
568 >                public void run() {
569 >                    lock.readLock().lock();
570 >                    lock.readLock().unlock();
571 >                }
572 >            });
573 >
574 >        try {
575 >            t1.start();
576 >            t2.start();
577 >            lock.readLock().lock();
578 >            lock.readLock().unlock();
579 >            Thread.sleep(SHORT_DELAY_MS);
580 >            lock.readLock().lock();
581 >            lock.readLock().unlock();
582 >            lock.writeLock().unlock();
583 >            t1.join(MEDIUM_DELAY_MS);
584 >            t2.join(MEDIUM_DELAY_MS);
585 >            assertTrue(!t1.isAlive());
586 >            assertTrue(!t2.isAlive());
587 >          
588 >        } catch(Exception e){
589 >            unexpectedException();
590 >        }
591 >    }
592 >
593 >
594 >    /**
595 >     * Fair Read lock succeeds if write locked by current thread even if
596 >     * other threads are waiting for writelock
597 >     */
598 >    public void testReadHoldingWriteLockFair3() {
599 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
600 >        lock.writeLock().lock();
601 >        Thread t1 = new Thread(new Runnable() {
602 >                public void run() {
603 >                    lock.writeLock().lock();
604 >                    lock.writeLock().unlock();
605 >                }
606 >            });
607 >        Thread t2 = new Thread(new Runnable() {
608 >                public void run() {
609 >                    lock.writeLock().lock();
610 >                    lock.writeLock().unlock();
611 >                }
612 >            });
613 >
614 >        try {
615 >            t1.start();
616 >            t2.start();
617 >            lock.readLock().lock();
618 >            lock.readLock().unlock();
619 >            Thread.sleep(SHORT_DELAY_MS);
620 >            lock.readLock().lock();
621 >            lock.readLock().unlock();
622 >            lock.writeLock().unlock();
623 >            t1.join(MEDIUM_DELAY_MS);
624 >            t2.join(MEDIUM_DELAY_MS);
625 >            assertTrue(!t1.isAlive());
626 >            assertTrue(!t2.isAlive());
627 >          
628 >        } catch(Exception e){
629 >            unexpectedException();
630 >        }
631 >    }
632 >
633 >
634 >    /**
635 >     * Fair Write lock succeeds if write locked by current thread even if
636 >     * other threads are waiting for writelock
637 >     */
638 >    public void testWriteHoldingWriteLockFair4() {
639 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
640 >        lock.writeLock().lock();
641 >        Thread t1 = new Thread(new Runnable() {
642 >                public void run() {
643 >                    lock.writeLock().lock();
644 >                    lock.writeLock().unlock();
645 >                }
646 >            });
647 >        Thread t2 = new Thread(new Runnable() {
648 >                public void run() {
649 >                    lock.writeLock().lock();
650 >                    lock.writeLock().unlock();
651 >                }
652 >            });
653 >
654 >        try {
655 >            t1.start();
656 >            t2.start();
657 >            Thread.sleep(SHORT_DELAY_MS);
658 >            assertTrue(lock.isWriteLockedByCurrentThread());
659 >            assertTrue(lock.getWriteHoldCount() == 1);
660 >            lock.writeLock().lock();
661 >            assertTrue(lock.getWriteHoldCount() == 2);
662 >            lock.writeLock().unlock();
663 >            lock.writeLock().lock();
664 >            lock.writeLock().unlock();
665 >            lock.writeLock().unlock();
666 >            t1.join(MEDIUM_DELAY_MS);
667 >            t2.join(MEDIUM_DELAY_MS);
668 >            assertTrue(!t1.isAlive());
669 >            assertTrue(!t2.isAlive());
670 >          
671 >        } catch(Exception e){
672 >            unexpectedException();
673 >        }
674 >    }
675 >
676 >
677 >    /**
678 >     * Read tryLock succeeds if readlocked but not writelocked
679       */
680      public void testTryLockWhenReadLocked() {
681          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 401 | Line 698 | public class ReentrantReadWriteLockTest
698      
699  
700      /**
701 <     * write trylock fails when readlocked
701 >     * write tryLock fails when readlocked
702       */
703      public void testWriteTryLockWhenReadLocked() {
704          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 420 | Line 717 | public class ReentrantReadWriteLockTest
717          }
718      }
719  
720 +
721 +    /**
722 +     * Fair Read tryLock succeeds if readlocked but not writelocked
723 +     */
724 +    public void testTryLockWhenReadLockedFair() {
725 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
726 +        lock.readLock().lock();
727 +        Thread t = new Thread(new Runnable() {
728 +                public void run() {
729 +                    threadAssertTrue(lock.readLock().tryLock());
730 +                    lock.readLock().unlock();
731 +                }
732 +            });
733 +        try {
734 +            t.start();
735 +            t.join();
736 +            lock.readLock().unlock();
737 +        } catch(Exception e){
738 +            unexpectedException();
739 +        }
740 +    }
741 +
742 +    
743 +
744 +    /**
745 +     * Fair write tryLock fails when readlocked
746 +     */
747 +    public void testWriteTryLockWhenReadLockedFair() {
748 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
749 +        lock.readLock().lock();
750 +        Thread t = new Thread(new Runnable() {
751 +                public void run() {
752 +                    threadAssertFalse(lock.writeLock().tryLock());
753 +                }
754 +            });
755 +        try {
756 +            t.start();
757 +            t.join();
758 +            lock.readLock().unlock();
759 +        } catch(Exception e){
760 +            unexpectedException();
761 +        }
762 +    }
763 +
764      
765  
766      /**
767 <     * write timed trylock times out if locked
767 >     * write timed tryLock times out if locked
768       */
769      public void testWriteTryLock_Timeout() {
770          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 447 | Line 788 | public class ReentrantReadWriteLockTest
788      }
789  
790      /**
791 <     * read timed trylock times out if write-locked
791 >     * read timed tryLock times out if write-locked
792       */
793      public void testReadTryLock_Timeout() {
794          final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
# Line 591 | Line 932 | public class ReentrantReadWriteLockTest
932          final Condition c = lock.writeLock().newCondition();
933          try {
934              lock.writeLock().lock();
594            assertFalse(c.await(10, TimeUnit.MILLISECONDS));
935              lock.writeLock().unlock();
936          }
937          catch (Exception ex) {
# Line 608 | Line 948 | public class ReentrantReadWriteLockTest
948          try {
949              lock.writeLock().lock();
950              java.util.Date d = new java.util.Date();
611            assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10)));
951              lock.writeLock().unlock();
952          }
953          catch (Exception ex) {
# Line 649 | Line 988 | public class ReentrantReadWriteLockTest
988          }
989      }
990  
991 +    /** A helper class for uninterruptible wait tests */
992 +    class UninterruptableThread extends Thread {
993 +        private Lock lock;
994 +        private Condition c;
995 +        
996 +        public volatile boolean canAwake = false;
997 +        public volatile boolean interrupted = false;
998 +        public volatile boolean lockStarted = false;
999 +        
1000 +        public UninterruptableThread(Lock lock, Condition c) {
1001 +            this.lock = lock;
1002 +            this.c = c;
1003 +        }
1004 +        
1005 +        public synchronized void run() {
1006 +            lock.lock();
1007 +            lockStarted = true;
1008 +            
1009 +            while (!canAwake) {
1010 +                c.awaitUninterruptibly();
1011 +            }
1012 +            
1013 +            interrupted = isInterrupted();
1014 +            lock.unlock();
1015 +        }
1016 +    }
1017 +
1018      /**
1019       * awaitUninterruptibly doesn't abort on interrupt
1020       */
1021      public void testAwaitUninterruptibly() {
1022 <        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();      
1022 >        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1023          final Condition c = lock.writeLock().newCondition();
1024 <        Thread t = new Thread(new Runnable() {
659 <                public void run() {
660 <                    lock.writeLock().lock();
661 <                    c.awaitUninterruptibly();
662 <                    lock.writeLock().unlock();
663 <                }
664 <            });
1024 >        UninterruptableThread thread = new UninterruptableThread(lock.writeLock(), c);
1025  
1026          try {
1027 <            t.start();
1028 <            Thread.sleep(SHORT_DELAY_MS);
1029 <            t.interrupt();
1027 >            thread.start();
1028 >
1029 >            while (!thread.lockStarted) {
1030 >                Thread.sleep(100);
1031 >            }
1032 >
1033              lock.writeLock().lock();
1034 <            c.signal();
1035 <            lock.writeLock().unlock();
1036 <            assert(t.isInterrupted());
1037 <            t.join(SHORT_DELAY_MS);
1038 <            assertFalse(t.isAlive());
1039 <        }
1040 <        catch (Exception ex) {
1034 >            try {
1035 >                thread.interrupt();
1036 >                thread.canAwake = true;
1037 >                c.signal();
1038 >            } finally {
1039 >                lock.writeLock().unlock();
1040 >            }
1041 >
1042 >            thread.join();
1043 >            assertTrue(thread.interrupted);
1044 >            assertFalse(thread.isAlive());
1045 >        } catch (Exception ex) {
1046              unexpectedException();
1047          }
1048      }
# Line 877 | Line 1245 | public class ReentrantReadWriteLockTest
1245      }
1246  
1247      /**
1248 +     * hasQueuedThread(null) throws NPE
1249 +     */
1250 +    public void testHasQueuedThreadNPE() {
1251 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1252 +        try {
1253 +            sync.hasQueuedThread(null);
1254 +            shouldThrow();
1255 +        } catch (NullPointerException success) {
1256 +        }
1257 +    }
1258 +
1259 +    /**
1260 +     * hasQueuedThread reports whether a thread is queued.
1261 +     */
1262 +    public void testHasQueuedThread() {
1263 +        final ReentrantReadWriteLock sync = new ReentrantReadWriteLock();
1264 +        Thread t1 = new Thread(new InterruptedLockRunnable(sync));
1265 +        Thread t2 = new Thread(new InterruptibleLockRunnable(sync));
1266 +        try {
1267 +            assertFalse(sync.hasQueuedThread(t1));
1268 +            assertFalse(sync.hasQueuedThread(t2));
1269 +            sync.writeLock().lock();
1270 +            t1.start();
1271 +            Thread.sleep(SHORT_DELAY_MS);
1272 +            assertTrue(sync.hasQueuedThread(t1));
1273 +            t2.start();
1274 +            Thread.sleep(SHORT_DELAY_MS);
1275 +            assertTrue(sync.hasQueuedThread(t1));
1276 +            assertTrue(sync.hasQueuedThread(t2));
1277 +            t1.interrupt();
1278 +            Thread.sleep(SHORT_DELAY_MS);
1279 +            assertFalse(sync.hasQueuedThread(t1));
1280 +            assertTrue(sync.hasQueuedThread(t2));
1281 +            sync.writeLock().unlock();
1282 +            Thread.sleep(SHORT_DELAY_MS);
1283 +            assertFalse(sync.hasQueuedThread(t1));
1284 +            Thread.sleep(SHORT_DELAY_MS);
1285 +            assertFalse(sync.hasQueuedThread(t2));
1286 +            t1.join();
1287 +            t2.join();
1288 +        } catch(Exception e){
1289 +            unexpectedException();
1290 +        }
1291 +    }
1292 +
1293 +
1294 +    /**
1295       * getQueueLength reports number of waiting threads
1296       */
1297      public void testGetQueueLength() {
# Line 938 | Line 1353 | public class ReentrantReadWriteLockTest
1353      }
1354  
1355      /**
1356 +     * hasWaiters throws NPE if null
1357 +     */
1358 +    public void testHasWaitersNPE() {
1359 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1360 +        try {
1361 +            lock.hasWaiters(null);
1362 +            shouldThrow();
1363 +        } catch (NullPointerException success) {
1364 +        } catch (Exception ex) {
1365 +            unexpectedException();
1366 +        }
1367 +    }
1368 +
1369 +    /**
1370 +     * getWaitQueueLength throws NPE if null
1371 +     */
1372 +    public void testGetWaitQueueLengthNPE() {
1373 +        final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1374 +        try {
1375 +            lock.getWaitQueueLength(null);
1376 +            shouldThrow();
1377 +        } catch (NullPointerException success) {
1378 +        } catch (Exception ex) {
1379 +            unexpectedException();
1380 +        }
1381 +    }
1382 +
1383 +
1384 +    /**
1385 +     * getWaitingThreads throws NPE if null
1386 +     */
1387 +    public void testGetWaitingThreadsNPE() {
1388 +        final PublicReentrantReadWriteLock lock = new PublicReentrantReadWriteLock();
1389 +        try {
1390 +            lock.getWaitingThreads(null);
1391 +            shouldThrow();
1392 +        } catch (NullPointerException success) {
1393 +        } catch (Exception ex) {
1394 +            unexpectedException();
1395 +        }
1396 +    }
1397 +
1398 +    /**
1399       * hasWaiters throws IAE if not owned
1400       */
1401      public void testHasWaitersIAE() {
# Line 1181 | Line 1639 | public class ReentrantReadWriteLockTest
1639          }
1640      }
1641  
1642 +    /**
1643 +     * toString indicates current lock state
1644 +     */
1645 +    public void testToString() {
1646 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1647 +        String us = lock.toString();
1648 +        assertTrue(us.indexOf("Write locks = 0") >= 0);
1649 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1650 +        lock.writeLock().lock();
1651 +        String ws = lock.toString();
1652 +        assertTrue(ws.indexOf("Write locks = 1") >= 0);
1653 +        assertTrue(ws.indexOf("Read locks = 0") >= 0);
1654 +        lock.writeLock().unlock();
1655 +        lock.readLock().lock();
1656 +        lock.readLock().lock();
1657 +        String rs = lock.toString();
1658 +        assertTrue(rs.indexOf("Write locks = 0") >= 0);
1659 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1660 +    }
1661 +
1662 +    /**
1663 +     * readLock.toString indicates current lock state
1664 +     */
1665 +    public void testReadLockToString() {
1666 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1667 +        String us = lock.readLock().toString();
1668 +        assertTrue(us.indexOf("Read locks = 0") >= 0);
1669 +        lock.readLock().lock();
1670 +        lock.readLock().lock();
1671 +        String rs = lock.readLock().toString();
1672 +        assertTrue(rs.indexOf("Read locks = 2") >= 0);
1673 +    }
1674 +
1675 +    /**
1676 +     * writeLock.toString indicates current lock state
1677 +     */
1678 +    public void testWriteLockToString() {
1679 +        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
1680 +        String us = lock.writeLock().toString();
1681 +        assertTrue(us.indexOf("Unlocked") >= 0);
1682 +        lock.writeLock().lock();
1683 +        String ls = lock.writeLock().toString();
1684 +        assertTrue(ls.indexOf("Locked") >= 0);
1685 +    }
1686 +
1687   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines