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.116 by jsr166, Sun Jun 8 00:15:50 2014 UTC vs.
Revision 1.131 by jsr166, Sat Apr 25 04:55:30 2015 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 >
12   import java.io.ByteArrayInputStream;
13   import java.io.ByteArrayOutputStream;
14   import java.io.ObjectInputStream;
# Line 14 | Line 16 | import java.io.ObjectOutputStream;
16   import java.lang.management.ManagementFactory;
17   import java.lang.management.ThreadInfo;
18   import java.lang.reflect.Method;
19 + import java.security.CodeSource;
20 + import java.security.Permission;
21 + import java.security.PermissionCollection;
22 + import java.security.Permissions;
23 + import java.security.Policy;
24 + import java.security.ProtectionDomain;
25 + import java.security.SecurityPermission;
26   import java.util.ArrayList;
27   import java.util.Arrays;
28   import java.util.Date;
29   import java.util.Enumeration;
30 + import java.util.Iterator;
31   import java.util.List;
32   import java.util.NoSuchElementException;
33   import java.util.PropertyPermission;
34 < import java.util.concurrent.*;
35 < import java.util.concurrent.atomic.AtomicBoolean;
34 > import java.util.concurrent.BlockingQueue;
35 > import java.util.concurrent.Callable;
36 > import java.util.concurrent.CountDownLatch;
37 > import java.util.concurrent.CyclicBarrier;
38 > import java.util.concurrent.ExecutorService;
39 > import java.util.concurrent.Future;
40 > import java.util.concurrent.RecursiveAction;
41 > import java.util.concurrent.RecursiveTask;
42 > import java.util.concurrent.RejectedExecutionHandler;
43 > import java.util.concurrent.Semaphore;
44 > import java.util.concurrent.ThreadFactory;
45 > import java.util.concurrent.ThreadPoolExecutor;
46 > import java.util.concurrent.TimeoutException;
47   import java.util.concurrent.atomic.AtomicReference;
27 import static java.util.concurrent.TimeUnit.MILLISECONDS;
28 import static java.util.concurrent.TimeUnit.NANOSECONDS;
48   import java.util.regex.Pattern;
49 < import java.security.CodeSource;
50 < import java.security.Permission;
51 < import java.security.PermissionCollection;
52 < import java.security.Permissions;
53 < import java.security.Policy;
54 < import java.security.ProtectionDomain;
36 < import java.security.SecurityPermission;
49 >
50 > import junit.framework.AssertionFailedError;
51 > import junit.framework.Test;
52 > import junit.framework.TestCase;
53 > import junit.framework.TestResult;
54 > import junit.framework.TestSuite;
55  
56   /**
57   * Base class for JSR166 Junit TCK tests.  Defines some constants,
# Line 116 | Line 134 | public class JSR166TestCase extends Test
134          Boolean.getBoolean("jsr166.expensiveTests");
135  
136      /**
137 +     * If true, also run tests that are not part of the official tck
138 +     * because they test unspecified implementation details.
139 +     */
140 +    protected static final boolean testImplementationDetails =
141 +        Boolean.getBoolean("jsr166.testImplementationDetails");
142 +
143 +    /**
144       * If true, report on stdout all "slow" tests, that is, ones that
145       * take more than profileThreshold milliseconds to execute.
146       */
# Line 136 | Line 161 | public class JSR166TestCase extends Test
161          Integer.getInteger("jsr166.runsPerTest", 1);
162  
163      /**
164 +     * The number of repetitions of the test suite (for finding leaks?).
165 +     */
166 +    private static final int suiteRuns =
167 +        Integer.getInteger("jsr166.suiteRuns", 1);
168 +
169 +    /**
170       * A filter for tests to run, matching strings of the form
171       * methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)"
172       * Usefully combined with jsr166.runsPerTest.
# Line 174 | Line 205 | public class JSR166TestCase extends Test
205  
206      /**
207       * Runs all JSR166 unit tests using junit.textui.TestRunner.
177     * Optional command line arg provides the number of iterations to
178     * repeat running the tests.
208       */
209      public static void main(String[] args) {
210 +        main(suite(), args);
211 +    }
212 +
213 +    /**
214 +     * Runs all unit tests in the given test suite.
215 +     * Actual behavior influenced by system properties jsr166.*
216 +     */
217 +    static void main(Test suite, String[] args) {
218          if (useSecurityManager) {
219              System.err.println("Setting a permissive security manager");
220              Policy.setPolicy(permissivePolicy());
221              System.setSecurityManager(new SecurityManager());
222          }
223 <        int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
224 <
225 <        Test s = suite();
226 <        for (int i = 0; i < iters; ++i) {
190 <            junit.textui.TestRunner.run(s);
223 >        for (int i = 0; i < suiteRuns; i++) {
224 >            TestResult result = junit.textui.TestRunner.run(suite);
225 >            if (!result.wasSuccessful())
226 >                System.exit(1);
227              System.gc();
228              System.runFinalization();
229          }
194        System.exit(0);
230      }
231  
232      public static TestSuite newTestSuite(Object... suiteOrClasses) {
# Line 241 | Line 276 | public class JSR166TestCase extends Test
276      public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
277      public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
278      public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
279 <    public static boolean atLeastJava9() {
245 <        // As of 2014-05, java9 still uses 52.0 class file version
246 <        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
247 <    }
279 >    public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; }
280  
281      /**
282       * Collects all JSR166 unit tests as one suite.
# Line 442 | Line 474 | public class JSR166TestCase extends Test
474      }
475  
476      /**
477 <     * Find missing try { ... } finally { joinPool(e); }
477 >     * Finds missing try { ... } finally { joinPool(e); }
478       */
479      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
480          Thread[] survivors = new Thread[5];
# Line 540 | Line 572 | public class JSR166TestCase extends Test
572      public void threadAssertEquals(Object x, Object y) {
573          try {
574              assertEquals(x, y);
575 <        } catch (AssertionFailedError t) {
576 <            threadRecordFailure(t);
577 <            throw t;
578 <        } catch (Throwable t) {
579 <            threadUnexpectedException(t);
575 >        } catch (AssertionFailedError fail) {
576 >            threadRecordFailure(fail);
577 >            throw fail;
578 >        } catch (Throwable fail) {
579 >            threadUnexpectedException(fail);
580          }
581      }
582  
# Line 556 | Line 588 | public class JSR166TestCase extends Test
588      public void threadAssertSame(Object x, Object y) {
589          try {
590              assertSame(x, y);
591 <        } catch (AssertionFailedError t) {
592 <            threadRecordFailure(t);
593 <            throw t;
591 >        } catch (AssertionFailedError fail) {
592 >            threadRecordFailure(fail);
593 >            throw fail;
594          }
595      }
596  
# Line 623 | Line 655 | public class JSR166TestCase extends Test
655      void joinPool(ExecutorService exec) {
656          try {
657              exec.shutdown();
658 <            assertTrue("ExecutorService did not terminate in a timely manner",
659 <                       exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
658 >            if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
659 >                fail("ExecutorService " + exec +
660 >                     " did not terminate in a timely manner");
661          } catch (SecurityException ok) {
662              // Allowed in case test doesn't have privs
663 <        } catch (InterruptedException ie) {
663 >        } catch (InterruptedException fail) {
664              fail("Unexpected InterruptedException");
665          }
666      }
# Line 658 | Line 691 | public class JSR166TestCase extends Test
691              // No need to optimize the failing case via Thread.join.
692              delay(millis);
693              assertTrue(thread.isAlive());
694 <        } catch (InterruptedException ie) {
694 >        } catch (InterruptedException fail) {
695              fail("Unexpected InterruptedException");
696          }
697      }
# Line 680 | Line 713 | public class JSR166TestCase extends Test
713              delay(millis);
714              for (Thread thread : threads)
715                  assertTrue(thread.isAlive());
716 <        } catch (InterruptedException ie) {
716 >        } catch (InterruptedException fail) {
717              fail("Unexpected InterruptedException");
718          }
719      }
# Line 702 | Line 735 | public class JSR166TestCase extends Test
735              future.get(timeoutMillis, MILLISECONDS);
736              shouldThrow();
737          } catch (TimeoutException success) {
738 <        } catch (Exception e) {
739 <            threadUnexpectedException(e);
738 >        } catch (Exception fail) {
739 >            threadUnexpectedException(fail);
740          } finally { future.cancel(true); }
741          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
742      }
# Line 859 | Line 892 | public class JSR166TestCase extends Test
892      void sleep(long millis) {
893          try {
894              delay(millis);
895 <        } catch (InterruptedException ie) {
895 >        } catch (InterruptedException fail) {
896              AssertionFailedError afe =
897                  new AssertionFailedError("Unexpected InterruptedException");
898 <            afe.initCause(ie);
898 >            afe.initCause(fail);
899              throw afe;
900          }
901      }
# Line 900 | Line 933 | public class JSR166TestCase extends Test
933      /**
934       * Returns the number of milliseconds since time given by
935       * startNanoTime, which must have been previously returned from a
936 <     * call to {@link System.nanoTime()}.
936 >     * call to {@link System#nanoTime()}.
937       */
938 <    long millisElapsedSince(long startNanoTime) {
938 >    static long millisElapsedSince(long startNanoTime) {
939          return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
940      }
941  
942 + //     void assertTerminatesPromptly(long timeoutMillis, Runnable r) {
943 + //         long startTime = System.nanoTime();
944 + //         try {
945 + //             r.run();
946 + //         } catch (Throwable fail) { threadUnexpectedException(fail); }
947 + //         if (millisElapsedSince(startTime) > timeoutMillis/2)
948 + //             throw new AssertionFailedError("did not return promptly");
949 + //     }
950 +
951 + //     void assertTerminatesPromptly(Runnable r) {
952 + //         assertTerminatesPromptly(LONG_DELAY_MS/2, r);
953 + //     }
954 +
955 +    /**
956 +     * Checks that timed f.get() returns the expected value, and does not
957 +     * wait for the timeout to elapse before returning.
958 +     */
959 +    <T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) {
960 +        long startTime = System.nanoTime();
961 +        try {
962 +            assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS));
963 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
964 +        if (millisElapsedSince(startTime) > timeoutMillis/2)
965 +            throw new AssertionFailedError("timed get did not return promptly");
966 +    }
967 +
968 +    <T> void checkTimedGet(Future<T> f, T expectedValue) {
969 +        checkTimedGet(f, expectedValue, LONG_DELAY_MS);
970 +    }
971 +
972      /**
973       * Returns a new started daemon Thread running the given runnable.
974       */
# Line 924 | Line 987 | public class JSR166TestCase extends Test
987      void awaitTermination(Thread t, long timeoutMillis) {
988          try {
989              t.join(timeoutMillis);
990 <        } catch (InterruptedException ie) {
991 <            threadUnexpectedException(ie);
990 >        } catch (InterruptedException fail) {
991 >            threadUnexpectedException(fail);
992          } finally {
993              if (t.getState() != Thread.State.TERMINATED) {
994                  t.interrupt();
# Line 951 | Line 1014 | public class JSR166TestCase extends Test
1014          public final void run() {
1015              try {
1016                  realRun();
1017 <            } catch (Throwable t) {
1018 <                threadUnexpectedException(t);
1017 >            } catch (Throwable fail) {
1018 >                threadUnexpectedException(fail);
1019              }
1020          }
1021      }
# Line 1006 | Line 1069 | public class JSR166TestCase extends Test
1069                  threadShouldThrow("InterruptedException");
1070              } catch (InterruptedException success) {
1071                  threadAssertFalse(Thread.interrupted());
1072 <            } catch (Throwable t) {
1073 <                threadUnexpectedException(t);
1072 >            } catch (Throwable fail) {
1073 >                threadUnexpectedException(fail);
1074              }
1075          }
1076      }
# Line 1018 | Line 1081 | public class JSR166TestCase extends Test
1081          public final T call() {
1082              try {
1083                  return realCall();
1084 <            } catch (Throwable t) {
1085 <                threadUnexpectedException(t);
1084 >            } catch (Throwable fail) {
1085 >                threadUnexpectedException(fail);
1086                  return null;
1087              }
1088          }
# Line 1036 | Line 1099 | public class JSR166TestCase extends Test
1099                  return result;
1100              } catch (InterruptedException success) {
1101                  threadAssertFalse(Thread.interrupted());
1102 <            } catch (Throwable t) {
1103 <                threadUnexpectedException(t);
1102 >            } catch (Throwable fail) {
1103 >                threadUnexpectedException(fail);
1104              }
1105              return null;
1106          }
# Line 1077 | Line 1140 | public class JSR166TestCase extends Test
1140      public void await(CountDownLatch latch) {
1141          try {
1142              assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1143 <        } catch (Throwable t) {
1144 <            threadUnexpectedException(t);
1143 >        } catch (Throwable fail) {
1144 >            threadUnexpectedException(fail);
1145          }
1146      }
1147  
1148      public void await(Semaphore semaphore) {
1149          try {
1150              assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1151 <        } catch (Throwable t) {
1152 <            threadUnexpectedException(t);
1151 >        } catch (Throwable fail) {
1152 >            threadUnexpectedException(fail);
1153          }
1154      }
1155  
# Line 1280 | Line 1343 | public class JSR166TestCase extends Test
1343          @Override protected final void compute() {
1344              try {
1345                  realCompute();
1346 <            } catch (Throwable t) {
1347 <                threadUnexpectedException(t);
1346 >            } catch (Throwable fail) {
1347 >                threadUnexpectedException(fail);
1348              }
1349          }
1350      }
# Line 1295 | Line 1358 | public class JSR166TestCase extends Test
1358          @Override protected final T compute() {
1359              try {
1360                  return realCompute();
1361 <            } catch (Throwable t) {
1362 <                threadUnexpectedException(t);
1361 >            } catch (Throwable fail) {
1362 >                threadUnexpectedException(fail);
1363                  return null;
1364              }
1365          }
# Line 1320 | Line 1383 | public class JSR166TestCase extends Test
1383          public int await() {
1384              try {
1385                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1386 <            } catch (TimeoutException e) {
1386 >            } catch (TimeoutException timedOut) {
1387                  throw new AssertionFailedError("timed out");
1388 <            } catch (Exception e) {
1388 >            } catch (Exception fail) {
1389                  AssertionFailedError afe =
1390 <                    new AssertionFailedError("Unexpected exception: " + e);
1391 <                afe.initCause(e);
1390 >                    new AssertionFailedError("Unexpected exception: " + fail);
1391 >                afe.initCause(fail);
1392                  throw afe;
1393              }
1394          }
# Line 1353 | Line 1416 | public class JSR166TestCase extends Test
1416                  q.remove();
1417                  shouldThrow();
1418              } catch (NoSuchElementException success) {}
1419 <        } catch (InterruptedException ie) {
1357 <            threadUnexpectedException(ie);
1358 <        }
1419 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
1420      }
1421  
1422      void assertSerialEquals(Object x, Object y) {
# Line 1374 | Line 1435 | public class JSR166TestCase extends Test
1435              oos.flush();
1436              oos.close();
1437              return bos.toByteArray();
1438 <        } catch (Throwable t) {
1439 <            threadUnexpectedException(t);
1438 >        } catch (Throwable fail) {
1439 >            threadUnexpectedException(fail);
1440              return new byte[0];
1441          }
1442      }
# Line 1388 | Line 1449 | public class JSR166TestCase extends Test
1449              T clone = (T) ois.readObject();
1450              assertSame(o.getClass(), clone.getClass());
1451              return clone;
1452 <        } catch (Throwable t) {
1453 <            threadUnexpectedException(t);
1452 >        } catch (Throwable fail) {
1453 >            threadUnexpectedException(fail);
1454              return null;
1455          }
1456      }
# Line 1414 | Line 1475 | public class JSR166TestCase extends Test
1475                  shouldThrow(expectedExceptionClass.getName());
1476          }
1477      }
1478 +
1479 +    public void assertIteratorExhausted(Iterator<?> it) {
1480 +        try {
1481 +            it.next();
1482 +            shouldThrow();
1483 +        } catch (NoSuchElementException success) {}
1484 +        assertFalse(it.hasNext());
1485 +    }
1486   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines