467 |
|
* toString returns name of unit |
468 |
|
*/ |
469 |
|
public void testToString() { |
470 |
+ |
assertEquals("NANOSECONDS", NANOSECONDS.toString()); |
471 |
+ |
assertEquals("MICROSECONDS", MICROSECONDS.toString()); |
472 |
+ |
assertEquals("MILLISECONDS", MILLISECONDS.toString()); |
473 |
|
assertEquals("SECONDS", SECONDS.toString()); |
474 |
+ |
assertEquals("MINUTES", MINUTES.toString()); |
475 |
+ |
assertEquals("HOURS", HOURS.toString()); |
476 |
+ |
assertEquals("DAYS", DAYS.toString()); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
|
* name returns name of unit |
481 |
|
*/ |
482 |
|
public void testName() { |
483 |
< |
assertEquals("SECONDS", SECONDS.name()); |
483 |
> |
for (TimeUnit x : TimeUnit.values()) |
484 |
> |
assertEquals(x.toString(), x.name()); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
491 |
|
public void testTimedWait_IllegalMonitorException() { |
492 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
493 |
|
public void realRun() throws InterruptedException { |
494 |
+ |
long startTime = System.nanoTime(); |
495 |
|
Object o = new Object(); |
488 |
– |
TimeUnit tu = MILLISECONDS; |
496 |
|
|
497 |
|
try { |
498 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
498 |
> |
MILLISECONDS.timedWait(o, LONG_DELAY_MS); |
499 |
|
threadShouldThrow(); |
500 |
|
} catch (IllegalMonitorStateException success) {} |
501 |
+ |
|
502 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
503 |
|
}}); |
504 |
|
|
505 |
|
awaitTermination(t); |
512 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
513 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
514 |
|
public void realRun() throws InterruptedException { |
515 |
+ |
long startTime = System.nanoTime(); |
516 |
|
Object o = new Object(); |
507 |
– |
TimeUnit tu = MILLISECONDS; |
517 |
|
|
518 |
|
Thread.currentThread().interrupt(); |
519 |
|
try { |
520 |
|
synchronized (o) { |
521 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
521 |
> |
MILLISECONDS.timedWait(o, LONG_DELAY_MS); |
522 |
|
} |
523 |
|
shouldThrow(); |
524 |
|
} catch (InterruptedException success) {} |
527 |
|
pleaseInterrupt.countDown(); |
528 |
|
try { |
529 |
|
synchronized (o) { |
530 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
530 |
> |
MILLISECONDS.timedWait(o, LONG_DELAY_MS); |
531 |
|
} |
532 |
|
shouldThrow(); |
533 |
|
} catch (InterruptedException success) {} |
534 |
|
assertFalse(Thread.interrupted()); |
535 |
+ |
|
536 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
537 |
|
}}); |
538 |
|
|
539 |
|
await(pleaseInterrupt); |
540 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
540 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
541 |
|
t.interrupt(); |
542 |
|
awaitTermination(t); |
543 |
|
} |
553 |
|
}}); |
554 |
|
final Thread t = newStartedThread(new CheckedRunnable() { |
555 |
|
public void realRun() throws InterruptedException { |
556 |
< |
TimeUnit tu = MILLISECONDS; |
556 |
> |
long startTime = System.nanoTime(); |
557 |
> |
|
558 |
|
Thread.currentThread().interrupt(); |
559 |
|
try { |
560 |
< |
tu.timedJoin(s, LONG_DELAY_MS); |
560 |
> |
MILLISECONDS.timedJoin(s, LONG_DELAY_MS); |
561 |
|
shouldThrow(); |
562 |
|
} catch (InterruptedException success) {} |
563 |
|
assertFalse(Thread.interrupted()); |
564 |
|
|
565 |
|
pleaseInterrupt.countDown(); |
566 |
|
try { |
567 |
< |
tu.timedJoin(s, LONG_DELAY_MS); |
567 |
> |
MILLISECONDS.timedJoin(s, LONG_DELAY_MS); |
568 |
|
shouldThrow(); |
569 |
|
} catch (InterruptedException success) {} |
570 |
|
assertFalse(Thread.interrupted()); |
571 |
+ |
|
572 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
573 |
|
}}); |
574 |
|
|
575 |
|
await(pleaseInterrupt); |
576 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
576 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
577 |
|
t.interrupt(); |
578 |
|
awaitTermination(t); |
579 |
|
s.interrupt(); |
587 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
588 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
589 |
|
public void realRun() throws InterruptedException { |
590 |
< |
TimeUnit tu = MILLISECONDS; |
590 |
> |
long startTime = System.nanoTime(); |
591 |
> |
|
592 |
|
Thread.currentThread().interrupt(); |
593 |
|
try { |
594 |
< |
tu.sleep(LONG_DELAY_MS); |
594 |
> |
MILLISECONDS.sleep(LONG_DELAY_MS); |
595 |
|
shouldThrow(); |
596 |
|
} catch (InterruptedException success) {} |
597 |
|
assertFalse(Thread.interrupted()); |
598 |
|
|
599 |
|
pleaseInterrupt.countDown(); |
600 |
|
try { |
601 |
< |
tu.sleep(LONG_DELAY_MS); |
601 |
> |
MILLISECONDS.sleep(LONG_DELAY_MS); |
602 |
|
shouldThrow(); |
603 |
|
} catch (InterruptedException success) {} |
604 |
|
assertFalse(Thread.interrupted()); |
605 |
+ |
|
606 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
607 |
|
}}); |
608 |
|
|
609 |
|
await(pleaseInterrupt); |
610 |
< |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
610 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
611 |
|
t.interrupt(); |
612 |
|
awaitTermination(t); |
613 |
|
} |