Home | History | Annotate | Download | only in jsr166
      1 /*
      2  * Written by Doug Lea with assistance from members of JCP JSR-166
      3  * Expert Group and released to the public domain, as explained at
      4  * http://creativecommons.org/publicdomain/zero/1.0/
      5  * Other contributors include Andrew Wright, Jeffrey Hayes,
      6  * Pat Fisher, Mike Judd.
      7  */
      8 
      9 package jsr166;
     10 
     11 import static java.util.concurrent.TimeUnit.DAYS;
     12 import static java.util.concurrent.TimeUnit.HOURS;
     13 import static java.util.concurrent.TimeUnit.MICROSECONDS;
     14 import static java.util.concurrent.TimeUnit.MILLISECONDS;
     15 import static java.util.concurrent.TimeUnit.MINUTES;
     16 import static java.util.concurrent.TimeUnit.NANOSECONDS;
     17 import static java.util.concurrent.TimeUnit.SECONDS;
     18 
     19 import java.util.concurrent.CountDownLatch;
     20 import java.util.concurrent.TimeUnit;
     21 
     22 import junit.framework.Test;
     23 import junit.framework.TestSuite;
     24 
     25 public class TimeUnitTest extends JSR166TestCase {
     26     // android-note: Removed because the CTS runner does a bad job of
     27     // retrying tests that have suite() declarations.
     28     //
     29     // public static void main(String[] args) {
     30     //     main(suite(), args);
     31     // }
     32     // public static Test suite() {
     33     //     return new TestSuite(TimeUnitTest.class);
     34     // }
     35 
     36     // (loops to 88888 check increments at all time divisions.)
     37 
     38     /**
     39      * convert correctly converts sample values across the units
     40      */
     41     public void testConvert() {
     42         for (long t = 0; t < 88888; ++t) {
     43             assertEquals(t*60*60*24,
     44                          SECONDS.convert(t, DAYS));
     45             assertEquals(t*60*60,
     46                          SECONDS.convert(t, HOURS));
     47             assertEquals(t*60,
     48                          SECONDS.convert(t, MINUTES));
     49             assertEquals(t,
     50                          SECONDS.convert(t, SECONDS));
     51             assertEquals(t,
     52                          SECONDS.convert(1000L*t, MILLISECONDS));
     53             assertEquals(t,
     54                          SECONDS.convert(1000000L*t, MICROSECONDS));
     55             assertEquals(t,
     56                          SECONDS.convert(1000000000L*t, NANOSECONDS));
     57 
     58             assertEquals(1000L*t*60*60*24,
     59                          MILLISECONDS.convert(t, DAYS));
     60             assertEquals(1000L*t*60*60,
     61                          MILLISECONDS.convert(t, HOURS));
     62             assertEquals(1000L*t*60,
     63                          MILLISECONDS.convert(t, MINUTES));
     64             assertEquals(1000L*t,
     65                          MILLISECONDS.convert(t, SECONDS));
     66             assertEquals(t,
     67                          MILLISECONDS.convert(t, MILLISECONDS));
     68             assertEquals(t,
     69                          MILLISECONDS.convert(1000L*t, MICROSECONDS));
     70             assertEquals(t,
     71                          MILLISECONDS.convert(1000000L*t, NANOSECONDS));
     72 
     73             assertEquals(1000000L*t*60*60*24,
     74                          MICROSECONDS.convert(t, DAYS));
     75             assertEquals(1000000L*t*60*60,
     76                          MICROSECONDS.convert(t, HOURS));
     77             assertEquals(1000000L*t*60,
     78                          MICROSECONDS.convert(t, MINUTES));
     79             assertEquals(1000000L*t,
     80                          MICROSECONDS.convert(t, SECONDS));
     81             assertEquals(1000L*t,
     82                          MICROSECONDS.convert(t, MILLISECONDS));
     83             assertEquals(t,
     84                          MICROSECONDS.convert(t, MICROSECONDS));
     85             assertEquals(t,
     86                          MICROSECONDS.convert(1000L*t, NANOSECONDS));
     87 
     88             assertEquals(1000000000L*t*60*60*24,
     89                          NANOSECONDS.convert(t, DAYS));
     90             assertEquals(1000000000L*t*60*60,
     91                          NANOSECONDS.convert(t, HOURS));
     92             assertEquals(1000000000L*t*60,
     93                          NANOSECONDS.convert(t, MINUTES));
     94             assertEquals(1000000000L*t,
     95                          NANOSECONDS.convert(t, SECONDS));
     96             assertEquals(1000000L*t,
     97                          NANOSECONDS.convert(t, MILLISECONDS));
     98             assertEquals(1000L*t,
     99                          NANOSECONDS.convert(t, MICROSECONDS));
    100             assertEquals(t,
    101                          NANOSECONDS.convert(t, NANOSECONDS));
    102         }
    103     }
    104 
    105     /**
    106      * toNanos correctly converts sample values in different units to
    107      * nanoseconds
    108      */
    109     public void testToNanos() {
    110         for (long t = 0; t < 88888; ++t) {
    111             assertEquals(t*1000000000L*60*60*24,
    112                          DAYS.toNanos(t));
    113             assertEquals(t*1000000000L*60*60,
    114                          HOURS.toNanos(t));
    115             assertEquals(t*1000000000L*60,
    116                          MINUTES.toNanos(t));
    117             assertEquals(1000000000L*t,
    118                          SECONDS.toNanos(t));
    119             assertEquals(1000000L*t,
    120                          MILLISECONDS.toNanos(t));
    121             assertEquals(1000L*t,
    122                          MICROSECONDS.toNanos(t));
    123             assertEquals(t,
    124                          NANOSECONDS.toNanos(t));
    125         }
    126     }
    127 
    128     /**
    129      * toMicros correctly converts sample values in different units to
    130      * microseconds
    131      */
    132     public void testToMicros() {
    133         for (long t = 0; t < 88888; ++t) {
    134             assertEquals(t*1000000L*60*60*24,
    135                          DAYS.toMicros(t));
    136             assertEquals(t*1000000L*60*60,
    137                          HOURS.toMicros(t));
    138             assertEquals(t*1000000L*60,
    139                          MINUTES.toMicros(t));
    140             assertEquals(1000000L*t,
    141                          SECONDS.toMicros(t));
    142             assertEquals(1000L*t,
    143                          MILLISECONDS.toMicros(t));
    144             assertEquals(t,
    145                          MICROSECONDS.toMicros(t));
    146             assertEquals(t,
    147                          NANOSECONDS.toMicros(t*1000L));
    148         }
    149     }
    150 
    151     /**
    152      * toMillis correctly converts sample values in different units to
    153      * milliseconds
    154      */
    155     public void testToMillis() {
    156         for (long t = 0; t < 88888; ++t) {
    157             assertEquals(t*1000L*60*60*24,
    158                          DAYS.toMillis(t));
    159             assertEquals(t*1000L*60*60,
    160                          HOURS.toMillis(t));
    161             assertEquals(t*1000L*60,
    162                          MINUTES.toMillis(t));
    163             assertEquals(1000L*t,
    164                          SECONDS.toMillis(t));
    165             assertEquals(t,
    166                          MILLISECONDS.toMillis(t));
    167             assertEquals(t,
    168                          MICROSECONDS.toMillis(t*1000L));
    169             assertEquals(t,
    170                          NANOSECONDS.toMillis(t*1000000L));
    171         }
    172     }
    173 
    174     /**
    175      * toSeconds correctly converts sample values in different units to
    176      * seconds
    177      */
    178     public void testToSeconds() {
    179         for (long t = 0; t < 88888; ++t) {
    180             assertEquals(t*60*60*24,
    181                          DAYS.toSeconds(t));
    182             assertEquals(t*60*60,
    183                          HOURS.toSeconds(t));
    184             assertEquals(t*60,
    185                          MINUTES.toSeconds(t));
    186             assertEquals(t,
    187                          SECONDS.toSeconds(t));
    188             assertEquals(t,
    189                          MILLISECONDS.toSeconds(t*1000L));
    190             assertEquals(t,
    191                          MICROSECONDS.toSeconds(t*1000000L));
    192             assertEquals(t,
    193                          NANOSECONDS.toSeconds(t*1000000000L));
    194         }
    195     }
    196 
    197     /**
    198      * toMinutes correctly converts sample values in different units to
    199      * minutes
    200      */
    201     public void testToMinutes() {
    202         for (long t = 0; t < 88888; ++t) {
    203             assertEquals(t*60*24,
    204                          DAYS.toMinutes(t));
    205             assertEquals(t*60,
    206                          HOURS.toMinutes(t));
    207             assertEquals(t,
    208                          MINUTES.toMinutes(t));
    209             assertEquals(t,
    210                          SECONDS.toMinutes(t*60));
    211             assertEquals(t,
    212                          MILLISECONDS.toMinutes(t*1000L*60));
    213             assertEquals(t,
    214                          MICROSECONDS.toMinutes(t*1000000L*60));
    215             assertEquals(t,
    216                          NANOSECONDS.toMinutes(t*1000000000L*60));
    217         }
    218     }
    219 
    220     /**
    221      * toHours correctly converts sample values in different units to
    222      * hours
    223      */
    224     public void testToHours() {
    225         for (long t = 0; t < 88888; ++t) {
    226             assertEquals(t*24,
    227                          DAYS.toHours(t));
    228             assertEquals(t,
    229                          HOURS.toHours(t));
    230             assertEquals(t,
    231                          MINUTES.toHours(t*60));
    232             assertEquals(t,
    233                          SECONDS.toHours(t*60*60));
    234             assertEquals(t,
    235                          MILLISECONDS.toHours(t*1000L*60*60));
    236             assertEquals(t,
    237                          MICROSECONDS.toHours(t*1000000L*60*60));
    238             assertEquals(t,
    239                          NANOSECONDS.toHours(t*1000000000L*60*60));
    240         }
    241     }
    242 
    243     /**
    244      * toDays correctly converts sample values in different units to
    245      * days
    246      */
    247     public void testToDays() {
    248         for (long t = 0; t < 88888; ++t) {
    249             assertEquals(t,
    250                          DAYS.toDays(t));
    251             assertEquals(t,
    252                          HOURS.toDays(t*24));
    253             assertEquals(t,
    254                          MINUTES.toDays(t*60*24));
    255             assertEquals(t,
    256                          SECONDS.toDays(t*60*60*24));
    257             assertEquals(t,
    258                          MILLISECONDS.toDays(t*1000L*60*60*24));
    259             assertEquals(t,
    260                          MICROSECONDS.toDays(t*1000000L*60*60*24));
    261             assertEquals(t,
    262                          NANOSECONDS.toDays(t*1000000000L*60*60*24));
    263         }
    264     }
    265 
    266     /**
    267      * convert saturates positive too-large values to Long.MAX_VALUE
    268      * and negative to LONG.MIN_VALUE
    269      */
    270     public void testConvertSaturate() {
    271         assertEquals(Long.MAX_VALUE,
    272                      NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS));
    273         assertEquals(Long.MIN_VALUE,
    274                      NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS));
    275         assertEquals(Long.MAX_VALUE,
    276                      NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES));
    277         assertEquals(Long.MIN_VALUE,
    278                      NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES));
    279         assertEquals(Long.MAX_VALUE,
    280                      NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS));
    281         assertEquals(Long.MIN_VALUE,
    282                      NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS));
    283         assertEquals(Long.MAX_VALUE,
    284                      NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS));
    285         assertEquals(Long.MIN_VALUE,
    286                      NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS));
    287     }
    288 
    289     /**
    290      * toNanos saturates positive too-large values to Long.MAX_VALUE
    291      * and negative to LONG.MIN_VALUE
    292      */
    293     public void testToNanosSaturate() {
    294         assertEquals(Long.MAX_VALUE,
    295                      MILLISECONDS.toNanos(Long.MAX_VALUE / 2));
    296         assertEquals(Long.MIN_VALUE,
    297                      MILLISECONDS.toNanos(-Long.MAX_VALUE / 3));
    298     }
    299 
    300     /**
    301      * toString returns name of unit
    302      */
    303     public void testToString() {
    304         assertEquals("SECONDS", SECONDS.toString());
    305     }
    306 
    307     /**
    308      * name returns name of unit
    309      */
    310     public void testName() {
    311         assertEquals("SECONDS", SECONDS.name());
    312     }
    313 
    314     /**
    315      * Timed wait without holding lock throws
    316      * IllegalMonitorStateException
    317      */
    318     public void testTimedWait_IllegalMonitorException() {
    319         Thread t = newStartedThread(new CheckedRunnable() {
    320             public void realRun() throws InterruptedException {
    321                 Object o = new Object();
    322                 TimeUnit tu = MILLISECONDS;
    323 
    324                 try {
    325                     tu.timedWait(o, LONG_DELAY_MS);
    326                     threadShouldThrow();
    327                 } catch (IllegalMonitorStateException success) {}
    328             }});
    329 
    330         awaitTermination(t);
    331     }
    332 
    333     /**
    334      * timedWait throws InterruptedException when interrupted
    335      */
    336     public void testTimedWait_Interruptible() {
    337         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    338         Thread t = newStartedThread(new CheckedRunnable() {
    339             public void realRun() throws InterruptedException {
    340                 Object o = new Object();
    341                 TimeUnit tu = MILLISECONDS;
    342 
    343                 Thread.currentThread().interrupt();
    344                 try {
    345                     synchronized (o) {
    346                         tu.timedWait(o, LONG_DELAY_MS);
    347                     }
    348                     shouldThrow();
    349                 } catch (InterruptedException success) {}
    350                 assertFalse(Thread.interrupted());
    351 
    352                 pleaseInterrupt.countDown();
    353                 try {
    354                     synchronized (o) {
    355                         tu.timedWait(o, LONG_DELAY_MS);
    356                     }
    357                     shouldThrow();
    358                 } catch (InterruptedException success) {}
    359                 assertFalse(Thread.interrupted());
    360             }});
    361 
    362         await(pleaseInterrupt);
    363         assertThreadStaysAlive(t);
    364         t.interrupt();
    365         awaitTermination(t);
    366     }
    367 
    368     /**
    369      * timedJoin throws InterruptedException when interrupted
    370      */
    371     public void testTimedJoin_Interruptible() {
    372         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    373         final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
    374             public void realRun() throws InterruptedException {
    375                 Thread.sleep(LONG_DELAY_MS);
    376             }});
    377         final Thread t = newStartedThread(new CheckedRunnable() {
    378             public void realRun() throws InterruptedException {
    379                 TimeUnit tu = MILLISECONDS;
    380                 Thread.currentThread().interrupt();
    381                 try {
    382                     tu.timedJoin(s, LONG_DELAY_MS);
    383                     shouldThrow();
    384                 } catch (InterruptedException success) {}
    385                 assertFalse(Thread.interrupted());
    386 
    387                 pleaseInterrupt.countDown();
    388                 try {
    389                     tu.timedJoin(s, LONG_DELAY_MS);
    390                     shouldThrow();
    391                 } catch (InterruptedException success) {}
    392                 assertFalse(Thread.interrupted());
    393             }});
    394 
    395         await(pleaseInterrupt);
    396         assertThreadStaysAlive(t);
    397         t.interrupt();
    398         awaitTermination(t);
    399         s.interrupt();
    400         awaitTermination(s);
    401     }
    402 
    403     /**
    404      * timedSleep throws InterruptedException when interrupted
    405      */
    406     public void testTimedSleep_Interruptible() {
    407         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
    408         Thread t = newStartedThread(new CheckedRunnable() {
    409             public void realRun() throws InterruptedException {
    410                 TimeUnit tu = MILLISECONDS;
    411                 Thread.currentThread().interrupt();
    412                 try {
    413                     tu.sleep(LONG_DELAY_MS);
    414                     shouldThrow();
    415                 } catch (InterruptedException success) {}
    416                 assertFalse(Thread.interrupted());
    417 
    418                 pleaseInterrupt.countDown();
    419                 try {
    420                     tu.sleep(LONG_DELAY_MS);
    421                     shouldThrow();
    422                 } catch (InterruptedException success) {}
    423                 assertFalse(Thread.interrupted());
    424             }});
    425 
    426         await(pleaseInterrupt);
    427         assertThreadStaysAlive(t);
    428         t.interrupt();
    429         awaitTermination(t);
    430     }
    431 
    432     /**
    433      * a deserialized serialized unit is the same instance
    434      */
    435     public void testSerialization() throws Exception {
    436         for (TimeUnit x : TimeUnit.values())
    437             assertSame(x, serialClone(x));
    438     }
    439 
    440 }
    441