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