--- jsr166/src/test/tck/PhaserTest.java 2009/08/05 00:48:28 1.7 +++ jsr166/src/test/tck/PhaserTest.java 2010/10/09 19:30:35 1.15 @@ -41,8 +41,7 @@ public class PhaserTest extends JSR166Te try { new Phaser(-1); shouldThrow(); - } catch (IllegalArgumentException success) { - } + } 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) {} } /** @@ -181,7 +176,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 +194,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 +224,7 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -241,7 +235,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 +252,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 +283,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 +295,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(); }}); @@ -336,7 +330,7 @@ public class PhaserTest extends JSR166Te phaser.register(); Thread t = newStartedThread(new CheckedRunnable() { - void realRun() throws InterruptedException { + public void realRun() throws InterruptedException { phaser.register(); sleepTillInterrupted(LONG_DELAY_MS); phaser.awaitAdvance(phaser.arrive()); @@ -359,12 +353,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) @@ -382,12 +376,12 @@ public class PhaserTest extends JSR166Te List threads = new ArrayList(); for (int i = 0; i < 8; i++) { threads.add(newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { sleepTillInterrupted(SHORT_DELAY_MS); phaser.arrive(); }})); phase = phaser.awaitAdvance(phaser.arrive()); - threadAssertEquals(phase, phaser.getPhase()); + assertEquals(phase, phaser.getPhase()); } for (Thread thread : threads) thread.join(); @@ -405,14 +399,14 @@ public class PhaserTest extends JSR166Te * waits for the second thread's party to arrive */ Thread t1 = newStartedThread(new CheckedRunnable() { - void realRun() { + 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()); + assertTrue(phase < 0); + assertTrue(phaser.isTerminated()); }}); /* * This thread will cause the first thread run to wait, in doing so @@ -420,12 +414,12 @@ public class PhaserTest extends JSR166Te * should exit peacefully as this one */ Thread t2 = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { sleepTillInterrupted(MEDIUM_DELAY_MS); int p1 = phaser.arrive(); int phase = phaser.awaitAdvance(p1); - threadAssertTrue(phase < 0); - threadAssertTrue(phaser.isTerminated()); + assertTrue(phase < 0); + assertTrue(phaser.isTerminated()); }}); phaser.arrive(); @@ -443,8 +437,7 @@ public class PhaserTest extends JSR166Te Phaser phaser = new Phaser(); phaser.arriveAndAwaitAdvance(); shouldThrow(); - } catch (IllegalStateException success) { - } + } catch (IllegalStateException success) {} } /** @@ -453,7 +446,7 @@ public class PhaserTest extends JSR166Te public void testArriveAndAwaitAdvance2() throws InterruptedException { final Phaser phaser = new Phaser(2); Thread th = newStartedThread(new CheckedRunnable() { - void realRun() { + public void realRun() { phaser.arriveAndAwaitAdvance(); }}); @@ -472,22 +465,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(); }