--- jsr166/src/test/tck/PhaserTest.java 2011/06/22 07:46:57 1.33 +++ jsr166/src/test/tck/PhaserTest.java 2021/01/26 13:33:06 1.51 @@ -5,21 +5,22 @@ * Other contributors include John Vint */ -import junit.framework.*; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + import java.util.ArrayList; import java.util.List; -import java.util.concurrent.Phaser; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Phaser; import java.util.concurrent.TimeoutException; -import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import junit.framework.Test; +import junit.framework.TestSuite; + public class PhaserTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { @@ -165,8 +166,8 @@ public class PhaserTest extends JSR166Te } /** - * register() correctly returns the current barrier phase number when - * invoked + * register() correctly returns the current barrier phase number + * when invoked */ public void testRegister3() { Phaser phaser = new Phaser(); @@ -177,8 +178,8 @@ public class PhaserTest extends JSR166Te } /** - * register causes the next arrive to not increment the phase rather retain - * the phase number + * register causes the next arrive to not increment the phase + * rather retain the phase number */ public void testRegister4() { Phaser phaser = new Phaser(1); @@ -189,6 +190,33 @@ public class PhaserTest extends JSR166Te } /** + * register on a subphaser that is currently empty succeeds, even + * in the presence of another non-empty subphaser + */ + public void testRegisterEmptySubPhaser() { + Phaser root = new Phaser(); + Phaser child1 = new Phaser(root, 1); + Phaser child2 = new Phaser(root, 0); + assertEquals(0, child2.register()); + assertState(root, 0, 2, 2); + assertState(child1, 0, 1, 1); + assertState(child2, 0, 1, 1); + assertEquals(0, child2.arriveAndDeregister()); + assertState(root, 0, 1, 1); + assertState(child1, 0, 1, 1); + assertState(child2, 0, 0, 0); + assertEquals(0, child2.register()); + assertEquals(0, child2.arriveAndDeregister()); + assertState(root, 0, 1, 1); + assertState(child1, 0, 1, 1); + assertState(child2, 0, 0, 0); + assertEquals(0, child1.arriveAndDeregister()); + assertTerminated(root, 1); + assertTerminated(child1, 1); + assertTerminated(child2, 1); + } + + /** * Invoking bulkRegister with a negative parameter throws an * IllegalArgumentException */ @@ -200,8 +228,8 @@ public class PhaserTest extends JSR166Te } /** - * bulkRegister should correctly record the number of unarrived parties with - * the number of parties being registered + * bulkRegister should correctly record the number of unarrived + * parties with the number of parties being registered */ public void testBulkRegister2() { Phaser phaser = new Phaser(); @@ -233,7 +261,7 @@ public class PhaserTest extends JSR166Te * the phase number increments correctly when tripping the barrier */ public void testPhaseIncrement1() { - for (int size = 1; size < nine; size++) { + for (int size = 1; size < 9; size++) { final Phaser phaser = new Phaser(size); for (int index = 0; index <= (1 << size); index++) { int phase = phaser.arrive(); @@ -274,7 +302,7 @@ public class PhaserTest extends JSR166Te public void testArrive2() { final Phaser phaser = new Phaser(); assertEquals(0, phaser.register()); - List threads = new ArrayList(); + List threads = new ArrayList<>(); for (int i = 0; i < 10; i++) { assertEquals(0, phaser.register()); threads.add(newStartedThread(new CheckedRunnable() { @@ -310,8 +338,8 @@ public class PhaserTest extends JSR166Te * registered or unarrived parties would become negative */ public void testArriveAndDeregister1() { + Phaser phaser = new Phaser(); try { - Phaser phaser = new Phaser(); phaser.arriveAndDeregister(); shouldThrow(); } catch (IllegalStateException success) {} @@ -425,7 +453,7 @@ public class PhaserTest extends JSR166Te /** * awaitAdvanceInterruptibly blocks interruptibly */ - public void testAwaitAdvanceInterruptibly_interruptible() throws InterruptedException { + public void testAwaitAdvanceInterruptibly_Interruptible() throws InterruptedException { final Phaser phaser = new Phaser(1); final CountDownLatch pleaseInterrupt = new CountDownLatch(2); @@ -450,14 +478,14 @@ public class PhaserTest extends JSR166Te public void realRun() throws TimeoutException { Thread.currentThread().interrupt(); try { - phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); + phaser.awaitAdvanceInterruptibly(0, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); pleaseInterrupt.countDown(); try { - phaser.awaitAdvanceInterruptibly(0, 2*LONG_DELAY_MS, MILLISECONDS); + phaser.awaitAdvanceInterruptibly(0, LONGER_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); @@ -465,7 +493,8 @@ public class PhaserTest extends JSR166Te await(pleaseInterrupt); assertState(phaser, 0, 1, 1); - assertThreadsStayAlive(t1, t2); + if (randomBoolean()) assertThreadBlocks(t1, Thread.State.WAITING); + if (randomBoolean()) assertThreadBlocks(t2, Thread.State.TIMED_WAITING); t1.interrupt(); t2.interrupt(); awaitTermination(t1); @@ -495,7 +524,7 @@ public class PhaserTest extends JSR166Te }}); await(pleaseArrive); - waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); + assertThreadBlocks(t, Thread.State.WAITING); assertEquals(0, phaser.arrive()); awaitTermination(t); @@ -523,7 +552,7 @@ public class PhaserTest extends JSR166Te }}); await(pleaseArrive); - waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); assertEquals(0, phaser.arrive()); awaitTermination(t); @@ -539,20 +568,20 @@ public class PhaserTest extends JSR166Te public void testArriveAndAwaitAdvanceAfterInterrupt() { final Phaser phaser = new Phaser(); assertEquals(0, phaser.register()); - final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + final CountDownLatch pleaseArrive = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() { Thread.currentThread().interrupt(); assertEquals(0, phaser.register()); - pleaseInterrupt.countDown(); + pleaseArrive.countDown(); assertTrue(Thread.currentThread().isInterrupted()); assertEquals(1, phaser.arriveAndAwaitAdvance()); - assertTrue(Thread.currentThread().isInterrupted()); + assertTrue(Thread.interrupted()); }}); - await(pleaseInterrupt); - waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); + await(pleaseArrive); + assertThreadBlocks(t, Thread.State.WAITING); Thread.currentThread().interrupt(); assertEquals(1, phaser.arriveAndAwaitAdvance()); assertTrue(Thread.interrupted()); @@ -573,11 +602,11 @@ public class PhaserTest extends JSR166Te assertFalse(Thread.currentThread().isInterrupted()); pleaseInterrupt.countDown(); assertEquals(1, phaser.arriveAndAwaitAdvance()); - assertTrue(Thread.currentThread().isInterrupted()); + assertTrue(Thread.interrupted()); }}); await(pleaseInterrupt); - waitForThreadToEnterWaitState(t, SHORT_DELAY_MS); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); Thread.currentThread().interrupt(); assertEquals(1, phaser.arriveAndAwaitAdvance()); @@ -592,16 +621,16 @@ public class PhaserTest extends JSR166Te public void testAwaitAdvance4() { final Phaser phaser = new Phaser(4); final AtomicInteger count = new AtomicInteger(0); - List threads = new ArrayList(); + List threads = new ArrayList<>(); for (int i = 0; i < 4; i++) threads.add(newStartedThread(new CheckedRunnable() { public void realRun() { for (int k = 0; k < 3; k++) { - assertEquals(2*k+1, phaser.arriveAndAwaitAdvance()); + assertEquals(2 * k + 1, phaser.arriveAndAwaitAdvance()); count.incrementAndGet(); - assertEquals(2*k+1, phaser.arrive()); - assertEquals(2*k+2, phaser.awaitAdvance(2*k+1)); - assertEquals(count.get(), 4*(k+1)); + assertEquals(2 * k + 1, phaser.arrive()); + assertEquals(2 * k + 2, phaser.awaitAdvance(2 * k + 1)); + assertEquals(4 * (k + 1), count.get()); }}})); for (Thread thread : threads) @@ -616,7 +645,7 @@ public class PhaserTest extends JSR166Te assertEquals(1, phaser.awaitAdvance(phaser.arrive())); assertEquals(1, phaser.getPhase()); assertEquals(1, phaser.register()); - List threads = new ArrayList(); + List threads = new ArrayList<>(); for (int i = 0; i < 8; i++) { final CountDownLatch latch = new CountDownLatch(1); final boolean goesFirst = ((i & 1) == 0); @@ -644,20 +673,20 @@ public class PhaserTest extends JSR166Te */ public void testAwaitAdvanceTieredPhaser() throws Exception { final Phaser parent = new Phaser(); - final List zeroPartyChildren = new ArrayList(3); - final List onePartyChildren = new ArrayList(3); + final List zeroPartyChildren = new ArrayList<>(3); + final List onePartyChildren = new ArrayList<>(3); for (int i = 0; i < 3; i++) { zeroPartyChildren.add(new Phaser(parent, 0)); onePartyChildren.add(new Phaser(parent, 1)); } - final List phasers = new ArrayList(); + final List phasers = new ArrayList<>(); phasers.addAll(zeroPartyChildren); phasers.addAll(onePartyChildren); phasers.add(parent); for (Phaser phaser : phasers) { assertEquals(-42, phaser.awaitAdvance(-42)); assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); - assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); + assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); } for (Phaser child : onePartyChildren) @@ -665,10 +694,10 @@ public class PhaserTest extends JSR166Te for (Phaser phaser : phasers) { assertEquals(-42, phaser.awaitAdvance(-42)); assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); - assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); + assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(1, phaser.awaitAdvance(0)); assertEquals(1, phaser.awaitAdvanceInterruptibly(0)); - assertEquals(1, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); + assertEquals(1, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); } for (Phaser child : onePartyChildren) @@ -676,13 +705,13 @@ public class PhaserTest extends JSR166Te for (Phaser phaser : phasers) { assertEquals(-42, phaser.awaitAdvance(-42)); assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42)); - assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, SMALL_DELAY_MS, MILLISECONDS)); + assertEquals(-42, phaser.awaitAdvanceInterruptibly(-42, MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(2, phaser.awaitAdvance(0)); assertEquals(2, phaser.awaitAdvanceInterruptibly(0)); - assertEquals(2, phaser.awaitAdvanceInterruptibly(0, SMALL_DELAY_MS, MILLISECONDS)); + assertEquals(2, phaser.awaitAdvanceInterruptibly(0, MEDIUM_DELAY_MS, MILLISECONDS)); assertEquals(2, phaser.awaitAdvance(1)); assertEquals(2, phaser.awaitAdvanceInterruptibly(1)); - assertEquals(2, phaser.awaitAdvanceInterruptibly(1, SMALL_DELAY_MS, MILLISECONDS)); + assertEquals(2, phaser.awaitAdvanceInterruptibly(1, MEDIUM_DELAY_MS, MILLISECONDS)); } } @@ -692,7 +721,7 @@ public class PhaserTest extends JSR166Te public void testAwaitAdvance6() { final Phaser phaser = new Phaser(3); final CountDownLatch pleaseForceTermination = new CountDownLatch(2); - final List threads = new ArrayList(); + final List threads = new ArrayList<>(); for (int i = 0; i < 2; i++) { Runnable r = new CheckedRunnable() { public void realRun() { @@ -720,8 +749,8 @@ public class PhaserTest extends JSR166Te * unarrived parties */ public void testArriveAndAwaitAdvance1() { + Phaser phaser = new Phaser(); try { - Phaser phaser = new Phaser(); phaser.arriveAndAwaitAdvance(); shouldThrow(); } catch (IllegalStateException success) {} @@ -736,7 +765,7 @@ public class PhaserTest extends JSR166Te final Phaser phaser = new Phaser(1); final int THREADS = 3; final CountDownLatch pleaseArrive = new CountDownLatch(THREADS); - final List threads = new ArrayList(); + final List threads = new ArrayList<>(); for (int i = 0; i < THREADS; i++) threads.add(newStartedThread(new CheckedRunnable() { public void realRun() { @@ -752,7 +781,7 @@ public class PhaserTest extends JSR166Te assertEquals(THREADS, phaser.getArrivedParties()); assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); for (Thread thread : threads) - waitForThreadToEnterWaitState(thread, SHORT_DELAY_MS); + assertThreadBlocks(thread, Thread.State.WAITING); for (Thread thread : threads) assertTrue(thread.isAlive()); assertState(phaser, 0, THREADS + 1, 1);