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.95 by jsr166, Mon Jan 21 19:43:52 2013 UTC vs.
Revision 1.116 by jsr166, Sun Jun 8 00:15:50 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 {
163 +        // Warmup run, notably to trigger all needed classloading.
164 +        super.runTest();
165          long t0 = System.nanoTime();
166          try {
167              super.runTest();
168          } finally {
169 <            long elapsedMillis =
141 <                (System.nanoTime() - t0) / (1000L * 1000L);
169 >            long elapsedMillis = millisElapsedSince(t0);
170              if (elapsedMillis >= profileThreshold)
171                  System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
172          }
# Line 179 | Line 207 | public class JSR166TestCase extends Test
207          return suite;
208      }
209  
210 +    public static void addNamedTestClasses(TestSuite suite,
211 +                                           String... testClassNames) {
212 +        for (String testClassName : testClassNames) {
213 +            try {
214 +                Class<?> testClass = Class.forName(testClassName);
215 +                Method m = testClass.getDeclaredMethod("suite",
216 +                                                       new Class<?>[0]);
217 +                suite.addTest(newTestSuite((Test)m.invoke(null)));
218 +            } catch (Exception e) {
219 +                throw new Error("Missing test class", e);
220 +            }
221 +        }
222 +    }
223 +
224 +    public static final double JAVA_CLASS_VERSION;
225 +    public static final String JAVA_SPECIFICATION_VERSION;
226 +    static {
227 +        try {
228 +            JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged(
229 +                new java.security.PrivilegedAction<Double>() {
230 +                public Double run() {
231 +                    return Double.valueOf(System.getProperty("java.class.version"));}});
232 +            JAVA_SPECIFICATION_VERSION = java.security.AccessController.doPrivileged(
233 +                new java.security.PrivilegedAction<String>() {
234 +                public String run() {
235 +                    return System.getProperty("java.specification.version");}});
236 +        } catch (Throwable t) {
237 +            throw new Error(t);
238 +        }
239 +    }
240 +
241 +    public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
242 +    public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
243 +    public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
244 +    public static boolean atLeastJava9() {
245 +        // As of 2014-05, java9 still uses 52.0 class file version
246 +        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
247 +    }
248 +
249      /**
250       * Collects all JSR166 unit tests as one suite.
251       */
252      public static Test suite() {
253 <        return newTestSuite(
253 >        // Java7+ test classes
254 >        TestSuite suite = newTestSuite(
255              ForkJoinPoolTest.suite(),
256              ForkJoinTaskTest.suite(),
257              RecursiveActionTest.suite(),
# Line 248 | Line 316 | public class JSR166TestCase extends Test
316              TreeSetTest.suite(),
317              TreeSubMapTest.suite(),
318              TreeSubSetTest.suite());
319 +
320 +        // Java8+ test classes
321 +        if (atLeastJava8()) {
322 +            String[] java8TestClassNames = {
323 +                "Atomic8Test",
324 +                "CompletableFutureTest",
325 +                "ConcurrentHashMap8Test",
326 +                "CountedCompleterTest",
327 +                "DoubleAccumulatorTest",
328 +                "DoubleAdderTest",
329 +                "ForkJoinPool8Test",
330 +                "ForkJoinTask8Test",
331 +                "LongAccumulatorTest",
332 +                "LongAdderTest",
333 +                "SplittableRandomTest",
334 +                "StampedLockTest",
335 +                "ThreadLocalRandom8Test",
336 +            };
337 +            addNamedTestClasses(suite, java8TestClassNames);
338 +        }
339 +
340 +        // Java9+ test classes
341 +        if (atLeastJava9()) {
342 +            String[] java9TestClassNames = {
343 +                "ThreadPoolExecutor9Test",
344 +            };
345 +            addNamedTestClasses(suite, java9TestClassNames);
346 +        }
347 +
348 +        return suite;
349      }
350  
351 +    // Delays for timing-dependent tests, in milliseconds.
352  
353      public static long SHORT_DELAY_MS;
354      public static long SMALL_DELAY_MS;
355      public static long MEDIUM_DELAY_MS;
356      public static long LONG_DELAY_MS;
357  
259
358      /**
359       * Returns the shortest timed delay. This could
360       * be reimplemented to use for example a Property.
# Line 339 | Line 437 | public class JSR166TestCase extends Test
437  
438          if (Thread.interrupted())
439              throw new AssertionFailedError("interrupt status set in main thread");
440 +
441 +        checkForkJoinPoolThreadLeaks();
442 +    }
443 +
444 +    /**
445 +     * Find missing try { ... } finally { joinPool(e); }
446 +     */
447 +    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
448 +        Thread[] survivors = new Thread[5];
449 +        int count = Thread.enumerate(survivors);
450 +        for (int i = 0; i < count; i++) {
451 +            Thread thread = survivors[i];
452 +            String name = thread.getName();
453 +            if (name.startsWith("ForkJoinPool-")) {
454 +                // give thread some time to terminate
455 +                thread.join(LONG_DELAY_MS);
456 +                if (!thread.isAlive()) continue;
457 +                thread.stop();
458 +                throw new AssertionFailedError
459 +                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
460 +                                   toString(), name));
461 +            }
462 +        }
463      }
464  
465      /**
# Line 514 | Line 635 | public class JSR166TestCase extends Test
635      /**
636       * A debugging tool to print all stack traces, as jstack does.
637       */
638 <    void printAllStackTraces() {
639 <        System.err.println(
640 <            Arrays.toString(
641 <                java.lang.management.ManagementFactory.getThreadMXBean()
642 <                .dumpAllThreads(true, true)));
638 >    static void printAllStackTraces() {
639 >        for (ThreadInfo info :
640 >                 ManagementFactory.getThreadMXBean()
641 >                 .dumpAllThreads(true, true))
642 >            System.err.print(info);
643      }
644  
645      /**
# Line 626 | Line 747 | public class JSR166TestCase extends Test
747      public static final Integer m6  = new Integer(-6);
748      public static final Integer m10 = new Integer(-10);
749  
629
750      /**
751       * Runs Runnable r with a security policy that permits precisely
752       * the specified permissions.  If there is no current security
# Line 1157 | Line 1277 | public class JSR166TestCase extends Test
1277      public abstract class CheckedRecursiveAction extends RecursiveAction {
1278          protected abstract void realCompute() throws Throwable;
1279  
1280 <        public final void compute() {
1280 >        @Override protected final void compute() {
1281              try {
1282                  realCompute();
1283              } catch (Throwable t) {
# Line 1172 | Line 1292 | public class JSR166TestCase extends Test
1292      public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> {
1293          protected abstract T realCompute() throws Throwable;
1294  
1295 <        public final T compute() {
1295 >        @Override protected final T compute() {
1296              try {
1297                  return realCompute();
1298              } catch (Throwable t) {
# Line 1273 | Line 1393 | public class JSR166TestCase extends Test
1393              return null;
1394          }
1395      }
1396 +
1397 +    public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
1398 +                             Runnable... throwingActions) {
1399 +        for (Runnable throwingAction : throwingActions) {
1400 +            boolean threw = false;
1401 +            try { throwingAction.run(); }
1402 +            catch (Throwable t) {
1403 +                threw = true;
1404 +                if (!expectedExceptionClass.isInstance(t)) {
1405 +                    AssertionFailedError afe =
1406 +                        new AssertionFailedError
1407 +                        ("Expected " + expectedExceptionClass.getName() +
1408 +                         ", got " + t.getClass().getName());
1409 +                    afe.initCause(t);
1410 +                    threadUnexpectedException(afe);
1411 +                }
1412 +            }
1413 +            if (!threw)
1414 +                shouldThrow(expectedExceptionClass.getName());
1415 +        }
1416 +    }
1417   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines