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.36 by jsr166, Fri Nov 18 20:11:17 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 25 | Line 26 | public class PhaserTest extends JSR166Te
26          return new TestSuite(PhaserTest.class);
27      }
28  
29 <    /** Checks state of phaser. */
29 >    private static final int maxParties = 65535;
30 >
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, int parties, int unarrived) {
42 >    protected void assertTerminated(Phaser phaser, int maxPhase, int parties) {
43          assertTrue(phaser.isTerminated());
44 <        assertTrue(phaser.getPhase() < 0);
44 >        int expectedPhase = maxPhase + Integer.MIN_VALUE;
45 >        assertEquals(expectedPhase, phaser.getPhase());
46          assertEquals(parties, phaser.getRegisteredParties());
47 <        assertEquals(unarrived, phaser.getUnarrivedParties());
48 <        assertEquals(parties - unarrived, phaser.getArrivedParties());
47 >        assertEquals(expectedPhase, phaser.register());
48 >        assertEquals(expectedPhase, phaser.arrive());
49 >        assertEquals(expectedPhase, phaser.arriveAndDeregister());
50      }
51  
52 <    protected void assertTerminated(Phaser phaser) {
53 <        assertTerminated(phaser, 0, 0);
52 >    protected void assertTerminated(Phaser phaser, int maxPhase) {
53 >        assertTerminated(phaser, maxPhase, 0);
54      }
55  
56      /**
57       * Empty constructor builds a new Phaser with no parent, no registered
58       * parties and initial phase number of 0
59       */
60 <    public void testConstructor1() {
60 >    public void testConstructorDefaultValues() {
61          Phaser phaser = new Phaser();
62          assertNull(phaser.getParent());
63 +        assertEquals(0, phaser.getRegisteredParties());
64          assertEquals(0, phaser.getArrivedParties());
65 +        assertEquals(0, phaser.getUnarrivedParties());
66          assertEquals(0, phaser.getPhase());
67      }
68  
69      /**
70 <     * A negative party number for the constructor throws illegal argument
71 <     * exception
70 >     * Constructing with a negative number of parties throws
71 >     * IllegalArgumentException
72       */
73 <    public void testConstructor2() {
73 >    public void testConstructorNegativeParties() {
74          try {
75              new Phaser(-1);
76              shouldThrow();
# Line 71 | Line 78 | public class PhaserTest extends JSR166Te
78      }
79  
80      /**
81 <     * The parent being input into the constructor should equal the original
82 <     * parent when being returned
81 >     * Constructing with a negative number of parties throws
82 >     * IllegalArgumentException
83       */
84 <    public void testConstructor3() {
85 <        Phaser parent = new Phaser();
86 <        assertEquals(parent, new Phaser(parent).getParent());
84 >    public void testConstructorNegativeParties2() {
85 >        try {
86 >            new Phaser(new Phaser(), -1);
87 >            shouldThrow();
88 >        } catch (IllegalArgumentException success) {}
89      }
90  
91      /**
92 <     * A negative party number for the constructor throws illegal argument
93 <     * exception
92 >     * Constructing with a number of parties > 65535 throws
93 >     * IllegalArgumentException
94       */
95 <    public void testConstructor4() {
95 >    public void testConstructorPartiesExceedsLimit() {
96 >        new Phaser(maxParties);
97          try {
98 <            new Phaser(new Phaser(), -1);
98 >            new Phaser(maxParties + 1);
99 >            shouldThrow();
100 >        } catch (IllegalArgumentException success) {}
101 >
102 >        new Phaser(new Phaser(), maxParties);
103 >        try {
104 >            new Phaser(new Phaser(), maxParties + 1);
105              shouldThrow();
106          } catch (IllegalArgumentException success) {}
107      }
108  
109      /**
110 +     * The parent provided to the constructor should be returned from
111 +     * a later call to getParent
112 +     */
113 +    public void testConstructor3() {
114 +        Phaser parent = new Phaser();
115 +        assertSame(parent, new Phaser(parent).getParent());
116 +        assertNull(new Phaser(null).getParent());
117 +    }
118 +
119 +    /**
120       * The parent being input into the parameter should equal the original
121       * parent when being returned
122       */
123      public void testConstructor5() {
124          Phaser parent = new Phaser();
125 <        assertEquals(parent, new Phaser(parent, 0).getParent());
125 >        assertSame(parent, new Phaser(parent, 0).getParent());
126 >        assertNull(new Phaser(null, 0).getParent());
127      }
128  
129      /**
130 <     * register() will increment the number of unarrived parties by one and not
131 <     * affect its arrived parties
130 >     * register() will increment the number of unarrived parties by
131 >     * one and not affect its arrived parties
132       */
133      public void testRegister1() {
134          Phaser phaser = new Phaser();
# Line 115 | Line 142 | public class PhaserTest extends JSR166Te
142       */
143      public void testRegister2() {
144          Phaser phaser = new Phaser(0);
118        int maxParties = (1 << 16) - 1;
145          assertState(phaser, 0, 0, 0);
146          assertEquals(0, phaser.bulkRegister(maxParties - 10));
147          assertState(phaser, 0, maxParties - 10, maxParties - 10);
# Line 128 | Line 154 | public class PhaserTest extends JSR166Te
154              phaser.register();
155              shouldThrow();
156          } catch (IllegalStateException success) {}
157 +
158 +        try {
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 143 | 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 155 | 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 166 | 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 219 | 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 229 | Line 292 | public class PhaserTest extends JSR166Te
292              assertState(phaser, 0, 1, 1);
293          }
294          assertEquals(0, phaser.arriveAndDeregister());
295 <        assertTerminated(phaser);
295 >        assertTerminated(phaser, 1);
296      }
297  
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 260 | Line 323 | public class PhaserTest extends JSR166Te
323      public void testArrive3() {
324          Phaser phaser = new Phaser(1);
325          phaser.forceTermination();
326 <        assertTerminated(phaser, 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);
330          assertTrue(phaser.arriveAndDeregister() < 0);
# Line 301 | Line 365 | public class PhaserTest extends JSR166Te
365          Phaser parent = new Phaser();
366          Phaser child = new Phaser(parent);
367          assertState(child, 0, 0, 0);
368 <        assertState(parent, 0, 1, 1);
368 >        assertState(parent, 0, 0, 0);
369          assertEquals(0, child.register());
370          assertState(child, 0, 1, 1);
371          assertState(parent, 0, 1, 1);
372          assertEquals(0, child.arriveAndDeregister());
373 <        assertTerminated(child);
374 <        assertTerminated(parent);
373 >        assertTerminated(child, 1);
374 >        assertTerminated(parent, 1);
375      }
376  
377      /**
# Line 334 | Line 398 | public class PhaserTest extends JSR166Te
398          Phaser root = new Phaser();
399          Phaser parent = new Phaser(root);
400          Phaser child = new Phaser(parent);
401 <        assertState(root, 0, 1, 1);
402 <        assertState(parent, 0, 1, 1);
401 >        assertState(root, 0, 0, 0);
402 >        assertState(parent, 0, 0, 0);
403          assertState(child, 0, 0, 0);
404          assertEquals(0, child.register());
405          assertState(root, 0, 1, 1);
406          assertState(parent, 0, 1, 1);
407          assertState(child, 0, 1, 1);
408          assertEquals(0, child.arriveAndDeregister());
409 <        assertTerminated(child);
410 <        assertTerminated(parent);
411 <        assertTerminated(root);
409 >        assertTerminated(child, 1);
410 >        assertTerminated(parent, 1);
411 >        assertTerminated(root, 1);
412      }
413  
414      /**
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 362 | Line 426 | public class PhaserTest extends JSR166Te
426          assertEquals(1, phaser.arriveAndDeregister());
427          assertState(phaser, 1, 1, 1);
428          assertEquals(1, phaser.arriveAndDeregister());
429 <        assertTerminated(phaser);
430 <        awaitTermination(t, SHORT_DELAY_MS);
429 >        assertTerminated(phaser, 2);
430 >        awaitTermination(t);
431      }
432  
433      /**
# Line 386 | Line 450 | public class PhaserTest extends JSR166Te
450      }
451  
452      /**
453 <     * awaitAdvance continues waiting if interrupted
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 testAwaitAdvance3() 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 <                threadStarted.countDown();
518 <                assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
517 >                assertEquals(0, phaser.arrive());
518 >                pleaseArrive.countDown();
519                  assertTrue(Thread.currentThread().isInterrupted());
520 +                assertEquals(1, phaser.awaitAdvance(0));
521 +                assertTrue(Thread.interrupted());
522 +            }});
523 +
524 +        await(pleaseArrive);
525 +        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
526 +        assertEquals(0, phaser.arrive());
527 +        awaitTermination(t);
528 +
529 +        Thread.currentThread().interrupt();
530 +        assertEquals(1, phaser.awaitAdvance(0));
531 +        assertTrue(Thread.interrupted());
532 +    }
533 +
534 +    /**
535 +     *  awaitAdvance continues waiting if interrupted while waiting
536 +     */
537 +    public void testAwaitAdvanceBeforeInterrupt() {
538 +        final Phaser phaser = new Phaser();
539 +        assertEquals(0, phaser.register());
540 +        final CountDownLatch pleaseArrive = new CountDownLatch(1);
541 +
542 +        Thread t = newStartedThread(new CheckedRunnable() {
543 +            public void realRun() {
544 +                assertEquals(0, phaser.register());
545 +                assertEquals(0, phaser.arrive());
546 +                assertFalse(Thread.currentThread().isInterrupted());
547 +                pleaseArrive.countDown();
548 +                assertEquals(1, phaser.awaitAdvance(0));
549 +                assertTrue(Thread.interrupted());
550              }});
551 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
551 >
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));
560 >        assertTrue(Thread.interrupted());
561 >    }
562 >
563 >    /**
564 >     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
565 >     */
566 >    public void testArriveAndAwaitAdvanceAfterInterrupt() {
567 >        final Phaser phaser = new Phaser();
568 >        assertEquals(0, phaser.register());
569 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
570 >
571 >        Thread t = newStartedThread(new CheckedRunnable() {
572 >            public void realRun() {
573 >                Thread.currentThread().interrupt();
574 >                assertEquals(0, phaser.register());
575 >                pleaseInterrupt.countDown();
576 >                assertTrue(Thread.currentThread().isInterrupted());
577 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
578 >                assertTrue(Thread.currentThread().isInterrupted());
579 >            }});
580 >
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);
587 >    }
588 >
589 >    /**
590 >     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
591 >     */
592 >    public void testArriveAndAwaitAdvanceBeforeInterrupt() {
593 >        final Phaser phaser = new Phaser();
594 >        assertEquals(0, phaser.register());
595 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
596 >
597 >        Thread t = newStartedThread(new CheckedRunnable() {
598 >            public void realRun() {
599 >                assertEquals(0, phaser.register());
600 >                assertFalse(Thread.currentThread().isInterrupted());
601 >                pleaseInterrupt.countDown();
602 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
603 >                assertTrue(Thread.currentThread().isInterrupted());
604 >            }});
605 >
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);
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 426 | Line 632 | public class PhaserTest extends JSR166Te
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 442 | 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);
731 +                    assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
732                      assertEquals(3, phaser.getRegisteredParties());
733                  }};
734              threads.add(newStartedThread(r));
735          }
736 <        threadsStarted.await();
736 >        await(pleaseForceTermination);
737          phaser.forceTermination();
484        for (Thread thread : threads)
485            awaitTermination(thread, SMALL_DELAY_MS);
738          assertTrue(phaser.isTerminated());
739 <        assertTrue(phaser.getPhase() < 0);
739 >        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
740 >        for (Thread thread : threads)
741 >            awaitTermination(thread);
742          assertEquals(3, phaser.getRegisteredParties());
743      }
744  
# Line 501 | Line 755 | public class PhaserTest extends JSR166Te
755      }
756  
757      /**
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    /**
758       * arriveAndAwaitAdvance waits for all threads to arrive, the
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