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

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.230 by jsr166, Mon May 15 16:21:07 2017 UTC vs.
Revision 1.232 by jsr166, Mon May 29 19:15:02 2017 UTC

# Line 88 | Line 88 | import java.util.concurrent.SynchronousQ
88   import java.util.concurrent.ThreadFactory;
89   import java.util.concurrent.ThreadLocalRandom;
90   import java.util.concurrent.ThreadPoolExecutor;
91 + import java.util.concurrent.TimeUnit;
92   import java.util.concurrent.TimeoutException;
93   import java.util.concurrent.atomic.AtomicBoolean;
94   import java.util.concurrent.atomic.AtomicReference;
# Line 638 | Line 639 | public class JSR166TestCase extends Test
639      public static long MEDIUM_DELAY_MS;
640      public static long LONG_DELAY_MS;
641  
642 +    private static final long RANDOM_TIMEOUT;
643 +    private static final long RANDOM_EXPIRED_TIMEOUT;
644 +    private static final TimeUnit RANDOM_TIMEUNIT;
645 +    static {
646 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
647 +        long[] timeouts = { Long.MIN_VALUE, -1, 0, 1, Long.MAX_VALUE };
648 +        RANDOM_TIMEOUT = timeouts[rnd.nextInt(timeouts.length)];
649 +        RANDOM_EXPIRED_TIMEOUT = timeouts[rnd.nextInt(3)];
650 +        TimeUnit[] timeUnits = TimeUnit.values();
651 +        RANDOM_TIMEUNIT = timeUnits[rnd.nextInt(timeUnits.length)];
652 +    }
653 +
654 +    /**
655 +     * Returns a timeout for use when any value at all will do.
656 +     */
657 +    static long randomTimeout() { return RANDOM_TIMEOUT; }
658 +
659 +    /**
660 +     * Returns a timeout that means "no waiting", i.e. not positive.
661 +     */
662 +    static long randomExpiredTimeout() { return RANDOM_EXPIRED_TIMEOUT; }
663 +
664 +    /**
665 +     * Returns a random non-null TimeUnit.
666 +     */
667 +    static TimeUnit randomTimeUnit() { return RANDOM_TIMEUNIT; }
668 +
669      /**
670       * Returns the shortest timed delay. This can be scaled up for
671       * slow machines using the jsr166.delay.factor system property,
# Line 1106 | Line 1134 | public class JSR166TestCase extends Test
1134      }
1135  
1136      /**
1137 +     * Checks that the threads do not terminate within the default
1138 +     * millisecond delay of {@code timeoutMillis()}.
1139 +     * TODO: REMOVEME
1140 +     */
1141 +    void assertThreadsStayAlive(Thread... threads) {
1142 +        assertThreadsStayAlive(timeoutMillis(), threads);
1143 +    }
1144 +
1145 +    /**
1146 +     * Checks that the threads do not terminate within the given millisecond delay.
1147 +     * TODO: REMOVEME
1148 +     */
1149 +    void assertThreadsStayAlive(long millis, Thread... threads) {
1150 +        try {
1151 +            // No need to optimize the failing case via Thread.join.
1152 +            delay(millis);
1153 +            for (Thread thread : threads)
1154 +                assertTrue(thread.isAlive());
1155 +        } catch (InterruptedException fail) {
1156 +            threadFail("Unexpected InterruptedException");
1157 +        }
1158 +    }
1159 +
1160 +    /**
1161       * Checks that future.get times out, with the default timeout of
1162       * {@code timeoutMillis()}.
1163       */
# Line 1648 | Line 1700 | public class JSR166TestCase extends Test
1700          public String call() { throw new NullPointerException(); }
1701      }
1702  
1651    public static class CallableOne implements Callable<Integer> {
1652        public Integer call() { return one; }
1653    }
1654
1655    public class ShortRunnable extends CheckedRunnable {
1656        protected void realRun() throws Throwable {
1657            delay(SHORT_DELAY_MS);
1658        }
1659    }
1660
1661    public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
1662        protected void realRun() throws InterruptedException {
1663            delay(SHORT_DELAY_MS);
1664        }
1665    }
1666
1667    public class SmallRunnable extends CheckedRunnable {
1668        protected void realRun() throws Throwable {
1669            delay(SMALL_DELAY_MS);
1670        }
1671    }
1672
1703      public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
1704          protected void realRun() {
1705              try {
# Line 1678 | Line 1708 | public class JSR166TestCase extends Test
1708          }
1709      }
1710  
1681    public class SmallCallable extends CheckedCallable {
1682        protected Object realCall() throws InterruptedException {
1683            delay(SMALL_DELAY_MS);
1684            return Boolean.TRUE;
1685        }
1686    }
1687
1688    public class MediumRunnable extends CheckedRunnable {
1689        protected void realRun() throws Throwable {
1690            delay(MEDIUM_DELAY_MS);
1691        }
1692    }
1693
1694    public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
1695        protected void realRun() throws InterruptedException {
1696            delay(MEDIUM_DELAY_MS);
1697        }
1698    }
1699
1711      public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
1712          return new CheckedRunnable() {
1713              protected void realRun() {
# Line 1706 | Line 1717 | public class JSR166TestCase extends Test
1717              }};
1718      }
1719  
1709    public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
1710        protected void realRun() {
1711            try {
1712                delay(MEDIUM_DELAY_MS);
1713            } catch (InterruptedException ok) {}
1714        }
1715    }
1716
1717    public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
1718        protected void realRun() {
1719            try {
1720                delay(LONG_DELAY_MS);
1721            } catch (InterruptedException ok) {}
1722        }
1723    }
1724
1720      /**
1721       * For use as ThreadFactory in constructors
1722       */
# Line 1735 | Line 1730 | public class JSR166TestCase extends Test
1730          boolean isDone();
1731      }
1732  
1738    public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
1739        return new TrackedRunnable() {
1740                private volatile boolean done = false;
1741                public boolean isDone() { return done; }
1742                public void run() {
1743                    try {
1744                        delay(timeoutMillis);
1745                        done = true;
1746                    } catch (InterruptedException ok) {}
1747                }
1748            };
1749    }
1750
1751    public static class TrackedShortRunnable implements Runnable {
1752        public volatile boolean done = false;
1753        public void run() {
1754            try {
1755                delay(SHORT_DELAY_MS);
1756                done = true;
1757            } catch (InterruptedException ok) {}
1758        }
1759    }
1760
1761    public static class TrackedSmallRunnable implements Runnable {
1762        public volatile boolean done = false;
1763        public void run() {
1764            try {
1765                delay(SMALL_DELAY_MS);
1766                done = true;
1767            } catch (InterruptedException ok) {}
1768        }
1769    }
1770
1771    public static class TrackedMediumRunnable implements Runnable {
1772        public volatile boolean done = false;
1773        public void run() {
1774            try {
1775                delay(MEDIUM_DELAY_MS);
1776                done = true;
1777            } catch (InterruptedException ok) {}
1778        }
1779    }
1780
1781    public static class TrackedLongRunnable implements Runnable {
1782        public volatile boolean done = false;
1783        public void run() {
1784            try {
1785                delay(LONG_DELAY_MS);
1786                done = true;
1787            } catch (InterruptedException ok) {}
1788        }
1789    }
1790
1733      public static class TrackedNoOpRunnable implements Runnable {
1734          public volatile boolean done = false;
1735          public void run() {
# Line 1795 | Line 1737 | public class JSR166TestCase extends Test
1737          }
1738      }
1739  
1798    public static class TrackedCallable implements Callable {
1799        public volatile boolean done = false;
1800        public Object call() {
1801            try {
1802                delay(SMALL_DELAY_MS);
1803                done = true;
1804            } catch (InterruptedException ok) {}
1805            return Boolean.TRUE;
1806        }
1807    }
1808
1740      /**
1741       * Analog of CheckedRunnable for RecursiveAction
1742       */
# Line 1872 | Line 1803 | public class JSR166TestCase extends Test
1803              assertEquals(0, q.size());
1804              assertNull(q.peek());
1805              assertNull(q.poll());
1806 <            assertNull(q.poll(0, MILLISECONDS));
1806 >            assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
1807              assertEquals(q.toString(), "[]");
1808              assertTrue(Arrays.equals(q.toArray(), new Object[0]));
1809              assertFalse(q.iterator().hasNext());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines