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.223 by jsr166, Sat May 13 19:13:09 2017 UTC vs.
Revision 1.233 by jsr166, Sat Jul 15 23:15:21 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 1063 | Line 1091 | public class JSR166TestCase extends Test
1091      }
1092  
1093      /**
1094 +     * Checks that thread eventually enters the expected blocked thread state.
1095 +     */
1096 +    void assertThreadBlocks(Thread thread, Thread.State expected) {
1097 +        // always sleep at least 1 ms, with high probability avoiding
1098 +        // transitory states
1099 +        for (long retries = LONG_DELAY_MS * 3 / 4; retries-->0; ) {
1100 +            try { delay(1); }
1101 +            catch (InterruptedException fail) {
1102 +                fail("Unexpected InterruptedException");
1103 +            }
1104 +            Thread.State s = thread.getState();
1105 +            if (s == expected)
1106 +                return;
1107 +            else if (s == Thread.State.TERMINATED)
1108 +                fail("Unexpected thread termination");
1109 +        }
1110 +        fail("timed out waiting for thread to enter thread state " + expected);
1111 +    }
1112 +
1113 +    /**
1114       * Checks that thread does not terminate within the default
1115       * millisecond delay of {@code timeoutMillis()}.
1116 +     * TODO: REMOVEME
1117       */
1118      void assertThreadStaysAlive(Thread thread) {
1119          assertThreadStaysAlive(thread, timeoutMillis());
# Line 1072 | Line 1121 | public class JSR166TestCase extends Test
1121  
1122      /**
1123       * Checks that thread does not terminate within the given millisecond delay.
1124 +     * TODO: REMOVEME
1125       */
1126      void assertThreadStaysAlive(Thread thread, long millis) {
1127          try {
# Line 1086 | Line 1136 | public class JSR166TestCase extends Test
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);
# Line 1093 | Line 1144 | public class JSR166TestCase extends Test
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 {
# Line 1617 | Line 1669 | public class JSR166TestCase extends Test
1669          }
1670      }
1671  
1672 +    public void await(CyclicBarrier barrier) {
1673 +        try {
1674 +            barrier.await(LONG_DELAY_MS, MILLISECONDS);
1675 +        } catch (Throwable fail) {
1676 +            threadUnexpectedException(fail);
1677 +        }
1678 +    }
1679 +
1680   //     /**
1681   //      * Spin-waits up to LONG_DELAY_MS until flag becomes true.
1682   //      */
# Line 1640 | Line 1700 | public class JSR166TestCase extends Test
1700          public String call() { throw new NullPointerException(); }
1701      }
1702  
1643    public static class CallableOne implements Callable<Integer> {
1644        public Integer call() { return one; }
1645    }
1646
1647    public class ShortRunnable extends CheckedRunnable {
1648        protected void realRun() throws Throwable {
1649            delay(SHORT_DELAY_MS);
1650        }
1651    }
1652
1653    public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
1654        protected void realRun() throws InterruptedException {
1655            delay(SHORT_DELAY_MS);
1656        }
1657    }
1658
1659    public class SmallRunnable extends CheckedRunnable {
1660        protected void realRun() throws Throwable {
1661            delay(SMALL_DELAY_MS);
1662        }
1663    }
1664
1703      public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
1704          protected void realRun() {
1705              try {
# Line 1670 | Line 1708 | public class JSR166TestCase extends Test
1708          }
1709      }
1710  
1673    public class SmallCallable extends CheckedCallable {
1674        protected Object realCall() throws InterruptedException {
1675            delay(SMALL_DELAY_MS);
1676            return Boolean.TRUE;
1677        }
1678    }
1679
1680    public class MediumRunnable extends CheckedRunnable {
1681        protected void realRun() throws Throwable {
1682            delay(MEDIUM_DELAY_MS);
1683        }
1684    }
1685
1686    public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
1687        protected void realRun() throws InterruptedException {
1688            delay(MEDIUM_DELAY_MS);
1689        }
1690    }
1691
1711      public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
1712          return new CheckedRunnable() {
1713              protected void realRun() {
# Line 1698 | Line 1717 | public class JSR166TestCase extends Test
1717              }};
1718      }
1719  
1701    public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
1702        protected void realRun() {
1703            try {
1704                delay(MEDIUM_DELAY_MS);
1705            } catch (InterruptedException ok) {}
1706        }
1707    }
1708
1709    public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
1710        protected void realRun() {
1711            try {
1712                delay(LONG_DELAY_MS);
1713            } catch (InterruptedException ok) {}
1714        }
1715    }
1716
1720      /**
1721       * For use as ThreadFactory in constructors
1722       */
# Line 1727 | Line 1730 | public class JSR166TestCase extends Test
1730          boolean isDone();
1731      }
1732  
1730    public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
1731        return new TrackedRunnable() {
1732                private volatile boolean done = false;
1733                public boolean isDone() { return done; }
1734                public void run() {
1735                    try {
1736                        delay(timeoutMillis);
1737                        done = true;
1738                    } catch (InterruptedException ok) {}
1739                }
1740            };
1741    }
1742
1743    public static class TrackedShortRunnable implements Runnable {
1744        public volatile boolean done = false;
1745        public void run() {
1746            try {
1747                delay(SHORT_DELAY_MS);
1748                done = true;
1749            } catch (InterruptedException ok) {}
1750        }
1751    }
1752
1753    public static class TrackedSmallRunnable implements Runnable {
1754        public volatile boolean done = false;
1755        public void run() {
1756            try {
1757                delay(SMALL_DELAY_MS);
1758                done = true;
1759            } catch (InterruptedException ok) {}
1760        }
1761    }
1762
1763    public static class TrackedMediumRunnable implements Runnable {
1764        public volatile boolean done = false;
1765        public void run() {
1766            try {
1767                delay(MEDIUM_DELAY_MS);
1768                done = true;
1769            } catch (InterruptedException ok) {}
1770        }
1771    }
1772
1773    public static class TrackedLongRunnable implements Runnable {
1774        public volatile boolean done = false;
1775        public void run() {
1776            try {
1777                delay(LONG_DELAY_MS);
1778                done = true;
1779            } catch (InterruptedException ok) {}
1780        }
1781    }
1782
1733      public static class TrackedNoOpRunnable implements Runnable {
1734          public volatile boolean done = false;
1735          public void run() {
# Line 1787 | Line 1737 | public class JSR166TestCase extends Test
1737          }
1738      }
1739  
1790    public static class TrackedCallable implements Callable {
1791        public volatile boolean done = false;
1792        public Object call() {
1793            try {
1794                delay(SMALL_DELAY_MS);
1795                done = true;
1796            } catch (InterruptedException ok) {}
1797            return Boolean.TRUE;
1798        }
1799    }
1800
1740      /**
1741       * Analog of CheckedRunnable for RecursiveAction
1742       */
# Line 1864 | 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());
# Line 2012 | Line 1951 | public class JSR166TestCase extends Test
1951                                 1000L, MILLISECONDS,
1952                                 new SynchronousQueue<Runnable>());
1953  
1954 +    /**
1955 +     * Returns maximum number of tasks that can be submitted to given
1956 +     * pool (with bounded queue) before saturation (when submission
1957 +     * throws RejectedExecutionException).
1958 +     */
1959 +    static final int saturatedSize(ThreadPoolExecutor pool) {
1960 +        BlockingQueue<Runnable> q = pool.getQueue();
1961 +        return pool.getMaximumPoolSize() + q.size() + q.remainingCapacity();
1962 +    }
1963 +
1964      static <T> void shuffle(T[] array) {
1965          Collections.shuffle(Arrays.asList(array), ThreadLocalRandom.current());
1966      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines