--- jsr166/src/test/tck/PhaserTest.java 2009/08/03 22:06:50 1.6 +++ jsr166/src/test/tck/PhaserTest.java 2010/10/11 07:43:53 1.17 @@ -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); @@ -190,25 +187,25 @@ public class PhaserTest extends JSR166Te } /** - * arrive does not wait for others to arrive at barrier + * arriveAndDeregister does not wait for others to arrive at barrier */ - public void testArrive2() { + public void testArrive2() throws InterruptedException { final Phaser phaser = new Phaser(1); phaser.register(); - Thread thread = null; - for (final Runnable r : getRunnables(10, SHORT_DELAY_MS)) { + List threads = new ArrayList(); + for (int i = 0; i < 10; i++) phaser.register(); - thread = new Thread(new CheckedRunnable() { - void realRun() { - r.run(); + threads.add(newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(SMALL_DELAY_MS); phaser.arriveAndDeregister(); - }}); - thread.start(); - } + }})); phaser.arrive(); - assertTrue(thread.isAlive()); + assertTrue(threads.get(0).isAlive()); assertFalse(phaser.isTerminated()); + for (Thread thread : threads) + thread.join(); } /** @@ -218,7 +215,6 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(1); phaser.forceTermination(); assertTrue(phaser.arrive() < 0); - } /** @@ -230,8 +226,7 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -242,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); } @@ -259,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()); } @@ -290,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()); } @@ -299,16 +294,17 @@ public class PhaserTest extends JSR166Te * arriveAndDeregister returns the phase in which it leaves the * phaser in after deregistration */ - public void testArriveAndDeregister6() { + public void testArriveAndDeregister6() throws InterruptedException { final Phaser phaser = new Phaser(2); - new Thread(new CheckedRunnable() { - void realRun() { - getRunnable(SHORT_DELAY_MS).run(); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() { + sleepTillInterrupted(SHORT_DELAY_MS); phaser.arrive(); - }}).start(); + }}); phaser.arriveAndAwaitAdvance(); int phase = phaser.arriveAndDeregister(); assertEquals(phase, phaser.getPhase()); + t.join(); } /** @@ -333,20 +329,20 @@ public class PhaserTest extends JSR166Te */ public void testAwaitAdvance3() throws InterruptedException { final Phaser phaser = new Phaser(); + phaser.register(); + final CountDownLatch threadStarted = new CountDownLatch(1); - Thread th1 = new Thread(new CheckedRunnable() { - void realRun() throws InterruptedException { + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { phaser.register(); - getRunnable(LONG_DELAY_MS).run(); + threadStarted.countDown(); phaser.awaitAdvance(phaser.arrive()); + assertTrue(Thread.currentThread().isInterrupted()); }}); - phaser.register(); - th1.start(); - Thread.sleep(SHORT_DELAY_MS); - th1.interrupt(); - Thread.sleep(LONG_DELAY_MS); + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + t.interrupt(); phaser.arrive(); - assertFalse(th1.isInterrupted()); + awaitTermination(t, SMALL_DELAY_MS); } /** @@ -358,44 +354,45 @@ public class PhaserTest extends JSR166Te final AtomicInteger phaseCount = new AtomicInteger(0); List threads = new ArrayList(); for (int i = 0; i < 4; i++) { - threads.add(new Thread(new CheckedRunnable() { - void realRun() { + threads.add(newStartedThread(new CheckedRunnable() { + public void realRun() { int phase = phaser.arrive(); phaseCount.incrementAndGet(); - getRunnable(LONG_DELAY_MS).run(); + sleepTillInterrupted(SMALL_DELAY_MS); phaser.awaitAdvance(phase); - threadAssertTrue(phaseCount.get() == 4); + assertEquals(phaseCount.get(), 4); }})); } for (Thread thread : threads) - thread.start(); - for (Thread thread : threads) thread.join(); } /** * awaitAdvance returns the current phase */ - public void testAwaitAdvance5() { + public void testAwaitAdvance5() throws InterruptedException { final Phaser phaser = new Phaser(1); int phase = phaser.awaitAdvance(phaser.arrive()); assertEquals(phase, phaser.getPhase()); phaser.register(); - for (int i = 0; i < eight; i++) { - new Thread(new CheckedRunnable() { - void realRun() { - getRunnable(SHORT_DELAY_MS).run(); + List threads = new ArrayList(); + for (int i = 0; i < 8; i++) { + threads.add(newStartedThread(new CheckedRunnable() { + public void realRun() { + sleepTillInterrupted(SHORT_DELAY_MS); phaser.arrive(); - }}).start(); + }})); phase = phaser.awaitAdvance(phaser.arrive()); - threadAssertEquals(phase, phaser.getPhase()); + assertEquals(phase, phaser.getPhase()); } + for (Thread thread : threads) + thread.join(); } /** * awaitAdvance returns when the phaser is externally terminated */ - public void testAwaitAdvance6() { + public void testAwaitAdvance6() throws InterruptedException { final Phaser phaser = new Phaser(3); /* * Start new thread. This thread waits a small amount of time @@ -403,32 +400,34 @@ public class PhaserTest extends JSR166Te * in the main thread arrives quickly so at best this thread * waits for the second thread's party to arrive */ - new Thread(new CheckedRunnable() { - void realRun() { - getRunnable(SMALL_DELAY_MS).run(); + 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 */ - threadAssertTrue(phase < 0); - threadAssertTrue(phaser.isTerminated()); - }}).start(); + 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 */ - new Thread(new CheckedRunnable() { - void realRun() { - getRunnable(LONG_DELAY_MS).run(); + Thread t2 = newStartedThread(new CheckedRunnable() { + public void realRun() { + sleepTillInterrupted(MEDIUM_DELAY_MS); int p1 = phaser.arrive(); int phase = phaser.awaitAdvance(p1); - threadAssertTrue(phase < 0); - threadAssertTrue(phaser.isTerminated()); - }}).start(); + assertTrue(phase < 0); + assertTrue(phaser.isTerminated()); + }}); phaser.arrive(); phaser.forceTermination(); + t1.join(); + t2.join(); } /** @@ -440,8 +439,7 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndAwaitAdvance(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -449,17 +447,27 @@ public class PhaserTest extends JSR166Te */ public void testArriveAndAwaitAdvance2() throws InterruptedException { final Phaser phaser = new Phaser(2); - Thread th = new Thread(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(); }}); - th.start(); - Thread.sleep(LONG_DELAY_MS); - th.interrupt(); - Thread.sleep(LONG_DELAY_MS); + assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); + t.interrupt(); phaser.arrive(); - assertFalse(th.isInterrupted()); + while (!advanced.get()) + Thread.yield(); + assertTrue(t.isInterrupted()); + checkedInterruptStatus.set(true); + awaitTermination(t, SMALL_DELAY_MS); } /** @@ -467,43 +475,21 @@ public class PhaserTest extends JSR166Te * number of arrived parties is the same number that is accounted * for when the main thread awaitsAdvance */ - public void testArriveAndAwaitAdvance3() { + public void testArriveAndAwaitAdvance3() throws InterruptedException { final Phaser phaser = new Phaser(1); - final AtomicInteger arrivingCount = new AtomicInteger(0); - for (final Runnable run : getRunnables(six, SHORT_DELAY_MS)) { - new Thread(new CheckedRunnable() { - void realRun() { - phaser.register(); - run.run(); - arrivingCount.getAndIncrement(); - phaser.arrive(); - }}).start(); + final List threads = new ArrayList(); + for (int i = 0; i < 3; i++) { + threads.add(newStartedThread(new CheckedRunnable() { + 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()); - } - // .. initially called, for n tasks via - private List getRunnables(int size, long wait) { - List list = new ArrayList(); - for (int i = 0; i < size; i++) { - list.add(getRunnable(wait)); - } - return list; - } - - private Runnable getRunnable(final long wait) { - return new CheckedRunnable() { - void realRun() { - try { - Thread.sleep(wait); - } catch (InterruptedException noop) { - // sleep interruption isn't a problem case for these example - } - } - }; + Thread.sleep(MEDIUM_DELAY_MS); + assertEquals(phaser.getArrivedParties(), 3); + phaser.arriveAndAwaitAdvance(); + for (Thread thread : threads) + thread.join(); } }