--- jsr166/src/test/tck/PhaserTest.java 2009/08/03 20:33:57 1.5 +++ jsr166/src/test/tck/PhaserTest.java 2009/12/02 10:50:48 1.12 @@ -40,9 +40,8 @@ public class PhaserTest extends JSR166Te public void testConstructor2() { try { new Phaser(-1); - this.shouldThrow(); - } catch (IllegalArgumentException success) { - } + shouldThrow(); + } catch (IllegalArgumentException success) {} } /** @@ -62,8 +61,7 @@ public class PhaserTest extends JSR166Te try { new Phaser(new Phaser(), -1); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -100,8 +98,7 @@ public class PhaserTest extends JSR166Te try { phaser.register(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -141,8 +138,7 @@ public class PhaserTest extends JSR166Te try { new Phaser().bulkRegister(-1); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -163,8 +159,7 @@ public class PhaserTest extends JSR166Te try { new Phaser().bulkRegister(1 << 16); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -190,25 +185,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 +213,6 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(1); phaser.forceTermination(); assertTrue(phaser.arrive() < 0); - } /** @@ -230,9 +224,7 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); - - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -300,16 +292,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(); } /** @@ -325,37 +318,29 @@ public class PhaserTest extends JSR166Te * phaser */ public void testAwaitAdvance2() { - try { - Phaser phaser = new Phaser(); - phaser.awaitAdvance(-1); - } catch (Exception failure) { - this.unexpectedException(); - } + Phaser phaser = new Phaser(); + phaser.awaitAdvance(-1); } /** * awaitAdvance while waiting does not abort on interrupt. */ - public void testAwaitAdvance3() { + public void testAwaitAdvance3() throws InterruptedException { final Phaser phaser = new Phaser(); + phaser.register(); - 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(); + sleepTillInterrupted(LONG_DELAY_MS); phaser.awaitAdvance(phaser.arrive()); }}); - phaser.register(); - th1.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - th1.interrupt(); - Thread.sleep(LONG_DELAY_MS); - phaser.arrive(); - } catch (InterruptedException failure) { - threadUnexpectedException(failure); - } - assertFalse(th1.isInterrupted()); + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + Thread.sleep(SMALL_DELAY_MS); + phaser.arrive(); + assertFalse(t.isInterrupted()); + t.join(); } /** @@ -367,45 +352,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 @@ -413,32 +398,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(); } /** @@ -450,30 +437,25 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndAwaitAdvance(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** * Interrupted arriveAndAwaitAdvance does not throw InterruptedException */ - public void testArriveAndAwaitAdvance2() { + public void testArriveAndAwaitAdvance2() throws InterruptedException { final Phaser phaser = new Phaser(2); - Thread th = new Thread(new CheckedRunnable() { - void realRun() { + Thread th = newStartedThread(new CheckedRunnable() { + public void realRun() { phaser.arriveAndAwaitAdvance(); }}); - try { - th.start(); - Thread.sleep(LONG_DELAY_MS); - th.interrupt(); - Thread.sleep(LONG_DELAY_MS); - phaser.arrive(); - } catch (InterruptedException failure) { - this.unexpectedException(); - } + Thread.sleep(SMALL_DELAY_MS); + th.interrupt(); + Thread.sleep(SMALL_DELAY_MS); + phaser.arrive(); assertFalse(th.isInterrupted()); + th.join(); } /** @@ -481,43 +463,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(LONG_DELAY_MS); + assertEquals(phaser.getArrivedParties(), 3); + phaser.arriveAndAwaitAdvance(); + for (Thread thread : threads) + thread.join(); } }