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.80 by jsr166, Fri May 13 21:48:58 2011 UTC vs.
Revision 1.84 by jsr166, Sat May 28 22:19:27 2011 UTC

# Line 12 | Line 12 | import java.io.ByteArrayOutputStream;
12   import java.io.ObjectInputStream;
13   import java.io.ObjectOutputStream;
14   import java.util.Arrays;
15 + import java.util.Date;
16   import java.util.NoSuchElementException;
17   import java.util.PropertyPermission;
18   import java.util.concurrent.*;
19 + import java.util.concurrent.atomic.AtomicBoolean;
20   import java.util.concurrent.atomic.AtomicReference;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
22   import static java.util.concurrent.TimeUnit.NANOSECONDS;
# Line 258 | Line 260 | public class JSR166TestCase extends Test
260          return 50;
261      }
262  
261
263      /**
264       * Sets delays as multiples of SHORT_DELAY.
265       */
# Line 270 | Line 271 | public class JSR166TestCase extends Test
271      }
272  
273      /**
274 +     * Returns a timeout in milliseconds to be used in tests that
275 +     * verify that operations block or time out.
276 +     */
277 +    long timeoutMillis() {
278 +        return SHORT_DELAY_MS / 4;
279 +    }
280 +
281 +    /**
282 +     * Returns a new Date instance representing a time delayMillis
283 +     * milliseconds in the future.
284 +     */
285 +    Date delayedDate(long delayMillis) {
286 +        return new Date(System.currentTimeMillis() + delayMillis);
287 +    }
288 +
289 +    /**
290       * The first exception encountered if any threadAssertXXX method fails.
291       */
292      private final AtomicReference<Throwable> threadFailure
# Line 441 | Line 458 | public class JSR166TestCase extends Test
458          else {
459              AssertionFailedError afe =
460                  new AssertionFailedError("unexpected exception: " + t);
461 <            t.initCause(t);
461 >            afe.initCause(t);
462              throw afe;
463          }
464      }
465  
466      /**
467 <     * Delays, via Thread.sleep for the given millisecond delay, but
467 >     * Delays, via Thread.sleep, for the given millisecond delay, but
468       * if the sleep is shorter than specified, may re-sleep or yield
469       * until time elapses.
470       */
471 <    public static void delay(long millis) throws InterruptedException {
471 >    static void delay(long millis) throws InterruptedException {
472          long startTime = System.nanoTime();
473          long ns = millis * 1000 * 1000;
474          for (;;) {
# Line 470 | Line 487 | public class JSR166TestCase extends Test
487      /**
488       * Waits out termination of a thread pool or fails doing so.
489       */
490 <    public void joinPool(ExecutorService exec) {
490 >    void joinPool(ExecutorService exec) {
491          try {
492              exec.shutdown();
493              assertTrue("ExecutorService did not terminate in a timely manner",
# Line 483 | Line 500 | public class JSR166TestCase extends Test
500      }
501  
502      /**
503 +     * Checks that thread does not terminate within the default
504 +     * millisecond delay of {@code timeoutMillis()}.
505 +     */
506 +    void assertThreadStaysAlive(Thread thread) {
507 +        assertThreadStaysAlive(thread, timeoutMillis());
508 +    }
509 +
510 +    /**
511       * Checks that thread does not terminate within the given millisecond delay.
512       */
513 <    public void assertThreadStaysAlive(Thread thread, long millis) {
513 >    void assertThreadStaysAlive(Thread thread, long millis) {
514          try {
515              // No need to optimize the failing case via Thread.join.
516              delay(millis);
# Line 496 | Line 521 | public class JSR166TestCase extends Test
521      }
522  
523      /**
524 +     * Checks that future.get times out, with the default timeout of
525 +     * {@code timeoutMillis()}.
526 +     */
527 +    void assertFutureTimesOut(Future future) {
528 +        assertFutureTimesOut(future, timeoutMillis());
529 +    }
530 +
531 +    /**
532 +     * Checks that future.get times out, with the given millisecond timeout.
533 +     */
534 +    void assertFutureTimesOut(Future future, long timeoutMillis) {
535 +        long startTime = System.nanoTime();
536 +        try {
537 +            future.get(timeoutMillis, MILLISECONDS);
538 +            shouldThrow();
539 +        } catch (TimeoutException success) {
540 +        } catch (Exception e) {
541 +            threadUnexpectedException(e);
542 +        } finally { future.cancel(true); }
543 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
544 +    }
545 +
546 +    /**
547       * Fails with message "should throw exception".
548       */
549      public void shouldThrow() {
# Line 635 | Line 683 | public class JSR166TestCase extends Test
683      }
684  
685      /**
638     * Sleeps until the timeout has elapsed, or interrupted.
639     * Does <em>NOT</em> throw InterruptedException.
640     */
641    void sleepTillInterrupted(long timeoutMillis) {
642        try {
643            Thread.sleep(timeoutMillis);
644        } catch (InterruptedException wakeup) {}
645    }
646
647    /**
686       * Waits up to the specified number of milliseconds for the given
687       * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
688       */
# Line 705 | Line 743 | public class JSR166TestCase extends Test
743          } catch (InterruptedException ie) {
744              threadUnexpectedException(ie);
745          } finally {
746 <            if (t.isAlive()) {
746 >            if (t.getState() != Thread.State.TERMINATED) {
747                  t.interrupt();
748                  fail("Test timed out");
749              }
# Line 783 | Line 821 | public class JSR166TestCase extends Test
821                  realRun();
822                  threadShouldThrow("InterruptedException");
823              } catch (InterruptedException success) {
824 +                threadAssertFalse(Thread.interrupted());
825              } catch (Throwable t) {
826                  threadUnexpectedException(t);
827              }
# Line 812 | Line 851 | public class JSR166TestCase extends Test
851                  threadShouldThrow("InterruptedException");
852                  return result;
853              } catch (InterruptedException success) {
854 +                threadAssertFalse(Thread.interrupted());
855              } catch (Throwable t) {
856                  threadUnexpectedException(t);
857              }
# Line 858 | Line 898 | public class JSR166TestCase extends Test
898          }
899      }
900  
901 + //     /**
902 + //      * Spin-waits up to LONG_DELAY_MS until flag becomes true.
903 + //      */
904 + //     public void await(AtomicBoolean flag) {
905 + //         await(flag, LONG_DELAY_MS);
906 + //     }
907 +
908 + //     /**
909 + //      * Spin-waits up to the specified timeout until flag becomes true.
910 + //      */
911 + //     public void await(AtomicBoolean flag, long timeoutMillis) {
912 + //         long startTime = System.nanoTime();
913 + //         while (!flag.get()) {
914 + //             if (millisElapsedSince(startTime) > timeoutMillis)
915 + //                 throw new AssertionFailedError("timed out");
916 + //             Thread.yield();
917 + //         }
918 + //     }
919 +
920      public static class NPETask implements Callable<String> {
921          public String call() { throw new NullPointerException(); }
922      }
# Line 1078 | Line 1137 | public class JSR166TestCase extends Test
1137          }
1138      }
1139  
1140 <    public void checkEmpty(BlockingQueue q) {
1140 >    void checkEmpty(BlockingQueue q) {
1141          try {
1142              assertTrue(q.isEmpty());
1143              assertEquals(0, q.size());
# Line 1106 | Line 1165 | public class JSR166TestCase extends Test
1165      }
1166  
1167      @SuppressWarnings("unchecked")
1168 <    public <T> T serialClone(T o) {
1168 >    <T> T serialClone(T o) {
1169          try {
1170              ByteArrayOutputStream bos = new ByteArrayOutputStream();
1171              ObjectOutputStream oos = new ObjectOutputStream(bos);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines