--- jsr166/src/test/tck/TimeUnitTest.java 2009/11/02 20:28:32 1.10 +++ jsr166/src/test/tck/TimeUnitTest.java 2011/05/31 16:16:24 1.22 @@ -1,23 +1,22 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * 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) { - junit.textui.TestRunner.run(suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { - return new TestSuite(TimeUnitTest.class); + return new TestSuite(TimeUnitTest.class); } // (loops to 88888 check increments at all time divisions.) @@ -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 @@ -309,7 +306,6 @@ public class TimeUnitTest extends JSR166 assertEquals(Long.MIN_VALUE, TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, TimeUnit.DAYS)); - } /** @@ -317,166 +313,150 @@ public class TimeUnitTest extends JSR166 * and negative to LONG.MIN_VALUE */ public void testToNanosSaturate() { - assertEquals(Long.MAX_VALUE, - TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); - assertEquals(Long.MIN_VALUE, - TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); + assertEquals(Long.MAX_VALUE, + TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); + assertEquals(Long.MIN_VALUE, + 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()); } - /** - * Timed wait without holding lock throws - * IllegalMonitorStateException + * name returns name of unit */ - public void testTimedWait_IllegalMonitorException() { - //created a new thread with anonymous runnable - - Thread t = new Thread(new Runnable() { - public void run() { - Object o = new Object(); - TimeUnit tu = TimeUnit.MILLISECONDS; - try { - tu.timedWait(o,LONG_DELAY_MS); - threadShouldThrow(); - } - catch (InterruptedException ie) { - threadUnexpectedException(); - } - catch(IllegalMonitorStateException success) { - } - - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - unexpectedException(); - } + public void testName() { + assertEquals("SECONDS", TimeUnit.SECONDS.name()); } /** - * timedWait throws InterruptedException when interrupted + * Timed wait without holding lock throws + * IllegalMonitorStateException */ - public void testTimedWait() { - Thread t = new Thread(new Runnable() { - public void run() { - Object o = new Object(); - - TimeUnit tu = TimeUnit.MILLISECONDS; - try { - synchronized(o) { - tu.timedWait(o,MEDIUM_DELAY_MS); - } - threadShouldThrow(); - } - catch(InterruptedException success) {} - catch(IllegalMonitorStateException failure) { - threadUnexpectedException(); - } - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - unexpectedException(); - } - } - + 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); + threadShouldThrow(); + } catch (IllegalMonitorStateException success) {} + }}); - /** - * timedJoin throws InterruptedException when interrupted - */ - public void testTimedJoin() { - Thread t = new Thread(new Runnable() { - public void run() { - TimeUnit tu = TimeUnit.MILLISECONDS; - try { - Thread s = new Thread(new Runnable() { - public void run() { - try { - Thread.sleep(MEDIUM_DELAY_MS); - } catch(InterruptedException success){} - } - }); - s.start(); - tu.timedJoin(s,MEDIUM_DELAY_MS); - threadShouldThrow(); - } - catch(Exception e) {} - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - unexpectedException(); - } + awaitTermination(t); } /** - * timedSleep throws InterruptedException when interrupted + * timedWait throws InterruptedException when interrupted */ - public void testTimedSleep() { - //created a new thread with anonymous runnable - - Thread t = new Thread(new Runnable() { - public void run() { - TimeUnit tu = TimeUnit.MILLISECONDS; - try { - tu.sleep(MEDIUM_DELAY_MS); - threadShouldThrow(); - } - catch(InterruptedException success) {} - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - unexpectedException(); - } + 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; + + 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()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); } /** - * a deserialized serialized unit is equal + * timedJoin throws InterruptedException when interrupted */ - public void testSerialization() { - TimeUnit q = TimeUnit.MILLISECONDS; - - try { - 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(); - - assertEquals(q.toString(), r.toString()); - } catch(Exception e){ - e.printStackTrace(); - unexpectedException(); - } + public void testTimedJoin_Interruptible() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + final Thread s = newStartedThread(new CheckedInterruptedRunnable() { + public void realRun() throws InterruptedException { + Thread.sleep(LONG_DELAY_MS); + }}); + final Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + TimeUnit tu = TimeUnit.MILLISECONDS; + 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()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + s.interrupt(); + awaitTermination(s); + } + + /** + * timedSleep throws InterruptedException when interrupted + */ + public void testTimedSleep_Interruptible() { + final CountDownLatch pleaseInterrupt = new CountDownLatch(1); + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + TimeUnit tu = TimeUnit.MILLISECONDS; + 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()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + } + + /** + * a deserialized serialized unit is the same instance + */ + public void testSerialization() throws Exception { + TimeUnit x = TimeUnit.MILLISECONDS; + assertSame(x, serialClone(x)); } }