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.168 by jsr166, Mon Oct 5 22:53:25 2015 UTC vs.
Revision 1.184 by jsr166, Wed Feb 10 00:05:20 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 + /*
10 + * @test
11 + * @summary JSR-166 tck tests
12 + * @modules java.management
13 + * @build *
14 + * @run junit/othervm/timeout=1000 -Djsr166.testImplementationDetails=true JSR166TestCase
15 + */
16 +
17   import static java.util.concurrent.TimeUnit.MILLISECONDS;
18   import static java.util.concurrent.TimeUnit.MINUTES;
19   import static java.util.concurrent.TimeUnit.NANOSECONDS;
# Line 20 | Line 28 | import java.lang.management.ThreadMXBean
28   import java.lang.reflect.Constructor;
29   import java.lang.reflect.Method;
30   import java.lang.reflect.Modifier;
31 + import java.nio.file.Files;
32 + import java.nio.file.Paths;
33   import java.security.CodeSource;
34   import java.security.Permission;
35   import java.security.PermissionCollection;
# Line 52 | Line 62 | import java.util.concurrent.ThreadFactor
62   import java.util.concurrent.ThreadPoolExecutor;
63   import java.util.concurrent.TimeoutException;
64   import java.util.concurrent.atomic.AtomicReference;
65 + import java.util.regex.Matcher;
66   import java.util.regex.Pattern;
67  
68   import junit.framework.AssertionFailedError;
# Line 109 | Line 120 | import junit.framework.TestSuite;
120   * methods as there are exceptions the method can throw. Sometimes
121   * there are multiple tests per JSR166 method when the different
122   * "normal" behaviors differ significantly. And sometimes testcases
123 < * cover multiple methods when they cannot be tested in
113 < * isolation.
123 > * cover multiple methods when they cannot be tested in isolation.
124   *
125   * <li>The documentation style for testcases is to provide as javadoc
126   * a simple sentence or two describing the property that the testcase
# Line 173 | Line 183 | public class JSR166TestCase extends Test
183      private static final int suiteRuns =
184          Integer.getInteger("jsr166.suiteRuns", 1);
185  
186 +    /**
187 +     * The scaling factor to apply to standard delays used in tests.
188 +     */
189 +    private static final int delayFactor =
190 +        Integer.getInteger("jsr166.delay.factor", 1);
191 +
192      public JSR166TestCase() { super(); }
193      public JSR166TestCase(String name) { super(name); }
194  
# Line 188 | Line 204 | public class JSR166TestCase extends Test
204          return (regex == null) ? null : Pattern.compile(regex);
205      }
206  
207 +    // Instrumentation to debug very rare, but very annoying hung test runs.
208      static volatile TestCase currentTestCase;
209 +    // static volatile int currentRun = 0;
210      static {
211          Runnable checkForWedgedTest = new Runnable() { public void run() {
212 <            // avoid spurious reports with enormous runsPerTest
213 <            final int timeoutMinutes = Math.max(runsPerTest / 10, 1);
212 >            // Avoid spurious reports with enormous runsPerTest.
213 >            // A single test case run should never take more than 1 second.
214 >            // But let's cap it at the high end too ...
215 >            final int timeoutMinutes =
216 >                Math.min(15, Math.max(runsPerTest / 60, 1));
217              for (TestCase lastTestCase = currentTestCase;;) {
218                  try { MINUTES.sleep(timeoutMinutes); }
219                  catch (InterruptedException unexpected) { break; }
220                  if (lastTestCase == currentTestCase) {
221 <                    System.err.println
222 <                        ("Looks like we're stuck running test: "
223 <                         + lastTestCase);
221 >                    System.err.printf(
222 >                        "Looks like we're stuck running test: %s%n",
223 >                        lastTestCase);
224 > //                     System.err.printf(
225 > //                         "Looks like we're stuck running test: %s (%d/%d)%n",
226 > //                         lastTestCase, currentRun, runsPerTest);
227 > //                     System.err.println("availableProcessors=" +
228 > //                         Runtime.getRuntime().availableProcessors());
229 > //                     System.err.printf("cpu model = %s%n", cpuModel());
230                      dumpTestThreads();
231                      // one stack dump is probably enough; more would be spam
232                      break;
# Line 211 | Line 238 | public class JSR166TestCase extends Test
238          thread.start();
239      }
240  
241 + //     public static String cpuModel() {
242 + //         try {
243 + //             Matcher matcher = Pattern.compile("model name\\s*: (.*)")
244 + //                 .matcher(new String(
245 + //                      Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
246 + //             matcher.find();
247 + //             return matcher.group(1);
248 + //         } catch (Exception ex) { return null; }
249 + //     }
250 +
251      public void runBare() throws Throwable {
252          currentTestCase = this;
253          if (methodFilter == null
# Line 220 | Line 257 | public class JSR166TestCase extends Test
257  
258      protected void runTest() throws Throwable {
259          for (int i = 0; i < runsPerTest; i++) {
260 +            // currentRun = i;
261              if (profileTests)
262                  runTestProfiled();
263              else
# Line 248 | Line 286 | public class JSR166TestCase extends Test
286          main(suite(), args);
287      }
288  
289 +    static class PithyResultPrinter extends junit.textui.ResultPrinter {
290 +        PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
291 +        long runTime;
292 +        public void startTest(Test test) {}
293 +        protected void printHeader(long runTime) {
294 +            this.runTime = runTime; // defer printing for later
295 +        }
296 +        protected void printFooter(TestResult result) {
297 +            if (result.wasSuccessful()) {
298 +                getWriter().println("OK (" + result.runCount() + " tests)"
299 +                    + "  Time: " + elapsedTimeAsString(runTime));
300 +            } else {
301 +                getWriter().println("Time: " + elapsedTimeAsString(runTime));
302 +                super.printFooter(result);
303 +            }
304 +        }
305 +    }
306 +
307 +    /**
308 +     * Returns a TestRunner that doesn't bother with unnecessary
309 +     * fluff, like printing a "." for each test case.
310 +     */
311 +    static junit.textui.TestRunner newPithyTestRunner() {
312 +        junit.textui.TestRunner runner = new junit.textui.TestRunner();
313 +        runner.setPrinter(new PithyResultPrinter(System.out));
314 +        return runner;
315 +    }
316 +
317      /**
318       * Runs all unit tests in the given test suite.
319       * Actual behavior influenced by jsr166.* system properties.
# Line 259 | Line 325 | public class JSR166TestCase extends Test
325              System.setSecurityManager(new SecurityManager());
326          }
327          for (int i = 0; i < suiteRuns; i++) {
328 <            TestResult result = junit.textui.TestRunner.run(suite);
328 >            TestResult result = newPithyTestRunner().doRun(suite);
329              if (!result.wasSuccessful())
330                  System.exit(1);
331              System.gc();
# Line 496 | Line 562 | public class JSR166TestCase extends Test
562      public static long LONG_DELAY_MS;
563  
564      /**
565 <     * Returns the shortest timed delay. This could
566 <     * be reimplemented to use for example a Property.
565 >     * Returns the shortest timed delay. This can be scaled up for
566 >     * slow machines using the jsr166.delay.factor system property.
567       */
568      protected long getShortDelay() {
569 <        return 50;
569 >        return 50 * delayFactor;
570      }
571  
572      /**
# Line 1203 | Line 1269 | public class JSR166TestCase extends Test
1269          } finally {
1270              if (t.getState() != Thread.State.TERMINATED) {
1271                  t.interrupt();
1272 <                threadFail("Test timed out");
1272 >                threadFail("timed out waiting for thread to terminate");
1273              }
1274          }
1275      }
# Line 1371 | Line 1437 | public class JSR166TestCase extends Test
1437  
1438      public void await(CountDownLatch latch) {
1439          try {
1440 <            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1440 >            if (!latch.await(LONG_DELAY_MS, MILLISECONDS))
1441 >                fail("timed out waiting for CountDownLatch for "
1442 >                     + (LONG_DELAY_MS/1000) + " sec");
1443          } catch (Throwable fail) {
1444              threadUnexpectedException(fail);
1445          }
# Line 1379 | Line 1447 | public class JSR166TestCase extends Test
1447  
1448      public void await(Semaphore semaphore) {
1449          try {
1450 <            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1450 >            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1451 >                fail("timed out waiting for Semaphore for "
1452 >                     + (LONG_DELAY_MS/1000) + " sec");
1453          } catch (Throwable fail) {
1454              threadUnexpectedException(fail);
1455          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines