--- jsr166/src/test/tck/TimeUnitTest.java 2011/03/15 19:47:07 1.19 +++ jsr166/src/test/tck/TimeUnitTest.java 2011/05/31 16:16:24 1.22 @@ -6,10 +6,9 @@ * Pat Fisher, Mike Judd. */ - import junit.framework.*; -import java.util.concurrent.*; -import java.io.*; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; public class TimeUnitTest extends JSR166TestCase { public static void main(String[] args) { @@ -49,7 +48,6 @@ public class TimeUnitTest extends JSR166 TimeUnit.SECONDS.convert(1000000000L*t, TimeUnit.NANOSECONDS)); - assertEquals(1000L*t*60*60*24, TimeUnit.MILLISECONDS.convert(t, TimeUnit.DAYS)); @@ -279,7 +277,6 @@ public class TimeUnitTest extends JSR166 } } - /** * convert saturates positive too-large values to Long.MAX_VALUE * and negative to LONG.MIN_VALUE @@ -322,109 +319,144 @@ public class TimeUnitTest extends JSR166 TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); } - /** - * toString returns string containing common name of unit + * toString returns name of unit */ public void testToString() { - String s = TimeUnit.SECONDS.toString(); - assertTrue(s.indexOf("ECOND") >= 0); + assertEquals("SECONDS", TimeUnit.SECONDS.toString()); } + /** + * name returns name of unit + */ + public void testName() { + assertEquals("SECONDS", TimeUnit.SECONDS.name()); + } /** * Timed wait without holding lock throws * IllegalMonitorStateException */ - public void testTimedWait_IllegalMonitorException() throws Exception { - Thread t = new Thread(new CheckedRunnable() { + public void testTimedWait_IllegalMonitorException() { + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { Object o = new Object(); TimeUnit tu = TimeUnit.MILLISECONDS; + try { - tu.timedWait(o,LONG_DELAY_MS); + tu.timedWait(o, LONG_DELAY_MS); threadShouldThrow(); - } catch (IllegalMonitorStateException success) {}}}); + } catch (IllegalMonitorStateException success) {} + }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); + awaitTermination(t); } /** * timedWait throws InterruptedException when interrupted */ - public void testTimedWait() throws InterruptedException { - Thread t = new Thread(new CheckedInterruptedRunnable() { + public void testTimedWait_Interruptible() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { Object o = new Object(); - TimeUnit tu = TimeUnit.MILLISECONDS; - synchronized (o) { - tu.timedWait(o,MEDIUM_DELAY_MS); - } + + Thread.currentThread().interrupt(); + try { + synchronized (o) { + tu.timedWait(o, LONG_DELAY_MS); + } + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + synchronized (o) { + tu.timedWait(o, LONG_DELAY_MS); + } + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } - /** * timedJoin throws InterruptedException when interrupted */ - public void testTimedJoin() throws InterruptedException { - final Thread s = new Thread(new CheckedInterruptedRunnable() { + public void testTimedJoin_Interruptible() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + final Thread s = newStartedThread(new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { - Thread.sleep(MEDIUM_DELAY_MS); + Thread.sleep(LONG_DELAY_MS); }}); - final Thread t = new Thread(new CheckedInterruptedRunnable() { + final Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { TimeUnit tu = TimeUnit.MILLISECONDS; - tu.timedJoin(s, MEDIUM_DELAY_MS); + Thread.currentThread().interrupt(); + try { + tu.timedJoin(s, LONG_DELAY_MS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + tu.timedJoin(s, LONG_DELAY_MS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - s.start(); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); s.interrupt(); - s.join(); + awaitTermination(s); } /** * timedSleep throws InterruptedException when interrupted */ - public void testTimedSleep() throws InterruptedException { - Thread t = new Thread(new CheckedInterruptedRunnable() { + public void testTimedSleep_Interruptible() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { public void realRun() throws InterruptedException { TimeUnit tu = TimeUnit.MILLISECONDS; - tu.sleep(MEDIUM_DELAY_MS); + Thread.currentThread().interrupt(); + try { + tu.sleep(LONG_DELAY_MS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + tu.sleep(LONG_DELAY_MS); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); }}); - t.start(); - Thread.sleep(SHORT_DELAY_MS); + await(pleaseInterrupt); + assertThreadStaysAlive(t); t.interrupt(); - t.join(); + awaitTermination(t); } /** * a deserialized serialized unit is the same instance */ public void testSerialization() throws Exception { - TimeUnit q = TimeUnit.MILLISECONDS; - - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - TimeUnit r = (TimeUnit)in.readObject(); - assertSame(q, r); + TimeUnit x = TimeUnit.MILLISECONDS; + assertSame(x, serialClone(x)); } }