--- jsr166/src/test/tck/PhaserTest.java 2010/10/15 22:43:02 1.19 +++ jsr166/src/test/tck/PhaserTest.java 2010/11/27 16:47:05 1.23 @@ -25,6 +25,8 @@ public class PhaserTest extends JSR166Te return new TestSuite(PhaserTest.class); } + private static final int maxParties = 65535; + /** Checks state of phaser. */ protected void assertState(Phaser phaser, int phase, int parties, int unarrived) { @@ -52,18 +54,20 @@ public class PhaserTest extends JSR166Te * Empty constructor builds a new Phaser with no parent, no registered * parties and initial phase number of 0 */ - public void testConstructor1() { + public void testConstructorDefaultValues() { Phaser phaser = new Phaser(); assertNull(phaser.getParent()); + assertEquals(0, phaser.getRegisteredParties()); assertEquals(0, phaser.getArrivedParties()); + assertEquals(0, phaser.getUnarrivedParties()); assertEquals(0, phaser.getPhase()); } /** - * A negative party number for the constructor throws illegal argument - * exception + * Constructing with a negative number of parties throws + * IllegalArgumentException */ - public void testConstructor2() { + public void testConstructorNegativeParties() { try { new Phaser(-1); shouldThrow(); @@ -71,37 +75,57 @@ public class PhaserTest extends JSR166Te } /** - * The parent being input into the constructor should equal the original - * parent when being returned + * Constructing with a negative number of parties throws + * IllegalArgumentException */ - public void testConstructor3() { - Phaser parent = new Phaser(); - assertEquals(parent, new Phaser(parent).getParent()); + public void testConstructorNegativeParties2() { + try { + new Phaser(new Phaser(), -1); + shouldThrow(); + } catch (IllegalArgumentException success) {} } /** - * A negative party number for the constructor throws illegal argument - * exception + * Constructing with a number of parties > 65535 throws + * IllegalArgumentException */ - public void testConstructor4() { + public void testConstructorPartiesExceedsLimit() { + new Phaser(maxParties); try { - new Phaser(new Phaser(), -1); + new Phaser(maxParties + 1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + + new Phaser(new Phaser(), maxParties); + try { + new Phaser(new Phaser(), maxParties + 1); shouldThrow(); } catch (IllegalArgumentException success) {} } /** + * The parent provided to the constructor should be returned from + * a later call to getParent + */ + public void testConstructor3() { + Phaser parent = new Phaser(); + assertSame(parent, new Phaser(parent).getParent()); + assertNull(new Phaser(null).getParent()); + } + + /** * The parent being input into the parameter should equal the original * parent when being returned */ public void testConstructor5() { Phaser parent = new Phaser(); - assertEquals(parent, new Phaser(parent, 0).getParent()); + assertSame(parent, new Phaser(parent, 0).getParent()); + assertNull(new Phaser(null, 0).getParent()); } /** - * register() will increment the number of unarrived parties by one and not - * affect its arrived parties + * register() will increment the number of unarrived parties by + * one and not affect its arrived parties */ public void testRegister1() { Phaser phaser = new Phaser(); @@ -115,7 +139,6 @@ public class PhaserTest extends JSR166Te */ public void testRegister2() { Phaser phaser = new Phaser(0); - int maxParties = (1 << 16) - 1; assertState(phaser, 0, 0, 0); assertEquals(0, phaser.bulkRegister(maxParties - 10)); assertState(phaser, 0, maxParties - 10, maxParties - 10); @@ -128,6 +151,11 @@ public class PhaserTest extends JSR166Te phaser.register(); shouldThrow(); } catch (IllegalStateException success) {} + + try { + phaser.bulkRegister(Integer.MAX_VALUE); + shouldThrow(); + } catch (IllegalStateException success) {} } /** @@ -301,7 +329,7 @@ public class PhaserTest extends JSR166Te Phaser parent = new Phaser(); Phaser child = new Phaser(parent); assertState(child, 0, 0, 0); - assertState(parent, 0, 1, 1); + assertState(parent, 0, 0, 0); assertEquals(0, child.register()); assertState(child, 0, 1, 1); assertState(parent, 0, 1, 1); @@ -334,8 +362,8 @@ public class PhaserTest extends JSR166Te Phaser root = new Phaser(); Phaser parent = new Phaser(root); Phaser child = new Phaser(parent); - assertState(root, 0, 1, 1); - assertState(parent, 0, 1, 1); + assertState(root, 0, 0, 0); + assertState(parent, 0, 0, 0); assertState(child, 0, 0, 0); assertEquals(0, child.register()); assertState(root, 0, 1, 1); @@ -386,9 +414,38 @@ public class PhaserTest extends JSR166Te } /** - * awaitAdvance continues waiting if interrupted + * awaitAdvance continues waiting if interrupted before waiting + */ + public void testAwaitAdvanceAfterInterrupt() throws InterruptedException { + final Phaser phaser = new Phaser(); + assertEquals(0, phaser.register()); + final CountDownLatch threadStarted = new CountDownLatch(1); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.currentThread().interrupt(); + assertEquals(0, phaser.register()); + assertEquals(0, phaser.arrive()); + threadStarted.countDown(); + assertTrue(Thread.currentThread().isInterrupted()); + assertEquals(1, phaser.awaitAdvance(0)); + assertTrue(Thread.currentThread().isInterrupted()); + }}); + + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + assertEquals(0, phaser.arrive()); + awaitTermination(t, SMALL_DELAY_MS); + + Thread.currentThread().interrupt(); + assertEquals(1, phaser.awaitAdvance(0)); + assertTrue(Thread.interrupted()); + } + + /** + * awaitAdvance continues waiting if interrupted while waiting */ - public void testAwaitAdvance3() throws InterruptedException { + public void testAwaitAdvanceBeforeInterrupt() throws InterruptedException { final Phaser phaser = new Phaser(); assertEquals(0, phaser.register()); final CountDownLatch threadStarted = new CountDownLatch(1); @@ -396,14 +453,76 @@ public class PhaserTest extends JSR166Te Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertEquals(0, phaser.register()); + assertEquals(0, phaser.arrive()); threadStarted.countDown(); - assertEquals(1, phaser.awaitAdvance(phaser.arrive())); + assertFalse(Thread.currentThread().isInterrupted()); + assertEquals(1, phaser.awaitAdvance(0)); assertTrue(Thread.currentThread().isInterrupted()); }}); + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); t.interrupt(); assertEquals(0, phaser.arrive()); awaitTermination(t, SMALL_DELAY_MS); + + Thread.currentThread().interrupt(); + assertEquals(1, phaser.awaitAdvance(0)); + assertTrue(Thread.interrupted()); + } + + /** + * arriveAndAwaitAdvance continues waiting if interrupted before waiting + */ + public void testArriveAndAwaitAdvanceAfterInterrupt() + throws InterruptedException { + final Phaser phaser = new Phaser(); + assertEquals(0, phaser.register()); + final CountDownLatch threadStarted = new CountDownLatch(1); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.currentThread().interrupt(); + assertEquals(0, phaser.register()); + threadStarted.countDown(); + assertTrue(Thread.currentThread().isInterrupted()); + assertEquals(1, phaser.arriveAndAwaitAdvance()); + assertTrue(Thread.currentThread().isInterrupted()); + }}); + + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + Thread.currentThread().interrupt(); + assertEquals(1, phaser.arriveAndAwaitAdvance()); + assertTrue(Thread.interrupted()); + awaitTermination(t, SMALL_DELAY_MS); + } + + /** + * arriveAndAwaitAdvance continues waiting if interrupted while waiting + */ + public void testArriveAndAwaitAdvanceBeforeInterrupt() + throws InterruptedException { + final Phaser phaser = new Phaser(); + assertEquals(0, phaser.register()); + final CountDownLatch threadStarted = new CountDownLatch(1); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + assertEquals(0, phaser.register()); + threadStarted.countDown(); + assertFalse(Thread.currentThread().isInterrupted()); + assertEquals(1, phaser.arriveAndAwaitAdvance()); + assertTrue(Thread.currentThread().isInterrupted()); + }}); + + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + waitForThreadToEnterWaitState(t, SMALL_DELAY_MS); + t.interrupt(); + Thread.currentThread().interrupt(); + assertEquals(1, phaser.arriveAndAwaitAdvance()); + assertTrue(Thread.interrupted()); + awaitTermination(t, SMALL_DELAY_MS); } /** @@ -501,35 +620,6 @@ public class PhaserTest extends JSR166Te } /** - * Interrupted arriveAndAwaitAdvance does not throw InterruptedException - */ - public void testArriveAndAwaitAdvance2() throws InterruptedException { - final Phaser phaser = new Phaser(2); - final CountDownLatch threadStarted = new CountDownLatch(1); - final AtomicBoolean advanced = new AtomicBoolean(false); - final AtomicBoolean checkedInterruptStatus = new AtomicBoolean(false); - Thread t = newStartedThread(new CheckedRunnable() { - public void realRun() throws InterruptedException { - threadStarted.countDown(); - assertEquals(1, phaser.arriveAndAwaitAdvance()); - assertState(phaser, 1, 2, 2); - advanced.set(true); - assertTrue(Thread.currentThread().isInterrupted()); - while (!checkedInterruptStatus.get()) - Thread.yield(); - }}); - - assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); - t.interrupt(); - assertEquals(0, phaser.arrive()); - while (!advanced.get()) - Thread.yield(); - assertTrue(t.isInterrupted()); - checkedInterruptStatus.set(true); - awaitTermination(t, SMALL_DELAY_MS); - } - - /** * arriveAndAwaitAdvance waits for all threads to arrive, the * number of arrived parties is the same number that is accounted * for when the main thread awaitsAdvance