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 |
|
/** |
492 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
493 |
|
public void realRun() throws InterruptedException { |
494 |
|
Object o = new Object(); |
488 |
– |
TimeUnit tu = MILLISECONDS; |
489 |
– |
|
495 |
|
try { |
496 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
496 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
497 |
|
threadShouldThrow(); |
498 |
|
} catch (IllegalMonitorStateException success) {} |
499 |
|
}}); |
509 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
510 |
|
public void realRun() throws InterruptedException { |
511 |
|
Object o = new Object(); |
507 |
– |
TimeUnit tu = MILLISECONDS; |
512 |
|
|
513 |
|
Thread.currentThread().interrupt(); |
514 |
|
try { |
515 |
|
synchronized (o) { |
516 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
516 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
517 |
|
} |
518 |
|
shouldThrow(); |
519 |
|
} catch (InterruptedException success) {} |
522 |
|
pleaseInterrupt.countDown(); |
523 |
|
try { |
524 |
|
synchronized (o) { |
525 |
< |
tu.timedWait(o, LONG_DELAY_MS); |
525 |
> |
MILLISECONDS.timedWait(o, LONGER_DELAY_MS); |
526 |
|
} |
527 |
|
shouldThrow(); |
528 |
|
} catch (InterruptedException success) {} |
530 |
|
}}); |
531 |
|
|
532 |
|
await(pleaseInterrupt); |
533 |
< |
assertThreadStaysAlive(t); |
533 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
534 |
|
t.interrupt(); |
535 |
|
awaitTermination(t); |
536 |
|
} |
542 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
543 |
|
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
544 |
|
public void realRun() throws InterruptedException { |
545 |
< |
Thread.sleep(LONG_DELAY_MS); |
545 |
> |
Thread.sleep(LONGER_DELAY_MS); |
546 |
|
}}); |
547 |
|
final Thread t = newStartedThread(new CheckedRunnable() { |
548 |
|
public void realRun() throws InterruptedException { |
545 |
– |
TimeUnit tu = MILLISECONDS; |
549 |
|
Thread.currentThread().interrupt(); |
550 |
|
try { |
551 |
< |
tu.timedJoin(s, LONG_DELAY_MS); |
551 |
> |
MILLISECONDS.timedJoin(s, LONGER_DELAY_MS); |
552 |
|
shouldThrow(); |
553 |
|
} catch (InterruptedException success) {} |
554 |
|
assertFalse(Thread.interrupted()); |
555 |
|
|
556 |
|
pleaseInterrupt.countDown(); |
557 |
|
try { |
558 |
< |
tu.timedJoin(s, LONG_DELAY_MS); |
558 |
> |
MILLISECONDS.timedJoin(s, LONGER_DELAY_MS); |
559 |
|
shouldThrow(); |
560 |
|
} catch (InterruptedException success) {} |
561 |
|
assertFalse(Thread.interrupted()); |
562 |
|
}}); |
563 |
|
|
564 |
|
await(pleaseInterrupt); |
565 |
< |
assertThreadStaysAlive(t); |
565 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
566 |
|
t.interrupt(); |
567 |
|
awaitTermination(t); |
568 |
|
s.interrupt(); |
570 |
|
} |
571 |
|
|
572 |
|
/** |
573 |
< |
* timedSleep throws InterruptedException when interrupted |
573 |
> |
* timeUnit.sleep throws InterruptedException when interrupted |
574 |
|
*/ |
575 |
|
public void testTimedSleep_Interruptible() { |
576 |
|
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
577 |
|
Thread t = newStartedThread(new CheckedRunnable() { |
578 |
|
public void realRun() throws InterruptedException { |
576 |
– |
TimeUnit tu = MILLISECONDS; |
579 |
|
Thread.currentThread().interrupt(); |
580 |
|
try { |
581 |
< |
tu.sleep(LONG_DELAY_MS); |
581 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
582 |
|
shouldThrow(); |
583 |
|
} catch (InterruptedException success) {} |
584 |
|
assertFalse(Thread.interrupted()); |
585 |
|
|
586 |
|
pleaseInterrupt.countDown(); |
587 |
|
try { |
588 |
< |
tu.sleep(LONG_DELAY_MS); |
588 |
> |
MILLISECONDS.sleep(LONGER_DELAY_MS); |
589 |
|
shouldThrow(); |
590 |
|
} catch (InterruptedException success) {} |
591 |
|
assertFalse(Thread.interrupted()); |
592 |
|
}}); |
593 |
|
|
594 |
|
await(pleaseInterrupt); |
595 |
< |
assertThreadStaysAlive(t); |
595 |
> |
if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
596 |
|
t.interrupt(); |
597 |
|
awaitTermination(t); |
598 |
|
} |
599 |
|
|
600 |
|
/** |
601 |
< |
* a deserialized serialized unit is the same instance |
601 |
> |
* timeUnit.sleep(x) for x <= 0 does not sleep at all. |
602 |
> |
*/ |
603 |
> |
public void testTimedSleep_nonPositive() throws InterruptedException { |
604 |
> |
boolean interrupt = randomBoolean(); |
605 |
> |
if (interrupt) Thread.currentThread().interrupt(); |
606 |
> |
randomTimeUnit().sleep(0L); |
607 |
> |
randomTimeUnit().sleep(-1L); |
608 |
> |
randomTimeUnit().sleep(Long.MIN_VALUE); |
609 |
> |
if (interrupt) assertTrue(Thread.interrupted()); |
610 |
> |
} |
611 |
> |
|
612 |
> |
/** |
613 |
> |
* a deserialized/reserialized unit is the same instance |
614 |
|
*/ |
615 |
|
public void testSerialization() throws Exception { |
616 |
|
for (TimeUnit x : TimeUnit.values()) |