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.34 by jsr166, Mon Jun 27 04:07:51 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 +     * XXXX broken (hangs) as of 2011-06-26
195 +     */
196 +    public void XXXXtestRegisterEmptySubPhaser() {
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 +        assertEquals(0, child2.arriveAndDeregister());
202 +        assertEquals(0, child2.register());
203 +        assertEquals(0, child2.arriveAndDeregister());
204 +        assertState(child2, 0, 0, 0);
205 +    }
206 +
207 +    /**
208       * Invoking bulkRegister with a negative parameter throws an
209       * IllegalArgumentException
210       */
# Line 166 | Line 216 | public class PhaserTest extends JSR166Te
216      }
217  
218      /**
219 <     * bulkRegister should correctly record the number of unarrived parties with
220 <     * the number of parties being registered
219 >     * bulkRegister should correctly record the number of unarrived
220 >     * parties with the number of parties being registered
221       */
222      public void testBulkRegister2() {
223          Phaser phaser = new Phaser();
224 +        assertEquals(0, phaser.bulkRegister(0));
225 +        assertState(phaser, 0, 0, 0);
226          assertEquals(0, phaser.bulkRegister(20));
227          assertState(phaser, 0, 20, 20);
228      }
# Line 219 | Line 271 | public class PhaserTest extends JSR166Te
271      /**
272       * arriveAndDeregister does not wait for others to arrive at barrier
273       */
274 <    public void testArriveAndDeregister() throws InterruptedException {
274 >    public void testArriveAndDeregister() {
275          final Phaser phaser = new Phaser(1);
276          for (int i = 0; i < 10; i++) {
277              assertState(phaser, 0, 1, 1);
# Line 229 | Line 281 | public class PhaserTest extends JSR166Te
281              assertState(phaser, 0, 1, 1);
282          }
283          assertEquals(0, phaser.arriveAndDeregister());
284 <        assertTerminated(phaser);
284 >        assertTerminated(phaser, 1);
285      }
286  
287      /**
288       * arriveAndDeregister does not wait for others to arrive at barrier
289       */
290 <    public void testArrive2() throws InterruptedException {
290 >    public void testArrive2() {
291          final Phaser phaser = new Phaser();
292          assertEquals(0, phaser.register());
293          List<Thread> threads = new ArrayList<Thread>();
294          for (int i = 0; i < 10; i++) {
295              assertEquals(0, phaser.register());
296              threads.add(newStartedThread(new CheckedRunnable() {
297 <                public void realRun() throws InterruptedException {
297 >                public void realRun() {
298                      assertEquals(0, phaser.arriveAndDeregister());
299                  }}));
300          }
301  
302          for (Thread thread : threads)
303 <            awaitTermination(thread, LONG_DELAY_MS);
303 >            awaitTermination(thread);
304          assertState(phaser, 0, 1, 1);
305          assertEquals(0, phaser.arrive());
306          assertState(phaser, 1, 1, 1);
# Line 260 | Line 312 | public class PhaserTest extends JSR166Te
312      public void testArrive3() {
313          Phaser phaser = new Phaser(1);
314          phaser.forceTermination();
315 <        assertTerminated(phaser, 1, 1);
315 >        assertTerminated(phaser, 0, 1);
316 >        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
317          assertTrue(phaser.arrive() < 0);
318          assertTrue(phaser.register() < 0);
319          assertTrue(phaser.arriveAndDeregister() < 0);
# Line 301 | Line 354 | public class PhaserTest extends JSR166Te
354          Phaser parent = new Phaser();
355          Phaser child = new Phaser(parent);
356          assertState(child, 0, 0, 0);
357 <        assertState(parent, 0, 1, 1);
357 >        assertState(parent, 0, 0, 0);
358          assertEquals(0, child.register());
359          assertState(child, 0, 1, 1);
360          assertState(parent, 0, 1, 1);
361          assertEquals(0, child.arriveAndDeregister());
362 <        assertTerminated(child);
363 <        assertTerminated(parent);
362 >        assertTerminated(child, 1);
363 >        assertTerminated(parent, 1);
364      }
365  
366      /**
# Line 334 | Line 387 | public class PhaserTest extends JSR166Te
387          Phaser root = new Phaser();
388          Phaser parent = new Phaser(root);
389          Phaser child = new Phaser(parent);
390 <        assertState(root, 0, 1, 1);
391 <        assertState(parent, 0, 1, 1);
390 >        assertState(root, 0, 0, 0);
391 >        assertState(parent, 0, 0, 0);
392          assertState(child, 0, 0, 0);
393          assertEquals(0, child.register());
394          assertState(root, 0, 1, 1);
395          assertState(parent, 0, 1, 1);
396          assertState(child, 0, 1, 1);
397          assertEquals(0, child.arriveAndDeregister());
398 <        assertTerminated(child);
399 <        assertTerminated(parent);
400 <        assertTerminated(root);
398 >        assertTerminated(child, 1);
399 >        assertTerminated(parent, 1);
400 >        assertTerminated(root, 1);
401      }
402  
403      /**
404       * arriveAndDeregister returns the phase in which it leaves the
405       * phaser in after deregistration
406       */
407 <    public void testArriveAndDeregister6() throws InterruptedException {
407 >    public void testArriveAndDeregister6() {
408          final Phaser phaser = new Phaser(2);
409          Thread t = newStartedThread(new CheckedRunnable() {
410              public void realRun() {
# Line 362 | Line 415 | public class PhaserTest extends JSR166Te
415          assertEquals(1, phaser.arriveAndDeregister());
416          assertState(phaser, 1, 1, 1);
417          assertEquals(1, phaser.arriveAndDeregister());
418 <        assertTerminated(phaser);
419 <        awaitTermination(t, SHORT_DELAY_MS);
418 >        assertTerminated(phaser, 2);
419 >        awaitTermination(t);
420      }
421  
422      /**
# Line 386 | Line 439 | public class PhaserTest extends JSR166Te
439      }
440  
441      /**
442 <     * awaitAdvance continues waiting if interrupted
442 >     * awaitAdvanceInterruptibly blocks interruptibly
443       */
444 <    public void testAwaitAdvance3() throws InterruptedException {
444 >    public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException {
445 >        final Phaser phaser = new Phaser(1);
446 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(2);
447 >
448 >        Thread t1 = newStartedThread(new CheckedRunnable() {
449 >            public void realRun() {
450 >                Thread.currentThread().interrupt();
451 >                try {
452 >                    phaser.awaitAdvanceInterruptibly(0);
453 >                    shouldThrow();
454 >                } catch (InterruptedException success) {}
455 >                assertFalse(Thread.interrupted());
456 >
457 >                pleaseInterrupt.countDown();
458 >                try {
459 >                    phaser.awaitAdvanceInterruptibly(0);
460 >                    shouldThrow();
461 >                } catch (InterruptedException success) {}
462 >                assertFalse(Thread.interrupted());
463 >            }});
464 >
465 >        Thread t2 = newStartedThread(new CheckedRunnable() {
466 >            public void realRun() throws TimeoutException {
467 >                Thread.currentThread().interrupt();
468 >                try {
469 >                    phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
470 >                    shouldThrow();
471 >                } catch (InterruptedException success) {}
472 >                assertFalse(Thread.interrupted());
473 >
474 >                pleaseInterrupt.countDown();
475 >                try {
476 >                    phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS);
477 >                    shouldThrow();
478 >                } catch (InterruptedException success) {}
479 >                assertFalse(Thread.interrupted());
480 >            }});
481 >
482 >        await(pleaseInterrupt);
483 >        assertState(phaser, 0, 1, 1);
484 >        assertThreadsStayAlive(t1, t2);
485 >        t1.interrupt();
486 >        t2.interrupt();
487 >        awaitTermination(t1);
488 >        awaitTermination(t2);
489 >        assertState(phaser, 0, 1, 1);
490 >        assertEquals(0, phaser.arrive());
491 >        assertState(phaser, 1, 1, 1);
492 >    }
493 >
494 >    /**
495 >     * awaitAdvance continues waiting if interrupted before waiting
496 >     */
497 >    public void testAwaitAdvanceAfterInterrupt() {
498          final Phaser phaser = new Phaser();
499          assertEquals(0, phaser.register());
500 <        final CountDownLatch threadStarted = new CountDownLatch(1);
500 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
501  
502          Thread t = newStartedThread(new CheckedRunnable() {
503 <            public void realRun() throws InterruptedException {
503 >            public void realRun() {
504 >                Thread.currentThread().interrupt();
505                  assertEquals(0, phaser.register());
506 <                threadStarted.countDown();
507 <                assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
506 >                assertEquals(0, phaser.arrive());
507 >                pleaseArrive.countDown();
508                  assertTrue(Thread.currentThread().isInterrupted());
509 +                assertEquals(1, phaser.awaitAdvance(0));
510 +                assertTrue(Thread.interrupted());
511 +            }});
512 +
513 +        await(pleaseArrive);
514 +        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
515 +        assertEquals(0, phaser.arrive());
516 +        awaitTermination(t);
517 +
518 +        Thread.currentThread().interrupt();
519 +        assertEquals(1, phaser.awaitAdvance(0));
520 +        assertTrue(Thread.interrupted());
521 +    }
522 +
523 +    /**
524 +     *  awaitAdvance continues waiting if interrupted while waiting
525 +     */
526 +    public void testAwaitAdvanceBeforeInterrupt() {
527 +        final Phaser phaser = new Phaser();
528 +        assertEquals(0, phaser.register());
529 +        final CountDownLatch pleaseArrive = new CountDownLatch(1);
530 +
531 +        Thread t = newStartedThread(new CheckedRunnable() {
532 +            public void realRun() {
533 +                assertEquals(0, phaser.register());
534 +                assertEquals(0, phaser.arrive());
535 +                assertFalse(Thread.currentThread().isInterrupted());
536 +                pleaseArrive.countDown();
537 +                assertEquals(1, phaser.awaitAdvance(0));
538 +                assertTrue(Thread.interrupted());
539              }});
540 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
540 >
541 >        await(pleaseArrive);
542 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
543          t.interrupt();
544          assertEquals(0, phaser.arrive());
545 <        awaitTermination(t, SMALL_DELAY_MS);
545 >        awaitTermination(t);
546 >
547 >        Thread.currentThread().interrupt();
548 >        assertEquals(1, phaser.awaitAdvance(0));
549 >        assertTrue(Thread.interrupted());
550 >    }
551 >
552 >    /**
553 >     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
554 >     */
555 >    public void testArriveAndAwaitAdvanceAfterInterrupt() {
556 >        final Phaser phaser = new Phaser();
557 >        assertEquals(0, phaser.register());
558 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
559 >
560 >        Thread t = newStartedThread(new CheckedRunnable() {
561 >            public void realRun() {
562 >                Thread.currentThread().interrupt();
563 >                assertEquals(0, phaser.register());
564 >                pleaseInterrupt.countDown();
565 >                assertTrue(Thread.currentThread().isInterrupted());
566 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
567 >                assertTrue(Thread.currentThread().isInterrupted());
568 >            }});
569 >
570 >        await(pleaseInterrupt);
571 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
572 >        Thread.currentThread().interrupt();
573 >        assertEquals(1, phaser.arriveAndAwaitAdvance());
574 >        assertTrue(Thread.interrupted());
575 >        awaitTermination(t);
576 >    }
577 >
578 >    /**
579 >     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
580 >     */
581 >    public void testArriveAndAwaitAdvanceBeforeInterrupt() {
582 >        final Phaser phaser = new Phaser();
583 >        assertEquals(0, phaser.register());
584 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
585 >
586 >        Thread t = newStartedThread(new CheckedRunnable() {
587 >            public void realRun() {
588 >                assertEquals(0, phaser.register());
589 >                assertFalse(Thread.currentThread().isInterrupted());
590 >                pleaseInterrupt.countDown();
591 >                assertEquals(1, phaser.arriveAndAwaitAdvance());
592 >                assertTrue(Thread.currentThread().isInterrupted());
593 >            }});
594 >
595 >        await(pleaseInterrupt);
596 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
597 >        t.interrupt();
598 >        Thread.currentThread().interrupt();
599 >        assertEquals(1, phaser.arriveAndAwaitAdvance());
600 >        assertTrue(Thread.interrupted());
601 >        awaitTermination(t);
602      }
603  
604      /**
605       * awaitAdvance atomically waits for all parties within the same phase to
606       * complete before continuing
607       */
608 <    public void testAwaitAdvance4() throws InterruptedException {
608 >    public void testAwaitAdvance4() {
609          final Phaser phaser = new Phaser(4);
610          final AtomicInteger count = new AtomicInteger(0);
611          List<Thread> threads = new ArrayList<Thread>();
# Line 426 | Line 621 | public class PhaserTest extends JSR166Te
621                      }}}));
622  
623          for (Thread thread : threads)
624 <            awaitTermination(thread, MEDIUM_DELAY_MS);
624 >            awaitTermination(thread);
625      }
626  
627      /**
628       * awaitAdvance returns the current phase
629       */
630 <    public void testAwaitAdvance5() throws InterruptedException {
630 >    public void testAwaitAdvance5() {
631          final Phaser phaser = new Phaser(1);
632          assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
633          assertEquals(1, phaser.getPhase());
# Line 442 | Line 637 | public class PhaserTest extends JSR166Te
637              final CountDownLatch latch = new CountDownLatch(1);
638              final boolean goesFirst = ((i & 1) == 0);
639              threads.add(newStartedThread(new CheckedRunnable() {
640 <                public void realRun() throws InterruptedException {
640 >                public void realRun() {
641                      if (goesFirst)
642                          latch.countDown();
643                      else
644 <                        assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
644 >                        await(latch);
645                      phaser.arrive();
646                  }}));
647              if (goesFirst)
648 <                assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS));
648 >                await(latch);
649              else
650                  latch.countDown();
651              assertEquals(i + 2, phaser.awaitAdvance(phaser.arrive()));
652              assertEquals(i + 2, phaser.getPhase());
653          }
654          for (Thread thread : threads)
655 <            awaitTermination(thread, SMALL_DELAY_MS);
655 >            awaitTermination(thread);
656 >    }
657 >
658 >    /**
659 >     * awaitAdvance returns the current phase in child phasers
660 >     */
661 >    public void testAwaitAdvanceTieredPhaser() throws Exception {
662 >        final Phaser parent = new Phaser();
663 >        final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3);
664 >        final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3);
665 >        for (int i = 0; i < 3; i++) {
666 >            zeroPartyChildren.add(new Phaser(parent, 0));
667 >            onePartyChildren.add(new Phaser(parent, 1));
668 >        }
669 >        final List<Phaser> phasers = new ArrayList<Phaser>();
670 >        phasers.addAll(zeroPartyChildren);
671 >        phasers.addAll(onePartyChildren);
672 >        phasers.add(parent);
673 >        for (Phaser phaser : phasers) {
674 >            assertEquals(-42, phaser.awaitAdvance(-42));
675 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
676 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
677 >        }
678 >
679 >        for (Phaser child : onePartyChildren)
680 >            assertEquals(0, child.arrive());
681 >        for (Phaser phaser : phasers) {
682 >            assertEquals(-42, phaser.awaitAdvance(-42));
683 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
684 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
685 >            assertEquals(1, phaser.awaitAdvance(0));
686 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
687 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
688 >        }
689 >
690 >        for (Phaser child : onePartyChildren)
691 >            assertEquals(1, 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(2, phaser.awaitAdvance(0));
697 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
698 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
699 >            assertEquals(2, phaser.awaitAdvance(1));
700 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
701 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS));
702 >        }
703      }
704  
705      /**
706       * awaitAdvance returns when the phaser is externally terminated
707       */
708 <    public void testAwaitAdvance6() throws InterruptedException {
708 >    public void testAwaitAdvance6() {
709          final Phaser phaser = new Phaser(3);
710 <        final CountDownLatch threadsStarted = new CountDownLatch(2);
710 >        final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
711          final List<Thread> threads = new ArrayList<Thread>();
712          for (int i = 0; i < 2; i++) {
713              Runnable r = new CheckedRunnable() {
714                  public void realRun() {
715                      assertEquals(0, phaser.arrive());
716 <                    threadsStarted.countDown();
716 >                    pleaseForceTermination.countDown();
717                      assertTrue(phaser.awaitAdvance(0) < 0);
718                      assertTrue(phaser.isTerminated());
719                      assertTrue(phaser.getPhase() < 0);
720 +                    assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
721                      assertEquals(3, phaser.getRegisteredParties());
722                  }};
723              threads.add(newStartedThread(r));
724          }
725 <        threadsStarted.await();
725 >        await(pleaseForceTermination);
726          phaser.forceTermination();
484        for (Thread thread : threads)
485            awaitTermination(thread, SMALL_DELAY_MS);
727          assertTrue(phaser.isTerminated());
728 <        assertTrue(phaser.getPhase() < 0);
728 >        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
729 >        for (Thread thread : threads)
730 >            awaitTermination(thread);
731          assertEquals(3, phaser.getRegisteredParties());
732      }
733  
# Line 501 | Line 744 | public class PhaserTest extends JSR166Te
744      }
745  
746      /**
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    /**
747       * arriveAndAwaitAdvance waits for all threads to arrive, the
748       * number of arrived parties is the same number that is accounted
749       * for when the main thread awaitsAdvance
750       */
751 <    public void testArriveAndAwaitAdvance3() throws InterruptedException {
751 >    public void testArriveAndAwaitAdvance3() {
752          final Phaser phaser = new Phaser(1);
753          final int THREADS = 3;
754 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
754 >        final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
755          final List<Thread> threads = new ArrayList<Thread>();
756          for (int i = 0; i < THREADS; i++)
757              threads.add(newStartedThread(new CheckedRunnable() {
758 <                public void realRun() throws InterruptedException {
758 >                public void realRun() {
759                      assertEquals(0, phaser.register());
760 <                    threadsStarted.countDown();
760 >                    pleaseArrive.countDown();
761                      assertEquals(1, phaser.arriveAndAwaitAdvance());
762                  }}));
763  
764 <        assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
765 <        long t0 = System.nanoTime();
764 >        await(pleaseArrive);
765 >        long startTime = System.nanoTime();
766          while (phaser.getArrivedParties() < THREADS)
767              Thread.yield();
768          assertEquals(THREADS, phaser.getArrivedParties());
769 <        assertTrue(NANOSECONDS.toMillis(System.nanoTime() - t0) < SMALL_DELAY_MS);
769 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
770 >        for (Thread thread : threads)
771 >            waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS);
772          for (Thread thread : threads)
773              assertTrue(thread.isAlive());
774          assertState(phaser, 0, THREADS + 1, 1);
775          phaser.arriveAndAwaitAdvance();
776          for (Thread thread : threads)
777 <            awaitTermination(thread, SMALL_DELAY_MS);
777 >            awaitTermination(thread);
778          assertState(phaser, 1, THREADS + 1, THREADS + 1);
779      }
780  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines