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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines