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.94 by jsr166, Mon Jan 21 19:32:19 2013 UTC vs.
Revision 1.115 by jsr166, Wed May 14 21:06:38 2014 UTC

# Line 11 | Line 11 | import java.io.ByteArrayInputStream;
11   import java.io.ByteArrayOutputStream;
12   import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14 + import java.lang.management.ManagementFactory;
15 + import java.lang.management.ThreadInfo;
16 + import java.lang.reflect.Method;
17   import java.util.ArrayList;
18   import java.util.Arrays;
19   import java.util.Date;
# Line 23 | Line 26 | import java.util.concurrent.atomic.Atomi
26   import java.util.concurrent.atomic.AtomicReference;
27   import static java.util.concurrent.TimeUnit.MILLISECONDS;
28   import static java.util.concurrent.TimeUnit.NANOSECONDS;
29 + import java.util.regex.Pattern;
30   import java.security.CodeSource;
31   import java.security.Permission;
32   import java.security.PermissionCollection;
# Line 125 | Line 129 | public class JSR166TestCase extends Test
129      private static final long profileThreshold =
130          Long.getLong("jsr166.profileThreshold", 100);
131  
132 +    /**
133 +     * The number of repetitions per test (for tickling rare bugs).
134 +     */
135 +    private static final int runsPerTest =
136 +        Integer.getInteger("jsr166.runsPerTest", 1);
137 +
138 +    /**
139 +     * A filter for tests to run, matching strings of the form
140 +     * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
141 +     * Usefully combined with jsr166.runsPerTest.
142 +     */
143 +    private static final Pattern methodFilter = methodFilter();
144 +
145 +    private static Pattern methodFilter() {
146 +        String regex = System.getProperty("jsr166.methodFilter");
147 +        return (regex == null) ? null : Pattern.compile(regex);
148 +    }
149 +
150      protected void runTest() throws Throwable {
151 <        if (profileTests)
152 <            runTestProfiled();
153 <        else
154 <            super.runTest();
151 >        if (methodFilter == null
152 >            || methodFilter.matcher(toString()).find()) {
153 >            for (int i = 0; i < runsPerTest; i++) {
154 >                if (profileTests)
155 >                    runTestProfiled();
156 >                else
157 >                    super.runTest();
158 >            }
159 >        }
160      }
161  
162      protected void runTestProfiled() throws Throwable {
# Line 179 | Line 206 | public class JSR166TestCase extends Test
206          return suite;
207      }
208  
209 +    public static void addNamedTestClasses(TestSuite suite,
210 +                                           String... testClassNames) {
211 +        for (String testClassName : testClassNames) {
212 +            try {
213 +                Class<?> testClass = Class.forName(testClassName);
214 +                Method m = testClass.getDeclaredMethod("suite",
215 +                                                       new Class<?>[0]);
216 +                suite.addTest(newTestSuite((Test)m.invoke(null)));
217 +            } catch (Exception e) {
218 +                throw new Error("Missing test class", e);
219 +            }
220 +        }
221 +    }
222 +
223 +    public static final double JAVA_CLASS_VERSION;
224 +    public static final String JAVA_SPECIFICATION_VERSION;
225 +    static {
226 +        try {
227 +            JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged(
228 +                new java.security.PrivilegedAction<Double>() {
229 +                public Double run() {
230 +                    return Double.valueOf(System.getProperty("java.class.version"));}});
231 +            JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged(
232 +                new java.security.PrivilegedAction<String>() {
233 +                public String run() {
234 +                    return System.getProperty("java.specification.version");}});
235 +        } catch (Throwable t) {
236 +            throw new Error(t);
237 +        }
238 +    }
239 +
240 +    public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
241 +    public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
242 +    public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
243 +    public static boolean atLeastJava9() {
244 +        // As of 2014-05, java9 still uses 52.0 class file version
245 +        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
246 +    }
247 +
248      /**
249       * Collects all JSR166 unit tests as one suite.
250       */
251      public static Test suite() {
252 <        return newTestSuite(
252 >        // Java7+ test classes
253 >        TestSuite suite = newTestSuite(
254              ForkJoinPoolTest.suite(),
255              ForkJoinTaskTest.suite(),
256              RecursiveActionTest.suite(),
# Line 248 | Line 315 | public class JSR166TestCase extends Test
315              TreeSetTest.suite(),
316              TreeSubMapTest.suite(),
317              TreeSubSetTest.suite());
318 +
319 +        // Java8+ test classes
320 +        if (atLeastJava8()) {
321 +            String[] java8TestClassNames = {
322 +                "Atomic8Test",
323 +                "CompletableFutureTest",
324 +                "ConcurrentHashMap8Test",
325 +                "CountedCompleterTest",
326 +                "DoubleAccumulatorTest",
327 +                "DoubleAdderTest",
328 +                "ForkJoinPool8Test",
329 +                "ForkJoinTask8Test",
330 +                "LongAccumulatorTest",
331 +                "LongAdderTest",
332 +                "SplittableRandomTest",
333 +                "StampedLockTest",
334 +                "ThreadLocalRandom8Test",
335 +            };
336 +            addNamedTestClasses(suite, java8TestClassNames);
337 +        }
338 +
339 +        // Java9+ test classes
340 +        if (atLeastJava9()) {
341 +            String[] java9TestClassNames = {
342 +                "ThreadPoolExecutor9Test",
343 +            };
344 +            addNamedTestClasses(suite, java9TestClassNames);
345 +        }
346 +
347 +        return suite;
348      }
349  
350 +    // Delays for timing-dependent tests, in milliseconds.
351  
352      public static long SHORT_DELAY_MS;
353      public static long SMALL_DELAY_MS;
354      public static long MEDIUM_DELAY_MS;
355      public static long LONG_DELAY_MS;
356  
259
357      /**
358       * Returns the shortest timed delay. This could
359       * be reimplemented to use for example a Property.
# Line 339 | Line 436 | public class JSR166TestCase extends Test
436  
437          if (Thread.interrupted())
438              throw new AssertionFailedError("interrupt status set in main thread");
439 +
440 +        checkForkJoinPoolThreadLeaks();
441 +    }
442 +
443 +    /**
444 +     * Find missing try { ... } finally { joinPool(e); }
445 +     */
446 +    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
447 +        Thread[] survivors = new Thread[5];
448 +        int count = Thread.enumerate(survivors);
449 +        for (int i = 0; i < count; i++) {
450 +            Thread thread = survivors[i];
451 +            String name = thread.getName();
452 +            if (name.startsWith("ForkJoinPool-")) {
453 +                // give thread some time to terminate
454 +                thread.join(LONG_DELAY_MS);
455 +                if (!thread.isAlive()) continue;
456 +                thread.stop();
457 +                throw new AssertionFailedError
458 +                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
459 +                                   toString(), name));
460 +            }
461 +        }
462      }
463  
464      /**
# Line 512 | Line 632 | public class JSR166TestCase extends Test
632      }
633  
634      /**
635 +     * A debugging tool to print all stack traces, as jstack does.
636 +     */
637 +    static void printAllStackTraces() {
638 +        for (ThreadInfo info :
639 +                 ManagementFactory.getThreadMXBean()
640 +                 .dumpAllThreads(true, true))
641 +            System.err.print(info);
642 +    }
643 +
644 +    /**
645       * Checks that thread does not terminate within the default
646       * millisecond delay of {@code timeoutMillis()}.
647       */
# Line 616 | Line 746 | public class JSR166TestCase extends Test
746      public static final Integer m6  = new Integer(-6);
747      public static final Integer m10 = new Integer(-10);
748  
619
749      /**
750       * Runs Runnable r with a security policy that permits precisely
751       * the specified permissions.  If there is no current security
# Line 1147 | Line 1276 | public class JSR166TestCase extends Test
1276      public abstract class CheckedRecursiveAction extends RecursiveAction {
1277          protected abstract void realCompute() throws Throwable;
1278  
1279 <        public final void compute() {
1279 >        @Override protected final void compute() {
1280              try {
1281                  realCompute();
1282              } catch (Throwable t) {
# Line 1162 | Line 1291 | public class JSR166TestCase extends Test
1291      public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
1292          protected abstract T realCompute() throws Throwable;
1293  
1294 <        public final T compute() {
1294 >        @Override protected final T compute() {
1295              try {
1296                  return realCompute();
1297              } catch (Throwable t) {
# Line 1263 | Line 1392 | public class JSR166TestCase extends Test
1392              return null;
1393          }
1394      }
1395 +
1396 +    public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
1397 +                             Runnable... throwingActions) {
1398 +        for (Runnable throwingAction : throwingActions) {
1399 +            boolean threw = false;
1400 +            try { throwingAction.run(); }
1401 +            catch (Throwable t) {
1402 +                threw = true;
1403 +                if (!expectedExceptionClass.isInstance(t)) {
1404 +                    AssertionFailedError afe =
1405 +                        new AssertionFailedError
1406 +                        ("Expected " + expectedExceptionClass.getName() +
1407 +                         ", got " + t.getClass().getName());
1408 +                    afe.initCause(t);
1409 +                    threadUnexpectedException(afe);
1410 +                }
1411 +            }
1412 +            if (!threw)
1413 +                shouldThrow(expectedExceptionClass.getName());
1414 +        }
1415 +    }
1416   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines