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.33 by jsr166, Wed Jun 22 07:46:57 2011 UTC vs.
Revision 1.47 by jsr166, Sun May 14 03:07:53 2017 UTC

# Line 5 | Line 5
5   * Other contributors include John Vint
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 >
10   import java.util.ArrayList;
11   import java.util.List;
11 import java.util.concurrent.Phaser;
12   import java.util.concurrent.CountDownLatch;
13 + import java.util.concurrent.Phaser;
14   import java.util.concurrent.TimeoutException;
14 import static java.util.concurrent.TimeUnit.MILLISECONDS;
15 import static java.util.concurrent.TimeUnit.NANOSECONDS;
16 import java.util.concurrent.atomic.AtomicBoolean;
15   import java.util.concurrent.atomic.AtomicInteger;
16  
17 + import junit.framework.Test;
18 + import junit.framework.TestSuite;
19 +
20   public class PhaserTest extends JSR166TestCase {
21  
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run(suite());
23 >        main(suite(), args);
24      }
25  
26      public static Test suite() {
# Line 165 | Line 166 | public class PhaserTest extends JSR166Te
166      }
167  
168      /**
169 <     * register() correctly returns the current barrier phase number when
170 <     * invoked
169 >     * register() correctly returns the current barrier phase number
170 >     * when invoked
171       */
172      public void testRegister3() {
173          Phaser phaser = new Phaser();
# Line 177 | Line 178 | public class PhaserTest extends JSR166Te
178      }
179  
180      /**
181 <     * register causes the next arrive to not increment the phase rather retain
182 <     * the phase number
181 >     * register causes the next arrive to not increment the phase
182 >     * rather retain the phase number
183       */
184      public void testRegister4() {
185          Phaser phaser = new Phaser(1);
# Line 189 | Line 190 | public class PhaserTest extends JSR166Te
190      }
191  
192      /**
193 +     * register on a subphaser that is currently empty succeeds, even
194 +     * in the presence of another non-empty subphaser
195 +     */
196 +    public void testRegisterEmptySubPhaser() {
197 +        Phaser root = new Phaser();
198 +        Phaser child1 = new Phaser(root, 1);
199 +        Phaser child2 = new Phaser(root, 0);
200 +        assertEquals(0, child2.register());
201 +        assertState(root, 0, 2, 2);
202 +        assertState(child1, 0, 1, 1);
203 +        assertState(child2, 0, 1, 1);
204 +        assertEquals(0, child2.arriveAndDeregister());
205 +        assertState(root, 0, 1, 1);
206 +        assertState(child1, 0, 1, 1);
207 +        assertState(child2, 0, 0, 0);
208 +        assertEquals(0, child2.register());
209 +        assertEquals(0, child2.arriveAndDeregister());
210 +        assertState(root, 0, 1, 1);
211 +        assertState(child1, 0, 1, 1);
212 +        assertState(child2, 0, 0, 0);
213 +        assertEquals(0, child1.arriveAndDeregister());
214 +        assertTerminated(root, 1);
215 +        assertTerminated(child1, 1);
216 +        assertTerminated(child2, 1);
217 +    }
218 +
219 +    /**
220       * Invoking bulkRegister with a negative parameter throws an
221       * IllegalArgumentException
222       */
# Line 200 | Line 228 | public class PhaserTest extends JSR166Te
228      }
229  
230      /**
231 <     * bulkRegister should correctly record the number of unarrived parties with
232 <     * the number of parties being registered
231 >     * bulkRegister should correctly record the number of unarrived
232 >     * parties with the number of parties being registered
233       */
234      public void testBulkRegister2() {
235          Phaser phaser = new Phaser();
# Line 274 | Line 302 | public class PhaserTest extends JSR166Te
302      public void testArrive2() {
303          final Phaser phaser = new Phaser();
304          assertEquals(0, phaser.register());
305 <        List<Thread> threads = new ArrayList<Thread>();
305 >        List<Thread> threads = new ArrayList<>();
306          for (int i = 0; i < 10; i++) {
307              assertEquals(0, phaser.register());
308              threads.add(newStartedThread(new CheckedRunnable() {
# Line 310 | Line 338 | public class PhaserTest extends JSR166Te
338       * registered or unarrived parties would become negative
339       */
340      public void testArriveAndDeregister1() {
341 +        Phaser phaser = new Phaser();
342          try {
314            Phaser phaser = new Phaser();
343              phaser.arriveAndDeregister();
344              shouldThrow();
345          } catch (IllegalStateException success) {}
# Line 465 | Line 493 | public class PhaserTest extends JSR166Te
493  
494          await(pleaseInterrupt);
495          assertState(phaser, 0, 1, 1);
496 <        assertThreadsStayAlive(t1, t2);
496 >        assertThreadBlocks(t1, Thread.State.WAITING);
497 >        assertThreadBlocks(t2, Thread.State.TIMED_WAITING);
498          t1.interrupt();
499          t2.interrupt();
500          awaitTermination(t1);
# Line 495 | Line 524 | public class PhaserTest extends JSR166Te
524              }});
525  
526          await(pleaseArrive);
527 <        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
527 >        assertThreadBlocks(t, Thread.State.WAITING);
528          assertEquals(0, phaser.arrive());
529          awaitTermination(t);
530  
# Line 523 | Line 552 | public class PhaserTest extends JSR166Te
552              }});
553  
554          await(pleaseArrive);
555 <        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
555 >        assertThreadBlocks(t, Thread.State.WAITING);
556          t.interrupt();
557          assertEquals(0, phaser.arrive());
558          awaitTermination(t);
# Line 539 | Line 568 | public class PhaserTest extends JSR166Te
568      public void testArriveAndAwaitAdvanceAfterInterrupt() {
569          final Phaser phaser = new Phaser();
570          assertEquals(0, phaser.register());
571 <        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
571 >        final CountDownLatch pleaseArrive = new CountDownLatch(1);
572  
573          Thread t = newStartedThread(new CheckedRunnable() {
574              public void realRun() {
575                  Thread.currentThread().interrupt();
576                  assertEquals(0, phaser.register());
577 <                pleaseInterrupt.countDown();
577 >                pleaseArrive.countDown();
578                  assertTrue(Thread.currentThread().isInterrupted());
579                  assertEquals(1, phaser.arriveAndAwaitAdvance());
580 <                assertTrue(Thread.currentThread().isInterrupted());
580 >                assertTrue(Thread.interrupted());
581              }});
582  
583 <        await(pleaseInterrupt);
584 <        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
583 >        await(pleaseArrive);
584 >        assertThreadBlocks(t, Thread.State.WAITING);
585          Thread.currentThread().interrupt();
586          assertEquals(1, phaser.arriveAndAwaitAdvance());
587          assertTrue(Thread.interrupted());
# Line 573 | Line 602 | public class PhaserTest extends JSR166Te
602                  assertFalse(Thread.currentThread().isInterrupted());
603                  pleaseInterrupt.countDown();
604                  assertEquals(1, phaser.arriveAndAwaitAdvance());
605 <                assertTrue(Thread.currentThread().isInterrupted());
605 >                assertTrue(Thread.interrupted());
606              }});
607  
608          await(pleaseInterrupt);
609 <        waitForThreadToEnterWaitState(t, SHORT_DELAY_MS);
609 >        assertThreadBlocks(t, Thread.State.WAITING);
610          t.interrupt();
611          Thread.currentThread().interrupt();
612          assertEquals(1, phaser.arriveAndAwaitAdvance());
# Line 592 | Line 621 | public class PhaserTest extends JSR166Te
621      public void testAwaitAdvance4() {
622          final Phaser phaser = new Phaser(4);
623          final AtomicInteger count = new AtomicInteger(0);
624 <        List<Thread> threads = new ArrayList<Thread>();
624 >        List<Thread> threads = new ArrayList<>();
625          for (int i = 0; i < 4; i++)
626              threads.add(newStartedThread(new CheckedRunnable() {
627                  public void realRun() {
628                      for (int k = 0; k < 3; k++) {
629 <                        assertEquals(2*k+1, phaser.arriveAndAwaitAdvance());
629 >                        assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance());
630                          count.incrementAndGet();
631 <                        assertEquals(2*k+1, phaser.arrive());
632 <                        assertEquals(2*k+2, phaser.awaitAdvance(2*k+1));
633 <                        assertEquals(count.get(), 4*(k+1));
631 >                        assertEquals(2 * k + 1, phaser.arrive());
632 >                        assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1));
633 >                        assertEquals(4 * (k + 1), count.get());
634                      }}}));
635  
636          for (Thread thread : threads)
# Line 616 | Line 645 | public class PhaserTest extends JSR166Te
645          assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
646          assertEquals(1, phaser.getPhase());
647          assertEquals(1, phaser.register());
648 <        List<Thread> threads = new ArrayList<Thread>();
648 >        List<Thread> threads = new ArrayList<>();
649          for (int i = 0; i < 8; i++) {
650              final CountDownLatch latch = new CountDownLatch(1);
651              final boolean goesFirst = ((i & 1) == 0);
# Line 644 | Line 673 | public class PhaserTest extends JSR166Te
673       */
674      public void testAwaitAdvanceTieredPhaser() throws Exception {
675          final Phaser parent = new Phaser();
676 <        final List<Phaser> zeroPartyChildren = new ArrayList<Phaser>(3);
677 <        final List<Phaser> onePartyChildren = new ArrayList<Phaser>(3);
676 >        final List<Phaser> zeroPartyChildren = new ArrayList<>(3);
677 >        final List<Phaser> onePartyChildren = new ArrayList<>(3);
678          for (int i = 0; i < 3; i++) {
679              zeroPartyChildren.add(new Phaser(parent, 0));
680              onePartyChildren.add(new Phaser(parent, 1));
681          }
682 <        final List<Phaser> phasers = new ArrayList<Phaser>();
682 >        final List<Phaser> phasers = new ArrayList<>();
683          phasers.addAll(zeroPartyChildren);
684          phasers.addAll(onePartyChildren);
685          phasers.add(parent);
686          for (Phaser phaser : phasers) {
687              assertEquals(-42, phaser.awaitAdvance(-42));
688              assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
689 <            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
689 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
690          }
691  
692          for (Phaser child : onePartyChildren)
# Line 665 | Line 694 | public class PhaserTest extends JSR166Te
694          for (Phaser phaser : phasers) {
695              assertEquals(-42, phaser.awaitAdvance(-42));
696              assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
697 <            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
697 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
698              assertEquals(1, phaser.awaitAdvance(0));
699              assertEquals(1, phaser.awaitAdvanceInterruptibly(0));
700 <            assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
700 >            assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
701          }
702  
703          for (Phaser child : onePartyChildren)
# Line 676 | Line 705 | public class PhaserTest extends JSR166Te
705          for (Phaser phaser : phasers) {
706              assertEquals(-42, phaser.awaitAdvance(-42));
707              assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42));
708 <            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS));
708 >            assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS));
709              assertEquals(2, phaser.awaitAdvance(0));
710              assertEquals(2, phaser.awaitAdvanceInterruptibly(0));
711 <            assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS));
711 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS));
712              assertEquals(2, phaser.awaitAdvance(1));
713              assertEquals(2, phaser.awaitAdvanceInterruptibly(1));
714 <            assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS));
714 >            assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS));
715          }
716      }
717  
# Line 692 | Line 721 | public class PhaserTest extends JSR166Te
721      public void testAwaitAdvance6() {
722          final Phaser phaser = new Phaser(3);
723          final CountDownLatch pleaseForceTermination = new CountDownLatch(2);
724 <        final List<Thread> threads = new ArrayList<Thread>();
724 >        final List<Thread> threads = new ArrayList<>();
725          for (int i = 0; i < 2; i++) {
726              Runnable r = new CheckedRunnable() {
727                  public void realRun() {
# Line 720 | Line 749 | public class PhaserTest extends JSR166Te
749       * unarrived parties
750       */
751      public void testArriveAndAwaitAdvance1() {
752 +        Phaser phaser = new Phaser();
753          try {
724            Phaser phaser = new Phaser();
754              phaser.arriveAndAwaitAdvance();
755              shouldThrow();
756          } catch (IllegalStateException success) {}
# Line 736 | Line 765 | public class PhaserTest extends JSR166Te
765          final Phaser phaser = new Phaser(1);
766          final int THREADS = 3;
767          final CountDownLatch pleaseArrive = new CountDownLatch(THREADS);
768 <        final List<Thread> threads = new ArrayList<Thread>();
768 >        final List<Thread> threads = new ArrayList<>();
769          for (int i = 0; i < THREADS; i++)
770              threads.add(newStartedThread(new CheckedRunnable() {
771                  public void realRun() {
# Line 752 | Line 781 | public class PhaserTest extends JSR166Te
781          assertEquals(THREADS, phaser.getArrivedParties());
782          assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
783          for (Thread thread : threads)
784 <            waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS);
784 >            assertThreadBlocks(thread, Thread.State.WAITING);
785          for (Thread thread : threads)
786              assertTrue(thread.isAlive());
787          assertState(phaser, 0, THREADS + 1, 1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines