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

Comparing jsr166/src/test/tck/PhaserTest.java (file contents):
Revision 1.27 by jsr166, Fri Dec 3 02:09:40 2010 UTC vs.
Revision 1.37 by jsr166, Sat Nov 26 05:42:14 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include John Vint
6   */
7  
8 + import junit.framework.*;
9   import java.util.ArrayList;
10   import java.util.List;
11 < import java.util.concurrent.atomic.AtomicInteger;
12 < import java.util.concurrent.atomic.AtomicBoolean;
13 < import java.util.concurrent.*;
11 > import java.util.concurrent.Phaser;
12 > import java.util.concurrent.CountDownLatch;
13 > import java.util.concurrent.TimeoutException;
14   import static java.util.concurrent.TimeUnit.MILLISECONDS;
15   import static java.util.concurrent.TimeUnit.NANOSECONDS;
16 < import junit.framework.Test;
17 < import junit.framework.TestSuite;
16 > import java.util.concurrent.atomic.AtomicBoolean;
17 > import java.util.concurrent.atomic.AtomicInteger;
18  
19   public class PhaserTest extends JSR166TestCase {
20  
# Line 27 | Line 28 | public class PhaserTest extends JSR166Te
28  
29      private static final int maxParties = 65535;
30  
31 <    /** Checks state of phaser. */
31 >    /** Checks state of unterminated phaser. */
32      protected void assertState(Phaser phaser,
33                                 int phase, int parties, int unarrived) {
34          assertEquals(phase, phaser.getPhase());
35          assertEquals(parties, phaser.getRegisteredParties());
36          assertEquals(unarrived, phaser.getUnarrivedParties());
37          assertEquals(parties - unarrived, phaser.getArrivedParties());
38 <        assertTrue((phaser.getPhase() >= 0) ^ phaser.isTerminated());
38 >        assertFalse(phaser.isTerminated());
39      }
40  
41      /** Checks state of terminated phaser. */
42 <    protected void assertTerminated(Phaser phaser,
42 <                                    int maxPhase, int parties, int unarrived) {
42 >    protected void assertTerminated(Phaser phaser, int maxPhase, int parties) {
43          assertTrue(phaser.isTerminated());
44          int expectedPhase = maxPhase + Integer.MIN_VALUE;
45          assertEquals(expectedPhase, phaser.getPhase());
# Line 50 | Line 50 | public class PhaserTest extends JSR166Te
50      }
51  
52      protected void assertTerminated(Phaser phaser, int maxPhase) {
53 <        assertTerminated(phaser, maxPhase, 0, 0);
53 >        assertTerminated(phaser, maxPhase, 0);
54      }
55  
56      /**
# Line 159 | Line 159 | public class PhaserTest extends JSR166Te
159              phaser.bulkRegister(Integer.MAX_VALUE);
160              shouldThrow();
161          } catch (IllegalStateException success) {}
162 +
163 +        assertEquals(0, phaser.bulkRegister(0));
164 +        assertState(phaser, 0, maxParties, maxParties);
165      }
166  
167      /**
168 <     * register() correctly returns the current barrier phase number when
169 <     * invoked
168 >     * register() correctly returns the current barrier phase number
169 >     * when invoked
170       */
171      public void testRegister3() {
172          Phaser phaser = new Phaser();
# Line 174 | Line 177 | public class PhaserTest extends JSR166Te
177      }
178  
179      /**
180 <     * register causes the next arrive to not increment the phase rather retain
181 <     * the phase number
180 >     * register causes the next arrive to not increment the phase
181 >     * rather retain the phase number
182       */
183      public void testRegister4() {
184          Phaser phaser = new Phaser(1);
# Line 186 | Line 189 | public class PhaserTest extends JSR166Te
189      }
190  
191      /**
192 +     * register on a subphaser that is currently empty succeeds, even
193 +     * in the presence of another non-empty subphaser
194 +     */
195 +    public void testRegisterEmptySubPhaser() {
196 +        Phaser root = new Phaser();
197 +        Phaser child1 = new Phaser(root, 1);
198 +        Phaser child2 = new Phaser(root, 0);
199 +        assertEquals(0, child2.register());
200 +        assertState(root, 0, 2, 2);
201 +        assertState(child1, 0, 1, 1);
202 +        assertState(child2, 0, 1, 1);
203 +        assertEquals(0, child2.arriveAndDeregister());
204 +        assertState(root, 0, 1, 1);
205 +        assertState(child1, 0, 1, 1);
206 +        assertState(child2, 0, 0, 0);
207 +        assertEquals(0, child2.register());
208 +        assertEquals(0, child2.arriveAndDeregister());
209 +        assertState(root, 0, 1, 1);
210 +        assertState(child1, 0, 1, 1);
211 +        assertState(child2, 0, 0, 0);
212 +        assertEquals(0, child1.arriveAndDeregister());
213 +        assertTerminated(root, 1);
214 +        assertTerminated(child1, 1);
215 +        assertTerminated(child2, 1);
216 +    }
217 +
218 +    /**
219       * Invoking bulkRegister with a negative parameter throws an
220       * IllegalArgumentException
221       */
# Line 197 | Line 227 | public class PhaserTest extends JSR166Te
227      }
228  
229      /**
230 <     * bulkRegister should correctly record the number of unarrived parties with
231 <     * the number of parties being registered
230 >     * bulkRegister should correctly record the number of unarrived
231 >     * parties with the number of parties being registered
232       */
233      public void testBulkRegister2() {
234          Phaser phaser = new Phaser();
235 +        assertEquals(0, phaser.bulkRegister(0));
236 +        assertState(phaser, 0, 0, 0);
237          assertEquals(0, phaser.bulkRegister(20));
238          assertState(phaser, 0, 20, 20);
239      }
# Line 250 | Line 282 | public class PhaserTest extends JSR166Te
282      /**
283       * arriveAndDeregister does not wait for others to arrive at barrier
284       */
285 <    public void testArriveAndDeregister() throws InterruptedException {
285 >    public void testArriveAndDeregister() {
286          final Phaser phaser = new Phaser(1);
287          for (int i = 0; i < 10; i++) {
288              assertState(phaser, 0, 1, 1);
# Line 266 | Line 298 | public class PhaserTest extends JSR166Te
298      /**
299       * arriveAndDeregister does not wait for others to arrive at barrier
300       */
301 <    public void testArrive2() throws InterruptedException {
301 >    public void testArrive2() {
302          final Phaser phaser = new Phaser();
303          assertEquals(0, phaser.register());
304          List<Thread> threads = new ArrayList<Thread>();
305          for (int i = 0; i < 10; i++) {
306              assertEquals(0, phaser.register());
307              threads.add(newStartedThread(new CheckedRunnable() {
308 <                public void realRun() throws InterruptedException {
308 >                public void realRun() {
309                      assertEquals(0, phaser.arriveAndDeregister());
310                  }}));
311          }
312  
313          for (Thread thread : threads)
314 <            awaitTermination(thread, LONG_DELAY_MS);
314 >            awaitTermination(thread);
315          assertState(phaser, 0, 1, 1);
316          assertEquals(0, phaser.arrive());
317          assertState(phaser, 1, 1, 1);
# Line 291 | Line 323 | public class PhaserTest extends JSR166Te
323      public void testArrive3() {
324          Phaser phaser = new Phaser(1);
325          phaser.forceTermination();
326 <        assertTerminated(phaser, 0, 1, 1);
326 >        assertTerminated(phaser, 0, 1);
327          assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
328          assertTrue(phaser.arrive() < 0);
329          assertTrue(phaser.register() < 0);
# Line 383 | Line 415 | public class PhaserTest extends JSR166Te
415       * arriveAndDeregister returns the phase in which it leaves the
416       * phaser in after deregistration
417       */
418 <    public void testArriveAndDeregister6() throws InterruptedException {
418 >    public void testArriveAndDeregister6() {
419          final Phaser phaser = new Phaser(2);
420          Thread t = newStartedThread(new CheckedRunnable() {
421              public void realRun() {
# Line 395 | Line 427 | public class PhaserTest extends JSR166Te
427          assertState(phaser, 1, 1, 1);
428          assertEquals(1, phaser.arriveAndDeregister());
429          assertTerminated(phaser, 2);
430 <        awaitTermination(t, SHORT_DELAY_MS);
430 >        awaitTermination(t);
431      }
432  
433      /**
# Line 418 | Line 450 | public class PhaserTest extends JSR166Te
450      }
451  
452      /**
453 +     * awaitAdvanceInterruptibly blocks interruptibly
454 +     */
455 +    public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException {
456 +        final Phaser phaser = new Phaser(1);
457 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
458 +
459 +        Thread t1 = newStartedThread(new CheckedRunnable() {
460 +            public void realRun() {
461 +                Thread.currentThread().interrupt();
462 +                try {
463 +                    phaser.awaitAdvanceInterruptibly(0);
464 +                    shouldThrow();
465 +                } catch (InterruptedException success) {}
466 +                assertFalse(Thread.interrupted());
467 +
468 +                pleaseInterrupt.countDown();
469 +                try {
470 +                    phaser.awaitAdvanceInterruptibly(0);
471 +                    shouldThrow();
472 +                } catch (InterruptedException success) {}
473 +                assertFalse(Thread.interrupted());
474 +            }});
475 +
476 +        Thread t2 = newStartedThread(new CheckedRunnable() {
477 +            public void realRun() throws TimeoutException {
478 +                Thread.currentThread().interrupt();
479 +                try {
480 +                    phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
481 +                    shouldThrow();
482 +                } catch (InterruptedException success) {}
483 +                assertFalse(Thread.interrupted());
484 +
485 +                pleaseInterrupt.countDown();
486 +                try {
487 +                    phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
488 +                    shouldThrow();
489 +                } catch (InterruptedException success) {}
490 +                assertFalse(Thread.interrupted());
491 +            }});
492 +
493 +        await(pleaseInterrupt);
494 +        assertState(phaser, 0, 1, 1);
495 +        assertThreadsStayAlive(t1, t2);
496 +        t1.interrupt();
497 +        t2.interrupt();
498 +        awaitTermination(t1);
499 +        awaitTermination(t2);
500 +        assertState(phaser, 0, 1, 1);
501 +        assertEquals(0, phaser.arrive());
502 +        assertState(phaser, 1, 1, 1);
503 +    }
504 +
505 +    /**
506       * awaitAdvance continues waiting if interrupted before waiting
507       */
508 <    public void testAwaitAdvanceAfterInterrupt() throws InterruptedException {
508 >    public void testAwaitAdvanceAfterInterrupt() {
509          final Phaser phaser = new Phaser();
510          assertEquals(0, phaser.register());
511 <        final CountDownLatch threadStarted = new CountDownLatch(1);
511 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
512  
513          Thread t = newStartedThread(new CheckedRunnable() {
514 <            public void realRun() throws InterruptedException {
514 >            public void realRun() {
515                  Thread.currentThread().interrupt();
516                  assertEquals(0, phaser.register());
517                  assertEquals(0, phaser.arrive());
518 <                threadStarted.countDown();
518 >                pleaseArrive.countDown();
519                  assertTrue(Thread.currentThread().isInterrupted());
520                  assertEquals(1, phaser.awaitAdvance(0));
521 <                assertTrue(Thread.currentThread().isInterrupted());
521 >                assertTrue(Thread.interrupted());
522              }});
523  
524 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
525 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
524 >        await(pleaseArrive);
525 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
526          assertEquals(0, phaser.arrive());
527 <        awaitTermination(t, SMALL_DELAY_MS);
527 >        awaitTermination(t);
528  
529          Thread.currentThread().interrupt();
530          assertEquals(1, phaser.awaitAdvance(0));
# Line 447 | Line 532 | public class PhaserTest extends JSR166Te
532      }
533  
534      /**
535 <     * awaitAdvance continues waiting if interrupted while waiting
535 >     *  awaitAdvance continues waiting if interrupted while waiting
536       */
537 <    public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException {
537 >    public void testAwaitAdvanceBeforeInterrupt() {
538          final Phaser phaser = new Phaser();
539          assertEquals(0, phaser.register());
540 <        final CountDownLatch threadStarted = new CountDownLatch(1);
540 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
541  
542          Thread t = newStartedThread(new CheckedRunnable() {
543 <            public void realRun() throws InterruptedException {
543 >            public void realRun() {
544                  assertEquals(0, phaser.register());
545                  assertEquals(0, phaser.arrive());
461                threadStarted.countDown();
546                  assertFalse(Thread.currentThread().isInterrupted());
547 +                pleaseArrive.countDown();
548                  assertEquals(1, phaser.awaitAdvance(0));
549 <                assertTrue(Thread.currentThread().isInterrupted());
549 >                assertTrue(Thread.interrupted());
550              }});
551  
552 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
553 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
552 >        await(pleaseArrive);
553 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
554          t.interrupt();
555          assertEquals(0, phaser.arrive());
556 <        awaitTermination(t, SMALL_DELAY_MS);
556 >        awaitTermination(t);
557  
558          Thread.currentThread().interrupt();
559          assertEquals(1, phaser.awaitAdvance(0));
# Line 478 | Line 563 | public class PhaserTest extends JSR166Te
563      /**
564       * arriveAndAwaitAdvance continues waiting if interrupted before waiting
565       */
566 <    public void testArriveAndAwaitAdvanceAfterInterrupt()
482 <            throws InterruptedException {
566 >    public void testArriveAndAwaitAdvanceAfterInterrupt() {
567          final Phaser phaser = new Phaser();
568          assertEquals(0, phaser.register());
569 <        final CountDownLatch threadStarted = new CountDownLatch(1);
569 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
570  
571          Thread t = newStartedThread(new CheckedRunnable() {
572 <            public void realRun() throws InterruptedException {
572 >            public void realRun() {
573                  Thread.currentThread().interrupt();
574                  assertEquals(0, phaser.register());
575 <                threadStarted.countDown();
575 >                pleaseInterrupt.countDown();
576                  assertTrue(Thread.currentThread().isInterrupted());
577                  assertEquals(1, phaser.arriveAndAwaitAdvance());
578                  assertTrue(Thread.currentThread().isInterrupted());
579              }});
580  
581 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
582 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
581 >        await(pleaseInterrupt);
582 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
583          Thread.currentThread().interrupt();
584          assertEquals(1, phaser.arriveAndAwaitAdvance());
585          assertTrue(Thread.interrupted());
586 <        awaitTermination(t, SMALL_DELAY_MS);
586 >        awaitTermination(t);
587      }
588  
589      /**
590       * arriveAndAwaitAdvance continues waiting if interrupted while waiting
591       */
592 <    public void testArriveAndAwaitAdvanceBeforeInterrupt()
509 <            throws InterruptedException {
592 >    public void testArriveAndAwaitAdvanceBeforeInterrupt() {
593          final Phaser phaser = new Phaser();
594          assertEquals(0, phaser.register());
595 <        final CountDownLatch threadStarted = new CountDownLatch(1);
595 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
596  
597          Thread t = newStartedThread(new CheckedRunnable() {
598 <            public void realRun() throws InterruptedException {
598 >            public void realRun() {
599                  assertEquals(0, phaser.register());
517                threadStarted.countDown();
600                  assertFalse(Thread.currentThread().isInterrupted());
601 +                pleaseInterrupt.countDown();
602                  assertEquals(1, phaser.arriveAndAwaitAdvance());
603                  assertTrue(Thread.currentThread().isInterrupted());
604              }});
605  
606 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
607 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
606 >        await(pleaseInterrupt);
607 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
608          t.interrupt();
609          Thread.currentThread().interrupt();
610          assertEquals(1, phaser.arriveAndAwaitAdvance());
611          assertTrue(Thread.interrupted());
612 <        awaitTermination(t, SMALL_DELAY_MS);
612 >        awaitTermination(t);
613      }
614  
615      /**
616       * awaitAdvance atomically waits for all parties within the same phase to
617       * complete before continuing
618       */
619 <    public void testAwaitAdvance4() throws InterruptedException {
619 >    public void testAwaitAdvance4() {
620          final Phaser phaser = new Phaser(4);
621          final AtomicInteger count = new AtomicInteger(0);
622          List<Thread> threads = new ArrayList<Thread>();
# Line 545 | Line 628 | public class PhaserTest extends JSR166Te
628                          count.incrementAndGet();
629                          assertEquals(2*k+1, phaser.arrive());
630                          assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
631 <                        assertEquals(count.get(), 4*(k+1));
631 >                        assertEquals(4*(k+1), count.get());
632                      }}}));
633  
634          for (Thread thread : threads)
635 <            awaitTermination(thread, MEDIUM_DELAY_MS);
635 >            awaitTermination(thread);
636      }
637  
638      /**
639       * awaitAdvance returns the current phase
640       */
641 <    public void testAwaitAdvance5() throws InterruptedException {
641 >    public void testAwaitAdvance5() {
642          final Phaser phaser = new Phaser(1);
643          assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
644          assertEquals(1, phaser.getPhase());
# Line 565 | Line 648 | public class PhaserTest extends JSR166Te
648              final CountDownLatch latch = new CountDownLatch(1);
649              final boolean goesFirst = ((i & 1) == 0);
650              threads.add(newStartedThread(new CheckedRunnable() {
651 <                public void realRun() throws InterruptedException {
651 >                public void realRun() {
652                      if (goesFirst)
653                          latch.countDown();
654                      else
655 <                        assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
655 >                        await(latch);
656                      phaser.arrive();
657                  }}));
658              if (goesFirst)
659 <                assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
659 >                await(latch);
660              else
661                  latch.countDown();
662              assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
663              assertEquals(i + 2, phaser.getPhase());
664          }
665          for (Thread thread : threads)
666 <            awaitTermination(thread, SMALL_DELAY_MS);
666 >            awaitTermination(thread);
667 >    }
668 >
669 >    /**
670 >     * awaitAdvance returns the current phase in child phasers
671 >     */
672 >    public void testAwaitAdvanceTieredPhaser() throws Exception {
673 >        final Phaser parent = new Phaser();
674 >        final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3);
675 >        final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3);
676 >        for (int i = 0; i < 3; i++) {
677 >            zeroPartyChildren.add(new Phaser(parent, 0));
678 >            onePartyChildren.add(new Phaser(parent, 1));
679 >        }
680 >        final List<Phaser> phasers = new ArrayList<Phaser>();
681 >        phasers.addAll(zeroPartyChildren);
682 >        phasers.addAll(onePartyChildren);
683 >        phasers.add(parent);
684 >        for (Phaser phaser : phasers) {
685 >            assertEquals(-42, phaser.awaitAdvance(-42));
686 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
687 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
688 >        }
689 >
690 >        for (Phaser child : onePartyChildren)
691 >            assertEquals(0, child.arrive());
692 >        for (Phaser phaser : phasers) {
693 >            assertEquals(-42, phaser.awaitAdvance(-42));
694 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
695 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
696 >            assertEquals(1, phaser.awaitAdvance(0));
697 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
698 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
699 >        }
700 >
701 >        for (Phaser child : onePartyChildren)
702 >            assertEquals(1, child.arrive());
703 >        for (Phaser phaser : phasers) {
704 >            assertEquals(-42, phaser.awaitAdvance(-42));
705 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
706 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
707 >            assertEquals(2, phaser.awaitAdvance(0));
708 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
709 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
710 >            assertEquals(2, phaser.awaitAdvance(1));
711 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
712 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS));
713 >        }
714      }
715  
716      /**
717       * awaitAdvance returns when the phaser is externally terminated
718       */
719 <    public void testAwaitAdvance6() throws InterruptedException {
719 >    public void testAwaitAdvance6() {
720          final Phaser phaser = new Phaser(3);
721 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
721 >        final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
722          final List<Thread> threads = new ArrayList<Thread>();
723          for (int i = 0; i < 2; i++) {
724              Runnable r = new CheckedRunnable() {
725                  public void realRun() {
726                      assertEquals(0, phaser.arrive());
727 <                    threadsStarted.countDown();
727 >                    pleaseForceTermination.countDown();
728                      assertTrue(phaser.awaitAdvance(0) < 0);
729                      assertTrue(phaser.isTerminated());
730                      assertTrue(phaser.getPhase() < 0);
# Line 603 | Line 733 | public class PhaserTest extends JSR166Te
733                  }};
734              threads.add(newStartedThread(r));
735          }
736 <        threadsStarted.await();
736 >        await(pleaseForceTermination);
737          phaser.forceTermination();
738          assertTrue(phaser.isTerminated());
739          assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
740          for (Thread thread : threads)
741 <            awaitTermination(thread, SMALL_DELAY_MS);
741 >            awaitTermination(thread);
742          assertEquals(3, phaser.getRegisteredParties());
743      }
744  
# Line 629 | Line 759 | public class PhaserTest extends JSR166Te
759       * number of arrived parties is the same number that is accounted
760       * for when the main thread awaitsAdvance
761       */
762 <    public void testArriveAndAwaitAdvance3() throws InterruptedException {
762 >    public void testArriveAndAwaitAdvance3() {
763          final Phaser phaser = new Phaser(1);
764          final int THREADS = 3;
765 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
765 >        final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
766          final List<Thread> threads = new ArrayList<Thread>();
767          for (int i = 0; i < THREADS; i++)
768              threads.add(newStartedThread(new CheckedRunnable() {
769 <                public void realRun() throws InterruptedException {
769 >                public void realRun() {
770                      assertEquals(0, phaser.register());
771 <                    threadsStarted.countDown();
771 >                    pleaseArrive.countDown();
772                      assertEquals(1, phaser.arriveAndAwaitAdvance());
773                  }}));
774  
775 <        assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
776 <        long t0 = System.nanoTime();
775 >        await(pleaseArrive);
776 >        long startTime = System.nanoTime();
777          while (phaser.getArrivedParties() < THREADS)
778              Thread.yield();
779          assertEquals(THREADS, phaser.getArrivedParties());
780 <        assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS);
780 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
781 >        for (Thread thread : threads)
782 >            waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS);
783          for (Thread thread : threads)
784              assertTrue(thread.isAlive());
785          assertState(phaser, 0, THREADS + 1, 1);
786          phaser.arriveAndAwaitAdvance();
787          for (Thread thread : threads)
788 <            awaitTermination(thread, SMALL_DELAY_MS);
788 >            awaitTermination(thread);
789          assertState(phaser, 1, THREADS + 1, THREADS + 1);
790      }
791  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines