--- jsr166/src/test/tck/PhaserTest.java 2009/11/21 20:58:45 1.8 +++ jsr166/src/test/tck/PhaserTest.java 2010/10/11 05:35:19 1.16 @@ -8,7 +8,9 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; import junit.framework.Test; import junit.framework.TestSuite; @@ -176,7 +178,7 @@ public class PhaserTest extends JSR166Te } /** - * Arrive() on a registered phaser increments phase. + * Arrive() on a registered phaser increments phase. */ public void testArrive1() { Phaser phaser = new Phaser(1); @@ -194,7 +196,7 @@ public class PhaserTest extends JSR166Te for (int i = 0; i < 10; i++) phaser.register(); threads.add(newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { Thread.sleep(SMALL_DELAY_MS); phaser.arriveAndDeregister(); }})); @@ -235,7 +237,7 @@ public class PhaserTest extends JSR166Te phaser.register(); phaser.arrive(); int p = phaser.getArrivedParties(); - assertTrue(p == 1); + assertEquals(1, p); phaser.arriveAndDeregister(); assertTrue(phaser.getArrivedParties() < p); } @@ -252,8 +254,8 @@ public class PhaserTest extends JSR166Te assertTrue(parent.getUnarrivedParties() > 0); assertTrue(root.getUnarrivedParties() > 0); root.arriveAndDeregister(); - assertTrue(parent.getUnarrivedParties() == 0); - assertTrue(root.getUnarrivedParties() == 0); + assertEquals(0, parent.getUnarrivedParties()); + assertEquals(0, root.getUnarrivedParties()); assertTrue(root.isTerminated() && parent.isTerminated()); } @@ -283,8 +285,8 @@ public class PhaserTest extends JSR166Te assertTrue(child.getUnarrivedParties() > 0); root.register(); root.arriveAndDeregister(); - assertTrue(parent.getUnarrivedParties() == 0); - assertTrue(child.getUnarrivedParties() == 0); + assertEquals(0, parent.getUnarrivedParties()); + assertEquals(0, child.getUnarrivedParties()); assertTrue(root.isTerminated()); } @@ -295,7 +297,7 @@ public class PhaserTest extends JSR166Te public void testArriveAndDeregister6() throws InterruptedException { final Phaser phaser = new Phaser(2); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { sleepTillInterrupted(SHORT_DELAY_MS); phaser.arrive(); }}); @@ -330,7 +332,7 @@ public class PhaserTest extends JSR166Te phaser.register(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { phaser.register(); sleepTillInterrupted(LONG_DELAY_MS); phaser.awaitAdvance(phaser.arrive()); @@ -353,12 +355,12 @@ public class PhaserTest extends JSR166Te List threads = new ArrayList(); for (int i = 0; i < 4; i++) { threads.add(newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { int phase = phaser.arrive(); phaseCount.incrementAndGet(); sleepTillInterrupted(SMALL_DELAY_MS); phaser.awaitAdvance(phase); - threadAssertTrue(phaseCount.get() == 4); + assertEquals(phaseCount.get(), 4); }})); } for (Thread thread : threads) @@ -376,12 +378,12 @@ public class PhaserTest extends JSR166Te List threads = new ArrayList(); for (int i = 0; i < 8; i++) { threads.add(newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { sleepTillInterrupted(SHORT_DELAY_MS); phaser.arrive(); }})); phase = phaser.awaitAdvance(phaser.arrive()); - threadAssertEquals(phase, phaser.getPhase()); + assertEquals(phase, phaser.getPhase()); } for (Thread thread : threads) thread.join(); @@ -399,14 +401,14 @@ public class PhaserTest extends JSR166Te * waits for the second thread's party to arrive */ Thread t1 = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { sleepTillInterrupted(SMALL_DELAY_MS); int phase = phaser.awaitAdvance(phaser.arrive()); /* * This point is reached when force termination is called in which phase = -1 */ - threadAssertTrue(phase < 0); - threadAssertTrue(phaser.isTerminated()); + assertTrue(phase < 0); + assertTrue(phaser.isTerminated()); }}); /* * This thread will cause the first thread run to wait, in doing so @@ -414,12 +416,12 @@ public class PhaserTest extends JSR166Te * should exit peacefully as this one */ Thread t2 = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { sleepTillInterrupted(MEDIUM_DELAY_MS); int p1 = phaser.arrive(); int phase = phaser.awaitAdvance(p1); - threadAssertTrue(phase < 0); - threadAssertTrue(phaser.isTerminated()); + assertTrue(phase < 0); + assertTrue(phaser.isTerminated()); }}); phaser.arrive(); @@ -445,17 +447,27 @@ public class PhaserTest extends JSR166Te */ public void testArriveAndAwaitAdvance2() throws InterruptedException { final Phaser phaser = new Phaser(2); - Thread th = newStartedThread(new CheckedRunnable() { - void realRun() { + 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(); phaser.arriveAndAwaitAdvance(); + advanced.set(true); + assertTrue(Thread.currentThread().isInterrupted()); + while (!checkedInterruptStatus.get()) + Thread.yield(); }}); - Thread.sleep(SMALL_DELAY_MS); - th.interrupt(); - Thread.sleep(SMALL_DELAY_MS); + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + t.interrupt(); phaser.arrive(); - assertFalse(th.isInterrupted()); - th.join(); + while (!advanced.get()) + Thread.yield(); + assertTrue(t.isInterrupted()); + checkedInterruptStatus.set(true); + awaitTermination(t, SMALL_DELAY_MS); } /** @@ -465,22 +477,17 @@ public class PhaserTest extends JSR166Te */ public void testArriveAndAwaitAdvance3() throws InterruptedException { final Phaser phaser = new Phaser(1); - final AtomicInteger arrivingCount = new AtomicInteger(0); final List threads = new ArrayList(); - for (int i = 0; i < 6; i++) { + for (int i = 0; i < 3; i++) { threads.add(newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { - phaser.register(); - sleepTillInterrupted(SHORT_DELAY_MS); - arrivingCount.getAndIncrement(); - phaser.arrive(); - }})); + public void realRun() throws InterruptedException { + phaser.register(); + phaser.arriveAndAwaitAdvance(); + }})); } - int phaseNumber = phaser.arriveAndAwaitAdvance(); - arrivingCount.incrementAndGet(); - //the + 1 adds to expectedArrive to account for the main threads arrival - int expectedArrived = phaseNumber > 0 ? phaseNumber * six + 1 : phaser.getArrivedParties() + 1; - threadAssertEquals(expectedArrived, arrivingCount.get()); + Thread.sleep(MEDIUM_DELAY_MS); + assertEquals(phaser.getArrivedParties(), 3); + phaser.arriveAndAwaitAdvance(); for (Thread thread : threads) thread.join(); }