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.97 by jsr166, Fri Feb 1 19:07:36 2013 UTC vs.
Revision 1.113 by dl, Sun Sep 8 23:00:36 2013 UTC

# Line 128 | Line 128 | public class JSR166TestCase extends Test
128      private static final long profileThreshold =
129          Long.getLong("jsr166.profileThreshold", 100);
130  
131 +    /**
132 +     * The number of repetitions per test (for tickling rare bugs).
133 +     */
134 +    private static final int runsPerTest =
135 +        Integer.getInteger("jsr166.runsPerTest", 1);
136 +
137      protected void runTest() throws Throwable {
138 <        if (profileTests)
139 <            runTestProfiled();
140 <        else
141 <            super.runTest();
138 >        for (int i = 0; i < runsPerTest; i++) {
139 >            if (profileTests)
140 >                runTestProfiled();
141 >            else
142 >                super.runTest();
143 >        }
144      }
145  
146      protected void runTestProfiled() throws Throwable {
# Line 182 | Line 190 | public class JSR166TestCase extends Test
190          return suite;
191      }
192  
193 <    static void addTestReflectively(TestSuite suite, String testClassName) {
194 <        try {
195 <            Class klazz = Class.forName(testClassName);
196 <            Method m = klazz.getDeclaredMethod("suite", new Class<?>[0]);
197 <            suite.addTest(newTestSuite((Test)m.invoke(null)));
198 <        } catch (Exception e) {
199 <            throw new Error(e);
193 >    public static void addNamedTestClasses(TestSuite suite,
194 >                                           String... testClassNames) {
195 >        for (String testClassName : testClassNames) {
196 >            try {
197 >                Class<?> testClass = Class.forName(testClassName);
198 >                Method m = testClass.getDeclaredMethod("suite",
199 >                                                       new Class<?>[0]);
200 >                suite.addTest(newTestSuite((Test)m.invoke(null)));
201 >            } catch (Exception e) {
202 >                throw new Error("Missing test class", e);
203 >            }
204          }
205      }
206  
# Line 204 | Line 216 | public class JSR166TestCase extends Test
216          }
217      }
218  
219 <    public static boolean isAtLeastJdk6() { return JAVA_CLASS_VERSION >= 50.0; }
220 <    public static boolean isAtLeastJdk7() { return JAVA_CLASS_VERSION >= 51.0; }
221 <    public static boolean isAtLeastJdk8() { return JAVA_CLASS_VERSION >= 52.0; }
219 >    public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
220 >    public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
221 >    public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
222  
223      /**
224       * Collects all JSR166 unit tests as one suite.
225       */
226      public static Test suite() {
227 +        // Java7+ test classes
228          TestSuite suite = newTestSuite(
229              ForkJoinPoolTest.suite(),
230              ForkJoinTaskTest.suite(),
# Line 277 | Line 290 | public class JSR166TestCase extends Test
290              TreeSetTest.suite(),
291              TreeSubMapTest.suite(),
292              TreeSubSetTest.suite());
293 <        if (isAtLeastJdk8()) {
294 <            addTestReflectively(suite, "StampedLockTest");
293 >
294 >        // Java8+ test classes
295 >        if (atLeastJava8()) {
296 >            String[] java8TestClassNames = {
297 >                "Atomic8Test",
298 >                "CompletableFutureTest",
299 >                "ConcurrentHashMap8Test",
300 >                "CountedCompleterTest",
301 >                "DoubleAccumulatorTest",
302 >                "DoubleAdderTest",
303 >                "ForkJoinPool8Test",
304 >                "ForkJoinTask8Test",
305 >                "LongAccumulatorTest",
306 >                "LongAdderTest",
307 >                "SplittableRandomTest",
308 >                "StampedLockTest",
309 >                "ThreadLocalRandom8Test",
310 >            };
311 >            addNamedTestClasses(suite, java8TestClassNames);
312          }
313 +
314          return suite;
315      }
316  
317 +    // Delays for timing-dependent tests, in milliseconds.
318  
319      public static long SHORT_DELAY_MS;
320      public static long SMALL_DELAY_MS;
321      public static long MEDIUM_DELAY_MS;
322      public static long LONG_DELAY_MS;
323  
292
324      /**
325       * Returns the shortest timed delay. This could
326       * be reimplemented to use for example a Property.
# Line 372 | Line 403 | public class JSR166TestCase extends Test
403  
404          if (Thread.interrupted())
405              throw new AssertionFailedError("interrupt status set in main thread");
406 +
407 +        checkForkJoinPoolThreadLeaks();
408 +    }
409 +
410 +    /**
411 +     * Find missing try { ... } finally { joinPool(e); }
412 +     */
413 +    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
414 +        Thread[] survivors = new Thread[5];
415 +        int count = Thread.enumerate(survivors);
416 +        for (int i = 0; i < count; i++) {
417 +            Thread thread = survivors[i];
418 +            String name = thread.getName();
419 +            if (name.startsWith("ForkJoinPool-")) {
420 +                // give thread some time to terminate
421 +                thread.join(LONG_DELAY_MS);
422 +                if (!thread.isAlive()) continue;
423 +                thread.stop();
424 +                throw new AssertionFailedError
425 +                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
426 +                                   toString(), name));
427 +            }
428 +        }
429      }
430  
431      /**
# Line 659 | Line 713 | public class JSR166TestCase extends Test
713      public static final Integer m6  = new Integer(-6);
714      public static final Integer m10 = new Integer(-10);
715  
662
716      /**
717       * Runs Runnable r with a security policy that permits precisely
718       * the specified permissions.  If there is no current security
# Line 1190 | Line 1243 | public class JSR166TestCase extends Test
1243      public abstract class CheckedRecursiveAction extends RecursiveAction {
1244          protected abstract void realCompute() throws Throwable;
1245  
1246 <        public final void compute() {
1246 >        @Override protected final void compute() {
1247              try {
1248                  realCompute();
1249              } catch (Throwable t) {
# Line 1205 | Line 1258 | public class JSR166TestCase extends Test
1258      public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
1259          protected abstract T realCompute() throws Throwable;
1260  
1261 <        public final T compute() {
1261 >        @Override protected final T compute() {
1262              try {
1263                  return realCompute();
1264              } catch (Throwable t) {
# Line 1306 | Line 1359 | public class JSR166TestCase extends Test
1359              return null;
1360          }
1361      }
1362 +
1363 +    public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
1364 +                             Runnable... throwingActions) {
1365 +        for (Runnable throwingAction : throwingActions) {
1366 +            boolean threw = false;
1367 +            try { throwingAction.run(); }
1368 +            catch (Throwable t) {
1369 +                threw = true;
1370 +                if (!expectedExceptionClass.isInstance(t)) {
1371 +                    AssertionFailedError afe =
1372 +                        new AssertionFailedError
1373 +                        ("Expected " + expectedExceptionClass.getName() +
1374 +                         ", got " + t.getClass().getName());
1375 +                    afe.initCause(t);
1376 +                    threadUnexpectedException(afe);
1377 +                }
1378 +            }
1379 +            if (!threw)
1380 +                shouldThrow(expectedExceptionClass.getName());
1381 +        }
1382 +    }
1383   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines