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.173 by jsr166, Fri Oct 9 16:24:12 2015 UTC vs.
Revision 1.192 by jsr166, Sun May 22 01:09:21 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 53 | Line 61 | import java.util.concurrent.Semaphore;
61   import java.util.concurrent.ThreadFactory;
62   import java.util.concurrent.ThreadPoolExecutor;
63   import java.util.concurrent.TimeoutException;
64 + import java.util.concurrent.atomic.AtomicBoolean;
65   import java.util.concurrent.atomic.AtomicReference;
66   import java.util.regex.Matcher;
67   import java.util.regex.Pattern;
# Line 112 | Line 121 | import junit.framework.TestSuite;
121   * methods as there are exceptions the method can throw. Sometimes
122   * there are multiple tests per JSR166 method when the different
123   * "normal" behaviors differ significantly. And sometimes testcases
124 < * cover multiple methods when they cannot be tested in
116 < * isolation.
124 > * cover multiple methods when they cannot be tested in isolation.
125   *
126   * <li>The documentation style for testcases is to provide as javadoc
127   * a simple sentence or two describing the property that the testcase
# Line 176 | Line 184 | public class JSR166TestCase extends Test
184      private static final int suiteRuns =
185          Integer.getInteger("jsr166.suiteRuns", 1);
186  
187 +    /**
188 +     * Returns the value of the system property, or NaN if not defined.
189 +     */
190 +    private static float systemPropertyValue(String name) {
191 +        String floatString = System.getProperty(name);
192 +        if (floatString == null)
193 +            return Float.NaN;
194 +        try {
195 +            return Float.parseFloat(floatString);
196 +        } catch (NumberFormatException ex) {
197 +            throw new IllegalArgumentException(
198 +                String.format("Bad float value in system property %s=%s",
199 +                              name, floatString));
200 +        }
201 +    }
202 +
203 +    /**
204 +     * The scaling factor to apply to standard delays used in tests.
205 +     * May be initialized from any of:
206 +     * - the "jsr166.delay.factor" system property
207 +     * - the "test.timeout.factor" system property (as used by jtreg)
208 +     *   See: http://openjdk.java.net/jtreg/tag-spec.html
209 +     * - hard-coded fuzz factor when using a known slowpoke VM
210 +     */
211 +    private static final float delayFactor = delayFactor();
212 +
213 +    private static float delayFactor() {
214 +        float x;
215 +        if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor")))
216 +            return x;
217 +        if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor")))
218 +            return x;
219 +        String prop = System.getProperty("java.vm.version");
220 +        if (prop != null && prop.matches(".*debug.*"))
221 +            return 4.0f; // How much slower is fastdebug than product?!
222 +        return 1.0f;
223 +    }
224 +
225      public JSR166TestCase() { super(); }
226      public JSR166TestCase(String name) { super(name); }
227  
# Line 196 | Line 242 | public class JSR166TestCase extends Test
242      // static volatile int currentRun = 0;
243      static {
244          Runnable checkForWedgedTest = new Runnable() { public void run() {
245 <            // avoid spurious reports with enormous runsPerTest
246 <            final int timeoutMinutes = Math.max(runsPerTest / 10, 1);
245 >            // Avoid spurious reports with enormous runsPerTest.
246 >            // A single test case run should never take more than 1 second.
247 >            // But let's cap it at the high end too ...
248 >            final int timeoutMinutes =
249 >                Math.min(15, Math.max(runsPerTest / 60, 1));
250              for (TestCase lastTestCase = currentTestCase;;) {
251                  try { MINUTES.sleep(timeoutMinutes); }
252                  catch (InterruptedException unexpected) { break; }
# Line 208 | Line 257 | public class JSR166TestCase extends Test
257   //                     System.err.printf(
258   //                         "Looks like we're stuck running test: %s (%d/%d)%n",
259   //                         lastTestCase, currentRun, runsPerTest);
260 <                    System.err.println("availableProcessors=" +
261 <                        Runtime.getRuntime().availableProcessors());
262 <                    System.err.printf("cpu model = %s%n", cpuModel());
260 > //                     System.err.println("availableProcessors=" +
261 > //                         Runtime.getRuntime().availableProcessors());
262 > //                     System.err.printf("cpu model = %s%n", cpuModel());
263                      dumpTestThreads();
264                      // one stack dump is probably enough; more would be spam
265                      break;
# Line 222 | Line 271 | public class JSR166TestCase extends Test
271          thread.start();
272      }
273  
274 <    public static String cpuModel() {
275 <        try {
276 <            Matcher matcher = Pattern.compile("model name\\s*: (.*)")
277 <                .matcher(new String(
278 <                     Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
279 <            matcher.find();
280 <            return matcher.group(1);
281 <        } catch (Exception ex) { return null; }
282 <    }
274 > //     public static String cpuModel() {
275 > //         try {
276 > //             Matcher matcher = Pattern.compile("model name\\s*: (.*)")
277 > //                 .matcher(new String(
278 > //                      Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
279 > //             matcher.find();
280 > //             return matcher.group(1);
281 > //         } catch (Exception ex) { return null; }
282 > //     }
283  
284      public void runBare() throws Throwable {
285          currentTestCase = this;
# Line 270 | Line 319 | public class JSR166TestCase extends Test
319          main(suite(), args);
320      }
321  
322 +    static class PithyResultPrinter extends junit.textui.ResultPrinter {
323 +        PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
324 +        long runTime;
325 +        public void startTest(Test test) {}
326 +        protected void printHeader(long runTime) {
327 +            this.runTime = runTime; // defer printing for later
328 +        }
329 +        protected void printFooter(TestResult result) {
330 +            if (result.wasSuccessful()) {
331 +                getWriter().println("OK (" + result.runCount() + " tests)"
332 +                    + "  Time: " + elapsedTimeAsString(runTime));
333 +            } else {
334 +                getWriter().println("Time: " + elapsedTimeAsString(runTime));
335 +                super.printFooter(result);
336 +            }
337 +        }
338 +    }
339 +
340 +    /**
341 +     * Returns a TestRunner that doesn't bother with unnecessary
342 +     * fluff, like printing a "." for each test case.
343 +     */
344 +    static junit.textui.TestRunner newPithyTestRunner() {
345 +        junit.textui.TestRunner runner = new junit.textui.TestRunner();
346 +        runner.setPrinter(new PithyResultPrinter(System.out));
347 +        return runner;
348 +    }
349 +
350      /**
351       * Runs all unit tests in the given test suite.
352       * Actual behavior influenced by jsr166.* system properties.
# Line 281 | Line 358 | public class JSR166TestCase extends Test
358              System.setSecurityManager(new SecurityManager());
359          }
360          for (int i = 0; i < suiteRuns; i++) {
361 <            TestResult result = junit.textui.TestRunner.run(suite);
361 >            TestResult result = newPithyTestRunner().doRun(suite);
362              if (!result.wasSuccessful())
363                  System.exit(1);
364              System.gc();
# Line 434 | Line 511 | public class JSR166TestCase extends Test
511                  "StampedLockTest",
512                  "SubmissionPublisherTest",
513                  "ThreadLocalRandom8Test",
514 +                "TimeUnit8Test",
515              };
516              addNamedTestClasses(suite, java8TestClassNames);
517          }
# Line 441 | Line 519 | public class JSR166TestCase extends Test
519          // Java9+ test classes
520          if (atLeastJava9()) {
521              String[] java9TestClassNames = {
522 <                // Currently empty, but expecting varhandle tests
522 >                "ExecutorCompletionService9Test",
523              };
524              addNamedTestClasses(suite, java9TestClassNames);
525          }
# Line 518 | Line 596 | public class JSR166TestCase extends Test
596      public static long LONG_DELAY_MS;
597  
598      /**
599 <     * Returns the shortest timed delay. This could
600 <     * be reimplemented to use for example a Property.
599 >     * Returns the shortest timed delay. This can be scaled up for
600 >     * slow machines using the jsr166.delay.factor system property,
601 >     * or via jtreg's -timeoutFactor: flag.
602 >     * http://openjdk.java.net/jtreg/command-help.html
603       */
604      protected long getShortDelay() {
605 <        return 50;
605 >        return (long) (50 * delayFactor);
606      }
607  
608      /**
# Line 835 | Line 915 | public class JSR166TestCase extends Test
915          }};
916      }
917  
918 +    PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
919 +        return new PoolCleanerWithReleaser(pool, releaser(flag));
920 +    }
921 +
922 +    Runnable releaser(final AtomicBoolean flag) {
923 +        return new Runnable() { public void run() { flag.set(true); }};
924 +    }
925 +
926      /**
927       * Waits out termination of a thread pool or fails doing so.
928       */
# Line 1225 | Line 1313 | public class JSR166TestCase extends Test
1313          } finally {
1314              if (t.getState() != Thread.State.TERMINATED) {
1315                  t.interrupt();
1316 <                threadFail("Test timed out");
1316 >                threadFail("timed out waiting for thread to terminate");
1317              }
1318          }
1319      }
# Line 1391 | Line 1479 | public class JSR166TestCase extends Test
1479          return new LatchAwaiter(latch);
1480      }
1481  
1482 <    public void await(CountDownLatch latch) {
1482 >    public void await(CountDownLatch latch, long timeoutMillis) {
1483          try {
1484 <            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1484 >            if (!latch.await(timeoutMillis, MILLISECONDS))
1485 >                fail("timed out waiting for CountDownLatch for "
1486 >                     + (timeoutMillis/1000) + " sec");
1487          } catch (Throwable fail) {
1488              threadUnexpectedException(fail);
1489          }
1490      }
1491  
1492 +    public void await(CountDownLatch latch) {
1493 +        await(latch, LONG_DELAY_MS);
1494 +    }
1495 +
1496      public void await(Semaphore semaphore) {
1497          try {
1498 <            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1498 >            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1499 >                fail("timed out waiting for Semaphore for "
1500 >                     + (LONG_DELAY_MS/1000) + " sec");
1501          } catch (Throwable fail) {
1502              threadUnexpectedException(fail);
1503          }
# Line 1737 | Line 1833 | public class JSR166TestCase extends Test
1833          } catch (NoSuchElementException success) {}
1834          assertFalse(it.hasNext());
1835      }
1836 +
1837 +    public <T> Callable<T> callableThrowing(final Exception ex) {
1838 +        return new Callable<T>() { public T call() throws Exception { throw ex; }};
1839 +    }
1840 +
1841 +    public Runnable runnableThrowing(final RuntimeException ex) {
1842 +        return new Runnable() { public void run() { throw ex; }};
1843 +    }
1844   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines