< prev index next >

test/jdk/java/util/concurrent/TimeUnit/Basic.java

Print this page
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
Reviewed-by: martin, alanb


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 5057341 6363898
  26  * @summary Basic tests for TimeUnit

  27  * @author Martin Buchholz
  28  */
  29 
  30 import static java.util.concurrent.TimeUnit.DAYS;
  31 import static java.util.concurrent.TimeUnit.HOURS;
  32 import static java.util.concurrent.TimeUnit.MICROSECONDS;
  33 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  34 import static java.util.concurrent.TimeUnit.MINUTES;
  35 import static java.util.concurrent.TimeUnit.NANOSECONDS;
  36 import static java.util.concurrent.TimeUnit.SECONDS;
  37 
  38 import java.io.ByteArrayInputStream;
  39 import java.io.ByteArrayOutputStream;
  40 import java.io.IOException;
  41 import java.io.InputStream;

  42 import java.io.ObjectOutputStream;
  43 import java.io.ObjectInputStream;
  44 import java.util.Arrays;


  45 import java.util.concurrent.TimeUnit;



  46 
  47 public class Basic {


  48     private static void realMain(String[] args) throws Throwable {
  49 
  50         for (TimeUnit u : TimeUnit.values()) {
  51             System.out.println(u);
  52             check(u instanceof TimeUnit);
  53             equal(42L, u.convert(42, u));
  54             for (TimeUnit v : TimeUnit.values())
  55                 if (u.convert(42, v) >= 42)
  56                     equal(42L, v.convert(u.convert(42, v), u));
  57             check(readObject(serializedForm(u)) == u);
  58         }
  59 
  60         equal(  24L, HOURS.convert       (1, DAYS));
  61         equal(  60L, MINUTES.convert     (1, HOURS));
  62         equal(  60L, SECONDS.convert     (1, MINUTES));
  63         equal(1000L, MILLISECONDS.convert(1, SECONDS));
  64         equal(1000L, MICROSECONDS.convert(1, MILLISECONDS));
  65         equal(1000L, NANOSECONDS.convert (1, MICROSECONDS));
  66 
  67         equal(  24L, DAYS.toHours(1));
  68         equal(  60L, HOURS.toMinutes(1));
  69         equal(  60L, MINUTES.toSeconds(1));
  70         equal(1000L, SECONDS.toMillis(1));
  71         equal(1000L, MILLISECONDS.toMicros(1));
  72         equal(1000L, MICROSECONDS.toNanos(1));
  73 
  74         long t0 = System.nanoTime();
  75         MILLISECONDS.sleep(3); /* See windows bug 6313903, might not sleep */
  76         long elapsedMillis = (System.nanoTime() - t0)/(1000L * 1000L);
  77         System.out.printf("elapsed=%d%n", elapsedMillis);
  78         check(elapsedMillis >= 0);
  79         /* Might not sleep on windows: check(elapsedMillis >= 3); */
  80         check(elapsedMillis < 1000);















  81 
  82         //----------------------------------------------------------------
  83         // Tests for serialized form compatibility with previous release
  84         //----------------------------------------------------------------
  85         byte[] serializedForm = /* Generated using tiger */
  86             {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.',
  87              'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e',
  88              'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0,
  89              0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14,
  90              'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u',
  91              'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x',
  92              'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', };
  93         check(Arrays.equals(serializedForm(SECONDS), serializedForm));
  94     }
  95 
  96     //--------------------- Infrastructure ---------------------------
  97     static volatile int passed = 0, failed = 0;
  98     static void pass() {passed++;}
  99     static void fail() {failed++; Thread.dumpStack();}
 100     static void fail(String msg) {System.out.println(msg); fail();}
 101     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 102     static void check(boolean cond) {if (cond) pass(); else fail();}
 103     static void equal(Object x, Object y) {
 104         if (x == null ? y == null : x.equals(y)) pass();
 105         else fail(x + " not equal to " + y);}


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 5057341 6363898
  26  * @summary Basic tests for TimeUnit
  27  * @library /test/lib
  28  * @author Martin Buchholz
  29  */
  30 
  31 import static java.util.concurrent.TimeUnit.DAYS;
  32 import static java.util.concurrent.TimeUnit.HOURS;
  33 import static java.util.concurrent.TimeUnit.MICROSECONDS;
  34 import static java.util.concurrent.TimeUnit.MILLISECONDS;
  35 import static java.util.concurrent.TimeUnit.MINUTES;
  36 import static java.util.concurrent.TimeUnit.NANOSECONDS;
  37 import static java.util.concurrent.TimeUnit.SECONDS;
  38 
  39 import java.io.ByteArrayInputStream;
  40 import java.io.ByteArrayOutputStream;
  41 import java.io.IOException;
  42 import java.io.InputStream;
  43 import java.util.List;
  44 import java.io.ObjectOutputStream;
  45 import java.io.ObjectInputStream;
  46 import java.util.Arrays;
  47 import java.util.concurrent.CompletableFuture;
  48 import java.util.concurrent.ThreadLocalRandom;
  49 import java.util.concurrent.TimeUnit;
  50 import java.util.stream.Collectors;
  51 import java.util.stream.IntStream;
  52 import jdk.test.lib.Utils;
  53 
  54 public class Basic {
  55     static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
  56 
  57     private static void realMain(String[] args) throws Throwable {
  58 
  59         for (TimeUnit u : TimeUnit.values()) {
  60             System.out.println(u);
  61             check(u instanceof TimeUnit);
  62             equal(42L, u.convert(42, u));
  63             for (TimeUnit v : TimeUnit.values())
  64                 if (u.convert(42, v) >= 42)
  65                     equal(42L, v.convert(u.convert(42, v), u));
  66             check(readObject(serializedForm(u)) == u);
  67         }
  68 
  69         equal(  24L, HOURS.convert       (1, DAYS));
  70         equal(  60L, MINUTES.convert     (1, HOURS));
  71         equal(  60L, SECONDS.convert     (1, MINUTES));
  72         equal(1000L, MILLISECONDS.convert(1, SECONDS));
  73         equal(1000L, MICROSECONDS.convert(1, MILLISECONDS));
  74         equal(1000L, NANOSECONDS.convert (1, MICROSECONDS));
  75 
  76         equal(  24L, DAYS.toHours(1));
  77         equal(  60L, HOURS.toMinutes(1));
  78         equal(  60L, MINUTES.toSeconds(1));
  79         equal(1000L, SECONDS.toMillis(1));
  80         equal(1000L, MILLISECONDS.toMicros(1));
  81         equal(1000L, MICROSECONDS.toNanos(1));
  82 
  83         //----------------------------------------------------------------
  84         // TimeUnit.sleep sleeps for at least the specified time.
  85         // TimeUnit.sleep(x, unit) for x <= 0 does not sleep at all.
  86         //----------------------------------------------------------------
  87         ThreadLocalRandom rnd = ThreadLocalRandom.current();
  88         int maxTimeoutMillis = rnd.nextInt(1, 12);
  89         List<CompletableFuture<?>> workers =
  90             IntStream.range(-1, maxTimeoutMillis + 1)
  91             .mapToObj(timeoutMillis -> (Runnable) () -> {
  92                 try {
  93                     long startTime = System.nanoTime();
  94                     MILLISECONDS.sleep(timeoutMillis);
  95                     long elapsedNanos = System.nanoTime() - startTime;
  96                     long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
  97                     check(elapsedNanos >= timeoutNanos);
  98                 } catch (InterruptedException fail) {
  99                     throw new AssertionError(fail);
 100                 }})
 101             .map(CompletableFuture::runAsync)
 102             .collect(Collectors.toList());
 103 
 104         workers.forEach(CompletableFuture<?>::join);
 105 
 106         //----------------------------------------------------------------
 107         // Tests for serialized form compatibility with previous release
 108         //----------------------------------------------------------------
 109         byte[] serializedForm = /* Generated using JDK 5 */
 110             {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.',
 111              'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e',
 112              'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0,
 113              0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14,
 114              'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u',
 115              'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x',
 116              'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', };
 117         check(Arrays.equals(serializedForm(SECONDS), serializedForm));
 118     }
 119 
 120     //--------------------- Infrastructure ---------------------------
 121     static volatile int passed = 0, failed = 0;
 122     static void pass() {passed++;}
 123     static void fail() {failed++; Thread.dumpStack();}
 124     static void fail(String msg) {System.out.println(msg); fail();}
 125     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 126     static void check(boolean cond) {if (cond) pass(); else fail();}
 127     static void equal(Object x, Object y) {
 128         if (x == null ? y == null : x.equals(y)) pass();
 129         else fail(x + " not equal to " + y);}
< prev index next >