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.93 by jsr166, Sun Dec 16 17:22:42 2012 UTC vs.
Revision 1.102 by jsr166, Wed Feb 6 19:55:06 2013 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 145 | Line 148 | public class JSR166TestCase extends Test
148      }
149  
150      /**
151 <     * Runs all JSR166 unit tests using junit.textui.TestRunner
151 >     * Runs all JSR166 unit tests using junit.textui.TestRunner.
152 >     * Optional command line arg provides the number of iterations to
153 >     * repeat running the tests.
154       */
155      public static void main(String[] args) {
156          if (useSecurityManager) {
# Line 177 | Line 182 | public class JSR166TestCase extends Test
182          return suite;
183      }
184  
185 +    public static void addNamedTestClasses(TestSuite suite,
186 +                                           String... testClassNames) {
187 +        for (String testClassName : testClassNames) {
188 +            try {
189 +                Class<?> testClass = Class.forName(testClassName);
190 +                Method m = testClass.getDeclaredMethod("suite",
191 +                                                       new Class<?>[0]);
192 +                suite.addTest(newTestSuite((Test)m.invoke(null)));
193 +            } catch (Exception e) {
194 +                throw new Error("Missing test class", e);
195 +            }
196 +        }
197 +    }
198 +
199 +    public static final double JAVA_CLASS_VERSION;
200 +    static {
201 +        try {
202 +            JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged(
203 +                new java.security.PrivilegedAction<Double>() {
204 +                public Double run() {
205 +                    return Double.valueOf(System.getProperty("java.class.version"));}});
206 +        } catch (Throwable t) {
207 +            throw new Error(t);
208 +        }
209 +    }
210 +
211 +    public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
212 +    public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
213 +    public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
214 +
215      /**
216       * Collects all JSR166 unit tests as one suite.
217       */
218      public static Test suite() {
219 <        return newTestSuite(
219 >        // Java7+ test classes
220 >        TestSuite suite = newTestSuite(
221              ForkJoinPoolTest.suite(),
222              ForkJoinTaskTest.suite(),
223              RecursiveActionTest.suite(),
# Line 246 | Line 282 | public class JSR166TestCase extends Test
282              TreeSetTest.suite(),
283              TreeSubMapTest.suite(),
284              TreeSubSetTest.suite());
285 +
286 +        // Java8+ test classes
287 +        if (atLeastJava8()) {
288 +            String[] java8TestClassNames = {
289 +                "CompletableFutureTest",
290 +                "ForkJoinPool8Test",
291 +                "StampedLockTest",
292 +            };
293 +            addNamedTestClasses(suite, java8TestClassNames);
294 +        }
295 +
296 +        return suite;
297      }
298  
299  
# Line 337 | Line 385 | public class JSR166TestCase extends Test
385  
386          if (Thread.interrupted())
387              throw new AssertionFailedError("interrupt status set in main thread");
388 +
389 +        checkForkJoinPoolThreadLeaks();
390 +    }
391 +
392 +    /**
393 +     * Find missing try { ... } finally { joinPool(e); }
394 +     */
395 +    void checkForkJoinPoolThreadLeaks() throws InterruptedException {
396 +        Thread[] survivors = new Thread[5];
397 +        int count = Thread.enumerate(survivors);
398 +        for (int i = 0; i < count; i++) {
399 +            Thread thread = survivors[i];
400 +            String name = thread.getName();
401 +            if (name.startsWith("ForkJoinPool-")) {
402 +                // give thread some time to terminate
403 +                thread.join(LONG_DELAY_MS);
404 +                if (!thread.isAlive()) continue;
405 +                thread.stop();
406 +                throw new AssertionFailedError
407 +                    (String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n",
408 +                                   toString(), name));
409 +            }
410 +        }
411      }
412  
413      /**
# Line 510 | Line 581 | public class JSR166TestCase extends Test
581      }
582  
583      /**
584 +     * A debugging tool to print all stack traces, as jstack does.
585 +     */
586 +    static void printAllStackTraces() {
587 +        for (ThreadInfo info :
588 +                 ManagementFactory.getThreadMXBean()
589 +                 .dumpAllThreads(true, true))
590 +            System.err.print(info);
591 +    }
592 +
593 +    /**
594       * Checks that thread does not terminate within the default
595       * millisecond delay of {@code timeoutMillis()}.
596       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines