--- jsr166/src/test/tck/PhaserTest.java 2009/08/05 00:48:28 1.7 +++ 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; @@ -41,8 +43,7 @@ public class PhaserTest extends JSR166Te try { new Phaser(-1); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -62,8 +63,7 @@ public class PhaserTest extends JSR166Te try { new Phaser(new Phaser(), -1); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -100,8 +100,7 @@ public class PhaserTest extends JSR166Te try { phaser.register(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -141,8 +140,7 @@ public class PhaserTest extends JSR166Te try { new Phaser().bulkRegister(-1); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -163,8 +161,7 @@ public class PhaserTest extends JSR166Te try { new Phaser().bulkRegister(1 << 16); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -181,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); @@ -199,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(); }})); @@ -229,8 +226,7 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -241,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); } @@ -258,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()); } @@ -289,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()); } @@ -301,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(); }}); @@ -334,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() { - void realRun() throws InterruptedException { + 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); } /** @@ -359,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) @@ -381,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() { - 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()); - threadAssertEquals(phase, phaser.getPhase()); + assertEquals(phase, phaser.getPhase()); } for (Thread thread : threads) - thread.join(); + awaitTermination(thread, SMALL_DELAY_MS); } /** @@ -398,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() { - 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()); - }}); - /* - * 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() { - void realRun() { - sleepTillInterrupted(MEDIUM_DELAY_MS); - int p1 = phaser.arrive(); - int phase = phaser.awaitAdvance(p1); - threadAssertTrue(phase < 0); - threadAssertTrue(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); } /** @@ -443,8 +432,7 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndAwaitAdvance(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -452,17 +440,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); } /** @@ -472,22 +470,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(); }