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.24 by dl, Mon Nov 29 15:47:29 2010 UTC vs.
Revision 1.48 by jsr166, Sun Aug 11 22:29:27 2019 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 static java.util.concurrent.TimeUnit.MILLISECONDS;
9 +
10   import java.util.ArrayList;
11   import java.util.List;
12 + import java.util.concurrent.CountDownLatch;
13 + import java.util.concurrent.Phaser;
14 + import java.util.concurrent.TimeoutException;
15   import java.util.concurrent.atomic.AtomicInteger;
16 < import java.util.concurrent.atomic.AtomicBoolean;
12 < import java.util.concurrent.*;
13 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 < import static java.util.concurrent.TimeUnit.NANOSECONDS;
16 >
17   import junit.framework.Test;
18   import junit.framework.TestSuite;
19  
20   public class PhaserTest extends JSR166TestCase {
21  
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run(suite());
23 >        main(suite(), args);
24      }
25  
26      public static Test suite() {
# Line 27 | Line 29 | public class PhaserTest extends JSR166Te
29  
30      private static final int maxParties = 65535;
31  
32 <    /** Checks state of phaser. */
32 >    /** Checks state of unterminated phaser. */
33      protected void assertState(Phaser phaser,
34                                 int phase, int parties, int unarrived) {
35          assertEquals(phase, phaser.getPhase());
36          assertEquals(parties, phaser.getRegisteredParties());
37          assertEquals(unarrived, phaser.getUnarrivedParties());
38          assertEquals(parties - unarrived, phaser.getArrivedParties());
39 <        assertTrue((phaser.getPhase() >= 0) ^ phaser.isTerminated());
39 >        assertFalse(phaser.isTerminated());
40      }
41  
42      /** Checks state of terminated phaser. */
43 <    protected void assertTerminated(Phaser phaser, int parties, int unarrived) {
43 >    protected void assertTerminated(Phaser phaser, int maxPhase, int parties) {
44          assertTrue(phaser.isTerminated());
45 <        assertTrue(phaser.getPhase() < 0);
45 >        int expectedPhase = maxPhase + Integer.MIN_VALUE;
46 >        assertEquals(expectedPhase, phaser.getPhase());
47          assertEquals(parties, phaser.getRegisteredParties());
48 <        assertEquals(unarrived, phaser.getUnarrivedParties());
49 <        assertEquals(parties - unarrived, phaser.getArrivedParties());
48 >        assertEquals(expectedPhase, phaser.register());
49 >        assertEquals(expectedPhase, phaser.arrive());
50 >        assertEquals(expectedPhase, phaser.arriveAndDeregister());
51      }
52  
53 <    protected void assertTerminated(Phaser phaser) {
54 <        assertTerminated(phaser, 0, 0);
53 >    protected void assertTerminated(Phaser phaser, int maxPhase) {
54 >        assertTerminated(phaser, maxPhase, 0);
55      }
56  
57      /**
# Line 156 | Line 160 | public class PhaserTest extends JSR166Te
160              phaser.bulkRegister(Integer.MAX_VALUE);
161              shouldThrow();
162          } catch (IllegalStateException success) {}
163 +
164 +        assertEquals(0, phaser.bulkRegister(0));
165 +        assertState(phaser, 0, maxParties, maxParties);
166      }
167  
168      /**
169 <     * register() correctly returns the current barrier phase number when
170 <     * invoked
169 >     * register() correctly returns the current barrier phase number
170 >     * when invoked
171       */
172      public void testRegister3() {
173          Phaser phaser = new Phaser();
# Line 171 | Line 178 | public class PhaserTest extends JSR166Te
178      }
179  
180      /**
181 <     * register causes the next arrive to not increment the phase rather retain
182 <     * the phase number
181 >     * register causes the next arrive to not increment the phase
182 >     * rather retain the phase number
183       */
184      public void testRegister4() {
185          Phaser phaser = new Phaser(1);
# Line 183 | Line 190 | public class PhaserTest extends JSR166Te
190      }
191  
192      /**
193 +     * register on a subphaser that is currently empty succeeds, even
194 +     * in the presence of another non-empty subphaser
195 +     */
196 +    public void testRegisterEmptySubPhaser() {
197 +        Phaser root = new Phaser();
198 +        Phaser child1 = new Phaser(root, 1);
199 +        Phaser child2 = new Phaser(root, 0);
200 +        assertEquals(0, child2.register());
201 +        assertState(root, 0, 2, 2);
202 +        assertState(child1, 0, 1, 1);
203 +        assertState(child2, 0, 1, 1);
204 +        assertEquals(0, child2.arriveAndDeregister());
205 +        assertState(root, 0, 1, 1);
206 +        assertState(child1, 0, 1, 1);
207 +        assertState(child2, 0, 0, 0);
208 +        assertEquals(0, child2.register());
209 +        assertEquals(0, child2.arriveAndDeregister());
210 +        assertState(root, 0, 1, 1);
211 +        assertState(child1, 0, 1, 1);
212 +        assertState(child2, 0, 0, 0);
213 +        assertEquals(0, child1.arriveAndDeregister());
214 +        assertTerminated(root, 1);
215 +        assertTerminated(child1, 1);
216 +        assertTerminated(child2, 1);
217 +    }
218 +
219 +    /**
220       * Invoking bulkRegister with a negative parameter throws an
221       * IllegalArgumentException
222       */
# Line 194 | Line 228 | public class PhaserTest extends JSR166Te
228      }
229  
230      /**
231 <     * bulkRegister should correctly record the number of unarrived parties with
232 <     * the number of parties being registered
231 >     * bulkRegister should correctly record the number of unarrived
232 >     * parties with the number of parties being registered
233       */
234      public void testBulkRegister2() {
235          Phaser phaser = new Phaser();
236 +        assertEquals(0, phaser.bulkRegister(0));
237 +        assertState(phaser, 0, 0, 0);
238          assertEquals(0, phaser.bulkRegister(20));
239          assertState(phaser, 0, 20, 20);
240      }
# Line 247 | Line 283 | public class PhaserTest extends JSR166Te
283      /**
284       * arriveAndDeregister does not wait for others to arrive at barrier
285       */
286 <    public void testArriveAndDeregister() throws InterruptedException {
286 >    public void testArriveAndDeregister() {
287          final Phaser phaser = new Phaser(1);
288          for (int i = 0; i < 10; i++) {
289              assertState(phaser, 0, 1, 1);
# Line 257 | Line 293 | public class PhaserTest extends JSR166Te
293              assertState(phaser, 0, 1, 1);
294          }
295          assertEquals(0, phaser.arriveAndDeregister());
296 <        assertTerminated(phaser);
261 <        assertEquals(1, phaser.getPhase() + Integer.MIN_VALUE);
296 >        assertTerminated(phaser, 1);
297      }
298  
299      /**
300       * arriveAndDeregister does not wait for others to arrive at barrier
301       */
302 <    public void testArrive2() throws InterruptedException {
302 >    public void testArrive2() {
303          final Phaser phaser = new Phaser();
304          assertEquals(0, phaser.register());
305 <        List<Thread> threads = new ArrayList<Thread>();
305 >        List<Thread> threads = new ArrayList<>();
306          for (int i = 0; i < 10; i++) {
307              assertEquals(0, phaser.register());
308              threads.add(newStartedThread(new CheckedRunnable() {
309 <                public void realRun() throws InterruptedException {
309 >                public void realRun() {
310                      assertEquals(0, phaser.arriveAndDeregister());
311                  }}));
312          }
313  
314          for (Thread thread : threads)
315 <            awaitTermination(thread, LONG_DELAY_MS);
315 >            awaitTermination(thread);
316          assertState(phaser, 0, 1, 1);
317          assertEquals(0, phaser.arrive());
318          assertState(phaser, 1, 1, 1);
# Line 289 | Line 324 | public class PhaserTest extends JSR166Te
324      public void testArrive3() {
325          Phaser phaser = new Phaser(1);
326          phaser.forceTermination();
327 <        assertTerminated(phaser, 1, 1);
327 >        assertTerminated(phaser, 0, 1);
328          assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
329          assertTrue(phaser.arrive() < 0);
330          assertTrue(phaser.register() < 0);
# Line 303 | Line 338 | public class PhaserTest extends JSR166Te
338       * registered or unarrived parties would become negative
339       */
340      public void testArriveAndDeregister1() {
341 +        Phaser phaser = new Phaser();
342          try {
307            Phaser phaser = new Phaser();
343              phaser.arriveAndDeregister();
344              shouldThrow();
345          } catch (IllegalStateException success) {}
# Line 336 | Line 371 | public class PhaserTest extends JSR166Te
371          assertState(child, 0, 1, 1);
372          assertState(parent, 0, 1, 1);
373          assertEquals(0, child.arriveAndDeregister());
374 <        assertTerminated(child);
375 <        assertTerminated(parent);
374 >        assertTerminated(child, 1);
375 >        assertTerminated(parent, 1);
376      }
377  
378      /**
# Line 372 | Line 407 | public class PhaserTest extends JSR166Te
407          assertState(parent, 0, 1, 1);
408          assertState(child, 0, 1, 1);
409          assertEquals(0, child.arriveAndDeregister());
410 <        assertTerminated(child);
411 <        assertTerminated(parent);
412 <        assertTerminated(root);
378 <        assertEquals(1, root.getPhase() + Integer.MIN_VALUE);
410 >        assertTerminated(child, 1);
411 >        assertTerminated(parent, 1);
412 >        assertTerminated(root, 1);
413      }
414  
415      /**
416       * arriveAndDeregister returns the phase in which it leaves the
417       * phaser in after deregistration
418       */
419 <    public void testArriveAndDeregister6() throws InterruptedException {
419 >    public void testArriveAndDeregister6() {
420          final Phaser phaser = new Phaser(2);
421          Thread t = newStartedThread(new CheckedRunnable() {
422              public void realRun() {
# Line 393 | Line 427 | public class PhaserTest extends JSR166Te
427          assertEquals(1, phaser.arriveAndDeregister());
428          assertState(phaser, 1, 1, 1);
429          assertEquals(1, phaser.arriveAndDeregister());
430 <        assertTerminated(phaser);
431 <        assertEquals(2, phaser.getPhase() + Integer.MIN_VALUE);
398 <        awaitTermination(t, SHORT_DELAY_MS);
430 >        assertTerminated(phaser, 2);
431 >        awaitTermination(t);
432      }
433  
434      /**
# Line 418 | Line 451 | public class PhaserTest extends JSR166Te
451      }
452  
453      /**
454 +     * awaitAdvanceInterruptibly blocks interruptibly
455 +     */
456 +    public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException {
457 +        final Phaser phaser = new Phaser(1);
458 +        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
459 +
460 +        Thread t1 = newStartedThread(new CheckedRunnable() {
461 +            public void realRun() {
462 +                Thread.currentThread().interrupt();
463 +                try {
464 +                    phaser.awaitAdvanceInterruptibly(0);
465 +                    shouldThrow();
466 +                } catch (InterruptedException success) {}
467 +                assertFalse(Thread.interrupted());
468 +
469 +                pleaseInterrupt.countDown();
470 +                try {
471 +                    phaser.awaitAdvanceInterruptibly(0);
472 +                    shouldThrow();
473 +                } catch (InterruptedException success) {}
474 +                assertFalse(Thread.interrupted());
475 +            }});
476 +
477 +        Thread t2 = newStartedThread(new CheckedRunnable() {
478 +            public void realRun() throws TimeoutException {
479 +                long startTime = System.nanoTime();
480 +
481 +                Thread.currentThread().interrupt();
482 +                try {
483 +                    phaser.awaitAdvanceInterruptibly(0, randomTimeout(), randomTimeUnit());
484 +                    shouldThrow();
485 +                } catch (InterruptedException success) {}
486 +                assertFalse(Thread.interrupted());
487 +
488 +                pleaseInterrupt.countDown();
489 +                try {
490 +                    phaser.awaitAdvanceInterruptibly(0, LONG_DELAY_MS, MILLISECONDS);
491 +                    shouldThrow();
492 +                } catch (InterruptedException success) {}
493 +                assertFalse(Thread.interrupted());
494 +
495 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
496 +            }});
497 +
498 +        await(pleaseInterrupt);
499 +        assertState(phaser, 0, 1, 1);
500 +        assertThreadBlocks(t1, Thread.State.WAITING);
501 +        assertThreadBlocks(t2, Thread.State.TIMED_WAITING);
502 +        t1.interrupt();
503 +        t2.interrupt();
504 +        awaitTermination(t1);
505 +        awaitTermination(t2);
506 +        assertState(phaser, 0, 1, 1);
507 +        assertEquals(0, phaser.arrive());
508 +        assertState(phaser, 1, 1, 1);
509 +    }
510 +
511 +    /**
512       * awaitAdvance continues waiting if interrupted before waiting
513       */
514 <    public void testAwaitAdvanceAfterInterrupt() throws InterruptedException {
514 >    public void testAwaitAdvanceAfterInterrupt() {
515          final Phaser phaser = new Phaser();
516          assertEquals(0, phaser.register());
517 <        final CountDownLatch threadStarted = new CountDownLatch(1);
517 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
518  
519          Thread t = newStartedThread(new CheckedRunnable() {
520 <            public void realRun() throws InterruptedException {
520 >            public void realRun() {
521                  Thread.currentThread().interrupt();
522                  assertEquals(0, phaser.register());
523                  assertEquals(0, phaser.arrive());
524 <                threadStarted.countDown();
524 >                pleaseArrive.countDown();
525                  assertTrue(Thread.currentThread().isInterrupted());
526                  assertEquals(1, phaser.awaitAdvance(0));
527 <                assertTrue(Thread.currentThread().isInterrupted());
527 >                assertTrue(Thread.interrupted());
528              }});
529  
530 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
531 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
530 >        await(pleaseArrive);
531 >        assertThreadBlocks(t, Thread.State.WAITING);
532          assertEquals(0, phaser.arrive());
533 <        awaitTermination(t, SMALL_DELAY_MS);
533 >        awaitTermination(t);
534  
535          Thread.currentThread().interrupt();
536          assertEquals(1, phaser.awaitAdvance(0));
# Line 447 | Line 538 | public class PhaserTest extends JSR166Te
538      }
539  
540      /**
541 <     * awaitAdvance continues waiting if interrupted while waiting
541 >     *  awaitAdvance continues waiting if interrupted while waiting
542       */
543 <    public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException {
543 >    public void testAwaitAdvanceBeforeInterrupt() {
544          final Phaser phaser = new Phaser();
545          assertEquals(0, phaser.register());
546 <        final CountDownLatch threadStarted = new CountDownLatch(1);
546 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
547  
548          Thread t = newStartedThread(new CheckedRunnable() {
549 <            public void realRun() throws InterruptedException {
549 >            public void realRun() {
550                  assertEquals(0, phaser.register());
551                  assertEquals(0, phaser.arrive());
461                threadStarted.countDown();
552                  assertFalse(Thread.currentThread().isInterrupted());
553 +                pleaseArrive.countDown();
554                  assertEquals(1, phaser.awaitAdvance(0));
555 <                assertTrue(Thread.currentThread().isInterrupted());
555 >                assertTrue(Thread.interrupted());
556              }});
557  
558 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
559 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
558 >        await(pleaseArrive);
559 >        assertThreadBlocks(t, Thread.State.WAITING);
560          t.interrupt();
561          assertEquals(0, phaser.arrive());
562 <        awaitTermination(t, SMALL_DELAY_MS);
562 >        awaitTermination(t);
563  
564          Thread.currentThread().interrupt();
565          assertEquals(1, phaser.awaitAdvance(0));
# Line 478 | Line 569 | public class PhaserTest extends JSR166Te
569      /**
570       * arriveAndAwaitAdvance continues waiting if interrupted before waiting
571       */
572 <    public void testArriveAndAwaitAdvanceAfterInterrupt()
482 <            throws InterruptedException {
572 >    public void testArriveAndAwaitAdvanceAfterInterrupt() {
573          final Phaser phaser = new Phaser();
574          assertEquals(0, phaser.register());
575 <        final CountDownLatch threadStarted = new CountDownLatch(1);
575 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
576  
577          Thread t = newStartedThread(new CheckedRunnable() {
578 <            public void realRun() throws InterruptedException {
578 >            public void realRun() {
579                  Thread.currentThread().interrupt();
580                  assertEquals(0, phaser.register());
581 <                threadStarted.countDown();
581 >                pleaseArrive.countDown();
582                  assertTrue(Thread.currentThread().isInterrupted());
583                  assertEquals(1, phaser.arriveAndAwaitAdvance());
584 <                assertTrue(Thread.currentThread().isInterrupted());
584 >                assertTrue(Thread.interrupted());
585              }});
586  
587 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
588 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
587 >        await(pleaseArrive);
588 >        assertThreadBlocks(t, Thread.State.WAITING);
589          Thread.currentThread().interrupt();
590          assertEquals(1, phaser.arriveAndAwaitAdvance());
591          assertTrue(Thread.interrupted());
592 <        awaitTermination(t, SMALL_DELAY_MS);
592 >        awaitTermination(t);
593      }
594  
595      /**
596       * arriveAndAwaitAdvance continues waiting if interrupted while waiting
597       */
598 <    public void testArriveAndAwaitAdvanceBeforeInterrupt()
509 <            throws InterruptedException {
598 >    public void testArriveAndAwaitAdvanceBeforeInterrupt() {
599          final Phaser phaser = new Phaser();
600          assertEquals(0, phaser.register());
601 <        final CountDownLatch threadStarted = new CountDownLatch(1);
601 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
602  
603          Thread t = newStartedThread(new CheckedRunnable() {
604 <            public void realRun() throws InterruptedException {
604 >            public void realRun() {
605                  assertEquals(0, phaser.register());
517                threadStarted.countDown();
606                  assertFalse(Thread.currentThread().isInterrupted());
607 +                pleaseInterrupt.countDown();
608                  assertEquals(1, phaser.arriveAndAwaitAdvance());
609 <                assertTrue(Thread.currentThread().isInterrupted());
609 >                assertTrue(Thread.interrupted());
610              }});
611  
612 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
613 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
612 >        await(pleaseInterrupt);
613 >        assertThreadBlocks(t, Thread.State.WAITING);
614          t.interrupt();
615          Thread.currentThread().interrupt();
616          assertEquals(1, phaser.arriveAndAwaitAdvance());
617          assertTrue(Thread.interrupted());
618 <        awaitTermination(t, SMALL_DELAY_MS);
618 >        awaitTermination(t);
619      }
620  
621      /**
622       * awaitAdvance atomically waits for all parties within the same phase to
623       * complete before continuing
624       */
625 <    public void testAwaitAdvance4() throws InterruptedException {
625 >    public void testAwaitAdvance4() {
626          final Phaser phaser = new Phaser(4);
627          final AtomicInteger count = new AtomicInteger(0);
628 <        List<Thread> threads = new ArrayList<Thread>();
628 >        List<Thread> threads = new ArrayList<>();
629          for (int i = 0; i < 4; i++)
630              threads.add(newStartedThread(new CheckedRunnable() {
631                  public void realRun() {
632                      for (int k = 0; k < 3; k++) {
633 <                        assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
633 >                        assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance());
634                          count.incrementAndGet();
635 <                        assertEquals(2*k+1, phaser.arrive());
636 <                        assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
637 <                        assertEquals(count.get(), 4*(k+1));
635 >                        assertEquals(2 * k + 1, phaser.arrive());
636 >                        assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1));
637 >                        assertEquals(4 * (k + 1), count.get());
638                      }}}));
639  
640          for (Thread thread : threads)
641 <            awaitTermination(thread, MEDIUM_DELAY_MS);
641 >            awaitTermination(thread);
642      }
643  
644      /**
645       * awaitAdvance returns the current phase
646       */
647 <    public void testAwaitAdvance5() throws InterruptedException {
647 >    public void testAwaitAdvance5() {
648          final Phaser phaser = new Phaser(1);
649          assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
650          assertEquals(1, phaser.getPhase());
651          assertEquals(1, phaser.register());
652 <        List<Thread> threads = new ArrayList<Thread>();
652 >        List<Thread> threads = new ArrayList<>();
653          for (int i = 0; i < 8; i++) {
654              final CountDownLatch latch = new CountDownLatch(1);
655              final boolean goesFirst = ((i & 1) == 0);
656              threads.add(newStartedThread(new CheckedRunnable() {
657 <                public void realRun() throws InterruptedException {
657 >                public void realRun() {
658                      if (goesFirst)
659                          latch.countDown();
660                      else
661 <                        assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
661 >                        await(latch);
662                      phaser.arrive();
663                  }}));
664              if (goesFirst)
665 <                assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
665 >                await(latch);
666              else
667                  latch.countDown();
668              assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
669              assertEquals(i + 2, phaser.getPhase());
670          }
671          for (Thread thread : threads)
672 <            awaitTermination(thread, SMALL_DELAY_MS);
672 >            awaitTermination(thread);
673 >    }
674 >
675 >    /**
676 >     * awaitAdvance returns the current phase in child phasers
677 >     */
678 >    public void testAwaitAdvanceTieredPhaser() throws Exception {
679 >        final Phaser parent = new Phaser();
680 >        final List<Phaser> zeroPartyChildren = new ArrayList<>(3);
681 >        final List<Phaser> onePartyChildren = new ArrayList<>(3);
682 >        for (int i = 0; i < 3; i++) {
683 >            zeroPartyChildren.add(new Phaser(parent, 0));
684 >            onePartyChildren.add(new Phaser(parent, 1));
685 >        }
686 >        final List<Phaser> phasers = new ArrayList<>();
687 >        phasers.addAll(zeroPartyChildren);
688 >        phasers.addAll(onePartyChildren);
689 >        phasers.add(parent);
690 >        for (Phaser phaser : phasers) {
691 >            assertEquals(-42, phaser.awaitAdvance(-42));
692 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
693 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
694 >        }
695 >
696 >        for (Phaser child : onePartyChildren)
697 >            assertEquals(0, child.arrive());
698 >        for (Phaser phaser : phasers) {
699 >            assertEquals(-42, phaser.awaitAdvance(-42));
700 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
701 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
702 >            assertEquals(1, phaser.awaitAdvance(0));
703 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
704 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
705 >        }
706 >
707 >        for (Phaser child : onePartyChildren)
708 >            assertEquals(1, child.arrive());
709 >        for (Phaser phaser : phasers) {
710 >            assertEquals(-42, phaser.awaitAdvance(-42));
711 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
712 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
713 >            assertEquals(2, phaser.awaitAdvance(0));
714 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
715 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
716 >            assertEquals(2, phaser.awaitAdvance(1));
717 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
718 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS));
719 >        }
720      }
721  
722      /**
723       * awaitAdvance returns when the phaser is externally terminated
724       */
725 <    public void testAwaitAdvance6() throws InterruptedException {
725 >    public void testAwaitAdvance6() {
726          final Phaser phaser = new Phaser(3);
727 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
728 <        final List<Thread> threads = new ArrayList<Thread>();
727 >        final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
728 >        final List<Thread> threads = new ArrayList<>();
729          for (int i = 0; i < 2; i++) {
730              Runnable r = new CheckedRunnable() {
731                  public void realRun() {
732                      assertEquals(0, phaser.arrive());
733 <                    threadsStarted.countDown();
733 >                    pleaseForceTermination.countDown();
734                      assertTrue(phaser.awaitAdvance(0) < 0);
735                      assertTrue(phaser.isTerminated());
736                      assertTrue(phaser.getPhase() < 0);
# Line 603 | Line 739 | public class PhaserTest extends JSR166Te
739                  }};
740              threads.add(newStartedThread(r));
741          }
742 <        threadsStarted.await();
742 >        await(pleaseForceTermination);
743          phaser.forceTermination();
744 +        assertTrue(phaser.isTerminated());
745          assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
746          for (Thread thread : threads)
747 <            awaitTermination(thread, SMALL_DELAY_MS);
611 <        assertTrue(phaser.isTerminated());
612 <        assertTrue(phaser.getPhase() < 0);
747 >            awaitTermination(thread);
748          assertEquals(3, phaser.getRegisteredParties());
749      }
750  
# Line 618 | Line 753 | public class PhaserTest extends JSR166Te
753       * unarrived parties
754       */
755      public void testArriveAndAwaitAdvance1() {
756 +        Phaser phaser = new Phaser();
757          try {
622            Phaser phaser = new Phaser();
758              phaser.arriveAndAwaitAdvance();
759              shouldThrow();
760          } catch (IllegalStateException success) {}
# Line 630 | Line 765 | public class PhaserTest extends JSR166Te
765       * number of arrived parties is the same number that is accounted
766       * for when the main thread awaitsAdvance
767       */
768 <    public void testArriveAndAwaitAdvance3() throws InterruptedException {
768 >    public void testArriveAndAwaitAdvance3() {
769          final Phaser phaser = new Phaser(1);
770          final int THREADS = 3;
771 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
772 <        final List<Thread> threads = new ArrayList<Thread>();
771 >        final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
772 >        final List<Thread> threads = new ArrayList<>();
773          for (int i = 0; i < THREADS; i++)
774              threads.add(newStartedThread(new CheckedRunnable() {
775 <                public void realRun() throws InterruptedException {
775 >                public void realRun() {
776                      assertEquals(0, phaser.register());
777 <                    threadsStarted.countDown();
777 >                    pleaseArrive.countDown();
778                      assertEquals(1, phaser.arriveAndAwaitAdvance());
779                  }}));
780  
781 <        assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
782 <        long t0 = System.nanoTime();
781 >        await(pleaseArrive);
782 >        long startTime = System.nanoTime();
783          while (phaser.getArrivedParties() < THREADS)
784              Thread.yield();
785          assertEquals(THREADS, phaser.getArrivedParties());
786 <        assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS);
786 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
787 >        for (Thread thread : threads)
788 >            assertThreadBlocks(thread, Thread.State.WAITING);
789          for (Thread thread : threads)
790              assertTrue(thread.isAlive());
791          assertState(phaser, 0, THREADS + 1, 1);
792          phaser.arriveAndAwaitAdvance();
793          for (Thread thread : threads)
794 <            awaitTermination(thread, SMALL_DELAY_MS);
794 >            awaitTermination(thread);
795          assertState(phaser, 1, THREADS + 1, THREADS + 1);
796      }
797  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines