1 |
/* |
2 |
* 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 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
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 |
import java.time.temporal.ChronoUnit; |
18 |
import java.util.concurrent.CountDownLatch; |
19 |
import java.util.concurrent.TimeUnit; |
20 |
|
21 |
import junit.framework.Test; |
22 |
import junit.framework.TestSuite; |
23 |
|
24 |
public class TimeUnitTest extends JSR166TestCase { |
25 |
public static void main(String[] args) { |
26 |
main(suite(), args); |
27 |
} |
28 |
|
29 |
public static Test suite() { |
30 |
return new TestSuite(TimeUnitTest.class); |
31 |
} |
32 |
|
33 |
// (loops to 88888 check increments at all time divisions.) |
34 |
|
35 |
/** |
36 |
* convert correctly converts sample values across the units |
37 |
*/ |
38 |
public void testConvert() { |
39 |
for (long t = 0; t < 88888; ++t) { |
40 |
assertEquals(t*60*60*24, |
41 |
SECONDS.convert(t, DAYS)); |
42 |
assertEquals(t*60*60, |
43 |
SECONDS.convert(t, HOURS)); |
44 |
assertEquals(t*60, |
45 |
SECONDS.convert(t, MINUTES)); |
46 |
assertEquals(t, |
47 |
SECONDS.convert(t, SECONDS)); |
48 |
assertEquals(t, |
49 |
SECONDS.convert(1000L*t, MILLISECONDS)); |
50 |
assertEquals(t, |
51 |
SECONDS.convert(1000000L*t, MICROSECONDS)); |
52 |
assertEquals(t, |
53 |
SECONDS.convert(1000000000L*t, NANOSECONDS)); |
54 |
|
55 |
assertEquals(1000L*t*60*60*24, |
56 |
MILLISECONDS.convert(t, DAYS)); |
57 |
assertEquals(1000L*t*60*60, |
58 |
MILLISECONDS.convert(t, HOURS)); |
59 |
assertEquals(1000L*t*60, |
60 |
MILLISECONDS.convert(t, MINUTES)); |
61 |
assertEquals(1000L*t, |
62 |
MILLISECONDS.convert(t, SECONDS)); |
63 |
assertEquals(t, |
64 |
MILLISECONDS.convert(t, MILLISECONDS)); |
65 |
assertEquals(t, |
66 |
MILLISECONDS.convert(1000L*t, MICROSECONDS)); |
67 |
assertEquals(t, |
68 |
MILLISECONDS.convert(1000000L*t, NANOSECONDS)); |
69 |
|
70 |
assertEquals(1000000L*t*60*60*24, |
71 |
MICROSECONDS.convert(t, DAYS)); |
72 |
assertEquals(1000000L*t*60*60, |
73 |
MICROSECONDS.convert(t, HOURS)); |
74 |
assertEquals(1000000L*t*60, |
75 |
MICROSECONDS.convert(t, MINUTES)); |
76 |
assertEquals(1000000L*t, |
77 |
MICROSECONDS.convert(t, SECONDS)); |
78 |
assertEquals(1000L*t, |
79 |
MICROSECONDS.convert(t, MILLISECONDS)); |
80 |
assertEquals(t, |
81 |
MICROSECONDS.convert(t, MICROSECONDS)); |
82 |
assertEquals(t, |
83 |
MICROSECONDS.convert(1000L*t, NANOSECONDS)); |
84 |
|
85 |
assertEquals(1000000000L*t*60*60*24, |
86 |
NANOSECONDS.convert(t, DAYS)); |
87 |
assertEquals(1000000000L*t*60*60, |
88 |
NANOSECONDS.convert(t, HOURS)); |
89 |
assertEquals(1000000000L*t*60, |
90 |
NANOSECONDS.convert(t, MINUTES)); |
91 |
assertEquals(1000000000L*t, |
92 |
NANOSECONDS.convert(t, SECONDS)); |
93 |
assertEquals(1000000L*t, |
94 |
NANOSECONDS.convert(t, MILLISECONDS)); |
95 |
assertEquals(1000L*t, |
96 |
NANOSECONDS.convert(t, MICROSECONDS)); |
97 |
assertEquals(t, |
98 |
NANOSECONDS.convert(t, NANOSECONDS)); |
99 |
} |
100 |
} |
101 |
|
102 |
/** |
103 |
* toNanos correctly converts sample values in different units to |
104 |
* nanoseconds |
105 |
*/ |
106 |
public void testToNanos() { |
107 |
for (long t = 0; t < 88888; ++t) { |
108 |
assertEquals(t*1000000000L*60*60*24, |
109 |
DAYS.toNanos(t)); |
110 |
assertEquals(t*1000000000L*60*60, |
111 |
HOURS.toNanos(t)); |
112 |
assertEquals(t*1000000000L*60, |
113 |
MINUTES.toNanos(t)); |
114 |
assertEquals(1000000000L*t, |
115 |
SECONDS.toNanos(t)); |
116 |
assertEquals(1000000L*t, |
117 |
MILLISECONDS.toNanos(t)); |
118 |
assertEquals(1000L*t, |
119 |
MICROSECONDS.toNanos(t)); |
120 |
assertEquals(t, |
121 |
NANOSECONDS.toNanos(t)); |
122 |
} |
123 |
} |
124 |
|
125 |
/** |
126 |
* toMicros correctly converts sample values in different units to |
127 |
* microseconds |
128 |
*/ |
129 |
public void testToMicros() { |
130 |
for (long t = 0; t < 88888; ++t) { |
131 |
assertEquals(t*1000000L*60*60*24, |
132 |
DAYS.toMicros(t)); |
133 |
assertEquals(t*1000000L*60*60, |
134 |
HOURS.toMicros(t)); |
135 |
assertEquals(t*1000000L*60, |
136 |
MINUTES.toMicros(t)); |
137 |
assertEquals(1000000L*t, |
138 |
SECONDS.toMicros(t)); |
139 |
assertEquals(1000L*t, |
140 |
MILLISECONDS.toMicros(t)); |
141 |
assertEquals(t, |
142 |
MICROSECONDS.toMicros(t)); |
143 |
assertEquals(t, |
144 |
NANOSECONDS.toMicros(t*1000L)); |
145 |
} |
146 |
} |
147 |
|
148 |
/** |
149 |
* toMillis correctly converts sample values in different units to |
150 |
* milliseconds |
151 |
*/ |
152 |
public void testToMillis() { |
153 |
for (long t = 0; t < 88888; ++t) { |
154 |
assertEquals(t*1000L*60*60*24, |
155 |
DAYS.toMillis(t)); |
156 |
assertEquals(t*1000L*60*60, |
157 |
HOURS.toMillis(t)); |
158 |
assertEquals(t*1000L*60, |
159 |
MINUTES.toMillis(t)); |
160 |
assertEquals(1000L*t, |
161 |
SECONDS.toMillis(t)); |
162 |
assertEquals(t, |
163 |
MILLISECONDS.toMillis(t)); |
164 |
assertEquals(t, |
165 |
MICROSECONDS.toMillis(t*1000L)); |
166 |
assertEquals(t, |
167 |
NANOSECONDS.toMillis(t*1000000L)); |
168 |
} |
169 |
} |
170 |
|
171 |
/** |
172 |
* toSeconds correctly converts sample values in different units to |
173 |
* seconds |
174 |
*/ |
175 |
public void testToSeconds() { |
176 |
for (long t = 0; t < 88888; ++t) { |
177 |
assertEquals(t*60*60*24, |
178 |
DAYS.toSeconds(t)); |
179 |
assertEquals(t*60*60, |
180 |
HOURS.toSeconds(t)); |
181 |
assertEquals(t*60, |
182 |
MINUTES.toSeconds(t)); |
183 |
assertEquals(t, |
184 |
SECONDS.toSeconds(t)); |
185 |
assertEquals(t, |
186 |
MILLISECONDS.toSeconds(t*1000L)); |
187 |
assertEquals(t, |
188 |
MICROSECONDS.toSeconds(t*1000000L)); |
189 |
assertEquals(t, |
190 |
NANOSECONDS.toSeconds(t*1000000000L)); |
191 |
} |
192 |
} |
193 |
|
194 |
/** |
195 |
* toMinutes correctly converts sample values in different units to |
196 |
* minutes |
197 |
*/ |
198 |
public void testToMinutes() { |
199 |
for (long t = 0; t < 88888; ++t) { |
200 |
assertEquals(t*60*24, |
201 |
DAYS.toMinutes(t)); |
202 |
assertEquals(t*60, |
203 |
HOURS.toMinutes(t)); |
204 |
assertEquals(t, |
205 |
MINUTES.toMinutes(t)); |
206 |
assertEquals(t, |
207 |
SECONDS.toMinutes(t*60)); |
208 |
assertEquals(t, |
209 |
MILLISECONDS.toMinutes(t*1000L*60)); |
210 |
assertEquals(t, |
211 |
MICROSECONDS.toMinutes(t*1000000L*60)); |
212 |
assertEquals(t, |
213 |
NANOSECONDS.toMinutes(t*1000000000L*60)); |
214 |
} |
215 |
} |
216 |
|
217 |
/** |
218 |
* toHours correctly converts sample values in different units to |
219 |
* hours |
220 |
*/ |
221 |
public void testToHours() { |
222 |
for (long t = 0; t < 88888; ++t) { |
223 |
assertEquals(t*24, |
224 |
DAYS.toHours(t)); |
225 |
assertEquals(t, |
226 |
HOURS.toHours(t)); |
227 |
assertEquals(t, |
228 |
MINUTES.toHours(t*60)); |
229 |
assertEquals(t, |
230 |
SECONDS.toHours(t*60*60)); |
231 |
assertEquals(t, |
232 |
MILLISECONDS.toHours(t*1000L*60*60)); |
233 |
assertEquals(t, |
234 |
MICROSECONDS.toHours(t*1000000L*60*60)); |
235 |
assertEquals(t, |
236 |
NANOSECONDS.toHours(t*1000000000L*60*60)); |
237 |
} |
238 |
} |
239 |
|
240 |
/** |
241 |
* toDays correctly converts sample values in different units to |
242 |
* days |
243 |
*/ |
244 |
public void testToDays() { |
245 |
for (long t = 0; t < 88888; ++t) { |
246 |
assertEquals(t, |
247 |
DAYS.toDays(t)); |
248 |
assertEquals(t, |
249 |
HOURS.toDays(t*24)); |
250 |
assertEquals(t, |
251 |
MINUTES.toDays(t*60*24)); |
252 |
assertEquals(t, |
253 |
SECONDS.toDays(t*60*60*24)); |
254 |
assertEquals(t, |
255 |
MILLISECONDS.toDays(t*1000L*60*60*24)); |
256 |
assertEquals(t, |
257 |
MICROSECONDS.toDays(t*1000000L*60*60*24)); |
258 |
assertEquals(t, |
259 |
NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
260 |
} |
261 |
} |
262 |
|
263 |
/** |
264 |
* convert saturates positive too-large values to Long.MAX_VALUE |
265 |
* and negative to LONG.MIN_VALUE |
266 |
*/ |
267 |
public void testConvertSaturate() { |
268 |
assertEquals(Long.MAX_VALUE, |
269 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS)); |
270 |
assertEquals(Long.MIN_VALUE, |
271 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS)); |
272 |
assertEquals(Long.MAX_VALUE, |
273 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES)); |
274 |
assertEquals(Long.MIN_VALUE, |
275 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES)); |
276 |
assertEquals(Long.MAX_VALUE, |
277 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS)); |
278 |
assertEquals(Long.MIN_VALUE, |
279 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS)); |
280 |
assertEquals(Long.MAX_VALUE, |
281 |
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); |
282 |
assertEquals(Long.MIN_VALUE, |
283 |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); |
284 |
} |
285 |
|
286 |
/** |
287 |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
288 |
* and negative to LONG.MIN_VALUE |
289 |
*/ |
290 |
public void testToNanosSaturate() { |
291 |
assertEquals(Long.MAX_VALUE, |
292 |
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
293 |
assertEquals(Long.MIN_VALUE, |
294 |
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
295 |
} |
296 |
|
297 |
/** |
298 |
* toString returns name of unit |
299 |
*/ |
300 |
public void testToString() { |
301 |
assertEquals("SECONDS", SECONDS.toString()); |
302 |
} |
303 |
|
304 |
/** |
305 |
* name returns name of unit |
306 |
*/ |
307 |
public void testName() { |
308 |
assertEquals("SECONDS", SECONDS.name()); |
309 |
} |
310 |
|
311 |
/** |
312 |
* Timed wait without holding lock throws |
313 |
* IllegalMonitorStateException |
314 |
*/ |
315 |
public void testTimedWait_IllegalMonitorException() { |
316 |
Thread t = newStartedThread(new CheckedRunnable() { |
317 |
public void realRun() throws InterruptedException { |
318 |
Object o = new Object(); |
319 |
TimeUnit tu = MILLISECONDS; |
320 |
|
321 |
try { |
322 |
tu.timedWait(o, LONG_DELAY_MS); |
323 |
threadShouldThrow(); |
324 |
} catch (IllegalMonitorStateException success) {} |
325 |
}}); |
326 |
|
327 |
awaitTermination(t); |
328 |
} |
329 |
|
330 |
/** |
331 |
* timedWait throws InterruptedException when interrupted |
332 |
*/ |
333 |
public void testTimedWait_Interruptible() { |
334 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
335 |
Thread t = newStartedThread(new CheckedRunnable() { |
336 |
public void realRun() throws InterruptedException { |
337 |
Object o = new Object(); |
338 |
TimeUnit tu = MILLISECONDS; |
339 |
|
340 |
Thread.currentThread().interrupt(); |
341 |
try { |
342 |
synchronized (o) { |
343 |
tu.timedWait(o, LONG_DELAY_MS); |
344 |
} |
345 |
shouldThrow(); |
346 |
} catch (InterruptedException success) {} |
347 |
assertFalse(Thread.interrupted()); |
348 |
|
349 |
pleaseInterrupt.countDown(); |
350 |
try { |
351 |
synchronized (o) { |
352 |
tu.timedWait(o, LONG_DELAY_MS); |
353 |
} |
354 |
shouldThrow(); |
355 |
} catch (InterruptedException success) {} |
356 |
assertFalse(Thread.interrupted()); |
357 |
}}); |
358 |
|
359 |
await(pleaseInterrupt); |
360 |
assertThreadStaysAlive(t); |
361 |
t.interrupt(); |
362 |
awaitTermination(t); |
363 |
} |
364 |
|
365 |
/** |
366 |
* timedJoin throws InterruptedException when interrupted |
367 |
*/ |
368 |
public void testTimedJoin_Interruptible() { |
369 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
370 |
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
371 |
public void realRun() throws InterruptedException { |
372 |
Thread.sleep(LONG_DELAY_MS); |
373 |
}}); |
374 |
final Thread t = newStartedThread(new CheckedRunnable() { |
375 |
public void realRun() throws InterruptedException { |
376 |
TimeUnit tu = MILLISECONDS; |
377 |
Thread.currentThread().interrupt(); |
378 |
try { |
379 |
tu.timedJoin(s, LONG_DELAY_MS); |
380 |
shouldThrow(); |
381 |
} catch (InterruptedException success) {} |
382 |
assertFalse(Thread.interrupted()); |
383 |
|
384 |
pleaseInterrupt.countDown(); |
385 |
try { |
386 |
tu.timedJoin(s, LONG_DELAY_MS); |
387 |
shouldThrow(); |
388 |
} catch (InterruptedException success) {} |
389 |
assertFalse(Thread.interrupted()); |
390 |
}}); |
391 |
|
392 |
await(pleaseInterrupt); |
393 |
assertThreadStaysAlive(t); |
394 |
t.interrupt(); |
395 |
awaitTermination(t); |
396 |
s.interrupt(); |
397 |
awaitTermination(s); |
398 |
} |
399 |
|
400 |
/** |
401 |
* timedSleep throws InterruptedException when interrupted |
402 |
*/ |
403 |
public void testTimedSleep_Interruptible() { |
404 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
405 |
Thread t = newStartedThread(new CheckedRunnable() { |
406 |
public void realRun() throws InterruptedException { |
407 |
TimeUnit tu = MILLISECONDS; |
408 |
Thread.currentThread().interrupt(); |
409 |
try { |
410 |
tu.sleep(LONG_DELAY_MS); |
411 |
shouldThrow(); |
412 |
} catch (InterruptedException success) {} |
413 |
assertFalse(Thread.interrupted()); |
414 |
|
415 |
pleaseInterrupt.countDown(); |
416 |
try { |
417 |
tu.sleep(LONG_DELAY_MS); |
418 |
shouldThrow(); |
419 |
} catch (InterruptedException success) {} |
420 |
assertFalse(Thread.interrupted()); |
421 |
}}); |
422 |
|
423 |
await(pleaseInterrupt); |
424 |
assertThreadStaysAlive(t); |
425 |
t.interrupt(); |
426 |
awaitTermination(t); |
427 |
} |
428 |
|
429 |
/** |
430 |
* a deserialized serialized unit is the same instance |
431 |
*/ |
432 |
public void testSerialization() throws Exception { |
433 |
for (TimeUnit x : TimeUnit.values()) |
434 |
assertSame(x, serialClone(x)); |
435 |
} |
436 |
|
437 |
/** |
438 |
* tests for toChronoUnit. |
439 |
*/ |
440 |
public void testToChronoUnit() throws Exception { |
441 |
assertSame(ChronoUnit.NANOS, NANOSECONDS.toChronoUnit()); |
442 |
assertSame(ChronoUnit.MICROS, MICROSECONDS.toChronoUnit()); |
443 |
assertSame(ChronoUnit.MILLIS, MILLISECONDS.toChronoUnit()); |
444 |
assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit()); |
445 |
assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit()); |
446 |
assertSame(ChronoUnit.HOURS, HOURS.toChronoUnit()); |
447 |
assertSame(ChronoUnit.DAYS, DAYS.toChronoUnit()); |
448 |
|
449 |
// Every TimeUnit has a defined ChronoUnit equivalent |
450 |
for (TimeUnit x : TimeUnit.values()) |
451 |
assertSame(x, TimeUnit.of(x.toChronoUnit())); |
452 |
} |
453 |
|
454 |
/** |
455 |
* tests for TimeUnit.of(ChronoUnit). |
456 |
*/ |
457 |
public void testTimeUnitOf() throws Exception { |
458 |
assertSame(NANOSECONDS, TimeUnit.of(ChronoUnit.NANOS)); |
459 |
assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS)); |
460 |
assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS)); |
461 |
assertSame(SECONDS, TimeUnit.of(ChronoUnit.SECONDS)); |
462 |
assertSame(MINUTES, TimeUnit.of(ChronoUnit.MINUTES)); |
463 |
assertSame(HOURS, TimeUnit.of(ChronoUnit.HOURS)); |
464 |
assertSame(DAYS, TimeUnit.of(ChronoUnit.DAYS)); |
465 |
|
466 |
assertThrows(NullPointerException.class, |
467 |
() -> TimeUnit.of((ChronoUnit)null)); |
468 |
|
469 |
// ChronoUnits either round trip to their TimeUnit |
470 |
// equivalents, or throw IllegalArgumentException. |
471 |
for (ChronoUnit cu : ChronoUnit.values()) { |
472 |
final TimeUnit tu; |
473 |
try { |
474 |
tu = TimeUnit.of(cu); |
475 |
} catch (IllegalArgumentException acceptable) { |
476 |
continue; |
477 |
} |
478 |
assertSame(cu, tu.toChronoUnit()); |
479 |
} |
480 |
} |
481 |
|
482 |
} |