test/jdk/java/util/concurrent/TimeUnit/Basic.java
author dl
Sat, 14 Sep 2019 11:26:26 -0700
changeset 58138 1e4270f875ee
parent 47216 71c04702a3d5
permissions -rw-r--r--
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09 Reviewed-by: martin, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2402
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 5057341 6363898
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Basic tests for TimeUnit
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    31
import static java.util.concurrent.TimeUnit.DAYS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    32
import static java.util.concurrent.TimeUnit.HOURS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    33
import static java.util.concurrent.TimeUnit.MICROSECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    34
import static java.util.concurrent.TimeUnit.MILLISECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    35
import static java.util.concurrent.TimeUnit.MINUTES;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    36
import static java.util.concurrent.TimeUnit.NANOSECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    37
import static java.util.concurrent.TimeUnit.SECONDS;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    38
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    39
import java.io.ByteArrayInputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    40
import java.io.ByteArrayOutputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    41
import java.io.IOException;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    42
import java.io.InputStream;
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    43
import java.util.List;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    44
import java.io.ObjectOutputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    45
import java.io.ObjectInputStream;
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    46
import java.util.Arrays;
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    47
import java.util.concurrent.CompletableFuture;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    48
import java.util.concurrent.ThreadLocalRandom;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 5506
diff changeset
    49
import java.util.concurrent.TimeUnit;
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    50
import java.util.stream.Collectors;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    51
import java.util.stream.IntStream;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    52
import jdk.test.lib.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class Basic {
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    55
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (TimeUnit u : TimeUnit.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println(u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            check(u instanceof TimeUnit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            equal(42L, u.convert(42, u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            for (TimeUnit v : TimeUnit.values())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                if (u.convert(42, v) >= 42)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    equal(42L, v.convert(u.convert(42, v), u));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            check(readObject(serializedForm(u)) == u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        equal(  24L, HOURS.convert       (1, DAYS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        equal(  60L, MINUTES.convert     (1, HOURS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        equal(  60L, SECONDS.convert     (1, MINUTES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        equal(1000L, MILLISECONDS.convert(1, SECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        equal(1000L, MICROSECONDS.convert(1, MILLISECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        equal(1000L, NANOSECONDS.convert (1, MICROSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        equal(  24L, DAYS.toHours(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        equal(  60L, HOURS.toMinutes(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        equal(  60L, MINUTES.toSeconds(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        equal(1000L, SECONDS.toMillis(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        equal(1000L, MILLISECONDS.toMicros(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        equal(1000L, MICROSECONDS.toNanos(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    83
        //----------------------------------------------------------------
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    84
        // TimeUnit.sleep sleeps for at least the specified time.
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    85
        // TimeUnit.sleep(x, unit) for x <= 0 does not sleep at all.
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    86
        //----------------------------------------------------------------
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    87
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    88
        int maxTimeoutMillis = rnd.nextInt(1, 12);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    89
        List<CompletableFuture<?>> workers =
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    90
            IntStream.range(-1, maxTimeoutMillis + 1)
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    91
            .mapToObj(timeoutMillis -> (Runnable) () -> {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    92
                try {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    93
                    long startTime = System.nanoTime();
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    94
                    MILLISECONDS.sleep(timeoutMillis);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    95
                    long elapsedNanos = System.nanoTime() - startTime;
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    96
                    long timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    97
                    check(elapsedNanos >= timeoutNanos);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    98
                } catch (InterruptedException fail) {
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
    99
                    throw new AssertionError(fail);
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
   100
                }})
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
   101
            .map(CompletableFuture::runAsync)
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
   102
            .collect(Collectors.toList());
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
   103
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
   104
        workers.forEach(CompletableFuture<?>::join);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Tests for serialized form compatibility with previous release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        //----------------------------------------------------------------
58138
1e4270f875ee 8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents: 47216
diff changeset
   109
        byte[] serializedForm = /* Generated using JDK 5 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            {-84, -19, 0, 5, '~', 'r', 0, 29, 'j', 'a', 'v', 'a', '.',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
             'u', 't', 'i', 'l', '.', 'c', 'o', 'n', 'c', 'u', 'r', 'r', 'e',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
             'n', 't', '.', 'T', 'i', 'm', 'e', 'U', 'n', 'i', 't', 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
             0, 0, 0, 0, 0, 0, 18, 0, 0, 'x', 'r', 0, 14,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
             'j', 'a', 'v', 'a', '.', 'l', 'a', 'n', 'g', '.', 'E', 'n', 'u',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
             'm', 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 'x',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
             'p', 't', 0, 7, 'S', 'E', 'C', 'O', 'N', 'D', 'S', };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        check(Arrays.equals(serializedForm(SECONDS), serializedForm));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static byte[] serializedForm(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return new ObjectInputStream(is).readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}