test/jdk/java/util/concurrent/tck/TimeUnitTest.java
changeset 58138 1e4270f875ee
parent 47216 71c04702a3d5
equal deleted inserted replaced
58137:6a556bcd94fc 58138:1e4270f875ee
    54 
    54 
    55     public static Test suite() {
    55     public static Test suite() {
    56         return new TestSuite(TimeUnitTest.class);
    56         return new TestSuite(TimeUnitTest.class);
    57     }
    57     }
    58 
    58 
    59     // (loops to 88888 check increments at all time divisions.)
    59     void testConversion(TimeUnit x, TimeUnit y, long n, long expected) {
    60 
    60         assertEquals(expected, x.convert(n, y));
    61     /**
    61         switch (x) {
    62      * convert correctly converts sample values across the units
    62         case NANOSECONDS:  assertEquals(expected, y.toNanos(n));   break;
    63      */
    63         case MICROSECONDS: assertEquals(expected, y.toMicros(n));  break;
    64     public void testConvert() {
    64         case MILLISECONDS: assertEquals(expected, y.toMillis(n));  break;
    65         for (long t = 0; t < 88888; ++t) {
    65         case SECONDS:      assertEquals(expected, y.toSeconds(n)); break;
    66             assertEquals(t*60*60*24,
    66         case MINUTES:      assertEquals(expected, y.toMinutes(n)); break;
    67                          SECONDS.convert(t, DAYS));
    67         case HOURS:        assertEquals(expected, y.toHours(n));   break;
    68             assertEquals(t*60*60,
    68         case DAYS:         assertEquals(expected, y.toDays(n));    break;
    69                          SECONDS.convert(t, HOURS));
    69         default: throw new AssertionError();
    70             assertEquals(t*60,
    70         }
    71                          SECONDS.convert(t, MINUTES));
    71 
    72             assertEquals(t,
    72         if (n > 0) testConversion(x, y, -n, -expected);
    73                          SECONDS.convert(t, SECONDS));
    73     }
    74             assertEquals(t,
    74 
    75                          SECONDS.convert(1000L*t, MILLISECONDS));
    75     void testConversion(TimeUnit x, TimeUnit y) {
    76             assertEquals(t,
    76         long ratio = x.toNanos(1)/y.toNanos(1);
    77                          SECONDS.convert(1000000L*t, MICROSECONDS));
    77         assertTrue(ratio > 0);
    78             assertEquals(t,
    78         long[] ns = { 0, 1, 2, Long.MAX_VALUE/ratio, Long.MIN_VALUE/ratio };
    79                          SECONDS.convert(1000000000L*t, NANOSECONDS));
    79         for (long n : ns) {
    80 
    80             testConversion(y, x, n, n * ratio);
    81             assertEquals(1000L*t*60*60*24,
    81             long[] ks = { n * ratio, n * ratio + 1, n * ratio - 1 };
    82                          MILLISECONDS.convert(t, DAYS));
    82             for (long k : ks) {
    83             assertEquals(1000L*t*60*60,
    83                 testConversion(x, y, k, k / ratio);
    84                          MILLISECONDS.convert(t, HOURS));
    84             }
    85             assertEquals(1000L*t*60,
    85         }
    86                          MILLISECONDS.convert(t, MINUTES));
    86     }
    87             assertEquals(1000L*t,
    87 
    88                          MILLISECONDS.convert(t, SECONDS));
    88     /**
    89             assertEquals(t,
    89      * Conversion methods correctly convert sample values
    90                          MILLISECONDS.convert(t, MILLISECONDS));
    90      */
    91             assertEquals(t,
    91     public void testConversions() {
    92                          MILLISECONDS.convert(1000L*t, MICROSECONDS));
    92         // Sanity check
    93             assertEquals(t,
    93         assertEquals(1, NANOSECONDS.toNanos(1));
    94                          MILLISECONDS.convert(1000000L*t, NANOSECONDS));
    94         assertEquals(1000L * NANOSECONDS.toNanos(1), MICROSECONDS.toNanos(1));
    95 
    95         assertEquals(1000L * MICROSECONDS.toNanos(1), MILLISECONDS.toNanos(1));
    96             assertEquals(1000000L*t*60*60*24,
    96         assertEquals(1000L * MILLISECONDS.toNanos(1), SECONDS.toNanos(1));
    97                          MICROSECONDS.convert(t, DAYS));
    97         assertEquals(60L * SECONDS.toNanos(1), MINUTES.toNanos(1));
    98             assertEquals(1000000L*t*60*60,
    98         assertEquals(60L * MINUTES.toNanos(1), HOURS.toNanos(1));
    99                          MICROSECONDS.convert(t, HOURS));
    99         assertEquals(24L * HOURS.toNanos(1), DAYS.toNanos(1));
   100             assertEquals(1000000L*t*60,
   100 
   101                          MICROSECONDS.convert(t, MINUTES));
   101         for (TimeUnit x : TimeUnit.values()) {
   102             assertEquals(1000000L*t,
   102             assertEquals(x.toNanos(1), NANOSECONDS.convert(1, x));
   103                          MICROSECONDS.convert(t, SECONDS));
   103         }
   104             assertEquals(1000L*t,
   104 
   105                          MICROSECONDS.convert(t, MILLISECONDS));
   105         for (TimeUnit x : TimeUnit.values())
   106             assertEquals(t,
   106             for (TimeUnit y : TimeUnit.values())
   107                          MICROSECONDS.convert(t, MICROSECONDS));
   107                 if (x.toNanos(1) >= y.toNanos(1))
   108             assertEquals(t,
   108                     testConversion(x, y);
   109                          MICROSECONDS.convert(1000L*t, NANOSECONDS));
       
   110 
       
   111             assertEquals(1000000000L*t*60*60*24,
       
   112                          NANOSECONDS.convert(t, DAYS));
       
   113             assertEquals(1000000000L*t*60*60,
       
   114                          NANOSECONDS.convert(t, HOURS));
       
   115             assertEquals(1000000000L*t*60,
       
   116                          NANOSECONDS.convert(t, MINUTES));
       
   117             assertEquals(1000000000L*t,
       
   118                          NANOSECONDS.convert(t, SECONDS));
       
   119             assertEquals(1000000L*t,
       
   120                          NANOSECONDS.convert(t, MILLISECONDS));
       
   121             assertEquals(1000L*t,
       
   122                          NANOSECONDS.convert(t, MICROSECONDS));
       
   123             assertEquals(t,
       
   124                          NANOSECONDS.convert(t, NANOSECONDS));
       
   125         }
       
   126 
       
   127         for (TimeUnit x : TimeUnit.values()) {
       
   128             long[] zs = {
       
   129                 0, 1, -1,
       
   130                 Integer.MAX_VALUE, Integer.MIN_VALUE,
       
   131                 Long.MAX_VALUE, Long.MIN_VALUE,
       
   132             };
       
   133             for (long z : zs) assertEquals(z, x.convert(z, x));
       
   134         }
       
   135     }
       
   136 
       
   137     /**
       
   138      * toNanos correctly converts sample values in different units to
       
   139      * nanoseconds
       
   140      */
       
   141     public void testToNanos() {
       
   142         for (long t = 0; t < 88888; ++t) {
       
   143             assertEquals(t*1000000000L*60*60*24,
       
   144                          DAYS.toNanos(t));
       
   145             assertEquals(t*1000000000L*60*60,
       
   146                          HOURS.toNanos(t));
       
   147             assertEquals(t*1000000000L*60,
       
   148                          MINUTES.toNanos(t));
       
   149             assertEquals(1000000000L*t,
       
   150                          SECONDS.toNanos(t));
       
   151             assertEquals(1000000L*t,
       
   152                          MILLISECONDS.toNanos(t));
       
   153             assertEquals(1000L*t,
       
   154                          MICROSECONDS.toNanos(t));
       
   155             assertEquals(t,
       
   156                          NANOSECONDS.toNanos(t));
       
   157         }
       
   158     }
       
   159 
       
   160     /**
       
   161      * toMicros correctly converts sample values in different units to
       
   162      * microseconds
       
   163      */
       
   164     public void testToMicros() {
       
   165         for (long t = 0; t < 88888; ++t) {
       
   166             assertEquals(t*1000000L*60*60*24,
       
   167                          DAYS.toMicros(t));
       
   168             assertEquals(t*1000000L*60*60,
       
   169                          HOURS.toMicros(t));
       
   170             assertEquals(t*1000000L*60,
       
   171                          MINUTES.toMicros(t));
       
   172             assertEquals(1000000L*t,
       
   173                          SECONDS.toMicros(t));
       
   174             assertEquals(1000L*t,
       
   175                          MILLISECONDS.toMicros(t));
       
   176             assertEquals(t,
       
   177                          MICROSECONDS.toMicros(t));
       
   178             assertEquals(t,
       
   179                          NANOSECONDS.toMicros(t*1000L));
       
   180         }
       
   181     }
       
   182 
       
   183     /**
       
   184      * toMillis correctly converts sample values in different units to
       
   185      * milliseconds
       
   186      */
       
   187     public void testToMillis() {
       
   188         for (long t = 0; t < 88888; ++t) {
       
   189             assertEquals(t*1000L*60*60*24,
       
   190                          DAYS.toMillis(t));
       
   191             assertEquals(t*1000L*60*60,
       
   192                          HOURS.toMillis(t));
       
   193             assertEquals(t*1000L*60,
       
   194                          MINUTES.toMillis(t));
       
   195             assertEquals(1000L*t,
       
   196                          SECONDS.toMillis(t));
       
   197             assertEquals(t,
       
   198                          MILLISECONDS.toMillis(t));
       
   199             assertEquals(t,
       
   200                          MICROSECONDS.toMillis(t*1000L));
       
   201             assertEquals(t,
       
   202                          NANOSECONDS.toMillis(t*1000000L));
       
   203         }
       
   204     }
       
   205 
       
   206     /**
       
   207      * toSeconds correctly converts sample values in different units to
       
   208      * seconds
       
   209      */
       
   210     public void testToSeconds() {
       
   211         for (long t = 0; t < 88888; ++t) {
       
   212             assertEquals(t*60*60*24,
       
   213                          DAYS.toSeconds(t));
       
   214             assertEquals(t*60*60,
       
   215                          HOURS.toSeconds(t));
       
   216             assertEquals(t*60,
       
   217                          MINUTES.toSeconds(t));
       
   218             assertEquals(t,
       
   219                          SECONDS.toSeconds(t));
       
   220             assertEquals(t,
       
   221                          MILLISECONDS.toSeconds(t*1000L));
       
   222             assertEquals(t,
       
   223                          MICROSECONDS.toSeconds(t*1000000L));
       
   224             assertEquals(t,
       
   225                          NANOSECONDS.toSeconds(t*1000000000L));
       
   226         }
       
   227     }
       
   228 
       
   229     /**
       
   230      * toMinutes correctly converts sample values in different units to
       
   231      * minutes
       
   232      */
       
   233     public void testToMinutes() {
       
   234         for (long t = 0; t < 88888; ++t) {
       
   235             assertEquals(t*60*24,
       
   236                          DAYS.toMinutes(t));
       
   237             assertEquals(t*60,
       
   238                          HOURS.toMinutes(t));
       
   239             assertEquals(t,
       
   240                          MINUTES.toMinutes(t));
       
   241             assertEquals(t,
       
   242                          SECONDS.toMinutes(t*60));
       
   243             assertEquals(t,
       
   244                          MILLISECONDS.toMinutes(t*1000L*60));
       
   245             assertEquals(t,
       
   246                          MICROSECONDS.toMinutes(t*1000000L*60));
       
   247             assertEquals(t,
       
   248                          NANOSECONDS.toMinutes(t*1000000000L*60));
       
   249         }
       
   250     }
       
   251 
       
   252     /**
       
   253      * toHours correctly converts sample values in different units to
       
   254      * hours
       
   255      */
       
   256     public void testToHours() {
       
   257         for (long t = 0; t < 88888; ++t) {
       
   258             assertEquals(t*24,
       
   259                          DAYS.toHours(t));
       
   260             assertEquals(t,
       
   261                          HOURS.toHours(t));
       
   262             assertEquals(t,
       
   263                          MINUTES.toHours(t*60));
       
   264             assertEquals(t,
       
   265                          SECONDS.toHours(t*60*60));
       
   266             assertEquals(t,
       
   267                          MILLISECONDS.toHours(t*1000L*60*60));
       
   268             assertEquals(t,
       
   269                          MICROSECONDS.toHours(t*1000000L*60*60));
       
   270             assertEquals(t,
       
   271                          NANOSECONDS.toHours(t*1000000000L*60*60));
       
   272         }
       
   273     }
       
   274 
       
   275     /**
       
   276      * toDays correctly converts sample values in different units to
       
   277      * days
       
   278      */
       
   279     public void testToDays() {
       
   280         for (long t = 0; t < 88888; ++t) {
       
   281             assertEquals(t,
       
   282                          DAYS.toDays(t));
       
   283             assertEquals(t,
       
   284                          HOURS.toDays(t*24));
       
   285             assertEquals(t,
       
   286                          MINUTES.toDays(t*60*24));
       
   287             assertEquals(t,
       
   288                          SECONDS.toDays(t*60*60*24));
       
   289             assertEquals(t,
       
   290                          MILLISECONDS.toDays(t*1000L*60*60*24));
       
   291             assertEquals(t,
       
   292                          MICROSECONDS.toDays(t*1000000L*60*60*24));
       
   293             assertEquals(t,
       
   294                          NANOSECONDS.toDays(t*1000000000L*60*60*24));
       
   295         }
       
   296     }
   109     }
   297 
   110 
   298     /**
   111     /**
   299      * convert saturates positive too-large values to Long.MAX_VALUE
   112      * convert saturates positive too-large values to Long.MAX_VALUE
   300      * and negative to LONG.MIN_VALUE
   113      * and negative to LONG.MIN_VALUE
   492 
   305 
   493     /**
   306     /**
   494      * toString returns name of unit
   307      * toString returns name of unit
   495      */
   308      */
   496     public void testToString() {
   309     public void testToString() {
       
   310         assertEquals("NANOSECONDS", NANOSECONDS.toString());
       
   311         assertEquals("MICROSECONDS", MICROSECONDS.toString());
       
   312         assertEquals("MILLISECONDS", MILLISECONDS.toString());
   497         assertEquals("SECONDS", SECONDS.toString());
   313         assertEquals("SECONDS", SECONDS.toString());
       
   314         assertEquals("MINUTES", MINUTES.toString());
       
   315         assertEquals("HOURS", HOURS.toString());
       
   316         assertEquals("DAYS", DAYS.toString());
   498     }
   317     }
   499 
   318 
   500     /**
   319     /**
   501      * name returns name of unit
   320      * name returns name of unit
   502      */
   321      */
   503     public void testName() {
   322     public void testName() {
   504         assertEquals("SECONDS", SECONDS.name());
   323         for (TimeUnit x : TimeUnit.values())
       
   324             assertEquals(x.toString(), x.name());
   505     }
   325     }
   506 
   326 
   507     /**
   327     /**
   508      * Timed wait without holding lock throws
   328      * Timed wait without holding lock throws
   509      * IllegalMonitorStateException
   329      * IllegalMonitorStateException
   510      */
   330      */
   511     public void testTimedWait_IllegalMonitorException() {
   331     public void testTimedWait_IllegalMonitorException() {
   512         Thread t = newStartedThread(new CheckedRunnable() {
   332         Thread t = newStartedThread(new CheckedRunnable() {
   513             public void realRun() throws InterruptedException {
   333             public void realRun() throws InterruptedException {
   514                 Object o = new Object();
   334                 Object o = new Object();
   515                 TimeUnit tu = MILLISECONDS;
   335                 try {
   516 
   336                     MILLISECONDS.timedWait(o, LONGER_DELAY_MS);
   517                 try {
       
   518                     tu.timedWait(o, LONG_DELAY_MS);
       
   519                     threadShouldThrow();
   337                     threadShouldThrow();
   520                 } catch (IllegalMonitorStateException success) {}
   338                 } catch (IllegalMonitorStateException success) {}
   521             }});
   339             }});
   522 
   340 
   523         awaitTermination(t);
   341         awaitTermination(t);
   529     public void testTimedWait_Interruptible() {
   347     public void testTimedWait_Interruptible() {
   530         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
   348         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
   531         Thread t = newStartedThread(new CheckedRunnable() {
   349         Thread t = newStartedThread(new CheckedRunnable() {
   532             public void realRun() throws InterruptedException {
   350             public void realRun() throws InterruptedException {
   533                 Object o = new Object();
   351                 Object o = new Object();
   534                 TimeUnit tu = MILLISECONDS;
       
   535 
   352 
   536                 Thread.currentThread().interrupt();
   353                 Thread.currentThread().interrupt();
   537                 try {
   354                 try {
   538                     synchronized (o) {
   355                     synchronized (o) {
   539                         tu.timedWait(o, LONG_DELAY_MS);
   356                         MILLISECONDS.timedWait(o, LONGER_DELAY_MS);
   540                     }
   357                     }
   541                     shouldThrow();
   358                     shouldThrow();
   542                 } catch (InterruptedException success) {}
   359                 } catch (InterruptedException success) {}
   543                 assertFalse(Thread.interrupted());
   360                 assertFalse(Thread.interrupted());
   544 
   361 
   545                 pleaseInterrupt.countDown();
   362                 pleaseInterrupt.countDown();
   546                 try {
   363                 try {
   547                     synchronized (o) {
   364                     synchronized (o) {
   548                         tu.timedWait(o, LONG_DELAY_MS);
   365                         MILLISECONDS.timedWait(o, LONGER_DELAY_MS);
   549                     }
   366                     }
   550                     shouldThrow();
   367                     shouldThrow();
   551                 } catch (InterruptedException success) {}
   368                 } catch (InterruptedException success) {}
   552                 assertFalse(Thread.interrupted());
   369                 assertFalse(Thread.interrupted());
   553             }});
   370             }});
   554 
   371 
   555         await(pleaseInterrupt);
   372         await(pleaseInterrupt);
   556         assertThreadBlocks(t, Thread.State.TIMED_WAITING);
   373         if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
   557         t.interrupt();
   374         t.interrupt();
   558         awaitTermination(t);
   375         awaitTermination(t);
   559     }
   376     }
   560 
   377 
   561     /**
   378     /**
   563      */
   380      */
   564     public void testTimedJoin_Interruptible() {
   381     public void testTimedJoin_Interruptible() {
   565         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
   382         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
   566         final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
   383         final Thread s = newStartedThread(new CheckedInterruptedRunnable() {
   567             public void realRun() throws InterruptedException {
   384             public void realRun() throws InterruptedException {
   568                 Thread.sleep(LONG_DELAY_MS);
   385                 Thread.sleep(LONGER_DELAY_MS);
   569             }});
   386             }});
   570         final Thread t = newStartedThread(new CheckedRunnable() {
   387         final Thread t = newStartedThread(new CheckedRunnable() {
   571             public void realRun() throws InterruptedException {
   388             public void realRun() throws InterruptedException {
   572                 TimeUnit tu = MILLISECONDS;
       
   573                 Thread.currentThread().interrupt();
   389                 Thread.currentThread().interrupt();
   574                 try {
   390                 try {
   575                     tu.timedJoin(s, LONG_DELAY_MS);
   391                     MILLISECONDS.timedJoin(s, LONGER_DELAY_MS);
   576                     shouldThrow();
   392                     shouldThrow();
   577                 } catch (InterruptedException success) {}
   393                 } catch (InterruptedException success) {}
   578                 assertFalse(Thread.interrupted());
   394                 assertFalse(Thread.interrupted());
   579 
   395 
   580                 pleaseInterrupt.countDown();
   396                 pleaseInterrupt.countDown();
   581                 try {
   397                 try {
   582                     tu.timedJoin(s, LONG_DELAY_MS);
   398                     MILLISECONDS.timedJoin(s, LONGER_DELAY_MS);
   583                     shouldThrow();
   399                     shouldThrow();
   584                 } catch (InterruptedException success) {}
   400                 } catch (InterruptedException success) {}
   585                 assertFalse(Thread.interrupted());
   401                 assertFalse(Thread.interrupted());
   586             }});
   402             }});
   587 
   403 
   588         await(pleaseInterrupt);
   404         await(pleaseInterrupt);
   589         assertThreadBlocks(t, Thread.State.TIMED_WAITING);
   405         if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
   590         t.interrupt();
   406         t.interrupt();
   591         awaitTermination(t);
   407         awaitTermination(t);
   592         s.interrupt();
   408         s.interrupt();
   593         awaitTermination(s);
   409         awaitTermination(s);
   594     }
   410     }
   595 
   411 
   596     /**
   412     /**
   597      * timedSleep throws InterruptedException when interrupted
   413      * timeUnit.sleep throws InterruptedException when interrupted
   598      */
   414      */
   599     public void testTimedSleep_Interruptible() {
   415     public void testTimedSleep_Interruptible() {
   600         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
   416         final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
   601         Thread t = newStartedThread(new CheckedRunnable() {
   417         Thread t = newStartedThread(new CheckedRunnable() {
   602             public void realRun() throws InterruptedException {
   418             public void realRun() throws InterruptedException {
   603                 TimeUnit tu = MILLISECONDS;
       
   604                 Thread.currentThread().interrupt();
   419                 Thread.currentThread().interrupt();
   605                 try {
   420                 try {
   606                     tu.sleep(LONG_DELAY_MS);
   421                     MILLISECONDS.sleep(LONGER_DELAY_MS);
   607                     shouldThrow();
   422                     shouldThrow();
   608                 } catch (InterruptedException success) {}
   423                 } catch (InterruptedException success) {}
   609                 assertFalse(Thread.interrupted());
   424                 assertFalse(Thread.interrupted());
   610 
   425 
   611                 pleaseInterrupt.countDown();
   426                 pleaseInterrupt.countDown();
   612                 try {
   427                 try {
   613                     tu.sleep(LONG_DELAY_MS);
   428                     MILLISECONDS.sleep(LONGER_DELAY_MS);
   614                     shouldThrow();
   429                     shouldThrow();
   615                 } catch (InterruptedException success) {}
   430                 } catch (InterruptedException success) {}
   616                 assertFalse(Thread.interrupted());
   431                 assertFalse(Thread.interrupted());
   617             }});
   432             }});
   618 
   433 
   619         await(pleaseInterrupt);
   434         await(pleaseInterrupt);
   620         assertThreadBlocks(t, Thread.State.TIMED_WAITING);
   435         if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
   621         t.interrupt();
   436         t.interrupt();
   622         awaitTermination(t);
   437         awaitTermination(t);
       
   438     }
       
   439 
       
   440     /**
       
   441      * timeUnit.sleep(x) for x <= 0 does not sleep at all.
       
   442      */
       
   443     public void testTimedSleep_nonPositive() throws InterruptedException {
       
   444         boolean interrupt = randomBoolean();
       
   445         if (interrupt) Thread.currentThread().interrupt();
       
   446         randomTimeUnit().sleep(0L);
       
   447         randomTimeUnit().sleep(-1L);
       
   448         randomTimeUnit().sleep(Long.MIN_VALUE);
       
   449         if (interrupt) assertTrue(Thread.interrupted());
   623     }
   450     }
   624 
   451 
   625     /**
   452     /**
   626      * a deserialized/reserialized unit is the same instance
   453      * a deserialized/reserialized unit is the same instance
   627      */
   454      */