--- jsr166/src/test/tck/TimeUnitTest.java 2003/09/14 20:42:41 1.3 +++ jsr166/src/test/tck/TimeUnitTest.java 2016/03/18 16:55:09 1.28 @@ -1,304 +1,639 @@ /* - * Written by members of JCP JSR-166 Expert Group and released to the - * public domain. Use, modify, and redistribute this code in any way - * without acknowledgement. Other contributors include Andrew Wright, - * Jeffrey Hayes, Pat Fischer, Mike Judd. + * 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/publicdomain/zero/1.0/ + * Other contributors include Andrew Wright, Jeffrey Hayes, + * Pat Fisher, Mike Judd. */ +import static java.util.concurrent.TimeUnit.DAYS; +import static java.util.concurrent.TimeUnit.HOURS; +import static java.util.concurrent.TimeUnit.MICROSECONDS; +import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.MINUTES; +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.time.temporal.ChronoUnit; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; -import junit.framework.*; -import java.util.concurrent.*; -import java.io.*; +import junit.framework.Test; +import junit.framework.TestSuite; public class TimeUnitTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } - + public static Test suite() { - return new TestSuite(TimeUnitTest.class); + return new TestSuite(TimeUnitTest.class); } + // (loops to 88888 check increments at all time divisions.) + + /** + * convert correctly converts sample values across the units + */ public void testConvert() { - for (long t = 0; t < 10; ++t) { - assertEquals(t, - TimeUnit.SECONDS.convert(t, - TimeUnit.SECONDS)); - assertEquals(t, - TimeUnit.SECONDS.convert(1000 * t, - TimeUnit.MILLISECONDS)); - assertEquals(t, - TimeUnit.SECONDS.convert(1000000 * t, - TimeUnit.MICROSECONDS)); - assertEquals(t, - TimeUnit.SECONDS.convert(1000000000 * t, - TimeUnit.NANOSECONDS)); - assertEquals(1000 * t, - TimeUnit.MILLISECONDS.convert(t, - TimeUnit.SECONDS)); - assertEquals(t, - TimeUnit.MILLISECONDS.convert(t, - TimeUnit.MILLISECONDS)); - assertEquals(t, - TimeUnit.MILLISECONDS.convert(1000 * t, - TimeUnit.MICROSECONDS)); - assertEquals(t, - TimeUnit.MILLISECONDS.convert(1000000 * t, - TimeUnit.NANOSECONDS)); - assertEquals(1000000 * t, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.SECONDS)); - assertEquals(1000 * t, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.MILLISECONDS)); - assertEquals(t, - TimeUnit.MICROSECONDS.convert(t, - TimeUnit.MICROSECONDS)); - assertEquals(t, - TimeUnit.MICROSECONDS.convert(1000 * t, - TimeUnit.NANOSECONDS)); - assertEquals(1000000000 * t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.SECONDS)); - assertEquals(1000000 * t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.MILLISECONDS)); - assertEquals(1000 * t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.MICROSECONDS)); - assertEquals(t, - TimeUnit.NANOSECONDS.convert(t, - TimeUnit.NANOSECONDS)); + for (long t = 0; t < 88888; ++t) { + assertEquals(t*60*60*24, + SECONDS.convert(t, DAYS)); + assertEquals(t*60*60, + SECONDS.convert(t, HOURS)); + assertEquals(t*60, + SECONDS.convert(t, MINUTES)); + assertEquals(t, + SECONDS.convert(t, SECONDS)); + assertEquals(t, + SECONDS.convert(1000L*t, MILLISECONDS)); + assertEquals(t, + SECONDS.convert(1000000L*t, MICROSECONDS)); + assertEquals(t, + SECONDS.convert(1000000000L*t, NANOSECONDS)); + + assertEquals(1000L*t*60*60*24, + MILLISECONDS.convert(t, DAYS)); + assertEquals(1000L*t*60*60, + MILLISECONDS.convert(t, HOURS)); + assertEquals(1000L*t*60, + MILLISECONDS.convert(t, MINUTES)); + assertEquals(1000L*t, + MILLISECONDS.convert(t, SECONDS)); + assertEquals(t, + MILLISECONDS.convert(t, MILLISECONDS)); + assertEquals(t, + MILLISECONDS.convert(1000L*t, MICROSECONDS)); + assertEquals(t, + MILLISECONDS.convert(1000000L*t, NANOSECONDS)); + + assertEquals(1000000L*t*60*60*24, + MICROSECONDS.convert(t, DAYS)); + assertEquals(1000000L*t*60*60, + MICROSECONDS.convert(t, HOURS)); + assertEquals(1000000L*t*60, + MICROSECONDS.convert(t, MINUTES)); + assertEquals(1000000L*t, + MICROSECONDS.convert(t, SECONDS)); + assertEquals(1000L*t, + MICROSECONDS.convert(t, MILLISECONDS)); + assertEquals(t, + MICROSECONDS.convert(t, MICROSECONDS)); + assertEquals(t, + MICROSECONDS.convert(1000L*t, NANOSECONDS)); + + assertEquals(1000000000L*t*60*60*24, + NANOSECONDS.convert(t, DAYS)); + assertEquals(1000000000L*t*60*60, + NANOSECONDS.convert(t, HOURS)); + assertEquals(1000000000L*t*60, + NANOSECONDS.convert(t, MINUTES)); + assertEquals(1000000000L*t, + NANOSECONDS.convert(t, SECONDS)); + assertEquals(1000000L*t, + NANOSECONDS.convert(t, MILLISECONDS)); + assertEquals(1000L*t, + NANOSECONDS.convert(t, MICROSECONDS)); + assertEquals(t, + NANOSECONDS.convert(t, NANOSECONDS)); + } + + for (TimeUnit x : TimeUnit.values()) { + long[] zs = { + 0, 1, -1, + Integer.MAX_VALUE, Integer.MIN_VALUE, + Long.MAX_VALUE, Long.MIN_VALUE, + }; + for (long z : zs) assertEquals(z, x.convert(z, x)); } } + /** + * toNanos correctly converts sample values in different units to + * nanoseconds + */ public void testToNanos() { - for (long t = 0; t < 10; ++t) { - assertEquals(1000000000 * t, - TimeUnit.SECONDS.toNanos(t)); - - assertEquals(1000000 * t, - TimeUnit.MILLISECONDS.toNanos(t)); - assertEquals(1000 * t, - TimeUnit.MICROSECONDS.toNanos(t)); - assertEquals(t, - TimeUnit.NANOSECONDS.toNanos(t)); + for (long t = 0; t < 88888; ++t) { + assertEquals(t*1000000000L*60*60*24, + DAYS.toNanos(t)); + assertEquals(t*1000000000L*60*60, + HOURS.toNanos(t)); + assertEquals(t*1000000000L*60, + MINUTES.toNanos(t)); + assertEquals(1000000000L*t, + SECONDS.toNanos(t)); + assertEquals(1000000L*t, + MILLISECONDS.toNanos(t)); + assertEquals(1000L*t, + MICROSECONDS.toNanos(t)); + assertEquals(t, + NANOSECONDS.toNanos(t)); } } + /** + * toMicros correctly converts sample values in different units to + * microseconds + */ public void testToMicros() { - for (long t = 0; t < 10; ++t) { - assertEquals(1000000 * t, - TimeUnit.SECONDS.toMicros(t)); - - assertEquals(1000 * t, - TimeUnit.MILLISECONDS.toMicros(t)); - assertEquals(t, - TimeUnit.MICROSECONDS.toMicros(t)); - assertEquals(t, - TimeUnit.NANOSECONDS.toMicros(t * 1000)); + for (long t = 0; t < 88888; ++t) { + assertEquals(t*1000000L*60*60*24, + DAYS.toMicros(t)); + assertEquals(t*1000000L*60*60, + HOURS.toMicros(t)); + assertEquals(t*1000000L*60, + MINUTES.toMicros(t)); + assertEquals(1000000L*t, + SECONDS.toMicros(t)); + assertEquals(1000L*t, + MILLISECONDS.toMicros(t)); + assertEquals(t, + MICROSECONDS.toMicros(t)); + assertEquals(t, + NANOSECONDS.toMicros(t*1000L)); } } + /** + * toMillis correctly converts sample values in different units to + * milliseconds + */ public void testToMillis() { - for (long t = 0; t < 10; ++t) { - assertEquals(1000 * t, - TimeUnit.SECONDS.toMillis(t)); - - assertEquals(t, - TimeUnit.MILLISECONDS.toMillis(t)); - assertEquals(t, - TimeUnit.MICROSECONDS.toMillis(t * 1000)); - assertEquals(t, - TimeUnit.NANOSECONDS.toMillis(t * 1000000)); + for (long t = 0; t < 88888; ++t) { + assertEquals(t*1000L*60*60*24, + DAYS.toMillis(t)); + assertEquals(t*1000L*60*60, + HOURS.toMillis(t)); + assertEquals(t*1000L*60, + MINUTES.toMillis(t)); + assertEquals(1000L*t, + SECONDS.toMillis(t)); + assertEquals(t, + MILLISECONDS.toMillis(t)); + assertEquals(t, + MICROSECONDS.toMillis(t*1000L)); + assertEquals(t, + NANOSECONDS.toMillis(t*1000000L)); } } + /** + * toSeconds correctly converts sample values in different units to + * seconds + */ public void testToSeconds() { - for (long t = 0; t < 10; ++t) { - assertEquals(t, - TimeUnit.SECONDS.toSeconds(t)); - - assertEquals(t, - TimeUnit.MILLISECONDS.toSeconds(t * 1000)); - assertEquals(t, - TimeUnit.MICROSECONDS.toSeconds(t * 1000000)); - assertEquals(t, - TimeUnit.NANOSECONDS.toSeconds(t * 1000000000)); + for (long t = 0; t < 88888; ++t) { + assertEquals(t*60*60*24, + DAYS.toSeconds(t)); + assertEquals(t*60*60, + HOURS.toSeconds(t)); + assertEquals(t*60, + MINUTES.toSeconds(t)); + assertEquals(t, + SECONDS.toSeconds(t)); + assertEquals(t, + MILLISECONDS.toSeconds(t*1000L)); + assertEquals(t, + MICROSECONDS.toSeconds(t*1000000L)); + assertEquals(t, + NANOSECONDS.toSeconds(t*1000000000L)); } } + /** + * toMinutes correctly converts sample values in different units to + * minutes + */ + public void testToMinutes() { + for (long t = 0; t < 88888; ++t) { + assertEquals(t*60*24, + DAYS.toMinutes(t)); + assertEquals(t*60, + HOURS.toMinutes(t)); + assertEquals(t, + MINUTES.toMinutes(t)); + assertEquals(t, + SECONDS.toMinutes(t*60)); + assertEquals(t, + MILLISECONDS.toMinutes(t*1000L*60)); + assertEquals(t, + MICROSECONDS.toMinutes(t*1000000L*60)); + assertEquals(t, + NANOSECONDS.toMinutes(t*1000000000L*60)); + } + } + /** + * toHours correctly converts sample values in different units to + * hours + */ + public void testToHours() { + for (long t = 0; t < 88888; ++t) { + assertEquals(t*24, + DAYS.toHours(t)); + assertEquals(t, + HOURS.toHours(t)); + assertEquals(t, + MINUTES.toHours(t*60)); + assertEquals(t, + SECONDS.toHours(t*60*60)); + assertEquals(t, + MILLISECONDS.toHours(t*1000L*60*60)); + assertEquals(t, + MICROSECONDS.toHours(t*1000000L*60*60)); + assertEquals(t, + NANOSECONDS.toHours(t*1000000000L*60*60)); + } + } + + /** + * toDays correctly converts sample values in different units to + * days + */ + public void testToDays() { + for (long t = 0; t < 88888; ++t) { + assertEquals(t, + DAYS.toDays(t)); + assertEquals(t, + HOURS.toDays(t*24)); + assertEquals(t, + MINUTES.toDays(t*60*24)); + assertEquals(t, + SECONDS.toDays(t*60*60*24)); + assertEquals(t, + MILLISECONDS.toDays(t*1000L*60*60*24)); + assertEquals(t, + MICROSECONDS.toDays(t*1000000L*60*60*24)); + assertEquals(t, + NANOSECONDS.toDays(t*1000000000L*60*60*24)); + } + } + + /** + * convert saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ public void testConvertSaturate() { assertEquals(Long.MAX_VALUE, - TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, - TimeUnit.SECONDS)); + NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS)); assertEquals(Long.MIN_VALUE, - TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, - TimeUnit.SECONDS)); + NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS)); + assertEquals(Long.MAX_VALUE, + NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES)); + assertEquals(Long.MIN_VALUE, + NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES)); + assertEquals(Long.MAX_VALUE, + NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS)); + assertEquals(Long.MIN_VALUE, + NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS)); + assertEquals(Long.MAX_VALUE, + NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); + assertEquals(Long.MIN_VALUE, + NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); + for (TimeUnit x : TimeUnit.values()) + for (TimeUnit y : TimeUnit.values()) { + long ratio = x.toNanos(1) / y.toNanos(1); + if (ratio > 1) { + assertEquals(ratio, y.convert(1, x)); + assertEquals(1, x.convert(ratio, y)); + long max = Long.MAX_VALUE/ratio; + assertEquals(max * ratio, y.convert(max, x)); + assertEquals(-max * ratio, y.convert(-max, x)); + assertEquals(max, x.convert(max * ratio, y)); + assertEquals(-max, x.convert(-max * ratio, y)); + assertEquals(Long.MAX_VALUE, y.convert(max + 1, x)); + assertEquals(Long.MIN_VALUE, y.convert(-max - 1, x)); + assertEquals(Long.MAX_VALUE, y.convert(Long.MAX_VALUE, x)); + assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE, x)); + assertEquals(Long.MIN_VALUE, y.convert(Long.MIN_VALUE + 1, x)); + } + } } + /** + * toNanos saturates positive too-large values to Long.MAX_VALUE + * 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, + MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); + assertEquals(Long.MIN_VALUE, + MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); + + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / NANOSECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toNanos(z)); + assertEquals(Long.MAX_VALUE, x.toNanos(max + 1)); + assertEquals(Long.MIN_VALUE, x.toNanos(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toNanos(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toNanos(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toNanos(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toNanos(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toMicros saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToMicrosSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / MICROSECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toMicros(z)); + assertEquals(Long.MAX_VALUE, x.toMicros(max + 1)); + assertEquals(Long.MIN_VALUE, x.toMicros(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toMicros(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMicros(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toMicros(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMicros(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toMillis saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToMillisSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / MILLISECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toMillis(z)); + assertEquals(Long.MAX_VALUE, x.toMillis(max + 1)); + assertEquals(Long.MIN_VALUE, x.toMillis(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toMillis(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMillis(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toMillis(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMillis(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toSeconds saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToSecondsSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / SECONDS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toSeconds(z)); + assertEquals(Long.MAX_VALUE, x.toSeconds(max + 1)); + assertEquals(Long.MIN_VALUE, x.toSeconds(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toSeconds(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toSeconds(Long.MIN_VALUE + 1)); + if (max < Integer.MAX_VALUE) { + assertEquals(Long.MAX_VALUE, x.toSeconds(Integer.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toSeconds(Integer.MIN_VALUE)); + } + } + } + } + + /** + * toMinutes saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToMinutesSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / MINUTES.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toMinutes(z)); + assertEquals(Long.MAX_VALUE, x.toMinutes(max + 1)); + assertEquals(Long.MIN_VALUE, x.toMinutes(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toMinutes(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toMinutes(Long.MIN_VALUE + 1)); + } + } } + /** + * toHours saturates positive too-large values to Long.MAX_VALUE + * and negative to LONG.MIN_VALUE + */ + public void testToHoursSaturate() { + for (TimeUnit x : TimeUnit.values()) { + long ratio = x.toNanos(1) / HOURS.toNanos(1); + if (ratio > 1) { + long max = Long.MAX_VALUE/ratio; + for (long z : new long[] {0, 1, -1, max, -max}) + assertEquals(z * ratio, x.toHours(z)); + assertEquals(Long.MAX_VALUE, x.toHours(max + 1)); + assertEquals(Long.MIN_VALUE, x.toHours(-max - 1)); + assertEquals(Long.MAX_VALUE, x.toHours(Long.MAX_VALUE)); + assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE)); + assertEquals(Long.MIN_VALUE, x.toHours(Long.MIN_VALUE + 1)); + } + } + } + /** + * toString returns name of unit + */ public void testToString() { - String s = TimeUnit.SECONDS.toString(); - assertTrue(s.indexOf("econd") >= 0); + assertEquals("SECONDS", SECONDS.toString()); + } + + /** + * name returns name of unit + */ + public void testName() { + assertEquals("SECONDS", SECONDS.name()); } - /** - * Timed wait without holding lock throws - * IllegalMonitorStateException + * Timed wait without holding lock throws + * IllegalMonitorStateException */ public void testTimedWait_IllegalMonitorException() { - //created a new thread with anonymous runnable + Thread t = newStartedThread(new CheckedRunnable() { + public void realRun() throws InterruptedException { + Object o = new Object(); + TimeUnit tu = MILLISECONDS; + + try { + tu.timedWait(o, LONG_DELAY_MS); + threadShouldThrow(); + } catch (IllegalMonitorStateException success) {} + }}); - Thread t = new Thread(new Runnable() { - public void run() { - Object o = new Object(); - TimeUnit tu = TimeUnit.MILLISECONDS; - try { - tu.timedWait(o,LONG_DELAY_MS); - fail("should throw"); + awaitTermination(t); + } + + /** + * timedWait throws InterruptedException when interrupted + */ + 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 = MILLISECONDS; + + Thread.currentThread().interrupt(); + try { + synchronized (o) { + tu.timedWait(o, LONG_DELAY_MS); } - catch (InterruptedException ie) { - fail("should not throw IE here"); - } - catch(IllegalMonitorStateException success) { + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + + pleaseInterrupt.countDown(); + try { + synchronized (o) { + tu.timedWait(o, LONG_DELAY_MS); } - - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - fail("Unexpected exception"); - } - } - - /** - * timedWait will throw InterruptedException. - * Thread t waits on timedWait while the main thread interrupts it. - * Note: This does not throw IllegalMonitorException since timeWait - * is synchronized on o - */ - 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); - } - fail("should throw"); - } - catch(InterruptedException success) {} - catch(IllegalMonitorStateException failure) { - fail("should not throw"); - } - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - fail("Unexpected exception"); - } - } - - - /** - * timedJoin will throw InterruptedException. - * Thread t waits on timedJoin while the main thread interrupts it. - */ - 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); - fail("should throw"); - } - catch(Exception e) {} - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - fail("Unexpected exception"); - } - } - - /** - * timedSleep will throw InterruptedException. - * Thread t waits on timedSleep while the main thread interrupts it. - */ - 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); - fail("should throw"); - } - catch(InterruptedException success) {} - } - }); - t.start(); - try { - Thread.sleep(SHORT_DELAY_MS); - t.interrupt(); - t.join(); - } catch(Exception e) { - fail("Unexpected exception"); - } - } - - 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(); - fail("unexpected exception"); + shouldThrow(); + } catch (InterruptedException success) {} + assertFalse(Thread.interrupted()); + }}); + + await(pleaseInterrupt); + assertThreadStaysAlive(t); + t.interrupt(); + awaitTermination(t); + } + + /** + * timedJoin throws InterruptedException when interrupted + */ + 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 = 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 = 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 { + for (TimeUnit x : TimeUnit.values()) + assertSame(x, serialClone(x)); + } + + /** + * tests for toChronoUnit. + */ + public void testToChronoUnit() throws Exception { + assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); + assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); + assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); + assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); + assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); + assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); + assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); + + // Every TimeUnit has a defined ChronoUnit equivalent + for (TimeUnit x : TimeUnit.values()) + assertSame(x, TimeUnit.of(x.toChronoUnit())); + } + + /** + * tests for TimeUnit.of(ChronoUnit). + */ + public void testTimeUnitOf() throws Exception { + assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); + assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); + assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); + assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); + assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); + assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); + assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); + + assertThrows(NullPointerException.class, + () -> TimeUnit.of((ChronoUnit)null)); + + // ChronoUnits either round trip to their TimeUnit + // equivalents, or throw IllegalArgumentException. + for (ChronoUnit cu : ChronoUnit.values()) { + final TimeUnit tu; + try { + tu = TimeUnit.of(cu); + } catch (IllegalArgumentException acceptable) { + continue; + } + assertSame(cu, tu.toChronoUnit()); } }