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.119 by jsr166, Mon Jun 16 20:13:54 2014 UTC vs.
Revision 1.130 by jsr166, Tue Apr 21 05:00:23 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;
35 < 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.TestSuite;
54  
55   /**
56   * Base class for JSR166 Junit TCK tests.  Defines some constants,
# Line 248 | Line 265 | public class JSR166TestCase extends Test
265      public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; }
266      public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; }
267      public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; }
268 <    public static boolean atLeastJava9() {
252 <        // As of 2014-05, java9 still uses 52.0 class file version
253 <        return JAVA_SPECIFICATION_VERSION.startsWith("1.9");
254 <    }
268 >    public static boolean atLeastJava9() { return JAVA_CLASS_VERSION >= 53.0; }
269  
270      /**
271       * Collects all JSR166 unit tests as one suite.
# Line 449 | Line 463 | public class JSR166TestCase extends Test
463      }
464  
465      /**
466 <     * Find missing try { ... } finally { joinPool(e); }
466 >     * Finds missing try { ... } finally { joinPool(e); }
467       */
468      void checkForkJoinPoolThreadLeaks() throws InterruptedException {
469          Thread[] survivors = new Thread[5];
# Line 547 | Line 561 | public class JSR166TestCase extends Test
561      public void threadAssertEquals(Object x, Object y) {
562          try {
563              assertEquals(x, y);
564 <        } catch (AssertionFailedError t) {
565 <            threadRecordFailure(t);
566 <            throw t;
567 <        } catch (Throwable t) {
568 <            threadUnexpectedException(t);
564 >        } catch (AssertionFailedError fail) {
565 >            threadRecordFailure(fail);
566 >            throw fail;
567 >        } catch (Throwable fail) {
568 >            threadUnexpectedException(fail);
569          }
570      }
571  
# Line 563 | Line 577 | public class JSR166TestCase extends Test
577      public void threadAssertSame(Object x, Object y) {
578          try {
579              assertSame(x, y);
580 <        } catch (AssertionFailedError t) {
581 <            threadRecordFailure(t);
582 <            throw t;
580 >        } catch (AssertionFailedError fail) {
581 >            threadRecordFailure(fail);
582 >            throw fail;
583          }
584      }
585  
# Line 630 | Line 644 | public class JSR166TestCase extends Test
644      void joinPool(ExecutorService exec) {
645          try {
646              exec.shutdown();
647 <            assertTrue("ExecutorService did not terminate in a timely manner",
648 <                       exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
647 >            if (!exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS))
648 >                fail("ExecutorService " + exec +
649 >                     " did not terminate in a timely manner");
650          } catch (SecurityException ok) {
651              // Allowed in case test doesn't have privs
652 <        } catch (InterruptedException ie) {
652 >        } catch (InterruptedException fail) {
653              fail("Unexpected InterruptedException");
654          }
655      }
# Line 665 | Line 680 | public class JSR166TestCase extends Test
680              // No need to optimize the failing case via Thread.join.
681              delay(millis);
682              assertTrue(thread.isAlive());
683 <        } catch (InterruptedException ie) {
683 >        } catch (InterruptedException fail) {
684              fail("Unexpected InterruptedException");
685          }
686      }
# Line 687 | Line 702 | public class JSR166TestCase extends Test
702              delay(millis);
703              for (Thread thread : threads)
704                  assertTrue(thread.isAlive());
705 <        } catch (InterruptedException ie) {
705 >        } catch (InterruptedException fail) {
706              fail("Unexpected InterruptedException");
707          }
708      }
# Line 709 | Line 724 | public class JSR166TestCase extends Test
724              future.get(timeoutMillis, MILLISECONDS);
725              shouldThrow();
726          } catch (TimeoutException success) {
727 <        } catch (Exception e) {
728 <            threadUnexpectedException(e);
727 >        } catch (Exception fail) {
728 >            threadUnexpectedException(fail);
729          } finally { future.cancel(true); }
730          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
731      }
# Line 866 | Line 881 | public class JSR166TestCase extends Test
881      void sleep(long millis) {
882          try {
883              delay(millis);
884 <        } catch (InterruptedException ie) {
884 >        } catch (InterruptedException fail) {
885              AssertionFailedError afe =
886                  new AssertionFailedError("Unexpected InterruptedException");
887 <            afe.initCause(ie);
887 >            afe.initCause(fail);
888              throw afe;
889          }
890      }
# Line 907 | Line 922 | public class JSR166TestCase extends Test
922      /**
923       * Returns the number of milliseconds since time given by
924       * startNanoTime, which must have been previously returned from a
925 <     * call to {@link System.nanoTime()}.
925 >     * call to {@link System#nanoTime()}.
926       */
927      static long millisElapsedSince(long startNanoTime) {
928          return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
929      }
930  
931 + //     void assertTerminatesPromptly(long timeoutMillis, Runnable r) {
932 + //         long startTime = System.nanoTime();
933 + //         try {
934 + //             r.run();
935 + //         } catch (Throwable fail) { threadUnexpectedException(fail); }
936 + //         if (millisElapsedSince(startTime) > timeoutMillis/2)
937 + //             throw new AssertionFailedError("did not return promptly");
938 + //     }
939 +
940 + //     void assertTerminatesPromptly(Runnable r) {
941 + //         assertTerminatesPromptly(LONG_DELAY_MS/2, r);
942 + //     }
943 +
944 +    /**
945 +     * Checks that timed f.get() returns the expected value, and does not
946 +     * wait for the timeout to elapse before returning.
947 +     */
948 +    <T> void checkTimedGet(Future<T> f, T expectedValue, long timeoutMillis) {
949 +        long startTime = System.nanoTime();
950 +        try {
951 +            assertEquals(expectedValue, f.get(timeoutMillis, MILLISECONDS));
952 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
953 +        if (millisElapsedSince(startTime) > timeoutMillis/2)
954 +            throw new AssertionFailedError("timed get did not return promptly");
955 +    }
956 +
957 +    <T> void checkTimedGet(Future<T> f, T expectedValue) {
958 +        checkTimedGet(f, expectedValue, LONG_DELAY_MS);
959 +    }
960 +
961      /**
962       * Returns a new started daemon Thread running the given runnable.
963       */
# Line 931 | Line 976 | public class JSR166TestCase extends Test
976      void awaitTermination(Thread t, long timeoutMillis) {
977          try {
978              t.join(timeoutMillis);
979 <        } catch (InterruptedException ie) {
980 <            threadUnexpectedException(ie);
979 >        } catch (InterruptedException fail) {
980 >            threadUnexpectedException(fail);
981          } finally {
982              if (t.getState() != Thread.State.TERMINATED) {
983                  t.interrupt();
# Line 958 | Line 1003 | public class JSR166TestCase extends Test
1003          public final void run() {
1004              try {
1005                  realRun();
1006 <            } catch (Throwable t) {
1007 <                threadUnexpectedException(t);
1006 >            } catch (Throwable fail) {
1007 >                threadUnexpectedException(fail);
1008              }
1009          }
1010      }
# Line 1013 | Line 1058 | public class JSR166TestCase extends Test
1058                  threadShouldThrow("InterruptedException");
1059              } catch (InterruptedException success) {
1060                  threadAssertFalse(Thread.interrupted());
1061 <            } catch (Throwable t) {
1062 <                threadUnexpectedException(t);
1061 >            } catch (Throwable fail) {
1062 >                threadUnexpectedException(fail);
1063              }
1064          }
1065      }
# Line 1025 | Line 1070 | public class JSR166TestCase extends Test
1070          public final T call() {
1071              try {
1072                  return realCall();
1073 <            } catch (Throwable t) {
1074 <                threadUnexpectedException(t);
1073 >            } catch (Throwable fail) {
1074 >                threadUnexpectedException(fail);
1075                  return null;
1076              }
1077          }
# Line 1043 | Line 1088 | public class JSR166TestCase extends Test
1088                  return result;
1089              } catch (InterruptedException success) {
1090                  threadAssertFalse(Thread.interrupted());
1091 <            } catch (Throwable t) {
1092 <                threadUnexpectedException(t);
1091 >            } catch (Throwable fail) {
1092 >                threadUnexpectedException(fail);
1093              }
1094              return null;
1095          }
# Line 1084 | Line 1129 | public class JSR166TestCase extends Test
1129      public void await(CountDownLatch latch) {
1130          try {
1131              assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
1132 <        } catch (Throwable t) {
1133 <            threadUnexpectedException(t);
1132 >        } catch (Throwable fail) {
1133 >            threadUnexpectedException(fail);
1134          }
1135      }
1136  
1137      public void await(Semaphore semaphore) {
1138          try {
1139              assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS));
1140 <        } catch (Throwable t) {
1141 <            threadUnexpectedException(t);
1140 >        } catch (Throwable fail) {
1141 >            threadUnexpectedException(fail);
1142          }
1143      }
1144  
# Line 1287 | Line 1332 | public class JSR166TestCase extends Test
1332          @Override protected final void compute() {
1333              try {
1334                  realCompute();
1335 <            } catch (Throwable t) {
1336 <                threadUnexpectedException(t);
1335 >            } catch (Throwable fail) {
1336 >                threadUnexpectedException(fail);
1337              }
1338          }
1339      }
# Line 1302 | Line 1347 | public class JSR166TestCase extends Test
1347          @Override protected final T compute() {
1348              try {
1349                  return realCompute();
1350 <            } catch (Throwable t) {
1351 <                threadUnexpectedException(t);
1350 >            } catch (Throwable fail) {
1351 >                threadUnexpectedException(fail);
1352                  return null;
1353              }
1354          }
# Line 1327 | Line 1372 | public class JSR166TestCase extends Test
1372          public int await() {
1373              try {
1374                  return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
1375 <            } catch (TimeoutException e) {
1375 >            } catch (TimeoutException timedOut) {
1376                  throw new AssertionFailedError("timed out");
1377 <            } catch (Exception e) {
1377 >            } catch (Exception fail) {
1378                  AssertionFailedError afe =
1379 <                    new AssertionFailedError("Unexpected exception: " + e);
1380 <                afe.initCause(e);
1379 >                    new AssertionFailedError("Unexpected exception: " + fail);
1380 >                afe.initCause(fail);
1381                  throw afe;
1382              }
1383          }
# Line 1360 | Line 1405 | public class JSR166TestCase extends Test
1405                  q.remove();
1406                  shouldThrow();
1407              } catch (NoSuchElementException success) {}
1408 <        } catch (InterruptedException ie) {
1364 <            threadUnexpectedException(ie);
1365 <        }
1408 >        } catch (InterruptedException fail) { threadUnexpectedException(fail); }
1409      }
1410  
1411      void assertSerialEquals(Object x, Object y) {
# Line 1381 | Line 1424 | public class JSR166TestCase extends Test
1424              oos.flush();
1425              oos.close();
1426              return bos.toByteArray();
1427 <        } catch (Throwable t) {
1428 <            threadUnexpectedException(t);
1427 >        } catch (Throwable fail) {
1428 >            threadUnexpectedException(fail);
1429              return new byte[0];
1430          }
1431      }
# Line 1395 | Line 1438 | public class JSR166TestCase extends Test
1438              T clone = (T) ois.readObject();
1439              assertSame(o.getClass(), clone.getClass());
1440              return clone;
1441 <        } catch (Throwable t) {
1442 <            threadUnexpectedException(t);
1441 >        } catch (Throwable fail) {
1442 >            threadUnexpectedException(fail);
1443              return null;
1444          }
1445      }
# Line 1421 | Line 1464 | public class JSR166TestCase extends Test
1464                  shouldThrow(expectedExceptionClass.getName());
1465          }
1466      }
1467 +
1468 +    public void assertIteratorExhausted(Iterator<?> it) {
1469 +        try {
1470 +            it.next();
1471 +            shouldThrow();
1472 +        } catch (NoSuchElementException success) {}
1473 +        assertFalse(it.hasNext());
1474 +    }
1475   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines