--- jsr166/src/test/tck/CountDownLatchTest.java 2011/05/31 16:16:23 1.21 +++ jsr166/src/test/tck/CountDownLatchTest.java 2019/09/05 21:33:55 1.29 @@ -6,21 +6,24 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.CountDownLatch; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ThreadLocalRandom; + +import junit.framework.Test; +import junit.framework.TestSuite; + public class CountDownLatchTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(CountDownLatchTest.class); } /** - * negative constructor argument throws IAE + * negative constructor argument throws IllegalArgumentException */ public void testConstructor() { try { @@ -70,7 +73,7 @@ public class CountDownLatchTest extends assertEquals(2, l.getCount()); l.countDown(); assertEquals(1, l.getCount()); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); l.countDown(); assertEquals(0, l.getCount()); awaitTermination(t); @@ -95,14 +98,14 @@ public class CountDownLatchTest extends assertEquals(2, l.getCount()); l.countDown(); assertEquals(1, l.getCount()); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); l.countDown(); assertEquals(0, l.getCount()); awaitTermination(t); } /** - * await throws IE if interrupted before counted down + * await throws InterruptedException if interrupted before counted down */ public void testAwait_Interruptible() { final CountDownLatch l = new CountDownLatch(1); @@ -127,38 +130,39 @@ public class CountDownLatchTest extends }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); awaitTermination(t); } /** - * timed await throws IE if interrupted before counted down + * timed await throws InterruptedException if interrupted before counted down */ public void testTimedAwait_Interruptible() { - final CountDownLatch l = new CountDownLatch(1); + final int initialCount = ThreadLocalRandom.current().nextInt(1, 3); + final CountDownLatch l = new CountDownLatch(initialCount); final CountDownLatch pleaseInterrupt = new CountDownLatch(1); Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { Thread.currentThread().interrupt(); try { - l.await(LONG_DELAY_MS, MILLISECONDS); + l.await(randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); pleaseInterrupt.countDown(); try { - l.await(LONG_DELAY_MS, MILLISECONDS); + l.await(LONGER_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (InterruptedException success) {} assertFalse(Thread.interrupted()); - assertEquals(1, l.getCount()); + assertEquals(initialCount, l.getCount()); }}); await(pleaseInterrupt); - assertThreadStaysAlive(t); + if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); awaitTermination(t); } @@ -171,7 +175,11 @@ public class CountDownLatchTest extends Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { assertEquals(1, l.getCount()); + + long startTime = System.nanoTime(); assertFalse(l.await(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + assertEquals(1, l.getCount()); }});