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.41 by jsr166, Sat May 7 02:51:33 2011 UTC vs.
Revision 1.43 by jsr166, Sat May 7 03:36:23 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 131 | Line 153 | public class ReentrantLockTest extends J
153          lock.unlock();
154          delay(SHORT_DELAY_MS);
155          assertFalse(lock.hasQueuedThreads());
156 <        t1.join();
157 <        t2.join();
156 >        awaitTermination(t1);
157 >        awaitTermination(t2);
158      }
159  
160      /**
# Line 156 | Line 178 | public class ReentrantLockTest extends J
178          lock.unlock();
179          delay(SHORT_DELAY_MS);
180          assertEquals(0, lock.getQueueLength());
181 <        t1.join();
182 <        t2.join();
181 >        awaitTermination(t1);
182 >        awaitTermination(t2);
183      }
184  
185      /**
# Line 181 | Line 203 | public class ReentrantLockTest extends J
203          lock.unlock();
204          delay(SHORT_DELAY_MS);
205          assertEquals(0, lock.getQueueLength());
206 <        t1.join();
207 <        t2.join();
206 >        awaitTermination(t1);
207 >        awaitTermination(t2);
208      }
209  
210      /**
# Line 222 | Line 244 | public class ReentrantLockTest extends J
244          assertFalse(sync.hasQueuedThread(t1));
245          delay(SHORT_DELAY_MS);
246          assertFalse(sync.hasQueuedThread(t2));
247 <        t1.join();
248 <        t2.join();
247 >        awaitTermination(t1);
248 >        awaitTermination(t2);
249      }
250  
251  
# Line 251 | Line 273 | public class ReentrantLockTest extends J
273          lock.unlock();
274          delay(SHORT_DELAY_MS);
275          assertTrue(lock.getQueuedThreads().isEmpty());
276 <        t1.join();
277 <        t2.join();
276 >        awaitTermination(t1);
277 >        awaitTermination(t2);
278      }
279  
280  
# Line 269 | Line 291 | public class ReentrantLockTest extends J
291  
292          delay(SHORT_DELAY_MS);
293          t.interrupt();
294 <        t.join();
294 >        awaitTermination(t);
295      }
296  
297  
# Line 284 | Line 306 | public class ReentrantLockTest extends J
306                  assertFalse(lock.tryLock());
307              }});
308  
309 <        t.join();
309 >        awaitTermination(t);
310          lock.unlock();
311      }
312  
# Line 299 | Line 321 | public class ReentrantLockTest extends J
321                  assertFalse(lock.tryLock(1, MILLISECONDS));
322              }});
323  
324 <        t.join();
324 >        awaitTermination(t);
325          lock.unlock();
326      }
327  
# Line 337 | Line 359 | public class ReentrantLockTest extends J
359  
360          delay(SHORT_DELAY_MS);
361          assertTrue(lock.isLocked());
362 <        t.join();
362 >        awaitTermination(t);
363          assertFalse(lock.isLocked());
364      }
365  
# Line 353 | Line 375 | public class ReentrantLockTest extends J
375          t.interrupt();
376          delay(SHORT_DELAY_MS);
377          lock.unlock();
378 <        t.join();
378 >        awaitTermination(t);
379      }
380  
381      /**
# Line 367 | Line 389 | public class ReentrantLockTest extends J
389          t.interrupt();
390          assertTrue(lock.isLocked());
391          assertTrue(lock.isHeldByCurrentThread());
392 <        t.join();
392 >        awaitTermination(t);
393      }
394  
395      /**
# Line 446 | Line 468 | public class ReentrantLockTest extends J
468          lock.lock();
469          c.signal();
470          lock.unlock();
471 <        t.join(SHORT_DELAY_MS);
450 <        assertFalse(t.isAlive());
471 >        awaitTermination(t);
472      }
473  
474      /**
# Line 589 | Line 610 | public class ReentrantLockTest extends J
610          assertFalse(lock.hasWaiters(c));
611          assertEquals(0, lock.getWaitQueueLength(c));
612          lock.unlock();
613 <        t.join(SHORT_DELAY_MS);
593 <        assertFalse(t.isAlive());
613 >        awaitTermination(t);
614      }
615  
616      /**
# Line 630 | Line 650 | public class ReentrantLockTest extends J
650          assertFalse(lock.hasWaiters(c));
651          assertEquals(0, lock.getWaitQueueLength(c));
652          lock.unlock();
653 <        t1.join(SHORT_DELAY_MS);
654 <        t2.join(SHORT_DELAY_MS);
635 <        assertFalse(t1.isAlive());
636 <        assertFalse(t2.isAlive());
653 >        awaitTermination(t1);
654 >        awaitTermination(t2);
655      }
656  
657      /**
# Line 676 | Line 694 | public class ReentrantLockTest extends J
694          assertFalse(lock.hasWaiters(c));
695          assertTrue(lock.getWaitingThreads(c).isEmpty());
696          lock.unlock();
697 <        t1.join(SHORT_DELAY_MS);
698 <        t2.join(SHORT_DELAY_MS);
681 <        assertFalse(t1.isAlive());
682 <        assertFalse(t2.isAlive());
697 >        awaitTermination(t1);
698 >        awaitTermination(t2);
699      }
700  
701      /** A helper class for uninterruptible wait tests */
# Line 741 | 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 <        t.join(SHORT_DELAY_MS);
785 <        assertFalse(t.isAlive());
784 >        awaitTermination(t);
785 >        assertFalse(lock.isLocked());
786      }
787  
788      /**
# Line 769 | Line 799 | public class ReentrantLockTest extends J
799  
800          delay(SHORT_DELAY_MS);
801          t.interrupt();
802 <        t.join(SHORT_DELAY_MS);
773 <        assertFalse(t.isAlive());
802 >        awaitTermination(t);
803      }
804  
805      /**
# Line 788 | Line 817 | public class ReentrantLockTest extends J
817  
818          delay(SHORT_DELAY_MS);
819          t.interrupt();
820 <        t.join(SHORT_DELAY_MS);
792 <        assertFalse(t.isAlive());
820 >        awaitTermination(t);
821      }
822  
823      /**
# Line 816 | Line 844 | public class ReentrantLockTest extends J
844          lock.lock();
845          c.signalAll();
846          lock.unlock();
847 <        t1.join(SHORT_DELAY_MS);
848 <        t2.join(SHORT_DELAY_MS);
821 <        assertFalse(t1.isAlive());
822 <        assertFalse(t2.isAlive());
847 >        awaitTermination(t1);
848 >        awaitTermination(t2);
849      }
850  
851      /**
# Line 852 | Line 878 | public class ReentrantLockTest extends J
878          lock.lock();
879          c.signalAll();
880          lock.unlock();
881 <        t1.join(SHORT_DELAY_MS);
882 <        t2.join(SHORT_DELAY_MS);
857 <        assertFalse(t1.isAlive());
858 <        assertFalse(t2.isAlive());
881 >        awaitTermination(t1);
882 >        awaitTermination(t2);
883      }
884  
885      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines