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.19 by jsr166, Fri Oct 15 22:43:02 2010 UTC vs.
Revision 1.49 by jsr166, Thu Sep 5 21:44:15 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() {
27          return new TestSuite(PhaserTest.class);
28      }
29  
30 <    /** Checks state of phaser. */
30 >    private static final int maxParties = 65535;
31 >
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      /**
58       * Empty constructor builds a new Phaser with no parent, no registered
59       * parties and initial phase number of 0
60       */
61 <    public void testConstructor1() {
61 >    public void testConstructorDefaultValues() {
62          Phaser phaser = new Phaser();
63          assertNull(phaser.getParent());
64 +        assertEquals(0, phaser.getRegisteredParties());
65          assertEquals(0, phaser.getArrivedParties());
66 +        assertEquals(0, phaser.getUnarrivedParties());
67          assertEquals(0, phaser.getPhase());
68      }
69  
70      /**
71 <     * A negative party number for the constructor throws illegal argument
72 <     * exception
71 >     * Constructing with a negative number of parties throws
72 >     * IllegalArgumentException
73       */
74 <    public void testConstructor2() {
74 >    public void testConstructorNegativeParties() {
75          try {
76              new Phaser(-1);
77              shouldThrow();
# Line 71 | Line 79 | public class PhaserTest extends JSR166Te
79      }
80  
81      /**
82 <     * The parent being input into the constructor should equal the original
83 <     * parent when being returned
82 >     * Constructing with a negative number of parties throws
83 >     * IllegalArgumentException
84       */
85 <    public void testConstructor3() {
86 <        Phaser parent = new Phaser();
87 <        assertEquals(parent, new Phaser(parent).getParent());
85 >    public void testConstructorNegativeParties2() {
86 >        try {
87 >            new Phaser(new Phaser(), -1);
88 >            shouldThrow();
89 >        } catch (IllegalArgumentException success) {}
90      }
91  
92      /**
93 <     * A negative party number for the constructor throws illegal argument
94 <     * exception
93 >     * Constructing with a number of parties > 65535 throws
94 >     * IllegalArgumentException
95       */
96 <    public void testConstructor4() {
96 >    public void testConstructorPartiesExceedsLimit() {
97 >        new Phaser(maxParties);
98          try {
99 <            new Phaser(new Phaser(), -1);
99 >            new Phaser(maxParties + 1);
100 >            shouldThrow();
101 >        } catch (IllegalArgumentException success) {}
102 >
103 >        new Phaser(new Phaser(), maxParties);
104 >        try {
105 >            new Phaser(new Phaser(), maxParties + 1);
106              shouldThrow();
107          } catch (IllegalArgumentException success) {}
108      }
109  
110      /**
111 +     * The parent provided to the constructor should be returned from
112 +     * a later call to getParent
113 +     */
114 +    public void testConstructor3() {
115 +        Phaser parent = new Phaser();
116 +        assertSame(parent, new Phaser(parent).getParent());
117 +        assertNull(new Phaser(null).getParent());
118 +    }
119 +
120 +    /**
121       * The parent being input into the parameter should equal the original
122       * parent when being returned
123       */
124      public void testConstructor5() {
125          Phaser parent = new Phaser();
126 <        assertEquals(parent, new Phaser(parent, 0).getParent());
126 >        assertSame(parent, new Phaser(parent, 0).getParent());
127 >        assertNull(new Phaser(null, 0).getParent());
128      }
129  
130      /**
131 <     * register() will increment the number of unarrived parties by one and not
132 <     * affect its arrived parties
131 >     * register() will increment the number of unarrived parties by
132 >     * one and not affect its arrived parties
133       */
134      public void testRegister1() {
135          Phaser phaser = new Phaser();
# Line 115 | Line 143 | public class PhaserTest extends JSR166Te
143       */
144      public void testRegister2() {
145          Phaser phaser = new Phaser(0);
118        int maxParties = (1 << 16) - 1;
146          assertState(phaser, 0, 0, 0);
147          assertEquals(0, phaser.bulkRegister(maxParties - 10));
148          assertState(phaser, 0, maxParties - 10, maxParties - 10);
# Line 128 | Line 155 | public class PhaserTest extends JSR166Te
155              phaser.register();
156              shouldThrow();
157          } catch (IllegalStateException success) {}
158 +
159 +        try {
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 143 | 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 155 | 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 166 | 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 219 | 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 229 | Line 293 | public class PhaserTest extends JSR166Te
293              assertState(phaser, 0, 1, 1);
294          }
295          assertEquals(0, phaser.arriveAndDeregister());
296 <        assertTerminated(phaser);
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 260 | 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);
331          assertTrue(phaser.arriveAndDeregister() < 0);
# Line 273 | 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 {
277            Phaser phaser = new Phaser();
343              phaser.arriveAndDeregister();
344              shouldThrow();
345          } catch (IllegalStateException success) {}
# Line 301 | Line 366 | public class PhaserTest extends JSR166Te
366          Phaser parent = new Phaser();
367          Phaser child = new Phaser(parent);
368          assertState(child, 0, 0, 0);
369 <        assertState(parent, 0, 1, 1);
369 >        assertState(parent, 0, 0, 0);
370          assertEquals(0, child.register());
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 334 | Line 399 | public class PhaserTest extends JSR166Te
399          Phaser root = new Phaser();
400          Phaser parent = new Phaser(root);
401          Phaser child = new Phaser(parent);
402 <        assertState(root, 0, 1, 1);
403 <        assertState(parent, 0, 1, 1);
402 >        assertState(root, 0, 0, 0);
403 >        assertState(parent, 0, 0, 0);
404          assertState(child, 0, 0, 0);
405          assertEquals(0, child.register());
406          assertState(root, 0, 1, 1);
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);
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 362 | 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 <        awaitTermination(t, SHORT_DELAY_MS);
430 >        assertTerminated(phaser, 2);
431 >        awaitTermination(t);
432      }
433  
434      /**
# Line 386 | Line 451 | public class PhaserTest extends JSR166Te
451      }
452  
453      /**
454 <     * awaitAdvance continues waiting if interrupted
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 >                Thread.currentThread().interrupt();
480 >                try {
481 >                    phaser.awaitAdvanceInterruptibly(0, randomTimeout(), randomTimeUnit());
482 >                    shouldThrow();
483 >                } catch (InterruptedException success) {}
484 >                assertFalse(Thread.interrupted());
485 >
486 >                pleaseInterrupt.countDown();
487 >                try {
488 >                    phaser.awaitAdvanceInterruptibly(0, LONGER_DELAY_MS, MILLISECONDS);
489 >                    shouldThrow();
490 >                } catch (InterruptedException success) {}
491 >                assertFalse(Thread.interrupted());
492 >            }});
493 >
494 >        await(pleaseInterrupt);
495 >        assertState(phaser, 0, 1, 1);
496 >        if (randomBoolean()) assertThreadBlocks(t1, Thread.State.WAITING);
497 >        if (randomBoolean()) assertThreadBlocks(t2, Thread.State.TIMED_WAITING);
498 >        t1.interrupt();
499 >        t2.interrupt();
500 >        awaitTermination(t1);
501 >        awaitTermination(t2);
502 >        assertState(phaser, 0, 1, 1);
503 >        assertEquals(0, phaser.arrive());
504 >        assertState(phaser, 1, 1, 1);
505 >    }
506 >
507 >    /**
508 >     * awaitAdvance continues waiting if interrupted before waiting
509       */
510 <    public void testAwaitAdvance3() throws InterruptedException {
510 >    public void testAwaitAdvanceAfterInterrupt() {
511          final Phaser phaser = new Phaser();
512          assertEquals(0, phaser.register());
513 <        final CountDownLatch threadStarted = new CountDownLatch(1);
513 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
514  
515          Thread t = newStartedThread(new CheckedRunnable() {
516 <            public void realRun() throws InterruptedException {
516 >            public void realRun() {
517 >                Thread.currentThread().interrupt();
518                  assertEquals(0, phaser.register());
519 <                threadStarted.countDown();
520 <                assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
519 >                assertEquals(0, phaser.arrive());
520 >                pleaseArrive.countDown();
521                  assertTrue(Thread.currentThread().isInterrupted());
522 +                assertEquals(1, phaser.awaitAdvance(0));
523 +                assertTrue(Thread.interrupted());
524 +            }});
525 +
526 +        await(pleaseArrive);
527 +        assertThreadBlocks(t, Thread.State.WAITING);
528 +        assertEquals(0, phaser.arrive());
529 +        awaitTermination(t);
530 +
531 +        Thread.currentThread().interrupt();
532 +        assertEquals(1, phaser.awaitAdvance(0));
533 +        assertTrue(Thread.interrupted());
534 +    }
535 +
536 +    /**
537 +     *  awaitAdvance continues waiting if interrupted while waiting
538 +     */
539 +    public void testAwaitAdvanceBeforeInterrupt() {
540 +        final Phaser phaser = new Phaser();
541 +        assertEquals(0, phaser.register());
542 +        final CountDownLatch pleaseArrive = new CountDownLatch(1);
543 +
544 +        Thread t = newStartedThread(new CheckedRunnable() {
545 +            public void realRun() {
546 +                assertEquals(0, phaser.register());
547 +                assertEquals(0, phaser.arrive());
548 +                assertFalse(Thread.currentThread().isInterrupted());
549 +                pleaseArrive.countDown();
550 +                assertEquals(1, phaser.awaitAdvance(0));
551 +                assertTrue(Thread.interrupted());
552              }});
553 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
553 >
554 >        await(pleaseArrive);
555 >        assertThreadBlocks(t, Thread.State.WAITING);
556          t.interrupt();
557          assertEquals(0, phaser.arrive());
558 <        awaitTermination(t, SMALL_DELAY_MS);
558 >        awaitTermination(t);
559 >
560 >        Thread.currentThread().interrupt();
561 >        assertEquals(1, phaser.awaitAdvance(0));
562 >        assertTrue(Thread.interrupted());
563 >    }
564 >
565 >    /**
566 >     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
567 >     */
568 >    public void testArriveAndAwaitAdvanceAfterInterrupt() {
569 >        final Phaser phaser = new Phaser();
570 >        assertEquals(0, phaser.register());
571 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
572 >
573 >        Thread t = newStartedThread(new CheckedRunnable() {
574 >            public void realRun() {
575 >                Thread.currentThread().interrupt();
576 >                assertEquals(0, phaser.register());
577 >                pleaseArrive.countDown();
578 >                assertTrue(Thread.currentThread().isInterrupted());
579 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
580 >                assertTrue(Thread.interrupted());
581 >            }});
582 >
583 >        await(pleaseArrive);
584 >        assertThreadBlocks(t, Thread.State.WAITING);
585 >        Thread.currentThread().interrupt();
586 >        assertEquals(1, phaser.arriveAndAwaitAdvance());
587 >        assertTrue(Thread.interrupted());
588 >        awaitTermination(t);
589 >    }
590 >
591 >    /**
592 >     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
593 >     */
594 >    public void testArriveAndAwaitAdvanceBeforeInterrupt() {
595 >        final Phaser phaser = new Phaser();
596 >        assertEquals(0, phaser.register());
597 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
598 >
599 >        Thread t = newStartedThread(new CheckedRunnable() {
600 >            public void realRun() {
601 >                assertEquals(0, phaser.register());
602 >                assertFalse(Thread.currentThread().isInterrupted());
603 >                pleaseInterrupt.countDown();
604 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
605 >                assertTrue(Thread.interrupted());
606 >            }});
607 >
608 >        await(pleaseInterrupt);
609 >        assertThreadBlocks(t, Thread.State.WAITING);
610 >        t.interrupt();
611 >        Thread.currentThread().interrupt();
612 >        assertEquals(1, phaser.arriveAndAwaitAdvance());
613 >        assertTrue(Thread.interrupted());
614 >        awaitTermination(t);
615      }
616  
617      /**
618       * awaitAdvance atomically waits for all parties within the same phase to
619       * complete before continuing
620       */
621 <    public void testAwaitAdvance4() throws InterruptedException {
621 >    public void testAwaitAdvance4() {
622          final Phaser phaser = new Phaser(4);
623          final AtomicInteger count = new AtomicInteger(0);
624 <        List<Thread> threads = new ArrayList<Thread>();
624 >        List<Thread> threads = new ArrayList<>();
625          for (int i = 0; i < 4; i++)
626              threads.add(newStartedThread(new CheckedRunnable() {
627                  public void realRun() {
628                      for (int k = 0; k < 3; k++) {
629 <                        assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
629 >                        assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance());
630                          count.incrementAndGet();
631 <                        assertEquals(2*k+1, phaser.arrive());
632 <                        assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
633 <                        assertEquals(count.get(), 4*(k+1));
631 >                        assertEquals(2 * k + 1, phaser.arrive());
632 >                        assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1));
633 >                        assertEquals(4 * (k + 1), count.get());
634                      }}}));
635  
636          for (Thread thread : threads)
637 <            awaitTermination(thread, MEDIUM_DELAY_MS);
637 >            awaitTermination(thread);
638      }
639  
640      /**
641       * awaitAdvance returns the current phase
642       */
643 <    public void testAwaitAdvance5() throws InterruptedException {
643 >    public void testAwaitAdvance5() {
644          final Phaser phaser = new Phaser(1);
645          assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
646          assertEquals(1, phaser.getPhase());
647          assertEquals(1, phaser.register());
648 <        List<Thread> threads = new ArrayList<Thread>();
648 >        List<Thread> threads = new ArrayList<>();
649          for (int i = 0; i < 8; i++) {
650              final CountDownLatch latch = new CountDownLatch(1);
651              final boolean goesFirst = ((i & 1) == 0);
652              threads.add(newStartedThread(new CheckedRunnable() {
653 <                public void realRun() throws InterruptedException {
653 >                public void realRun() {
654                      if (goesFirst)
655                          latch.countDown();
656                      else
657 <                        assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
657 >                        await(latch);
658                      phaser.arrive();
659                  }}));
660              if (goesFirst)
661 <                assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
661 >                await(latch);
662              else
663                  latch.countDown();
664              assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
665              assertEquals(i + 2, phaser.getPhase());
666          }
667          for (Thread thread : threads)
668 <            awaitTermination(thread, SMALL_DELAY_MS);
668 >            awaitTermination(thread);
669 >    }
670 >
671 >    /**
672 >     * awaitAdvance returns the current phase in child phasers
673 >     */
674 >    public void testAwaitAdvanceTieredPhaser() throws Exception {
675 >        final Phaser parent = new Phaser();
676 >        final List<Phaser> zeroPartyChildren = new ArrayList<>(3);
677 >        final List<Phaser> onePartyChildren = new ArrayList<>(3);
678 >        for (int i = 0; i < 3; i++) {
679 >            zeroPartyChildren.add(new Phaser(parent, 0));
680 >            onePartyChildren.add(new Phaser(parent, 1));
681 >        }
682 >        final List<Phaser> phasers = new ArrayList<>();
683 >        phasers.addAll(zeroPartyChildren);
684 >        phasers.addAll(onePartyChildren);
685 >        phasers.add(parent);
686 >        for (Phaser phaser : phasers) {
687 >            assertEquals(-42, phaser.awaitAdvance(-42));
688 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
689 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
690 >        }
691 >
692 >        for (Phaser child : onePartyChildren)
693 >            assertEquals(0, child.arrive());
694 >        for (Phaser phaser : phasers) {
695 >            assertEquals(-42, phaser.awaitAdvance(-42));
696 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
697 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
698 >            assertEquals(1, phaser.awaitAdvance(0));
699 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
700 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
701 >        }
702 >
703 >        for (Phaser child : onePartyChildren)
704 >            assertEquals(1, child.arrive());
705 >        for (Phaser phaser : phasers) {
706 >            assertEquals(-42, phaser.awaitAdvance(-42));
707 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
708 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
709 >            assertEquals(2, phaser.awaitAdvance(0));
710 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
711 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
712 >            assertEquals(2, phaser.awaitAdvance(1));
713 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
714 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS));
715 >        }
716      }
717  
718      /**
719       * awaitAdvance returns when the phaser is externally terminated
720       */
721 <    public void testAwaitAdvance6() throws InterruptedException {
721 >    public void testAwaitAdvance6() {
722          final Phaser phaser = new Phaser(3);
723 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
724 <        final List<Thread> threads = new ArrayList<Thread>();
723 >        final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
724 >        final List<Thread> threads = new ArrayList<>();
725          for (int i = 0; i < 2; i++) {
726              Runnable r = new CheckedRunnable() {
727                  public void realRun() {
728                      assertEquals(0, phaser.arrive());
729 <                    threadsStarted.countDown();
729 >                    pleaseForceTermination.countDown();
730                      assertTrue(phaser.awaitAdvance(0) < 0);
731                      assertTrue(phaser.isTerminated());
732                      assertTrue(phaser.getPhase() < 0);
733 +                    assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
734                      assertEquals(3, phaser.getRegisteredParties());
735                  }};
736              threads.add(newStartedThread(r));
737          }
738 <        threadsStarted.await();
738 >        await(pleaseForceTermination);
739          phaser.forceTermination();
484        for (Thread thread : threads)
485            awaitTermination(thread, SMALL_DELAY_MS);
740          assertTrue(phaser.isTerminated());
741 <        assertTrue(phaser.getPhase() < 0);
741 >        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
742 >        for (Thread thread : threads)
743 >            awaitTermination(thread);
744          assertEquals(3, phaser.getRegisteredParties());
745      }
746  
# Line 493 | Line 749 | public class PhaserTest extends JSR166Te
749       * unarrived parties
750       */
751      public void testArriveAndAwaitAdvance1() {
752 +        Phaser phaser = new Phaser();
753          try {
497            Phaser phaser = new Phaser();
754              phaser.arriveAndAwaitAdvance();
755              shouldThrow();
756          } catch (IllegalStateException success) {}
757      }
758  
759      /**
504     * Interrupted arriveAndAwaitAdvance does not throw InterruptedException
505     */
506    public void testArriveAndAwaitAdvance2() throws InterruptedException {
507        final Phaser phaser = new Phaser(2);
508        final CountDownLatch threadStarted = new CountDownLatch(1);
509        final AtomicBoolean advanced = new AtomicBoolean(false);
510        final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false);
511        Thread t = newStartedThread(new CheckedRunnable() {
512            public void realRun() throws InterruptedException {
513                threadStarted.countDown();
514                assertEquals(1, phaser.arriveAndAwaitAdvance());
515                assertState(phaser, 1, 2, 2);
516                advanced.set(true);
517                assertTrue(Thread.currentThread().isInterrupted());
518                while (!checkedInterruptStatus.get())
519                    Thread.yield();
520            }});
521
522        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
523        t.interrupt();
524        assertEquals(0, phaser.arrive());
525        while (!advanced.get())
526            Thread.yield();
527        assertTrue(t.isInterrupted());
528        checkedInterruptStatus.set(true);
529        awaitTermination(t, SMALL_DELAY_MS);
530    }
531
532    /**
760       * arriveAndAwaitAdvance waits for all threads to arrive, the
761       * number of arrived parties is the same number that is accounted
762       * for when the main thread awaitsAdvance
763       */
764 <    public void testArriveAndAwaitAdvance3() throws InterruptedException {
764 >    public void testArriveAndAwaitAdvance3() {
765          final Phaser phaser = new Phaser(1);
766          final int THREADS = 3;
767 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
768 <        final List<Thread> threads = new ArrayList<Thread>();
767 >        final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
768 >        final List<Thread> threads = new ArrayList<>();
769          for (int i = 0; i < THREADS; i++)
770              threads.add(newStartedThread(new CheckedRunnable() {
771 <                public void realRun() throws InterruptedException {
771 >                public void realRun() {
772                      assertEquals(0, phaser.register());
773 <                    threadsStarted.countDown();
773 >                    pleaseArrive.countDown();
774                      assertEquals(1, phaser.arriveAndAwaitAdvance());
775                  }}));
776  
777 <        assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
778 <        long t0 = System.nanoTime();
777 >        await(pleaseArrive);
778 >        long startTime = System.nanoTime();
779          while (phaser.getArrivedParties() < THREADS)
780              Thread.yield();
781          assertEquals(THREADS, phaser.getArrivedParties());
782 <        assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS);
782 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
783 >        for (Thread thread : threads)
784 >            assertThreadBlocks(thread, Thread.State.WAITING);
785          for (Thread thread : threads)
786              assertTrue(thread.isAlive());
787          assertState(phaser, 0, THREADS + 1, 1);
788          phaser.arriveAndAwaitAdvance();
789          for (Thread thread : threads)
790 <            awaitTermination(thread, SMALL_DELAY_MS);
790 >            awaitTermination(thread);
791          assertState(phaser, 1, THREADS + 1, THREADS + 1);
792      }
793  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines