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.96 by jsr166, Mon Jan 21 19:51:46 2013 UTC vs.
Revision 1.112 by jsr166, Fri Aug 16 07:07:01 2013 UTC

# Line 13 | Line 13 | 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 127 | 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 181 | Line 190 | public class JSR166TestCase extends Test
190          return suite;
191      }
192  
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 +
207 +    public static final double JAVA_CLASS_VERSION;
208 +    static {
209 +        try {
210 +            JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged(
211 +                new java.security.PrivilegedAction<Double>() {
212 +                public Double run() {
213 +                    return Double.valueOf(System.getProperty("java.class.version"));}});
214 +        } catch (Throwable t) {
215 +            throw new Error(t);
216 +        }
217 +    }
218 +
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 <        return newTestSuite(
227 >        // Java7+ test classes
228 >        TestSuite suite = newTestSuite(
229              ForkJoinPoolTest.suite(),
230              ForkJoinTaskTest.suite(),
231              RecursiveActionTest.suite(),
# Line 250 | Line 290 | public class JSR166TestCase extends Test
290              TreeSetTest.suite(),
291              TreeSubMapTest.suite(),
292              TreeSubSetTest.suite());
293 +
294 +        // Java8+ test classes
295 +        if (atLeastJava8()) {
296 +            String[] java8TestClassNames = {
297 +                "CompletableFutureTest",
298 +                "ConcurrentHashMap8Test",
299 +                "CountedCompleterTest",
300 +                "DoubleAccumulatorTest",
301 +                "DoubleAdderTest",
302 +                "ForkJoinPool8Test",
303 +                "ForkJoinTask8Test",
304 +                "LongAccumulatorTest",
305 +                "LongAdderTest",
306 +                "SplittableRandomTest",
307 +                "StampedLockTest",
308 +                "ThreadLocalRandom8Test",
309 +            };
310 +            addNamedTestClasses(suite, java8TestClassNames);
311 +        }
312 +
313 +        return suite;
314      }
315  
316 +    // Delays for timing-dependent tests, in milliseconds.
317  
318      public static long SHORT_DELAY_MS;
319      public static long SMALL_DELAY_MS;
320      public static long MEDIUM_DELAY_MS;
321      public static long LONG_DELAY_MS;
322  
261
323      /**
324       * Returns the shortest timed delay. This could
325       * be reimplemented to use for example a Property.
# Line 341 | Line 402 | public class JSR166TestCase extends Test
402  
403          if (Thread.interrupted())
404              throw new AssertionFailedError("interrupt status set in main thread");
405 +
406 +        checkForkJoinPoolThreadLeaks();
407 +    }
408 +
409 +    /**
410 +     * Find missing try { ... } finally { joinPool(e); }
411 +     */
412 +    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
413 +        Thread[] survivors = new Thread[5];
414 +        int count = Thread.enumerate(survivors);
415 +        for (int i = 0; i < count; i++) {
416 +            Thread thread = survivors[i];
417 +            String name = thread.getName();
418 +            if (name.startsWith("ForkJoinPool-")) {
419 +                // give thread some time to terminate
420 +                thread.join(LONG_DELAY_MS);
421 +                if (!thread.isAlive()) continue;
422 +                thread.stop();
423 +                throw new AssertionFailedError
424 +                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
425 +                                   toString(), name));
426 +            }
427 +        }
428      }
429  
430      /**
# Line 628 | Line 712 | public class JSR166TestCase extends Test
712      public static final Integer m6  = new Integer(-6);
713      public static final Integer m10 = new Integer(-10);
714  
631
715      /**
716       * Runs Runnable r with a security policy that permits precisely
717       * the specified permissions.  If there is no current security
# Line 1159 | Line 1242 | public class JSR166TestCase extends Test
1242      public abstract class CheckedRecursiveAction extends RecursiveAction {
1243          protected abstract void realCompute() throws Throwable;
1244  
1245 <        public final void compute() {
1245 >        @Override protected final void compute() {
1246              try {
1247                  realCompute();
1248              } catch (Throwable t) {
# Line 1174 | Line 1257 | public class JSR166TestCase extends Test
1257      public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
1258          protected abstract T realCompute() throws Throwable;
1259  
1260 <        public final T compute() {
1260 >        @Override protected final T compute() {
1261              try {
1262                  return realCompute();
1263              } catch (Throwable t) {
# Line 1275 | Line 1358 | public class JSR166TestCase extends Test
1358              return null;
1359          }
1360      }
1361 +
1362 +    public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
1363 +                             Runnable... throwingActions) {
1364 +        for (Runnable throwingAction : throwingActions) {
1365 +            boolean threw = false;
1366 +            try { throwingAction.run(); }
1367 +            catch (Throwable t) {
1368 +                threw = true;
1369 +                if (!expectedExceptionClass.isInstance(t)) {
1370 +                    AssertionFailedError afe =
1371 +                        new AssertionFailedError
1372 +                        ("Expected " + expectedExceptionClass.getName() +
1373 +                         ", got " + t.getClass().getName());
1374 +                    afe.initCause(t);
1375 +                    threadUnexpectedException(afe);
1376 +                }
1377 +            }
1378 +            if (!threw)
1379 +                shouldThrow(expectedExceptionClass.getName());
1380 +        }
1381 +    }
1382   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines