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.79 by jsr166, Mon May 9 20:00:19 2011 UTC vs.
Revision 1.81 by jsr166, Sat May 21 06:24:33 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 270 | Line 272 | public class JSR166TestCase extends Test
272      }
273  
274      /**
275 +     * Returns a timeout in milliseconds to be used in tests that
276 +     * verify that operations block or time out.
277 +     */
278 +    long timeoutMillis() {
279 +        return SHORT_DELAY_MS / 4;
280 +    }
281 +
282 +    /**
283 +     * Returns a new Date instance representing a time delayMillis
284 +     * milliseconds in the future.
285 +     */
286 +    Date delayedDate(long delayMillis) {
287 +        return new Date(new Date().getTime() + delayMillis);
288 +    }
289 +
290 +    /**
291       * The first exception encountered if any threadAssertXXX method fails.
292       */
293      private final AtomicReference<Throwable> threadFailure
# Line 447 | Line 465 | public class JSR166TestCase extends Test
465      }
466  
467      /**
468 <     * Delays, via Thread.sleep for the given millisecond delay, but
468 >     * Delays, via Thread.sleep, for the given millisecond delay, but
469       * if the sleep is shorter than specified, may re-sleep or yield
470       * until time elapses.
471       */
472 <    public static void delay(long ms) throws InterruptedException {
472 >    static void delay(long millis) throws InterruptedException {
473          long startTime = System.nanoTime();
474 <        long ns = ms * 1000 * 1000;
474 >        long ns = millis * 1000 * 1000;
475          for (;;) {
476 <            if (ms > 0L)
477 <                Thread.sleep(ms);
476 >            if (millis > 0L)
477 >                Thread.sleep(millis);
478              else // too short to sleep
479                  Thread.yield();
480              long d = ns - (System.nanoTime() - startTime);
481              if (d > 0L)
482 <                ms = d / (1000 * 1000);
482 >                millis = d / (1000 * 1000);
483              else
484                  break;
485          }
# Line 470 | Line 488 | public class JSR166TestCase extends Test
488      /**
489       * Waits out termination of a thread pool or fails doing so.
490       */
491 <    public void joinPool(ExecutorService exec) {
491 >    void joinPool(ExecutorService exec) {
492          try {
493              exec.shutdown();
494              assertTrue("ExecutorService did not terminate in a timely manner",
# Line 483 | Line 501 | public class JSR166TestCase extends Test
501      }
502  
503      /**
504 <     * Checks that thread does not terminate within timeoutMillis
505 <     * milliseconds (that is, Thread.join times out).
504 >     * Checks that thread does not terminate within the default
505 >     * millisecond delay of {@code timeoutMillis()}.
506       */
507 <    public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) {
507 >    void assertThreadStaysAlive(Thread thread) {
508 >        assertThreadStaysAlive(thread, timeoutMillis());
509 >    }
510 >
511 >    /**
512 >     * Checks that thread does not terminate within the given millisecond delay.
513 >     */
514 >    void assertThreadStaysAlive(Thread thread, long millis) {
515          try {
516 <            long startTime = System.nanoTime();
517 <            thread.join(timeoutMillis);
516 >            // No need to optimize the failing case via Thread.join.
517 >            delay(millis);
518              assertTrue(thread.isAlive());
494            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
519          } catch (InterruptedException ie) {
520              fail("Unexpected InterruptedException");
521          }
# Line 637 | Line 661 | public class JSR166TestCase extends Test
661      }
662  
663      /**
640     * Sleeps until the timeout has elapsed, or interrupted.
641     * Does <em>NOT</em> throw InterruptedException.
642     */
643    void sleepTillInterrupted(long timeoutMillis) {
644        try {
645            Thread.sleep(timeoutMillis);
646        } catch (InterruptedException wakeup) {}
647    }
648
649    /**
664       * Waits up to the specified number of milliseconds for the given
665       * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
666       */
# Line 785 | Line 799 | public class JSR166TestCase extends Test
799                  realRun();
800                  threadShouldThrow("InterruptedException");
801              } catch (InterruptedException success) {
802 +                threadAssertFalse(Thread.interrupted());
803              } catch (Throwable t) {
804                  threadUnexpectedException(t);
805              }
# Line 814 | Line 829 | public class JSR166TestCase extends Test
829                  threadShouldThrow("InterruptedException");
830                  return result;
831              } catch (InterruptedException success) {
832 +                threadAssertFalse(Thread.interrupted());
833              } catch (Throwable t) {
834                  threadUnexpectedException(t);
835              }
# Line 860 | Line 876 | public class JSR166TestCase extends Test
876          }
877      }
878  
879 + //     /**
880 + //      * Spin-waits up to LONG_DELAY_MS until flag becomes true.
881 + //      */
882 + //     public void await(AtomicBoolean flag) {
883 + //         await(flag, LONG_DELAY_MS);
884 + //     }
885 +
886 + //     /**
887 + //      * Spin-waits up to the specified timeout until flag becomes true.
888 + //      */
889 + //     public void await(AtomicBoolean flag, long timeoutMillis) {
890 + //         long startTime = System.nanoTime();
891 + //         while (!flag.get()) {
892 + //             if (millisElapsedSince(startTime) > timeoutMillis)
893 + //                 throw new AssertionFailedError("timed out");
894 + //             Thread.yield();
895 + //         }
896 + //     }
897 +
898      public static class NPETask implements Callable<String> {
899          public String call() { throw new NullPointerException(); }
900      }
# Line 1080 | Line 1115 | public class JSR166TestCase extends Test
1115          }
1116      }
1117  
1118 <    public void checkEmpty(BlockingQueue q) {
1118 >    void checkEmpty(BlockingQueue q) {
1119          try {
1120              assertTrue(q.isEmpty());
1121              assertEquals(0, q.size());
# Line 1108 | Line 1143 | public class JSR166TestCase extends Test
1143      }
1144  
1145      @SuppressWarnings("unchecked")
1146 <    public <T> T serialClone(T o) {
1146 >    <T> T serialClone(T o) {
1147          try {
1148              ByteArrayOutputStream bos = new ByteArrayOutputStream();
1149              ObjectOutputStream oos = new ObjectOutputStream(bos);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines