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.165 by jsr166, Mon Oct 5 01:10:09 2015 UTC vs.
Revision 1.177 by jsr166, Mon Oct 12 23:52:44 2015 UTC

# Line 20 | Line 20 | import java.lang.management.ThreadMXBean
20   import java.lang.reflect.Constructor;
21   import java.lang.reflect.Method;
22   import java.lang.reflect.Modifier;
23 + import java.nio.file.Files;
24 + import java.nio.file.Paths;
25   import java.security.CodeSource;
26   import java.security.Permission;
27   import java.security.PermissionCollection;
# Line 52 | Line 54 | import java.util.concurrent.ThreadFactor
54   import java.util.concurrent.ThreadPoolExecutor;
55   import java.util.concurrent.TimeoutException;
56   import java.util.concurrent.atomic.AtomicReference;
57 + import java.util.regex.Matcher;
58   import java.util.regex.Pattern;
59  
60   import junit.framework.AssertionFailedError;
# Line 188 | Line 191 | public class JSR166TestCase extends Test
191          return (regex == null) ? null : Pattern.compile(regex);
192      }
193  
194 +    // Instrumentation to debug very rare, but very annoying hung test runs.
195      static volatile TestCase currentTestCase;
196 +    // static volatile int currentRun = 0;
197      static {
198          Runnable checkForWedgedTest = new Runnable() { public void run() {
199 <            // avoid spurious reports with enormous runsPerTest
200 <            final int timeoutMinutes = Math.max(runsPerTest / 10, 1);
199 >            // Avoid spurious reports with enormous runsPerTest.
200 >            // A single test case run should never take more than 1 second.
201 >            // But let's cap it at the high end too ...
202 >            final int timeoutMinutes =
203 >                Math.min(15, Math.max(runsPerTest / 60, 1));
204              for (TestCase lastTestCase = currentTestCase;;) {
205                  try { MINUTES.sleep(timeoutMinutes); }
206                  catch (InterruptedException unexpected) { break; }
207                  if (lastTestCase == currentTestCase) {
208 <                    System.err.println
209 <                        ("Looks like we're stuck running test: "
210 <                         + lastTestCase);
208 >                    System.err.printf(
209 >                        "Looks like we're stuck running test: %s%n",
210 >                        lastTestCase);
211 > //                     System.err.printf(
212 > //                         "Looks like we're stuck running test: %s (%d/%d)%n",
213 > //                         lastTestCase, currentRun, runsPerTest);
214 > //                     System.err.println("availableProcessors=" +
215 > //                         Runtime.getRuntime().availableProcessors());
216 > //                     System.err.printf("cpu model = %s%n", cpuModel());
217                      dumpTestThreads();
218                      // one stack dump is probably enough; more would be spam
219                      break;
# Line 211 | Line 225 | public class JSR166TestCase extends Test
225          thread.start();
226      }
227  
228 + //     public static String cpuModel() {
229 + //         try {
230 + //             Matcher matcher = Pattern.compile("model name\\s*: (.*)")
231 + //                 .matcher(new String(
232 + //                      Files.readAllBytes(Paths.get("/proc/cpuinfo")), "UTF-8"));
233 + //             matcher.find();
234 + //             return matcher.group(1);
235 + //         } catch (Exception ex) { return null; }
236 + //     }
237 +
238      public void runBare() throws Throwable {
239          currentTestCase = this;
240          if (methodFilter == null
# Line 220 | Line 244 | public class JSR166TestCase extends Test
244  
245      protected void runTest() throws Throwable {
246          for (int i = 0; i < runsPerTest; i++) {
247 +            // currentRun = i;
248              if (profileTests)
249                  runTestProfiled();
250              else
# Line 776 | Line 801 | public class JSR166TestCase extends Test
801          public void close() { joinPool(pool); }
802      }
803  
804 +    /**
805 +     * An extension of PoolCleaner that has an action to release the pool.
806 +     */
807 +    class PoolCleanerWithReleaser extends PoolCleaner {
808 +        private final Runnable releaser;
809 +        public PoolCleanerWithReleaser(ExecutorService pool, Runnable releaser) {
810 +            super(pool);
811 +            this.releaser = releaser;
812 +        }
813 +        public void close() {
814 +            try {
815 +                releaser.run();
816 +            } finally {
817 +                super.close();
818 +            }
819 +        }
820 +    }
821 +
822      PoolCleaner cleaner(ExecutorService pool) {
823          return new PoolCleaner(pool);
824      }
825  
826 +    PoolCleaner cleaner(ExecutorService pool, Runnable releaser) {
827 +        return new PoolCleanerWithReleaser(pool, releaser);
828 +    }
829 +
830 +    PoolCleaner cleaner(ExecutorService pool, CountDownLatch latch) {
831 +        return new PoolCleanerWithReleaser(pool, releaser(latch));
832 +    }
833 +
834 +    Runnable releaser(final CountDownLatch latch) {
835 +        return new Runnable() { public void run() {
836 +            do { latch.countDown(); }
837 +            while (latch.getCount() > 0);
838 +        }};
839 +    }
840 +
841      /**
842       * Waits out termination of a thread pool or fails doing so.
843       */
# Line 793 | Line 851 | public class JSR166TestCase extends Test
851                  } finally {
852                      // last resort, for the benefit of subsequent tests
853                      pool.shutdownNow();
854 <                    pool.awaitTermination(SMALL_DELAY_MS, MILLISECONDS);
854 >                    pool.awaitTermination(MEDIUM_DELAY_MS, MILLISECONDS);
855                  }
856              }
857          } catch (SecurityException ok) {
# Line 1170 | Line 1228 | public class JSR166TestCase extends Test
1228          } finally {
1229              if (t.getState() != Thread.State.TERMINATED) {
1230                  t.interrupt();
1231 <                fail("Test timed out");
1231 >                threadFail("timed out waiting for thread to terminate");
1232              }
1233          }
1234      }
# Line 1338 | Line 1396 | public class JSR166TestCase extends Test
1396  
1397      public void await(CountDownLatch latch) {
1398          try {
1399 <            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1399 >            if (!latch.await(LONG_DELAY_MS, MILLISECONDS))
1400 >                fail("timed out waiting for CountDownLatch for "
1401 >                     + (LONG_DELAY_MS/1000) + " sec");
1402          } catch (Throwable fail) {
1403              threadUnexpectedException(fail);
1404          }
# Line 1346 | Line 1406 | public class JSR166TestCase extends Test
1406  
1407      public void await(Semaphore semaphore) {
1408          try {
1409 <            assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1409 >            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1410 >                fail("timed out waiting for Semaphore for "
1411 >                     + (LONG_DELAY_MS/1000) + " sec");
1412          } catch (Throwable fail) {
1413              threadUnexpectedException(fail);
1414          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines