--- jsr166/src/test/tck/CountDownLatchTest.java 2011/05/06 11:22:07 1.17 +++ jsr166/src/test/tck/CountDownLatchTest.java 2019/08/05 14:42:54 1.27 @@ -6,21 +6,23 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.concurrent.CountDownLatch; + +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 { @@ -54,82 +56,113 @@ public class CountDownLatchTest extends /** * await returns after countDown to zero, but not before */ - public void testAwait() throws InterruptedException { + public void testAwait() { final CountDownLatch l = new CountDownLatch(2); + final CountDownLatch pleaseCountDown = new CountDownLatch(1); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertTrue(l.getCount() > 0); + assertEquals(2, l.getCount()); + pleaseCountDown.countDown(); l.await(); assertEquals(0, l.getCount()); }}); - t.start(); - assertEquals(l.getCount(), 2); - delay(SHORT_DELAY_MS); + await(pleaseCountDown); + assertEquals(2, l.getCount()); l.countDown(); - assertEquals(l.getCount(), 1); + assertEquals(1, l.getCount()); + assertThreadBlocks(t, Thread.State.WAITING); l.countDown(); - assertEquals(l.getCount(), 0); - t.join(); + assertEquals(0, l.getCount()); + awaitTermination(t); } - /** * timed await returns after countDown to zero */ - public void testTimedAwait() throws InterruptedException { + public void testTimedAwait() { final CountDownLatch l = new CountDownLatch(2); + final CountDownLatch pleaseCountDown = new CountDownLatch(1); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertTrue(l.getCount() > 0); - assertTrue(l.await(SMALL_DELAY_MS, MILLISECONDS)); + assertEquals(2, l.getCount()); + pleaseCountDown.countDown(); + assertTrue(l.await(LONG_DELAY_MS, MILLISECONDS)); + assertEquals(0, l.getCount()); }}); - t.start(); - assertEquals(l.getCount(), 2); - delay(SHORT_DELAY_MS); + await(pleaseCountDown); + assertEquals(2, l.getCount()); l.countDown(); - assertEquals(l.getCount(), 1); + assertEquals(1, l.getCount()); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); l.countDown(); - assertEquals(l.getCount(), 0); - t.join(); + 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_InterruptedException() throws InterruptedException { + public void testAwait_Interruptible() { final CountDownLatch l = new CountDownLatch(1); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertTrue(l.getCount() > 0); - l.await(); + Thread.currentThread().interrupt(); + try { + l.await(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + l.await(); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertEquals(1, l.getCount()); }}); - t.start(); - assertEquals(l.getCount(), 1); + await(pleaseInterrupt); + assertThreadBlocks(t, Thread.State.WAITING); t.interrupt(); - t.join(); + awaitTermination(t); } /** - * timed await throws IE if interrupted before counted down + * timed await throws InterruptedException if interrupted before counted down */ - public void testTimedAwait_InterruptedException() throws InterruptedException { + public void testTimedAwait_Interruptible() { final CountDownLatch l = new CountDownLatch(1); - Thread t = new Thread(new CheckedInterruptedRunnable() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertTrue(l.getCount() > 0); - l.await(MEDIUM_DELAY_MS, MILLISECONDS); + Thread.currentThread().interrupt(); + try { + l.await(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + l.await(LONG_DELAY_MS, MILLISECONDS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + assertEquals(1, l.getCount()); }}); - t.start(); - delay(SHORT_DELAY_MS); - assertEquals(l.getCount(), 1); + await(pleaseInterrupt); + assertThreadBlocks(t, Thread.State.TIMED_WAITING); t.interrupt(); - t.join(); + awaitTermination(t); } /** @@ -137,16 +170,19 @@ public class CountDownLatchTest extends */ public void testAwaitTimeout() throws InterruptedException { final CountDownLatch l = new CountDownLatch(1); - Thread t = new Thread(new CheckedRunnable() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { - assertTrue(l.getCount() > 0); - assertFalse(l.await(SHORT_DELAY_MS, MILLISECONDS)); - assertTrue(l.getCount() > 0); + assertEquals(1, l.getCount()); + + long startTime = System.nanoTime(); + assertFalse(l.await(timeoutMillis(), MILLISECONDS)); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); + + assertEquals(1, l.getCount()); }}); - t.start(); - assertEquals(l.getCount(), 1); - t.join(); + awaitTermination(t); + assertEquals(1, l.getCount()); } /** @@ -154,14 +190,11 @@ public class CountDownLatchTest extends */ public void testToString() { CountDownLatch s = new CountDownLatch(2); - String us = s.toString(); - assertTrue(us.indexOf("Count = 2") >= 0); + assertTrue(s.toString().contains("Count = 2")); s.countDown(); - String s1 = s.toString(); - assertTrue(s1.indexOf("Count = 1") >= 0); + assertTrue(s.toString().contains("Count = 1")); s.countDown(); - String s2 = s.toString(); - assertTrue(s2.indexOf("Count = 0") >= 0); + assertTrue(s.toString().contains("Count = 0")); } }