--- jsr166/src/test/tck/PhaserTest.java 2009/12/02 19:02:37 1.13 +++ jsr166/src/test/tck/PhaserTest.java 2010/10/12 06:19:44 1.18 @@ -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); @@ -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()); } @@ -328,19 +330,19 @@ public class PhaserTest extends JSR166Te public void testAwaitAdvance3() throws InterruptedException { final Phaser phaser = new Phaser(); phaser.register(); + final CountDownLatch threadStarted = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { phaser.register(); - sleepTillInterrupted(LONG_DELAY_MS); + threadStarted.countDown(); phaser.awaitAdvance(phaser.arrive()); + assertTrue(Thread.currentThread().isInterrupted()); }}); - Thread.sleep(SMALL_DELAY_MS); + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); t.interrupt(); - Thread.sleep(SMALL_DELAY_MS); phaser.arrive(); - assertFalse(t.isInterrupted()); - t.join(); + awaitTermination(t, SMALL_DELAY_MS); } /** @@ -375,16 +377,25 @@ public class PhaserTest extends JSR166Te phaser.register(); List threads = new ArrayList(); for (int i = 0; i < 8; i++) { + final CountDownLatch latch = new CountDownLatch(1); + final boolean goesFirst = ((i & 1) == 0); threads.add(newStartedThread(new CheckedRunnable() { - public void realRun() { - sleepTillInterrupted(SHORT_DELAY_MS); + public void realRun() throws InterruptedException { + if (goesFirst) + latch.countDown(); + else + assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); phaser.arrive(); }})); + if (goesFirst) + assertTrue(latch.await(SMALL_DELAY_MS, MILLISECONDS)); + else + latch.countDown(); phase = phaser.awaitAdvance(phaser.arrive()); assertEquals(phase, phaser.getPhase()); } for (Thread thread : threads) - thread.join(); + awaitTermination(thread, SMALL_DELAY_MS); } /** @@ -392,40 +403,24 @@ public class PhaserTest extends JSR166Te */ public void testAwaitAdvance6() throws InterruptedException { final Phaser phaser = new Phaser(3); - /* - * Start new thread. This thread waits a small amount of time - * and waits for the other two parties to arrive. The party - * in the main thread arrives quickly so at best this thread - * waits for the second thread's party to arrive - */ - Thread t1 = newStartedThread(new CheckedRunnable() { - 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 - */ - assertTrue(phase < 0); - assertTrue(phaser.isTerminated()); - }}); - /* - * This thread will cause the first thread run to wait, in doing so - * the main thread will force termination in which the first thread - * should exit peacefully as this one - */ - Thread t2 = newStartedThread(new CheckedRunnable() { - public void realRun() { - sleepTillInterrupted(MEDIUM_DELAY_MS); - int p1 = phaser.arrive(); - int phase = phaser.awaitAdvance(p1); - assertTrue(phase < 0); - assertTrue(phaser.isTerminated()); - }}); - - phaser.arrive(); + final CountDownLatch threadsStarted = new CountDownLatch(2); + final List threads = new ArrayList(); + for (int i = 0; i < 2; i++) { + Runnable r = new CheckedRunnable() { + public void realRun() { + int p1 = phaser.arrive(); + assertTrue(p1 >= 0); + threadsStarted.countDown(); + int phase = phaser.awaitAdvance(p1); + assertTrue(phase < 0); + assertTrue(phaser.isTerminated()); + }}; + threads.add(newStartedThread(r)); + } + threadsStarted.await(); phaser.forceTermination(); - t1.join(); - t2.join(); + for (Thread thread : threads) + awaitTermination(thread, SMALL_DELAY_MS); } /** @@ -445,17 +440,27 @@ public class PhaserTest extends JSR166Te */ public void testArriveAndAwaitAdvance2() throws InterruptedException { final Phaser phaser = new Phaser(2); - Thread th = newStartedThread(new CheckedRunnable() { - public 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); } /**