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.77 by jsr166, Fri May 6 17:26:29 2011 UTC vs.
Revision 1.82 by jsr166, Tue May 24 23:34:03 2011 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 + import java.io.ByteArrayInputStream;
11 + 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 266 | 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 437 | Line 459 | public class JSR166TestCase extends Test
459          else {
460              AssertionFailedError afe =
461                  new AssertionFailedError("unexpected exception: " + t);
462 <            t.initCause(t);
462 >            afe.initCause(t);
463              throw afe;
464          }
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 466 | 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 478 | Line 500 | public class JSR166TestCase extends Test
500          }
501      }
502  
503 +    /**
504 +     * Checks that thread does not terminate within the default
505 +     * millisecond delay of {@code timeoutMillis()}.
506 +     */
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 +            // No need to optimize the failing case via Thread.join.
517 +            delay(millis);
518 +            assertTrue(thread.isAlive());
519 +        } catch (InterruptedException ie) {
520 +            fail("Unexpected InterruptedException");
521 +        }
522 +    }
523  
524      /**
525       * Fails with message "should throw exception".
# Line 619 | Line 661 | public class JSR166TestCase extends Test
661      }
662  
663      /**
622     * Sleeps until the timeout has elapsed, or interrupted.
623     * Does <em>NOT</em> throw InterruptedException.
624     */
625    void sleepTillInterrupted(long timeoutMillis) {
626        try {
627            Thread.sleep(timeoutMillis);
628        } catch (InterruptedException wakeup) {}
629    }
630
631    /**
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 767 | 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 796 | 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 830 | Line 864 | public class JSR166TestCase extends Test
864      public Runnable awaiter(final CountDownLatch latch) {
865          return new CheckedRunnable() {
866              public void realRun() throws InterruptedException {
867 <                latch.await();
867 >                await(latch);
868              }};
869      }
870  
871 +    public void await(CountDownLatch latch) {
872 +        try {
873 +            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
874 +        } catch (Throwable t) {
875 +            threadUnexpectedException(t);
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 1054 | 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 1081 | Line 1142 | public class JSR166TestCase extends Test
1142          }
1143      }
1144  
1145 +    @SuppressWarnings("unchecked")
1146 +    <T> T serialClone(T o) {
1147 +        try {
1148 +            ByteArrayOutputStream bos = new ByteArrayOutputStream();
1149 +            ObjectOutputStream oos = new ObjectOutputStream(bos);
1150 +            oos.writeObject(o);
1151 +            oos.flush();
1152 +            oos.close();
1153 +            ByteArrayInputStream bin =
1154 +                new ByteArrayInputStream(bos.toByteArray());
1155 +            ObjectInputStream ois = new ObjectInputStream(bin);
1156 +            return (T) ois.readObject();
1157 +        } catch (Throwable t) {
1158 +            threadUnexpectedException(t);
1159 +            return null;
1160 +        }
1161 +    }
1162   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines