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.22 by jsr166, Mon Nov 8 20:02:57 2010 UTC

# Line 25 | Line 25 | public class PhaserTest extends JSR166Te
25          return new TestSuite(PhaserTest.class);
26      }
27  
28 +    private static final int maxParties = 65535;
29 +
30      /** Checks state of phaser. */
31      protected void assertState(Phaser phaser,
32                                 int phase, int parties, int unarrived) {
# Line 52 | Line 54 | public class PhaserTest extends JSR166Te
54       * Empty constructor builds a new Phaser with no parent, no registered
55       * parties and initial phase number of 0
56       */
57 <    public void testConstructor1() {
57 >    public void testConstructorDefaultValues() {
58          Phaser phaser = new Phaser();
59          assertNull(phaser.getParent());
60 +        assertEquals(0, phaser.getRegisteredParties());
61          assertEquals(0, phaser.getArrivedParties());
62 +        assertEquals(0, phaser.getUnarrivedParties());
63          assertEquals(0, phaser.getPhase());
64      }
65  
66      /**
67 <     * A negative party number for the constructor throws illegal argument
68 <     * exception
67 >     * Constructing with a negative number of parties throws
68 >     * IllegalArgumentException
69       */
70 <    public void testConstructor2() {
70 >    public void testConstructorNegativeParties() {
71          try {
72              new Phaser(-1);
73              shouldThrow();
# Line 71 | Line 75 | public class PhaserTest extends JSR166Te
75      }
76  
77      /**
78 <     * The parent being input into the constructor should equal the original
79 <     * parent when being returned
78 >     * Constructing with a negative number of parties throws
79 >     * IllegalArgumentException
80       */
81 <    public void testConstructor3() {
82 <        Phaser parent = new Phaser();
83 <        assertEquals(parent, new Phaser(parent).getParent());
81 >    public void testConstructorNegativeParties2() {
82 >        try {
83 >            new Phaser(new Phaser(), -1);
84 >            shouldThrow();
85 >        } catch (IllegalArgumentException success) {}
86      }
87  
88      /**
89 <     * A negative party number for the constructor throws illegal argument
90 <     * exception
89 >     * Constructing with a number of parties > 65535 throws
90 >     * IllegalArgumentException
91       */
92 <    public void testConstructor4() {
92 >    public void testConstructorPartiesExceedsLimit() {
93 >        new Phaser(maxParties);
94          try {
95 <            new Phaser(new Phaser(), -1);
95 >            new Phaser(maxParties + 1);
96 >            shouldThrow();
97 >        } catch (IllegalArgumentException success) {}
98 >
99 >        new Phaser(new Phaser(), maxParties);
100 >        try {
101 >            new Phaser(new Phaser(), maxParties + 1);
102              shouldThrow();
103          } catch (IllegalArgumentException success) {}
104      }
105  
106      /**
107 +     * The parent provided to the constructor should be returned from
108 +     * a later call to getParent
109 +     */
110 +    public void testConstructor3() {
111 +        Phaser parent = new Phaser();
112 +        assertSame(parent, new Phaser(parent).getParent());
113 +        assertNull(new Phaser(null).getParent());
114 +    }
115 +
116 +    /**
117       * The parent being input into the parameter should equal the original
118       * parent when being returned
119       */
120      public void testConstructor5() {
121          Phaser parent = new Phaser();
122 <        assertEquals(parent, new Phaser(parent, 0).getParent());
122 >        assertSame(parent, new Phaser(parent, 0).getParent());
123 >        assertNull(new Phaser(null, 0).getParent());
124      }
125  
126      /**
127 <     * register() will increment the number of unarrived parties by one and not
128 <     * affect its arrived parties
127 >     * register() will increment the number of unarrived parties by
128 >     * one and not affect its arrived parties
129       */
130      public void testRegister1() {
131          Phaser phaser = new Phaser();
# Line 115 | Line 139 | public class PhaserTest extends JSR166Te
139       */
140      public void testRegister2() {
141          Phaser phaser = new Phaser(0);
118        int maxParties = (1 << 16) - 1;
142          assertState(phaser, 0, 0, 0);
143          assertEquals(0, phaser.bulkRegister(maxParties - 10));
144          assertState(phaser, 0, maxParties - 10, maxParties - 10);
# Line 128 | Line 151 | public class PhaserTest extends JSR166Te
151              phaser.register();
152              shouldThrow();
153          } catch (IllegalStateException success) {}
154 +
155 +        try {
156 +            phaser.bulkRegister(Integer.MAX_VALUE);
157 +            shouldThrow();
158 +        } catch (IllegalStateException success) {}
159      }
160  
161      /**
# Line 386 | Line 414 | public class PhaserTest extends JSR166Te
414      }
415  
416      /**
417 <     * awaitAdvance continues waiting if interrupted
417 >     * awaitAdvance continues waiting if interrupted before waiting
418       */
419 <    public void testAwaitAdvance3() throws InterruptedException {
419 >    public void testAwaitAdvanceAfterInterrupt() throws InterruptedException {
420          final Phaser phaser = new Phaser();
421          assertEquals(0, phaser.register());
422          final CountDownLatch threadStarted = new CountDownLatch(1);
423  
424          Thread t = newStartedThread(new CheckedRunnable() {
425              public void realRun() throws InterruptedException {
426 +                Thread.currentThread().interrupt();
427                  assertEquals(0, phaser.register());
428 +                assertEquals(0, phaser.arrive());
429                  threadStarted.countDown();
430 <                assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
430 >                assertTrue(Thread.currentThread().isInterrupted());
431 >                assertEquals(1, phaser.awaitAdvance(0));
432                  assertTrue(Thread.currentThread().isInterrupted());
433              }});
434 +
435          assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
436 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
437 +        assertEquals(0, phaser.arrive());
438 +        awaitTermination(t, SMALL_DELAY_MS);
439 +
440 +        Thread.currentThread().interrupt();
441 +        assertEquals(1, phaser.awaitAdvance(0));
442 +        assertTrue(Thread.interrupted());
443 +    }
444 +
445 +    /**
446 +     * awaitAdvance continues waiting if interrupted while waiting
447 +     */
448 +    public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException {
449 +        final Phaser phaser = new Phaser();
450 +        assertEquals(0, phaser.register());
451 +        final CountDownLatch threadStarted = new CountDownLatch(1);
452 +
453 +        Thread t = newStartedThread(new CheckedRunnable() {
454 +            public void realRun() throws InterruptedException {
455 +                assertEquals(0, phaser.register());
456 +                assertEquals(0, phaser.arrive());
457 +                threadStarted.countDown();
458 +                assertFalse(Thread.currentThread().isInterrupted());
459 +                assertEquals(1, phaser.awaitAdvance(0));
460 +                assertTrue(Thread.currentThread().isInterrupted());
461 +            }});
462 +
463 +        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
464 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
465          t.interrupt();
466          assertEquals(0, phaser.arrive());
467          awaitTermination(t, SMALL_DELAY_MS);
468 +
469 +        Thread.currentThread().interrupt();
470 +        assertEquals(1, phaser.awaitAdvance(0));
471 +        assertTrue(Thread.interrupted());
472 +    }
473 +
474 +    /**
475 +     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
476 +     */
477 +    public void testArriveAndAwaitAdvanceAfterInterrupt()
478 +            throws InterruptedException {
479 +        final Phaser phaser = new Phaser();
480 +        assertEquals(0, phaser.register());
481 +        final CountDownLatch threadStarted = new CountDownLatch(1);
482 +
483 +        Thread t = newStartedThread(new CheckedRunnable() {
484 +            public void realRun() throws InterruptedException {
485 +                Thread.currentThread().interrupt();
486 +                assertEquals(0, phaser.register());
487 +                threadStarted.countDown();
488 +                assertTrue(Thread.currentThread().isInterrupted());
489 +                assertEquals(1, phaser.arriveAndAwaitAdvance());
490 +                assertTrue(Thread.currentThread().isInterrupted());
491 +            }});
492 +
493 +        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
494 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
495 +        Thread.currentThread().interrupt();
496 +        assertEquals(1, phaser.arriveAndAwaitAdvance());
497 +        assertTrue(Thread.interrupted());
498 +        awaitTermination(t, SMALL_DELAY_MS);
499 +    }
500 +
501 +    /**
502 +     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
503 +     */
504 +    public void testArriveAndAwaitAdvanceBeforeInterrupt()
505 +            throws InterruptedException {
506 +        final Phaser phaser = new Phaser();
507 +        assertEquals(0, phaser.register());
508 +        final CountDownLatch threadStarted = new CountDownLatch(1);
509 +
510 +        Thread t = newStartedThread(new CheckedRunnable() {
511 +            public void realRun() throws InterruptedException {
512 +                assertEquals(0, phaser.register());
513 +                threadStarted.countDown();
514 +                assertFalse(Thread.currentThread().isInterrupted());
515 +                assertEquals(1, phaser.arriveAndAwaitAdvance());
516 +                assertTrue(Thread.currentThread().isInterrupted());
517 +            }});
518 +
519 +        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
520 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
521 +        t.interrupt();
522 +        Thread.currentThread().interrupt();
523 +        assertEquals(1, phaser.arriveAndAwaitAdvance());
524 +        assertTrue(Thread.interrupted());
525 +        awaitTermination(t, SMALL_DELAY_MS);
526      }
527  
528      /**
# Line 501 | Line 620 | public class PhaserTest extends JSR166Te
620      }
621  
622      /**
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    /**
623       * arriveAndAwaitAdvance waits for all threads to arrive, the
624       * number of arrived parties is the same number that is accounted
625       * for when the main thread awaitsAdvance

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines