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.107 by jsr166, Sun Apr 21 06:19:58 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 >                "CompletableFutureTest",
298 >                "ConcurrentHashMap8Test",
299 >                "CountedCompleterTest",
300 >                "DoubleAccumulatorTest",
301 >                "DoubleAdderTest",
302 >                "ForkJoinPool8Test",
303 >                "LongAccumulatorTest",
304 >                "LongAdderTest",
305 >                "StampedLockTest",
306 >            };
307 >            addNamedTestClasses(suite, java8TestClassNames);
308          }
309 +
310          return suite;
311      }
312  
# Line 372 | Line 399 | public class JSR166TestCase extends Test
399  
400          if (Thread.interrupted())
401              throw new AssertionFailedError("interrupt status set in main thread");
402 +
403 +        checkForkJoinPoolThreadLeaks();
404 +    }
405 +
406 +    /**
407 +     * Find missing try { ... } finally { joinPool(e); }
408 +     */
409 +    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
410 +        Thread[] survivors = new Thread[5];
411 +        int count = Thread.enumerate(survivors);
412 +        for (int i = 0; i < count; i++) {
413 +            Thread thread = survivors[i];
414 +            String name = thread.getName();
415 +            if (name.startsWith("ForkJoinPool-")) {
416 +                // give thread some time to terminate
417 +                thread.join(LONG_DELAY_MS);
418 +                if (!thread.isAlive()) continue;
419 +                thread.stop();
420 +                throw new AssertionFailedError
421 +                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
422 +                                   toString(), name));
423 +            }
424 +        }
425      }
426  
427      /**
# Line 1306 | Line 1356 | public class JSR166TestCase extends Test
1356              return null;
1357          }
1358      }
1359 +
1360 +    public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
1361 +                             Runnable... throwingActions) {
1362 +        for (Runnable throwingAction : throwingActions) {
1363 +            boolean threw = false;
1364 +            try { throwingAction.run(); }
1365 +            catch (Throwable t) {
1366 +                threw = true;
1367 +                if (!expectedExceptionClass.isInstance(t)) {
1368 +                    AssertionFailedError afe =
1369 +                        new AssertionFailedError
1370 +                        ("Expected " + expectedExceptionClass.getName() +
1371 +                         ", got " + t.getClass().getName());
1372 +                    afe.initCause(t);
1373 +                    threadUnexpectedException(afe);
1374 +                }
1375 +            }
1376 +            if (!threw)
1377 +                shouldThrow(expectedExceptionClass.getName());
1378 +        }
1379 +    }
1380   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines