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.26 by dl, Wed Dec 1 17:20:51 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 36 | Line 38 | public class PhaserTest extends JSR166Te
38      }
39  
40      /** Checks state of terminated phaser. */
41 <    protected void assertTerminated(Phaser phaser, int parties, int unarrived) {
41 >    protected void assertTerminated(Phaser phaser,
42 >                                    int maxPhase, int parties, int unarrived) {
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, 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      /**
# Line 96 | Line 122 | public class PhaserTest extends JSR166Te
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  
164      /**
# Line 229 | Line 260 | public class PhaserTest extends JSR166Te
260              assertState(phaser, 0, 1, 1);
261          }
262          assertEquals(0, phaser.arriveAndDeregister());
263 <        assertTerminated(phaser);
263 >        assertTerminated(phaser, 1);
264      }
265  
266      /**
# Line 260 | Line 291 | public class PhaserTest extends JSR166Te
291      public void testArrive3() {
292          Phaser phaser = new Phaser(1);
293          phaser.forceTermination();
294 <        assertTerminated(phaser, 1, 1);
294 >        assertTerminated(phaser, 0, 1, 1);
295 >        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
296          assertTrue(phaser.arrive() < 0);
297          assertTrue(phaser.register() < 0);
298          assertTrue(phaser.arriveAndDeregister() < 0);
# Line 301 | Line 333 | public class PhaserTest extends JSR166Te
333          Phaser parent = new Phaser();
334          Phaser child = new Phaser(parent);
335          assertState(child, 0, 0, 0);
336 <        assertState(parent, 0, 1, 1);
336 >        assertState(parent, 0, 0, 0);
337          assertEquals(0, child.register());
338          assertState(child, 0, 1, 1);
339          assertState(parent, 0, 1, 1);
340          assertEquals(0, child.arriveAndDeregister());
341 <        assertTerminated(child);
342 <        assertTerminated(parent);
341 >        assertTerminated(child, 1);
342 >        assertTerminated(parent, 1);
343      }
344  
345      /**
# Line 334 | Line 366 | public class PhaserTest extends JSR166Te
366          Phaser root = new Phaser();
367          Phaser parent = new Phaser(root);
368          Phaser child = new Phaser(parent);
369 <        assertState(root, 0, 1, 1);
370 <        assertState(parent, 0, 1, 1);
369 >        assertState(root, 0, 0, 0);
370 >        assertState(parent, 0, 0, 0);
371          assertState(child, 0, 0, 0);
372          assertEquals(0, child.register());
373          assertState(root, 0, 1, 1);
374          assertState(parent, 0, 1, 1);
375          assertState(child, 0, 1, 1);
376          assertEquals(0, child.arriveAndDeregister());
377 <        assertTerminated(child);
378 <        assertTerminated(parent);
379 <        assertTerminated(root);
377 >        assertTerminated(child, 1);
378 >        assertTerminated(parent, 1);
379 >        assertTerminated(root, 1);
380      }
381  
382      /**
# Line 362 | Line 394 | public class PhaserTest extends JSR166Te
394          assertEquals(1, phaser.arriveAndDeregister());
395          assertState(phaser, 1, 1, 1);
396          assertEquals(1, phaser.arriveAndDeregister());
397 <        assertTerminated(phaser);
397 >        assertTerminated(phaser, 2);
398          awaitTermination(t, SHORT_DELAY_MS);
399      }
400  
# Line 386 | Line 418 | public class PhaserTest extends JSR166Te
418      }
419  
420      /**
421 <     * awaitAdvance continues waiting if interrupted
421 >     * awaitAdvance continues waiting if interrupted before waiting
422       */
423 <    public void testAwaitAdvance3() throws InterruptedException {
423 >    public void testAwaitAdvanceAfterInterrupt() throws InterruptedException {
424          final Phaser phaser = new Phaser();
425          assertEquals(0, phaser.register());
426          final CountDownLatch threadStarted = new CountDownLatch(1);
427  
428          Thread t = newStartedThread(new CheckedRunnable() {
429              public void realRun() throws InterruptedException {
430 +                Thread.currentThread().interrupt();
431                  assertEquals(0, phaser.register());
432 +                assertEquals(0, phaser.arrive());
433                  threadStarted.countDown();
434 <                assertEquals(1, phaser.awaitAdvance(phaser.arrive()));
434 >                assertTrue(Thread.currentThread().isInterrupted());
435 >                assertEquals(1, phaser.awaitAdvance(0));
436                  assertTrue(Thread.currentThread().isInterrupted());
437              }});
438 +
439          assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
440 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
441 +        assertEquals(0, phaser.arrive());
442 +        awaitTermination(t, SMALL_DELAY_MS);
443 +
444 +        Thread.currentThread().interrupt();
445 +        assertEquals(1, phaser.awaitAdvance(0));
446 +        assertTrue(Thread.interrupted());
447 +    }
448 +
449 +    /**
450 +     * awaitAdvance continues waiting if interrupted while waiting
451 +     */
452 +    public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException {
453 +        final Phaser phaser = new Phaser();
454 +        assertEquals(0, phaser.register());
455 +        final CountDownLatch threadStarted = new CountDownLatch(1);
456 +
457 +        Thread t = newStartedThread(new CheckedRunnable() {
458 +            public void realRun() throws InterruptedException {
459 +                assertEquals(0, phaser.register());
460 +                assertEquals(0, phaser.arrive());
461 +                threadStarted.countDown();
462 +                assertFalse(Thread.currentThread().isInterrupted());
463 +                assertEquals(1, phaser.awaitAdvance(0));
464 +                assertTrue(Thread.currentThread().isInterrupted());
465 +            }});
466 +
467 +        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
468 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
469          t.interrupt();
470          assertEquals(0, phaser.arrive());
471          awaitTermination(t, SMALL_DELAY_MS);
472 +
473 +        Thread.currentThread().interrupt();
474 +        assertEquals(1, phaser.awaitAdvance(0));
475 +        assertTrue(Thread.interrupted());
476 +    }
477 +
478 +    /**
479 +     * arriveAndAwaitAdvance continues waiting if interrupted before waiting
480 +     */
481 +    public void testArriveAndAwaitAdvanceAfterInterrupt()
482 +            throws InterruptedException {
483 +        final Phaser phaser = new Phaser();
484 +        assertEquals(0, phaser.register());
485 +        final CountDownLatch threadStarted = new CountDownLatch(1);
486 +
487 +        Thread t = newStartedThread(new CheckedRunnable() {
488 +            public void realRun() throws InterruptedException {
489 +                Thread.currentThread().interrupt();
490 +                assertEquals(0, phaser.register());
491 +                threadStarted.countDown();
492 +                assertTrue(Thread.currentThread().isInterrupted());
493 +                assertEquals(1, phaser.arriveAndAwaitAdvance());
494 +                assertTrue(Thread.currentThread().isInterrupted());
495 +            }});
496 +
497 +        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
498 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
499 +        Thread.currentThread().interrupt();
500 +        assertEquals(1, phaser.arriveAndAwaitAdvance());
501 +        assertTrue(Thread.interrupted());
502 +        awaitTermination(t, SMALL_DELAY_MS);
503 +    }
504 +
505 +    /**
506 +     * arriveAndAwaitAdvance continues waiting if interrupted while waiting
507 +     */
508 +    public void testArriveAndAwaitAdvanceBeforeInterrupt()
509 +            throws InterruptedException {
510 +        final Phaser phaser = new Phaser();
511 +        assertEquals(0, phaser.register());
512 +        final CountDownLatch threadStarted = new CountDownLatch(1);
513 +
514 +        Thread t = newStartedThread(new CheckedRunnable() {
515 +            public void realRun() throws InterruptedException {
516 +                assertEquals(0, phaser.register());
517 +                threadStarted.countDown();
518 +                assertFalse(Thread.currentThread().isInterrupted());
519 +                assertEquals(1, phaser.arriveAndAwaitAdvance());
520 +                assertTrue(Thread.currentThread().isInterrupted());
521 +            }});
522 +
523 +        assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
524 +        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
525 +        t.interrupt();
526 +        Thread.currentThread().interrupt();
527 +        assertEquals(1, phaser.arriveAndAwaitAdvance());
528 +        assertTrue(Thread.interrupted());
529 +        awaitTermination(t, SMALL_DELAY_MS);
530      }
531  
532      /**
# Line 475 | Line 598 | public class PhaserTest extends JSR166Te
598                      assertTrue(phaser.awaitAdvance(0) < 0);
599                      assertTrue(phaser.isTerminated());
600                      assertTrue(phaser.getPhase() < 0);
601 +                    assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
602                      assertEquals(3, phaser.getRegisteredParties());
603                  }};
604              threads.add(newStartedThread(r));
605          }
606          threadsStarted.await();
607          phaser.forceTermination();
608 +        assertEquals(0, phaser.getPhase() + Integer.MIN_VALUE);
609          for (Thread thread : threads)
610              awaitTermination(thread, SMALL_DELAY_MS);
611          assertTrue(phaser.isTerminated());
# Line 501 | Line 626 | public class PhaserTest extends JSR166Te
626      }
627  
628      /**
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    /**
629       * arriveAndAwaitAdvance waits for all threads to arrive, the
630       * number of arrived parties is the same number that is accounted
631       * for when the main thread awaitsAdvance

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines