--- jsr166/src/test/tck/PhaserTest.java 2011/06/22 07:46:57 1.33 +++ jsr166/src/test/tck/PhaserTest.java 2015/05/15 18:21:19 1.41 @@ -5,21 +5,22 @@ * Other contributors include John Vint */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Phaser; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Phaser; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import junit.framework.Test; +import junit.framework.TestSuite; + public class PhaserTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -165,8 +166,8 @@ public class PhaserTest extends JSR166Te } /** - * register() correctly returns the current barrier phase number when - * invoked + * register() correctly returns the current barrier phase number + * when invoked */ public void testRegister3() { Phaser phaser = new Phaser(); @@ -177,8 +178,8 @@ public class PhaserTest extends JSR166Te } /** - * register causes the next arrive to not increment the phase rather retain - * the phase number + * register causes the next arrive to not increment the phase + * rather retain the phase number */ public void testRegister4() { Phaser phaser = new Phaser(1); @@ -189,6 +190,33 @@ public class PhaserTest extends JSR166Te } /** + * register on a subphaser that is currently empty succeeds, even + * in the presence of another non-empty subphaser + */ + public void testRegisterEmptySubPhaser() { + Phaser root = new Phaser(); + Phaser child1 = new Phaser(root, 1); + Phaser child2 = new Phaser(root, 0); + assertEquals(0, child2.register()); + assertState(root, 0, 2, 2); + assertState(child1, 0, 1, 1); + assertState(child2, 0, 1, 1); + assertEquals(0, child2.arriveAndDeregister()); + assertState(root, 0, 1, 1); + assertState(child1, 0, 1, 1); + assertState(child2, 0, 0, 0); + assertEquals(0, child2.register()); + assertEquals(0, child2.arriveAndDeregister()); + assertState(root, 0, 1, 1); + assertState(child1, 0, 1, 1); + assertState(child2, 0, 0, 0); + assertEquals(0, child1.arriveAndDeregister()); + assertTerminated(root, 1); + assertTerminated(child1, 1); + assertTerminated(child2, 1); + } + + /** * Invoking bulkRegister with a negative parameter throws an * IllegalArgumentException */ @@ -200,8 +228,8 @@ public class PhaserTest extends JSR166Te } /** - * bulkRegister should correctly record the number of unarrived parties with - * the number of parties being registered + * bulkRegister should correctly record the number of unarrived + * parties with the number of parties being registered */ public void testBulkRegister2() { Phaser phaser = new Phaser(); @@ -310,8 +338,8 @@ public class PhaserTest extends JSR166Te * registered or unarrived parties would become negative */ public void testArriveAndDeregister1() { + Phaser phaser = new Phaser(); try { - Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); } catch (IllegalStateException success) {} @@ -601,7 +629,7 @@ public class PhaserTest extends JSR166Te count.incrementAndGet(); assertEquals(2*k+1, phaser.arrive()); assertEquals(2*k+2, phaser.awaitAdvance(2*k+1)); - assertEquals(count.get(), 4*(k+1)); + assertEquals(4*(k+1), count.get()); }}})); for (Thread thread : threads) @@ -720,8 +748,8 @@ public class PhaserTest extends JSR166Te * unarrived parties */ public void testArriveAndAwaitAdvance1() { + Phaser phaser = new Phaser(); try { - Phaser phaser = new Phaser(); phaser.arriveAndAwaitAdvance(); shouldThrow(); } catch (IllegalStateException success) {}