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.179 by jsr166, Fri Oct 23 21:59:58 2015 UTC vs.
Revision 1.199 by jsr166, Sat Aug 6 16:24:05 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 + * @run junit/othervm/timeout=1000 -Djava.util.concurrent.ForkJoinPool.common.parallelism=1 -Djava.util.secureRandomSeed=true JSR166TestCase
17 + */
18 +
19   import static java.util.concurrent.TimeUnit.MILLISECONDS;
20   import static java.util.concurrent.TimeUnit.MINUTES;
21   import static java.util.concurrent.TimeUnit.NANOSECONDS;
# Line 50 | Line 60 | import java.util.concurrent.RecursiveAct
60   import java.util.concurrent.RecursiveTask;
61   import java.util.concurrent.RejectedExecutionHandler;
62   import java.util.concurrent.Semaphore;
63 + import java.util.concurrent.SynchronousQueue;
64   import java.util.concurrent.ThreadFactory;
65   import java.util.concurrent.ThreadPoolExecutor;
66   import java.util.concurrent.TimeoutException;
67 + import java.util.concurrent.atomic.AtomicBoolean;
68   import java.util.concurrent.atomic.AtomicReference;
69   import java.util.regex.Matcher;
70   import java.util.regex.Pattern;
# Line 112 | Line 124 | import junit.framework.TestSuite;
124   * methods as there are exceptions the method can throw. Sometimes
125   * there are multiple tests per JSR166 method when the different
126   * "normal" behaviors differ significantly. And sometimes testcases
127 < * cover multiple methods when they cannot be tested in
116 < * isolation.
127 > * cover multiple methods when they cannot be tested in isolation.
128   *
129   * <li>The documentation style for testcases is to provide as javadoc
130   * a simple sentence or two describing the property that the testcase
# Line 176 | Line 187 | public class JSR166TestCase extends Test
187      private static final int suiteRuns =
188          Integer.getInteger("jsr166.suiteRuns", 1);
189  
190 +    /**
191 +     * Returns the value of the system property, or NaN if not defined.
192 +     */
193 +    private static float systemPropertyValue(String name) {
194 +        String floatString = System.getProperty(name);
195 +        if (floatString == null)
196 +            return Float.NaN;
197 +        try {
198 +            return Float.parseFloat(floatString);
199 +        } catch (NumberFormatException ex) {
200 +            throw new IllegalArgumentException(
201 +                String.format("Bad float value in system property %s=%s",
202 +                              name, floatString));
203 +        }
204 +    }
205 +
206 +    /**
207 +     * The scaling factor to apply to standard delays used in tests.
208 +     * May be initialized from any of:
209 +     * - the "jsr166.delay.factor" system property
210 +     * - the "test.timeout.factor" system property (as used by jtreg)
211 +     *   See: http://openjdk.java.net/jtreg/tag-spec.html
212 +     * - hard-coded fuzz factor when using a known slowpoke VM
213 +     */
214 +    private static final float delayFactor = delayFactor();
215 +
216 +    private static float delayFactor() {
217 +        float x;
218 +        if (!Float.isNaN(x = systemPropertyValue("jsr166.delay.factor")))
219 +            return x;
220 +        if (!Float.isNaN(x = systemPropertyValue("test.timeout.factor")))
221 +            return x;
222 +        String prop = System.getProperty("java.vm.version");
223 +        if (prop != null && prop.matches(".*debug.*"))
224 +            return 4.0f; // How much slower is fastdebug than product?!
225 +        return 1.0f;
226 +    }
227 +
228      public JSR166TestCase() { super(); }
229      public JSR166TestCase(String name) { super(name); }
230  
# Line 465 | Line 514 | public class JSR166TestCase extends Test
514                  "StampedLockTest",
515                  "SubmissionPublisherTest",
516                  "ThreadLocalRandom8Test",
517 +                "TimeUnit8Test",
518              };
519              addNamedTestClasses(suite, java8TestClassNames);
520          }
# Line 472 | Line 522 | public class JSR166TestCase extends Test
522          // Java9+ test classes
523          if (atLeastJava9()) {
524              String[] java9TestClassNames = {
525 <                // Currently empty, but expecting varhandle tests
525 >                "AtomicBoolean9Test",
526 >                "AtomicInteger9Test",
527 >                "AtomicIntegerArray9Test",
528 >                "AtomicLong9Test",
529 >                "AtomicLongArray9Test",
530 >                "AtomicReference9Test",
531 >                "AtomicReferenceArray9Test",
532 >                "ExecutorCompletionService9Test",
533              };
534              addNamedTestClasses(suite, java9TestClassNames);
535          }
# Line 549 | Line 606 | public class JSR166TestCase extends Test
606      public static long LONG_DELAY_MS;
607  
608      /**
609 <     * Returns the shortest timed delay. This could
610 <     * be reimplemented to use for example a Property.
609 >     * Returns the shortest timed delay. This can be scaled up for
610 >     * slow machines using the jsr166.delay.factor system property,
611 >     * or via jtreg's -timeoutFactor: flag.
612 >     * http://openjdk.java.net/jtreg/command-help.html
613       */
614      protected long getShortDelay() {
615 <        return 50;
615 >        return (long) (50 * delayFactor);
616      }
617  
618      /**
# Line 866 | Line 925 | public class JSR166TestCase extends Test
925          }};
926      }
927  
928 +    PoolCleaner cleaner(ExecutorService pool, AtomicBoolean flag) {
929 +        return new PoolCleanerWithReleaser(pool, releaser(flag));
930 +    }
931 +
932 +    Runnable releaser(final AtomicBoolean flag) {
933 +        return new Runnable() { public void run() { flag.set(true); }};
934 +    }
935 +
936      /**
937       * Waits out termination of a thread pool or fails doing so.
938       */
# Line 889 | Line 956 | public class JSR166TestCase extends Test
956          }
957      }
958  
959 <    /** Like Runnable, but with the freedom to throw anything */
959 >    /**
960 >     * Like Runnable, but with the freedom to throw anything.
961 >     * junit folks had the same idea:
962 >     * http://junit.org/junit5/docs/snapshot/api/org/junit/gen5/api/Executable.html
963 >     */
964      interface Action { public void run() throws Throwable; }
965  
966      /**
# Line 920 | Line 991 | public class JSR166TestCase extends Test
991       * Uninteresting threads are filtered out.
992       */
993      static void dumpTestThreads() {
994 +        SecurityManager sm = System.getSecurityManager();
995 +        if (sm != null) {
996 +            try {
997 +                System.setSecurityManager(null);
998 +            } catch (SecurityException giveUp) {
999 +                return;
1000 +            }
1001 +        }
1002 +
1003          ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
1004          System.err.println("------ stacktrace dump start ------");
1005          for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
# Line 937 | Line 1017 | public class JSR166TestCase extends Test
1017              System.err.print(info);
1018          }
1019          System.err.println("------ stacktrace dump end ------");
1020 +
1021 +        if (sm != null) System.setSecurityManager(sm);
1022      }
1023  
1024      /**
# Line 1169 | Line 1251 | public class JSR166TestCase extends Test
1251       * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
1252       */
1253      void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
1254 <        long startTime = System.nanoTime();
1254 >        long startTime = 0L;
1255          for (;;) {
1256              Thread.State s = thread.getState();
1257              if (s == Thread.State.BLOCKED ||
# Line 1178 | Line 1260 | public class JSR166TestCase extends Test
1260                  return;
1261              else if (s == Thread.State.TERMINATED)
1262                  fail("Unexpected thread termination");
1263 +            else if (startTime == 0L)
1264 +                startTime = System.nanoTime();
1265              else if (millisElapsedSince(startTime) > timeoutMillis) {
1266                  threadAssertTrue(thread.isAlive());
1267                  return;
# Line 1422 | Line 1506 | public class JSR166TestCase extends Test
1506          return new LatchAwaiter(latch);
1507      }
1508  
1509 <    public void await(CountDownLatch latch) {
1509 >    public void await(CountDownLatch latch, long timeoutMillis) {
1510          try {
1511 <            if (!latch.await(LONG_DELAY_MS, MILLISECONDS))
1511 >            if (!latch.await(timeoutMillis, MILLISECONDS))
1512                  fail("timed out waiting for CountDownLatch for "
1513 <                     + (LONG_DELAY_MS/1000) + " sec");
1513 >                     + (timeoutMillis/1000) + " sec");
1514          } catch (Throwable fail) {
1515              threadUnexpectedException(fail);
1516          }
1517      }
1518  
1519 +    public void await(CountDownLatch latch) {
1520 +        await(latch, LONG_DELAY_MS);
1521 +    }
1522 +
1523      public void await(Semaphore semaphore) {
1524          try {
1525              if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
# Line 1772 | Line 1860 | public class JSR166TestCase extends Test
1860          } catch (NoSuchElementException success) {}
1861          assertFalse(it.hasNext());
1862      }
1863 +
1864 +    public <T> Callable<T> callableThrowing(final Exception ex) {
1865 +        return new Callable<T>() { public T call() throws Exception { throw ex; }};
1866 +    }
1867 +
1868 +    public Runnable runnableThrowing(final RuntimeException ex) {
1869 +        return new Runnable() { public void run() { throw ex; }};
1870 +    }
1871 +
1872 +    /** A reusable thread pool to be shared by tests. */
1873 +    static final ExecutorService cachedThreadPool =
1874 +        new ThreadPoolExecutor(0, Integer.MAX_VALUE,
1875 +                               1000L, MILLISECONDS,
1876 +                               new SynchronousQueue<Runnable>());
1877 +
1878   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines