--- jsr166/src/test/tck/PhaserTest.java 2009/07/31 23:37:31 1.2 +++ jsr166/src/test/tck/PhaserTest.java 2009/12/01 10:03:59 1.10 @@ -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) {} } /** @@ -88,7 +86,7 @@ public class PhaserTest extends JSR166Te } /** - * Registering any more then 65536 parties causes IllegalStateExceptiom + * Registering more than 65536 parties causes IllegalStateException */ public void testRegister2() { Phaser phaser = new Phaser(0); @@ -100,10 +98,7 @@ public class PhaserTest extends JSR166Te try { phaser.register(); shouldThrow(); - } catch (IllegalStateException success) { - } catch (Exception ex) { - threadUnexpectedException(ex); - } + } catch (IllegalStateException success) {} } /** @@ -137,14 +132,13 @@ public class PhaserTest extends JSR166Te /** * Invoking bulkRegister with a negative parameter throws an - * IllegalArgumentExceptiom + * IllegalArgumentException */ public void testBulkRegister1() { try { new Phaser().bulkRegister(-1); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } catch (IllegalArgumentException success) {} } /** @@ -158,15 +152,14 @@ public class PhaserTest extends JSR166Te } /** - * Registering with a number of parties greater then or equal to 1<<16 - * throws IllegalStateExceptiom. + * Registering with a number of parties greater than or equal to 1<<16 + * throws IllegalStateException. */ public void testBulkRegister3() { try { new Phaser().bulkRegister(1 << 16); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -192,55 +185,50 @@ 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() { - - public void run() { - 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(); } /** - * arrive() returns a negative number if the Phaser is termindated + * arrive() returns a negative number if the Phaser is terminated */ public void testArrive3() { Phaser phaser = new Phaser(1); phaser.forceTermination(); assertTrue(phaser.arrive() < 0); - } /** * arriveAndDeregister() throws IllegalStateException if number of - * registered or unnarived parties would become negative + * registered or unarrived parties would become negative */ public void testArriveAndDeregister1() { try { Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); - - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** - * arriveAndDeregister derigisters reduces the number of arrived parties + * arriveAndDeregister deregisters reduces the number of arrived parties */ public void testArriveAndDergeister2() { final Phaser phaser = new Phaser(1); @@ -304,18 +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() { - - public void run() { - 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(); } /** @@ -331,91 +318,79 @@ 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(); - Thread th1 = new Thread() { - - public void run() { - try { - phaser.register(); - getRunnable(LONG_DELAY_MS).run(); - phaser.awaitAdvance(phaser.arrive()); - } catch (Exception failure) { - threadUnexpectedException(failure); - } - - } - }; phaser.register(); - th1.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - th1.interrupt(); - Thread.sleep(LONG_DELAY_MS); - phaser.arrive(); - } catch (Exception failure) { - unexpectedException(); - } - assertFalse(th1.isInterrupted()); + + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + phaser.register(); + sleepTillInterrupted(LONG_DELAY_MS); + phaser.awaitAdvance(phaser.arrive()); + }}); + Thread.sleep(SMALL_DELAY_MS); + t.interrupt(); + Thread.sleep(SMALL_DELAY_MS); + phaser.arrive(); + assertFalse(t.isInterrupted()); + t.join(); } /** * awaitAdvance atomically waits for all parties within the same phase to * complete before continuing */ - public void testAwaitAdvance4() { - final Phaser phaser = new Phaser(four); + public void testAwaitAdvance4() throws InterruptedException { + final Phaser phaser = new Phaser(4); final AtomicInteger phaseCount = new AtomicInteger(0); - for (int i = 0; i < four; i++) { - new Thread() { - - public void run() { + List threads = new ArrayList(); + for (int i = 0; i < 4; i++) { + 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); - assertTrue(phaseCount.get() == four); - } - }.start(); + assertEquals(phaseCount.get(), 4); + }})); } + 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() { - - public void run() { - 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()); 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 @@ -423,36 +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() { - - public void run() { - 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() { - - public void run() { - 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(); } /** @@ -464,35 +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() { - public void run() { - try { - phaser.arriveAndAwaitAdvance(); - } catch (Exception failure) { - threadUnexpectedException(failure); - } - } - }; - - try { - th.start(); - Thread.sleep(LONG_DELAY_MS); - th.interrupt(); - Thread.sleep(LONG_DELAY_MS); - phaser.arrive(); - } catch (InterruptedException failure) { - this.unexpectedException(); - } + Thread th = newStartedThread(new CheckedRunnable() { + public void realRun() { + phaser.arriveAndAwaitAdvance(); + }}); + + Thread.sleep(SMALL_DELAY_MS); + th.interrupt(); + Thread.sleep(SMALL_DELAY_MS); + phaser.arrive(); assertFalse(th.isInterrupted()); + th.join(); } /** @@ -500,49 +463,26 @@ 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() { - - public void run() { + final List threads = new ArrayList(); + for (int i = 0; i < 6; i++) { + threads.add(newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { phaser.register(); - run.run(); + sleepTillInterrupted(SHORT_DELAY_MS); arrivingCount.getAndIncrement(); phaser.arrive(); - } - }.start(); + }})); } 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 Runnable() { - - public void run() { - try { - Thread.sleep(wait); - } catch (InterruptedException noop) { - // sleep interruption isn't a problem case for these example - } catch (Exception ex) { - threadUnexpectedException(ex); - } - - } - }; + assertEquals(expectedArrived, arrivingCount.get()); + for (Thread thread : threads) + thread.join(); } }