author | dl |
Sat, 14 Sep 2019 11:26:26 -0700 | |
changeset 58138 | 1e4270f875ee |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 5057341 6363898 |
|
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 | 28 |
* @author Martin Buchholz |
29 |
*/ |
|
30 |
||
34347 | 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; |
|
58138
1e4270f875ee
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
dl
parents:
47216
diff
changeset
|
43 |
import java.util.List; |
34347 | 44 |
import java.io.ObjectOutputStream; |
45 |
import java.io.ObjectInputStream; |
|
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 | 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 | 53 |
|
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 | 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 |
||
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 | 105 |
|
106 |
//---------------------------------------------------------------- |
|
107 |
// Tests for serialized form compatibility with previous release |
|
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 | 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);} |
|
130 |
public static void main(String[] args) throws Throwable { |
|
131 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
132 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
133 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
134 |
static byte[] serializedForm(Object obj) throws IOException { |
|
135 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
136 |
new ObjectOutputStream(baos).writeObject(obj); |
|
137 |
return baos.toByteArray(); |
|
138 |
} |
|
139 |
static Object readObject(byte[] bytes) |
|
140 |
throws IOException, ClassNotFoundException { |
|
141 |
InputStream is = new ByteArrayInputStream(bytes); |
|
142 |
return new ObjectInputStream(is).readObject(); |
|
143 |
} |
|
144 |
} |