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.42 by jsr166, Sat May 7 03:12:48 2011 UTC vs.
Revision 1.44 by jsr166, Sat May 7 03:40:45 2011 UTC

# Line 59 | Line 59 | public class ReentrantLockTest extends J
59      }
60  
61      /**
62 +     * Checks that condition c has no waiters.
63 +     */
64 +    void assertHasNoWaiters(PublicReentrantLock lock, Condition c) {
65 +        assertHasWaiters(lock, c, new Thread[] {});
66 +    }
67 +
68 +    /**
69 +     * Checks that condition c has exactly the given waiter threads.
70 +     */
71 +    void assertHasWaiters(PublicReentrantLock lock, Condition c,
72 +                          Thread... threads) {
73 +        lock.lock();
74 +        assertEquals(threads.length > 0, lock.hasWaiters(c));
75 +        assertEquals(threads.length, lock.getWaitQueueLength(c));
76 +        assertEquals(threads.length == 0, lock.getWaitingThreads(c).isEmpty());
77 +        assertEquals(threads.length, lock.getWaitingThreads(c).size());
78 +        assertEquals(new HashSet<Thread>(lock.getWaitingThreads(c)),
79 +                     new HashSet<Thread>(Arrays.asList(threads)));
80 +        lock.unlock();
81 +    }
82 +
83 +    /**
84       * Constructor sets given fairness
85       */
86      public void testConstructor() {
# Line 735 | Line 757 | public class ReentrantLockTest extends J
757       * await is interruptible
758       */
759      public void testAwait_Interrupt() throws InterruptedException {
760 <        final ReentrantLock lock = new ReentrantLock();
760 >        final PublicReentrantLock lock = new PublicReentrantLock();
761          final Condition c = lock.newCondition();
762 +        final CountDownLatch locked = new CountDownLatch(1);
763          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
764              public void realRun() throws InterruptedException {
765                  lock.lock();
766 <                c.await();
766 >                assertTrue(lock.isLocked());
767 >                assertTrue(lock.isHeldByCurrentThread());
768 >                assertHasNoWaiters(lock, c);
769 >                locked.countDown();
770 >                try {
771 >                    c.await();
772 >                } finally {
773 >                    assertTrue(lock.isLocked());
774 >                    assertTrue(lock.isHeldByCurrentThread());
775 >                    assertHasNoWaiters(lock, c);
776 >                    lock.unlock();
777 >                    assertFalse(Thread.interrupted());
778 >                }
779              }});
780  
781 <        delay(SHORT_DELAY_MS);
781 >        locked.await();
782 >        assertHasWaiters(lock, c, t);
783          t.interrupt();
784          awaitTermination(t);
785 +        assertFalse(lock.isLocked());
786      }
787  
788      /**
789       * awaitNanos is interruptible
790       */
791      public void testAwaitNanos_Interrupt() throws InterruptedException {
792 <        final ReentrantLock lock = new ReentrantLock();
792 >        final PublicReentrantLock lock = new PublicReentrantLock();
793          final Condition c = lock.newCondition();
794 +        final CountDownLatch locked = new CountDownLatch(1);
795          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
796              public void realRun() throws InterruptedException {
797                  lock.lock();
798 <                c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
798 >                assertTrue(lock.isLocked());
799 >                assertTrue(lock.isHeldByCurrentThread());
800 >                assertHasNoWaiters(lock, c);
801 >                locked.countDown();
802 >                try {
803 >                    c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS));
804 >                } finally {
805 >                    assertTrue(lock.isLocked());
806 >                    assertTrue(lock.isHeldByCurrentThread());
807 >                    assertHasNoWaiters(lock, c);
808 >                    lock.unlock();
809 >                    assertFalse(Thread.interrupted());
810 >                }
811              }});
812  
813 <        delay(SHORT_DELAY_MS);
813 >        locked.await();
814 >        assertHasWaiters(lock, c, t);
815          t.interrupt();
816          awaitTermination(t);
817 +        assertFalse(lock.isLocked());
818      }
819  
820      /**
821       * awaitUntil is interruptible
822       */
823      public void testAwaitUntil_Interrupt() throws InterruptedException {
824 <        final ReentrantLock lock = new ReentrantLock();
824 >        final PublicReentrantLock lock = new PublicReentrantLock();
825          final Condition c = lock.newCondition();
826 +        final CountDownLatch locked = new CountDownLatch(1);
827          Thread t = newStartedThread(new CheckedInterruptedRunnable() {
828              public void realRun() throws InterruptedException {
829                  lock.lock();
830 +                assertTrue(lock.isLocked());
831 +                assertTrue(lock.isHeldByCurrentThread());
832 +                assertHasNoWaiters(lock, c);
833 +                locked.countDown();
834                  java.util.Date d = new java.util.Date();
835 <                c.awaitUntil(new java.util.Date(d.getTime() + 10000));
835 >                try {
836 >                    c.awaitUntil(new java.util.Date(d.getTime() + 10000));
837 >                } finally {
838 >                    assertTrue(lock.isLocked());
839 >                    assertTrue(lock.isHeldByCurrentThread());
840 >                    assertHasNoWaiters(lock, c);
841 >                    lock.unlock();
842 >                    assertFalse(Thread.interrupted());
843 >                }
844              }});
845  
846 <        delay(SHORT_DELAY_MS);
846 >        locked.await();
847 >        assertHasWaiters(lock, c, t);
848          t.interrupt();
849          awaitTermination(t);
850 +        assertFalse(lock.isLocked());
851      }
852  
853      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines