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.28 by jsr166, Fri Dec 3 21:54:32 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 158 | Line 159 | public class PhaserTest extends JSR166Te
159              phaser.bulkRegister(Integer.MAX_VALUE);
160              shouldThrow();
161          } catch (IllegalStateException success) {}
162 +
163 +        assertEquals(0, phaser.bulkRegister(0));
164 +        assertState(phaser, 0, maxParties, maxParties);
165      }
166  
167      /**
168 <     * register() correctly returns the current barrier phase number when
169 <     * invoked
168 >     * register() correctly returns the current barrier phase number
169 >     * when invoked
170       */
171      public void testRegister3() {
172          Phaser phaser = new Phaser();
# Line 173 | 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 185 | 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 196 | 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 249 | 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 265 | Line 287 | public class PhaserTest extends JSR166Te
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 382 | Line 404 | public class PhaserTest extends JSR166Te
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 394 | Line 416 | public class PhaserTest extends JSR166Te
416          assertState(phaser, 1, 1, 1);
417          assertEquals(1, phaser.arriveAndDeregister());
418          assertTerminated(phaser, 2);
419 <        awaitTermination(t, SHORT_DELAY_MS);
419 >        awaitTermination(t);
420      }
421  
422      /**
# Line 417 | Line 439 | public class PhaserTest extends JSR166Te
439      }
440  
441      /**
442 +     * awaitAdvanceInterruptibly blocks interruptibly
443 +     */
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() throws InterruptedException {
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                  assertEquals(0, phaser.arrive());
507 <                threadStarted.countDown();
507 >                pleaseArrive.countDown();
508                  assertTrue(Thread.currentThread().isInterrupted());
509                  assertEquals(1, phaser.awaitAdvance(0));
510 <                assertTrue(Thread.currentThread().isInterrupted());
510 >                assertTrue(Thread.interrupted());
511              }});
512  
513 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
514 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
513 >        await(pleaseArrive);
514 >        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
515          assertEquals(0, phaser.arrive());
516 <        awaitTermination(t, SMALL_DELAY_MS);
516 >        awaitTermination(t);
517  
518          Thread.currentThread().interrupt();
519          assertEquals(1, phaser.awaitAdvance(0));
# Line 446 | Line 521 | public class PhaserTest extends JSR166Te
521      }
522  
523      /**
524 <     * awaitAdvance continues waiting if interrupted while waiting
524 >     *  awaitAdvance continues waiting if interrupted while waiting
525       */
526 <    public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException {
526 >    public void testAwaitAdvanceBeforeInterrupt() {
527          final Phaser phaser = new Phaser();
528          assertEquals(0, phaser.register());
529 <        final CountDownLatch threadStarted = new CountDownLatch(1);
529 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
530  
531          Thread t = newStartedThread(new CheckedRunnable() {
532 <            public void realRun() throws InterruptedException {
532 >            public void realRun() {
533                  assertEquals(0, phaser.register());
534                  assertEquals(0, phaser.arrive());
460                threadStarted.countDown();
535                  assertFalse(Thread.currentThread().isInterrupted());
536 +                pleaseArrive.countDown();
537                  assertEquals(1, phaser.awaitAdvance(0));
538 <                assertTrue(Thread.currentThread().isInterrupted());
538 >                assertTrue(Thread.interrupted());
539              }});
540  
541 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
542 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
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));
# Line 477 | Line 552 | public class PhaserTest extends JSR166Te
552      /**
553       * arriveAndAwaitAdvance continues waiting if interrupted before waiting
554       */
555 <    public void testArriveAndAwaitAdvanceAfterInterrupt()
481 <            throws InterruptedException {
555 >    public void testArriveAndAwaitAdvanceAfterInterrupt() {
556          final Phaser phaser = new Phaser();
557          assertEquals(0, phaser.register());
558 <        final CountDownLatch threadStarted = new CountDownLatch(1);
558 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
559  
560          Thread t = newStartedThread(new CheckedRunnable() {
561 <            public void realRun() throws InterruptedException {
561 >            public void realRun() {
562                  Thread.currentThread().interrupt();
563                  assertEquals(0, phaser.register());
564 <                threadStarted.countDown();
564 >                pleaseInterrupt.countDown();
565                  assertTrue(Thread.currentThread().isInterrupted());
566                  assertEquals(1, phaser.arriveAndAwaitAdvance());
567                  assertTrue(Thread.currentThread().isInterrupted());
568              }});
569  
570 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
571 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
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, SMALL_DELAY_MS);
575 >        awaitTermination(t);
576      }
577  
578      /**
579       * arriveAndAwaitAdvance continues waiting if interrupted while waiting
580       */
581 <    public void testArriveAndAwaitAdvanceBeforeInterrupt()
508 <            throws InterruptedException {
581 >    public void testArriveAndAwaitAdvanceBeforeInterrupt() {
582          final Phaser phaser = new Phaser();
583          assertEquals(0, phaser.register());
584 <        final CountDownLatch threadStarted = new CountDownLatch(1);
584 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
585  
586          Thread t = newStartedThread(new CheckedRunnable() {
587 <            public void realRun() throws InterruptedException {
587 >            public void realRun() {
588                  assertEquals(0, phaser.register());
516                threadStarted.countDown();
589                  assertFalse(Thread.currentThread().isInterrupted());
590 +                pleaseInterrupt.countDown();
591                  assertEquals(1, phaser.arriveAndAwaitAdvance());
592                  assertTrue(Thread.currentThread().isInterrupted());
593              }});
594  
595 <        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
596 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
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, SMALL_DELAY_MS);
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 548 | 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 564 | 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);
# Line 602 | Line 722 | public class PhaserTest extends JSR166Te
722                  }};
723              threads.add(newStartedThread(r));
724          }
725 <        threadsStarted.await();
725 >        await(pleaseForceTermination);
726          phaser.forceTermination();
727          assertTrue(phaser.isTerminated());
728          assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
729          for (Thread thread : threads)
730 <            awaitTermination(thread, SMALL_DELAY_MS);
730 >            awaitTermination(thread);
731          assertEquals(3, phaser.getRegisteredParties());
732      }
733  
# Line 628 | Line 748 | public class PhaserTest extends JSR166Te
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