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.256 by jsr166, Sun Aug 11 22:29:26 2019 UTC vs.
Revision 1.266 by jsr166, Sat Sep 7 17:40:05 2019 UTC

# Line 284 | Line 284 | public class JSR166TestCase extends Test
284      static volatile TestCase currentTestCase;
285      // static volatile int currentRun = 0;
286      static {
287 <        Runnable checkForWedgedTest = new Runnable() { public void run() {
287 >        Runnable wedgedTestDetector = new Runnable() { public void run() {
288              // Avoid spurious reports with enormous runsPerTest.
289              // A single test case run should never take more than 1 second.
290              // But let's cap it at the high end too ...
# Line 310 | Line 310 | public class JSR166TestCase extends Test
310                  }
311                  lastTestCase = currentTestCase;
312              }}};
313 <        Thread thread = new Thread(checkForWedgedTest, "checkForWedgedTest");
313 >        Thread thread = new Thread(wedgedTestDetector, "WedgedTestDetector");
314          thread.setDaemon(true);
315          thread.start();
316      }
# Line 657 | Line 657 | public class JSR166TestCase extends Test
657      public static long MEDIUM_DELAY_MS;
658      public static long LONG_DELAY_MS;
659  
660 +    /**
661 +     * A delay significantly longer than LONG_DELAY_MS.
662 +     * Use this in a thread that is waited for via awaitTermination(Thread).
663 +     */
664 +    public static long LONGER_DELAY_MS;
665 +
666      private static final long RANDOM_TIMEOUT;
667      private static final long RANDOM_EXPIRED_TIMEOUT;
668      private static final TimeUnit RANDOM_TIMEUNIT;
# Line 692 | Line 698 | public class JSR166TestCase extends Test
698      }
699  
700      /**
701 +     * Returns a random element from given choices.
702 +     */
703 +    <T> T chooseRandomly(T... choices) {
704 +        return choices[ThreadLocalRandom.current().nextInt(choices.length)];
705 +    }
706 +
707 +    /**
708       * Returns the shortest timed delay. This can be scaled up for
709       * slow machines using the jsr166.delay.factor system property,
710       * or via jtreg's -timeoutFactor: flag.
# Line 709 | Line 722 | public class JSR166TestCase extends Test
722          SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
723          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
724          LONG_DELAY_MS   = SHORT_DELAY_MS * 200;
725 +        LONGER_DELAY_MS = 2 * LONG_DELAY_MS;
726      }
727  
728      private static final long TIMEOUT_DELAY_MS
# Line 747 | Line 761 | public class JSR166TestCase extends Test
761       */
762      public void threadRecordFailure(Throwable t) {
763          System.err.println(t);
764 <        dumpTestThreads();
765 <        threadFailure.compareAndSet(null, t);
764 >        if (threadFailure.compareAndSet(null, t))
765 >            dumpTestThreads();
766      }
767  
768      public void setUp() {
# Line 1069 | Line 1083 | public class JSR166TestCase extends Test
1083          }
1084      }
1085  
1086 +    /** Returns true if thread info might be useful in a thread dump. */
1087 +    static boolean threadOfInterest(ThreadInfo info) {
1088 +        final String name = info.getThreadName();
1089 +        String lockName;
1090 +        if (name == null)
1091 +            return true;
1092 +        if (name.equals("Signal Dispatcher")
1093 +            || name.equals("WedgedTestDetector"))
1094 +            return false;
1095 +        if (name.equals("Reference Handler")) {
1096 +            // Reference Handler stacktrace changed in JDK-8156500
1097 +            StackTraceElement[] stackTrace; String methodName;
1098 +            if ((stackTrace = info.getStackTrace()) != null
1099 +                && stackTrace.length > 0
1100 +                && (methodName = stackTrace[0].getMethodName()) != null
1101 +                && methodName.equals("waitForReferencePendingList"))
1102 +                return false;
1103 +            // jdk8 Reference Handler stacktrace
1104 +            if ((lockName = info.getLockName()) != null
1105 +                && lockName.startsWith("java.lang.ref"))
1106 +                return false;
1107 +        }
1108 +        if ((name.equals("Finalizer") || name.equals("Common-Cleaner"))
1109 +            && (lockName = info.getLockName()) != null
1110 +            && lockName.startsWith("java.lang.ref"))
1111 +            return false;
1112 +        if (name.startsWith("ForkJoinPool.commonPool-worker")
1113 +            && (lockName = info.getLockName()) != null
1114 +            && lockName.startsWith("java.util.concurrent.ForkJoinPool"))
1115 +            return false;
1116 +        return true;
1117 +    }
1118 +
1119      /**
1120       * A debugging tool to print stack traces of most threads, as jstack does.
1121       * Uninteresting threads are filtered out.
# Line 1085 | Line 1132 | public class JSR166TestCase extends Test
1132  
1133          ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
1134          System.err.println("------ stacktrace dump start ------");
1135 <        for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true)) {
1136 <            final String name = info.getThreadName();
1137 <            String lockName;
1091 <            if ("Signal Dispatcher".equals(name))
1092 <                continue;
1093 <            if ("Reference Handler".equals(name)
1094 <                && (lockName = info.getLockName()) != null
1095 <                && lockName.startsWith("java.lang.ref.Reference$Lock"))
1096 <                continue;
1097 <            if ("Finalizer".equals(name)
1098 <                && (lockName = info.getLockName()) != null
1099 <                && lockName.startsWith("java.lang.ref.ReferenceQueue$Lock"))
1100 <                continue;
1101 <            if ("checkForWedgedTest".equals(name))
1102 <                continue;
1103 <            System.err.print(info);
1104 <        }
1135 >        for (ThreadInfo info : threadMXBean.dumpAllThreads(true, true))
1136 >            if (threadOfInterest(info))
1137 >                System.err.print(info);
1138          System.err.println("------ stacktrace dump end ------");
1139  
1140          if (sm != null) System.setSecurityManager(sm);
# Line 1374 | Line 1407 | public class JSR166TestCase extends Test
1407      }
1408  
1409      /**
1410 +     * Spin-waits up to LONG_DELAY_MS milliseconds for the current thread to
1411 +     * be interrupted.  Clears the interrupt status before returning.
1412 +     */
1413 +    void awaitInterrupted() {
1414 +        for (long startTime = 0L; !Thread.interrupted(); ) {
1415 +            if (startTime == 0L)
1416 +                startTime = System.nanoTime();
1417 +            else if (millisElapsedSince(startTime) > LONG_DELAY_MS)
1418 +                fail("timed out waiting for thread interrupt");
1419 +            Thread.yield();
1420 +        }
1421 +    }
1422 +
1423 +    /**
1424       * Returns the number of milliseconds since time given by
1425       * startNanoTime, which must have been previously returned from a
1426       * call to {@link System#nanoTime()}.
# Line 1382 | Line 1429 | public class JSR166TestCase extends Test
1429          return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
1430      }
1431  
1385 //     void assertTerminatesPromptly(long timeoutMillis, Runnable r) {
1386 //         long startTime = System.nanoTime();
1387 //         try {
1388 //             r.run();
1389 //         } catch (Throwable fail) { threadUnexpectedException(fail); }
1390 //         if (millisElapsedSince(startTime) > timeoutMillis/2)
1391 //             throw new AssertionError("did not return promptly");
1392 //     }
1393
1394 //     void assertTerminatesPromptly(Runnable r) {
1395 //         assertTerminatesPromptly(LONG_DELAY_MS/2, r);
1396 //     }
1397
1432      /**
1433       * Checks that timed f.get() returns the expected value, and does not
1434       * wait for the timeout to elapse before returning.
# Line 1429 | Line 1463 | public class JSR166TestCase extends Test
1463       * to terminate (using {@link Thread#join(long)}), else interrupts
1464       * the thread (in the hope that it may terminate later) and fails.
1465       */
1466 <    void awaitTermination(Thread t, long timeoutMillis) {
1466 >    void awaitTermination(Thread thread, long timeoutMillis) {
1467          try {
1468 <            t.join(timeoutMillis);
1468 >            thread.join(timeoutMillis);
1469          } catch (InterruptedException fail) {
1470              threadUnexpectedException(fail);
1471 <        } finally {
1472 <            if (t.getState() != Thread.State.TERMINATED) {
1473 <                t.interrupt();
1474 <                threadFail("timed out waiting for thread to terminate");
1471 >        }
1472 >        if (thread.getState() != Thread.State.TERMINATED) {
1473 >            String detail = String.format(
1474 >                    "timed out waiting for thread to terminate, thread=%s, state=%s" ,
1475 >                    thread, thread.getState());
1476 >            try {
1477 >                threadFail(detail);
1478 >            } finally {
1479 >                // Interrupt thread __after__ having reported its stack trace
1480 >                thread.interrupt();
1481              }
1482          }
1483      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines