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