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.245 by jsr166, Sun Jul 22 21:19:14 2018 UTC vs.
Revision 1.251 by jsr166, Wed Dec 12 16:59:55 2018 UTC

# Line 117 | Line 117 | import junit.framework.TestSuite;
117   *
118   * <ol>
119   *
120 < * <li>All assertions in code running in generated threads must use
121 < * the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link
122 < * #threadAssertEquals}, or {@link #threadAssertNull}, (not
123 < * {@code fail}, {@code assertTrue}, etc.) It is OK (but not
124 < * particularly recommended) for other code to use these forms too.
125 < * Only the most typically used JUnit assertion methods are defined
126 < * this way, but enough to live with.
120 > * <li>All code not running in the main test thread (manually spawned threads
121 > * or the common fork join pool) must be checked for failure (and completion!).
122 > * Mechanisms that can be used to ensure this are:
123 > *   <ol>
124 > *   <li>Signalling via a synchronizer like AtomicInteger or CountDownLatch
125 > *    that the task completed normally, which is checked before returning from
126 > *    the test method in the main thread.
127 > *   <li>Using the forms {@link #threadFail}, {@link #threadAssertTrue},
128 > *    or {@link #threadAssertNull}, (not {@code fail}, {@code assertTrue}, etc.)
129 > *    Only the most typically used JUnit assertion methods are defined
130 > *    this way, but enough to live with.
131 > *   <li>Recording failure explicitly using {@link #threadUnexpectedException}
132 > *    or {@link #threadRecordFailure}.
133 > *   <li>Using a wrapper like CheckedRunnable that uses one the mechanisms above.
134 > *   </ol>
135   *
136   * <li>If you override {@link #setUp} or {@link #tearDown}, make sure
137   * to invoke {@code super.setUp} and {@code super.tearDown} within
# Line 542 | Line 550 | public class JSR166TestCase extends Test
550                  "HashMapTest",
551                  "LinkedBlockingDeque8Test",
552                  "LinkedBlockingQueue8Test",
553 +                "LinkedHashMapTest",
554                  "LongAccumulatorTest",
555                  "LongAdderTest",
556                  "SplittableRandomTest",
# Line 1291 | Line 1300 | public class JSR166TestCase extends Test
1300      /**
1301       * Spin-waits up to the specified number of milliseconds for the given
1302       * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
1303 +     * @param waitingForGodot if non-null, an additional condition to satisfy
1304       */
1305 <    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
1306 <        long startTime = 0L;
1307 <        for (;;) {
1308 <            Thread.State s = thread.getState();
1309 <            if (s == Thread.State.BLOCKED ||
1310 <                s == Thread.State.WAITING ||
1311 <                s == Thread.State.TIMED_WAITING)
1312 <                return;
1313 <            else if (s == Thread.State.TERMINATED)
1305 >    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis,
1306 >                                       Callable<Boolean> waitingForGodot) {
1307 >        for (long startTime = 0L;;) {
1308 >            switch (thread.getState()) {
1309 >            default: break;
1310 >            case BLOCKED: case WAITING: case TIMED_WAITING:
1311 >                try {
1312 >                    if (waitingForGodot == null || waitingForGodot.call())
1313 >                        return;
1314 >                } catch (Throwable fail) { threadUnexpectedException(fail); }
1315 >                break;
1316 >            case TERMINATED:
1317                  fail("Unexpected thread termination");
1318 <            else if (startTime == 0L)
1318 >            }
1319 >
1320 >            if (startTime == 0L)
1321                  startTime = System.nanoTime();
1322              else if (millisElapsedSince(startTime) > timeoutMillis) {
1323 <                threadAssertTrue(thread.isAlive());
1324 <                fail("timed out waiting for thread to enter wait state");
1323 >                assertTrue(thread.isAlive());
1324 >                if (waitingForGodot == null
1325 >                    || thread.getState() == Thread.State.RUNNABLE)
1326 >                    fail("timed out waiting for thread to enter wait state");
1327 >                else
1328 >                    fail("timed out waiting for condition, thread state="
1329 >                         + thread.getState());
1330              }
1331              Thread.yield();
1332          }
# Line 1314 | Line 1334 | public class JSR166TestCase extends Test
1334  
1335      /**
1336       * Spin-waits up to the specified number of milliseconds for the given
1337 <     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING,
1318 <     * and additionally satisfy the given condition.
1337 >     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
1338       */
1339 <    void waitForThreadToEnterWaitState(
1340 <        Thread thread, long timeoutMillis, Callable<Boolean> waitingForGodot) {
1322 <        long startTime = 0L;
1323 <        for (;;) {
1324 <            Thread.State s = thread.getState();
1325 <            if (s == Thread.State.BLOCKED ||
1326 <                s == Thread.State.WAITING ||
1327 <                s == Thread.State.TIMED_WAITING) {
1328 <                try {
1329 <                    if (waitingForGodot.call())
1330 <                        return;
1331 <                } catch (Throwable fail) { threadUnexpectedException(fail); }
1332 <            }
1333 <            else if (s == Thread.State.TERMINATED)
1334 <                fail("Unexpected thread termination");
1335 <            else if (startTime == 0L)
1336 <                startTime = System.nanoTime();
1337 <            else if (millisElapsedSince(startTime) > timeoutMillis) {
1338 <                threadAssertTrue(thread.isAlive());
1339 <                fail("timed out waiting for thread to enter wait state");
1340 <            }
1341 <            Thread.yield();
1342 <        }
1339 >    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
1340 >        waitForThreadToEnterWaitState(thread, timeoutMillis, null);
1341      }
1342  
1343      /**
# Line 1347 | Line 1345 | public class JSR166TestCase extends Test
1345       * enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
1346       */
1347      void waitForThreadToEnterWaitState(Thread thread) {
1348 <        waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
1348 >        waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, null);
1349      }
1350  
1351      /**
# Line 1355 | Line 1353 | public class JSR166TestCase extends Test
1353       * enter a wait state: BLOCKED, WAITING, or TIMED_WAITING,
1354       * and additionally satisfy the given condition.
1355       */
1356 <    void waitForThreadToEnterWaitState(
1357 <        Thread thread, Callable<Boolean> waitingForGodot) {
1356 >    void waitForThreadToEnterWaitState(Thread thread,
1357 >                                       Callable<Boolean> waitingForGodot) {
1358          waitForThreadToEnterWaitState(thread, LONG_DELAY_MS, waitingForGodot);
1359      }
1360  
# Line 1464 | Line 1462 | public class JSR166TestCase extends Test
1462          public final void run() {
1463              try {
1464                  realRun();
1467                threadShouldThrow(exceptionClass.getSimpleName());
1465              } catch (Throwable t) {
1466                  if (! exceptionClass.isInstance(t))
1467                      threadUnexpectedException(t);
1468 +                return;
1469              }
1470 +            threadShouldThrow(exceptionClass.getSimpleName());
1471          }
1472      }
1473  
# Line 1478 | Line 1477 | public class JSR166TestCase extends Test
1477          public final void run() {
1478              try {
1479                  realRun();
1481                threadShouldThrow("InterruptedException");
1480              } catch (InterruptedException success) {
1481                  threadAssertFalse(Thread.interrupted());
1482 +                return;
1483              } catch (Throwable fail) {
1484                  threadUnexpectedException(fail);
1485              }
1486 +            threadShouldThrow("InterruptedException");
1487          }
1488      }
1489  
# Line 1495 | Line 1495 | public class JSR166TestCase extends Test
1495                  return realCall();
1496              } catch (Throwable fail) {
1497                  threadUnexpectedException(fail);
1498                return null;
1499            }
1500        }
1501    }
1502
1503    public abstract class CheckedInterruptedCallable<T>
1504        implements Callable<T> {
1505        protected abstract T realCall() throws Throwable;
1506
1507        public final T call() {
1508            try {
1509                T result = realCall();
1510                threadShouldThrow("InterruptedException");
1511                return result;
1512            } catch (InterruptedException success) {
1513                threadAssertFalse(Thread.interrupted());
1514            } catch (Throwable fail) {
1515                threadUnexpectedException(fail);
1498              }
1499 <            return null;
1499 >            throw new AssertionError("unreached");
1500          }
1501      }
1502  
# Line 1571 | Line 1553 | public class JSR166TestCase extends Test
1553      }
1554  
1555      public void await(CountDownLatch latch, long timeoutMillis) {
1556 +        boolean timedOut = false;
1557          try {
1558 <            if (!latch.await(timeoutMillis, MILLISECONDS))
1576 <                fail("timed out waiting for CountDownLatch for "
1577 <                     + (timeoutMillis/1000) + " sec");
1558 >            timedOut = !latch.await(timeoutMillis, MILLISECONDS);
1559          } catch (Throwable fail) {
1560              threadUnexpectedException(fail);
1561          }
1562 +        if (timedOut)
1563 +            fail("timed out waiting for CountDownLatch for "
1564 +                 + (timeoutMillis/1000) + " sec");
1565      }
1566  
1567      public void await(CountDownLatch latch) {
# Line 1585 | Line 1569 | public class JSR166TestCase extends Test
1569      }
1570  
1571      public void await(Semaphore semaphore) {
1572 +        boolean timedOut = false;
1573          try {
1574 <            if (!semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS))
1590 <                fail("timed out waiting for Semaphore for "
1591 <                     + (LONG_DELAY_MS/1000) + " sec");
1574 >            timedOut = !semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS);
1575          } catch (Throwable fail) {
1576              threadUnexpectedException(fail);
1577          }
1578 +        if (timedOut)
1579 +            fail("timed out waiting for Semaphore for "
1580 +                 + (LONG_DELAY_MS/1000) + " sec");
1581      }
1582  
1583      public void await(CyclicBarrier barrier) {
# Line 1625 | Line 1611 | public class JSR166TestCase extends Test
1611          public String call() { throw new NullPointerException(); }
1612      }
1613  
1628    public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
1629        protected void realRun() {
1630            try {
1631                delay(SMALL_DELAY_MS);
1632            } catch (InterruptedException ok) {}
1633        }
1634    }
1635
1614      public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
1615          return new CheckedRunnable() {
1616              protected void realRun() {
# Line 1688 | Line 1666 | public class JSR166TestCase extends Test
1666                  return realCompute();
1667              } catch (Throwable fail) {
1668                  threadUnexpectedException(fail);
1691                return null;
1669              }
1670 +            throw new AssertionError("unreached");
1671          }
1672      }
1673  
# Line 1777 | Line 1755 | public class JSR166TestCase extends Test
1755  
1756      @SuppressWarnings("unchecked")
1757      <T> T serialClone(T o) {
1758 +        T clone = null;
1759          try {
1760              ObjectInputStream ois = new ObjectInputStream
1761                  (new ByteArrayInputStream(serialBytes(o)));
1762 <            T clone = (T) ois.readObject();
1784 <            if (o == clone) assertImmutable(o);
1785 <            assertSame(o.getClass(), clone.getClass());
1786 <            return clone;
1762 >            clone = (T) ois.readObject();
1763          } catch (Throwable fail) {
1764              threadUnexpectedException(fail);
1789            return null;
1765          }
1766 +        if (o == clone) assertImmutable(o);
1767 +        else assertSame(o.getClass(), clone.getClass());
1768 +        return clone;
1769      }
1770  
1771      /**
# Line 1806 | Line 1784 | public class JSR166TestCase extends Test
1784              (new ByteArrayInputStream(bos.toByteArray()));
1785          T clone = (T) ois.readObject();
1786          if (o == clone) assertImmutable(o);
1787 <        assertSame(o.getClass(), clone.getClass());
1787 >        else assertSame(o.getClass(), clone.getClass());
1788          return clone;
1789      }
1790  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines