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.178 by jsr166, Fri Oct 23 17:34:47 2015 UTC vs.
Revision 1.194 by jsr166, Mon May 23 18:42:17 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 + * @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=0 -Djsr166.testImplementationDetails=true JSR166TestCase
16 + */
17 +
18   import static java.util.concurrent.TimeUnit.MILLISECONDS;
19   import static java.util.concurrent.TimeUnit.MINUTES;
20   import static java.util.concurrent.TimeUnit.NANOSECONDS;
# Line 50 | Line 59 | import java.util.concurrent.RecursiveAct
59   import java.util.concurrent.RecursiveTask;
60   import java.util.concurrent.RejectedExecutionHandler;
61   import java.util.concurrent.Semaphore;
62 + import java.util.concurrent.SynchronousQueue;
63   import java.util.concurrent.ThreadFactory;
64   import java.util.concurrent.ThreadPoolExecutor;
65   import java.util.concurrent.TimeoutException;
66 + import java.util.concurrent.atomic.AtomicBoolean;
67   import java.util.concurrent.atomic.AtomicReference;
68   import java.util.regex.Matcher;
69   import java.util.regex.Pattern;
# Line 112 | Line 123 | import junit.framework.TestSuite;
123   * methods as there are exceptions the method can throw. Sometimes
124   * there are multiple tests per JSR166 method when the different
125   * "normal" behaviors differ significantly. And sometimes testcases
126 < * cover multiple methods when they cannot be tested in
116 < * isolation.
126 > * cover multiple methods when they cannot be tested in isolation.
127   *
128   * <li>The documentation style for testcases is to provide as javadoc
129   * a simple sentence or two describing the property that the testcase
# Line 176 | Line 186 | public class JSR166TestCase extends Test
186      private static final int suiteRuns =
187          Integer.getInteger("jsr166.suiteRuns", 1);
188  
189 +    /**
190 +     * Returns the value of the system property, or NaN if not defined.
191 +     */
192 +    private static float systemPropertyValue(String name) {
193 +        String floatString = System.getProperty(name);
194 +        if (floatString == null)
195 +            return Float.NaN;
196 +        try {
197 +            return Float.parseFloat(floatString);
198 +        } catch (NumberFormatException ex) {
199 +            throw new IllegalArgumentException(
200 +                String.format("Bad float value in system property %s=%s",
201 +                              name, floatString));
202 +        }
203 +    }
204 +
205 +    /**
206 +     * The scaling factor to apply to standard delays used in tests.
207 +     * May be initialized from any of:
208 +     * - the "jsr166.delay.factor" system property
209 +     * - the "test.timeout.factor" system property (as used by jtreg)
210 +     *   See: http://openjdk.java.net/jtreg/tag-spec.html
211 +     * - hard-coded fuzz factor when using a known slowpoke VM
212 +     */
213 +    private static final float delayFactor = delayFactor();
214 +
215 +    private static float delayFactor() {
216 +        float x;
217 +        if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor")))
218 +            return x;
219 +        if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor")))
220 +            return x;
221 +        String prop = System.getProperty("java.vm.version");
222 +        if (prop != null && prop.matches(".*debug.*"))
223 +            return 4.0f; // How much slower is fastdebug than product?!
224 +        return 1.0f;
225 +    }
226 +
227      public JSR166TestCase() { super(); }
228      public JSR166TestCase(String name) { super(name); }
229  
# Line 277 | Line 325 | public class JSR166TestCase extends Test
325          PithyResultPrinter(java.io.PrintStream writer) { super(writer); }
326          long runTime;
327          public void startTest(Test test) {}
328 <        protected void printHeader(long runTime) {
328 >        protected void printHeader(long runTime) {
329              this.runTime = runTime; // defer printing for later
330 <        }
331 <        protected void printFooter(TestResult result) {
330 >        }
331 >        protected void printFooter(TestResult result) {
332              if (result.wasSuccessful()) {
333                  getWriter().println("OK (" + result.runCount() + " tests)"
334                      + "  Time: " + elapsedTimeAsString(runTime));
# Line 465 | Line 513 | public class JSR166TestCase extends Test
513                  "StampedLockTest",
514                  "SubmissionPublisherTest",
515                  "ThreadLocalRandom8Test",
516 +                "TimeUnit8Test",
517              };
518              addNamedTestClasses(suite, java8TestClassNames);
519          }
# Line 472 | Line 521 | public class JSR166TestCase extends Test
521          // Java9+ test classes
522          if (atLeastJava9()) {
523              String[] java9TestClassNames = {
524 <                // Currently empty, but expecting varhandle tests
524 >                "ExecutorCompletionService9Test",
525              };
526              addNamedTestClasses(suite, java9TestClassNames);
527          }
# Line 549 | Line 598 | public class JSR166TestCase extends Test
598      public static long LONG_DELAY_MS;
599  
600      /**
601 <     * Returns the shortest timed delay. This could
602 <     * be reimplemented to use for example a Property.
601 >     * Returns the shortest timed delay. This can be scaled up for
602 >     * slow machines using the jsr166.delay.factor system property,
603 >     * or via jtreg's -timeoutFactor: flag.
604 >     * http://openjdk.java.net/jtreg/command-help.html
605       */
606      protected long getShortDelay() {
607 <        return 50;
607 >        return (long) (50 * delayFactor);
608      }
609  
610      /**
# Line 866 | Line 917 | public class JSR166TestCase extends Test
917          }};
918      }
919  
920 +    PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
921 +        return new PoolCleanerWithReleaser(pool, releaser(flag));
922 +    }
923 +
924 +    Runnable releaser(final AtomicBoolean flag) {
925 +        return new Runnable() { public void run() { flag.set(true); }};
926 +    }
927 +
928      /**
929       * Waits out termination of a thread pool or fails doing so.
930       */
# Line 1422 | Line 1481 | public class JSR166TestCase extends Test
1481          return new LatchAwaiter(latch);
1482      }
1483  
1484 <    public void await(CountDownLatch latch) {
1484 >    public void await(CountDownLatch latch, long timeoutMillis) {
1485          try {
1486 <            if (!latch.await(LONG_DELAY_MS, MILLISECONDS))
1486 >            if (!latch.await(timeoutMillis, MILLISECONDS))
1487                  fail("timed out waiting for CountDownLatch for "
1488 <                     + (LONG_DELAY_MS/1000) + " sec");
1488 >                     + (timeoutMillis/1000) + " sec");
1489          } catch (Throwable fail) {
1490              threadUnexpectedException(fail);
1491          }
1492      }
1493  
1494 +    public void await(CountDownLatch latch) {
1495 +        await(latch, LONG_DELAY_MS);
1496 +    }
1497 +
1498      public void await(Semaphore semaphore) {
1499          try {
1500              if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
# Line 1772 | Line 1835 | public class JSR166TestCase extends Test
1835          } catch (NoSuchElementException success) {}
1836          assertFalse(it.hasNext());
1837      }
1838 +
1839 +    public <T> Callable<T> callableThrowing(final Exception ex) {
1840 +        return new Callable<T>() { public T call() throws Exception { throw ex; }};
1841 +    }
1842 +
1843 +    public Runnable runnableThrowing(final RuntimeException ex) {
1844 +        return new Runnable() { public void run() { throw ex; }};
1845 +    }
1846 +
1847 +    /** A reusable thread pool to be shared by tests. */
1848 +    static final ExecutorService cachedThreadPool =
1849 +        new ThreadPoolExecutor(0, Integer.MAX_VALUE,
1850 +                               1000L, MILLISECONDS,
1851 +                               new SynchronousQueue<Runnable>());
1852 +
1853   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines