ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TimeUnitTest.java (file contents):
Revision 1.29 by jsr166, Fri Mar 25 04:53:28 2016 UTC vs.
Revision 1.34 by jsr166, Wed Sep 4 22:18:03 2019 UTC

# Line 14 | Line 14 | import static java.util.concurrent.TimeU
14   import static java.util.concurrent.TimeUnit.NANOSECONDS;
15   import static java.util.concurrent.TimeUnit.SECONDS;
16  
17 import java.time.temporal.ChronoUnit;
17   import java.util.concurrent.CountDownLatch;
18   import java.util.concurrent.TimeUnit;
19  
# Line 468 | Line 467 | public class TimeUnitTest extends JSR166
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      /**
# Line 485 | Line 491 | public class TimeUnitTest extends JSR166
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();
489                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);
# Line 504 | Line 512 | public class TimeUnitTest extends JSR166
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();
508                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) {}
# Line 519 | Line 527 | public class TimeUnitTest extends JSR166
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 <        assertThreadStaysAlive(t);
540 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
541          t.interrupt();
542          awaitTermination(t);
543      }
# Line 543 | Line 553 | public class TimeUnitTest extends JSR166
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 <        assertThreadStaysAlive(t);
576 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
577          t.interrupt();
578          awaitTermination(t);
579          s.interrupt();
# Line 568 | Line 581 | public class TimeUnitTest extends JSR166
581      }
582  
583      /**
584 <     * timedSleep throws InterruptedException when interrupted
584 >     * timeUnit.sleep throws InterruptedException when interrupted
585       */
586      public void testTimedSleep_Interruptible() {
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 <        assertThreadStaysAlive(t);
610 >        if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
611          t.interrupt();
612          awaitTermination(t);
613      }
614  
615      /**
616 <     * a deserialized serialized unit is the same instance
616 >     * timeUnit.sleep(x) for x <= 0 does not sleep at all.
617       */
618 <    public void testSerialization() throws Exception {
619 <        for (TimeUnit x : TimeUnit.values())
620 <            assertSame(x, serialClone(x));
618 >    public void testTimedSleep_nonPositive() throws InterruptedException {
619 >        long startTime = System.nanoTime();
620 >        boolean interrupt = randomBoolean();
621 >        if (interrupt) Thread.currentThread().interrupt();
622 >        randomTimeUnit().sleep(0L);
623 >        randomTimeUnit().sleep(-1L);
624 >        randomTimeUnit().sleep(Long.MIN_VALUE);
625 >        if (interrupt) assertTrue(Thread.interrupted());
626      }
627  
628      /**
629 <     * tests for toChronoUnit.
629 >     * a deserialized/reserialized unit is the same instance
630       */
631 <    public void testToChronoUnit() throws Exception {
611 <        assertSame(ChronoUnit.NANOS,   NANOSECONDS.toChronoUnit());
612 <        assertSame(ChronoUnit.MICROS,  MICROSECONDS.toChronoUnit());
613 <        assertSame(ChronoUnit.MILLIS,  MILLISECONDS.toChronoUnit());
614 <        assertSame(ChronoUnit.SECONDS, SECONDS.toChronoUnit());
615 <        assertSame(ChronoUnit.MINUTES, MINUTES.toChronoUnit());
616 <        assertSame(ChronoUnit.HOURS,   HOURS.toChronoUnit());
617 <        assertSame(ChronoUnit.DAYS,    DAYS.toChronoUnit());
618 <
619 <        // Every TimeUnit has a defined ChronoUnit equivalent
631 >    public void testSerialization() throws Exception {
632          for (TimeUnit x : TimeUnit.values())
633 <            assertSame(x, TimeUnit.of(x.toChronoUnit()));
622 <    }
623 <
624 <    /**
625 <     * tests for TimeUnit.of(ChronoUnit).
626 <     */
627 <    public void testTimeUnitOf() throws Exception {
628 <        assertSame(NANOSECONDS,  TimeUnit.of(ChronoUnit.NANOS));
629 <        assertSame(MICROSECONDS, TimeUnit.of(ChronoUnit.MICROS));
630 <        assertSame(MILLISECONDS, TimeUnit.of(ChronoUnit.MILLIS));
631 <        assertSame(SECONDS,      TimeUnit.of(ChronoUnit.SECONDS));
632 <        assertSame(MINUTES,      TimeUnit.of(ChronoUnit.MINUTES));
633 <        assertSame(HOURS,        TimeUnit.of(ChronoUnit.HOURS));
634 <        assertSame(DAYS,         TimeUnit.of(ChronoUnit.DAYS));
635 <
636 <        assertThrows(NullPointerException.class,
637 <                     () -> TimeUnit.of((ChronoUnit)null));
638 <
639 <        // ChronoUnits either round trip to their TimeUnit
640 <        // equivalents, or throw IllegalArgumentException.
641 <        for (ChronoUnit cu : ChronoUnit.values()) {
642 <            final TimeUnit tu;
643 <            try {
644 <                tu = TimeUnit.of(cu);
645 <            } catch (IllegalArgumentException acceptable) {
646 <                continue;
647 <            }
648 <            assertSame(cu, tu.toChronoUnit());
649 <        }
633 >            assertSame(x, serialClone(x));
634      }
635  
636   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines