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.75 by jsr166, Tue May 3 06:08:49 2011 UTC vs.
Revision 1.78 by jsr166, Sat May 7 19:03:26 2011 UTC

# Line 443 | Line 443 | public class JSR166TestCase extends Test
443      }
444  
445      /**
446 +     * Delays, via Thread.sleep for the given millisecond delay, but
447 +     * if the sleep is shorter than specified, may re-sleep or yield
448 +     * until time elapses.
449 +     */
450 +    public static void delay(long ms) throws InterruptedException {
451 +        long startTime = System.nanoTime();
452 +        long ns = ms * 1000 * 1000;
453 +        for (;;) {
454 +            if (ms > 0L)
455 +                Thread.sleep(ms);
456 +            else // too short to sleep
457 +                Thread.yield();
458 +            long d = ns - (System.nanoTime() - startTime);
459 +            if (d > 0L)
460 +                ms = d / (1000 * 1000);
461 +            else
462 +                break;
463 +        }
464 +    }
465 +
466 +    /**
467       * Waits out termination of a thread pool or fails doing so.
468       */
469      public void joinPool(ExecutorService exec) {
# Line 457 | Line 478 | public class JSR166TestCase extends Test
478          }
479      }
480  
481 +    /**
482 +     * Checks that thread does not terminate within timeoutMillis
483 +     * milliseconds (that is, Thread.join times out).
484 +     */
485 +    public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) {
486 +        try {
487 +            long startTime = System.nanoTime();
488 +            thread.join(timeoutMillis);
489 +            assertTrue(thread.isAlive());
490 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
491 +        } catch (InterruptedException ie) {
492 +            fail("Unexpected InterruptedException");
493 +        }
494 +    }
495  
496      /**
497       * Fails with message "should throw exception".
# Line 588 | Line 623 | public class JSR166TestCase extends Test
623       */
624      void sleep(long millis) {
625          try {
626 <            Thread.sleep(millis);
626 >            delay(millis);
627          } catch (InterruptedException ie) {
628              AssertionFailedError afe =
629                  new AssertionFailedError("Unexpected InterruptedException");
# Line 823 | Line 858 | public class JSR166TestCase extends Test
858  
859      public class ShortRunnable extends CheckedRunnable {
860          protected void realRun() throws Throwable {
861 <            Thread.sleep(SHORT_DELAY_MS);
861 >            delay(SHORT_DELAY_MS);
862          }
863      }
864  
865      public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
866          protected void realRun() throws InterruptedException {
867 <            Thread.sleep(SHORT_DELAY_MS);
867 >            delay(SHORT_DELAY_MS);
868          }
869      }
870  
871      public class SmallRunnable extends CheckedRunnable {
872          protected void realRun() throws Throwable {
873 <            Thread.sleep(SMALL_DELAY_MS);
873 >            delay(SMALL_DELAY_MS);
874          }
875      }
876  
877      public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
878          protected void realRun() {
879              try {
880 <                Thread.sleep(SMALL_DELAY_MS);
880 >                delay(SMALL_DELAY_MS);
881              } catch (InterruptedException ok) {}
882          }
883      }
884  
885      public class SmallCallable extends CheckedCallable {
886          protected Object realCall() throws InterruptedException {
887 <            Thread.sleep(SMALL_DELAY_MS);
887 >            delay(SMALL_DELAY_MS);
888              return Boolean.TRUE;
889          }
890      }
891  
892      public class MediumRunnable extends CheckedRunnable {
893          protected void realRun() throws Throwable {
894 <            Thread.sleep(MEDIUM_DELAY_MS);
894 >            delay(MEDIUM_DELAY_MS);
895          }
896      }
897  
898      public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
899          protected void realRun() throws InterruptedException {
900 <            Thread.sleep(MEDIUM_DELAY_MS);
900 >            delay(MEDIUM_DELAY_MS);
901          }
902      }
903  
# Line 870 | Line 905 | public class JSR166TestCase extends Test
905          return new CheckedRunnable() {
906              protected void realRun() {
907                  try {
908 <                    Thread.sleep(timeoutMillis);
908 >                    delay(timeoutMillis);
909                  } catch (InterruptedException ok) {}
910              }};
911      }
# Line 878 | Line 913 | public class JSR166TestCase extends Test
913      public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
914          protected void realRun() {
915              try {
916 <                Thread.sleep(MEDIUM_DELAY_MS);
916 >                delay(MEDIUM_DELAY_MS);
917              } catch (InterruptedException ok) {}
918          }
919      }
# Line 886 | Line 921 | public class JSR166TestCase extends Test
921      public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
922          protected void realRun() {
923              try {
924 <                Thread.sleep(LONG_DELAY_MS);
924 >                delay(LONG_DELAY_MS);
925              } catch (InterruptedException ok) {}
926          }
927      }
# Line 910 | Line 945 | public class JSR166TestCase extends Test
945                  public boolean isDone() { return done; }
946                  public void run() {
947                      try {
948 <                        Thread.sleep(timeoutMillis);
948 >                        delay(timeoutMillis);
949                          done = true;
950                      } catch (InterruptedException ok) {}
951                  }
# Line 921 | Line 956 | public class JSR166TestCase extends Test
956          public volatile boolean done = false;
957          public void run() {
958              try {
959 <                Thread.sleep(SHORT_DELAY_MS);
959 >                delay(SHORT_DELAY_MS);
960                  done = true;
961              } catch (InterruptedException ok) {}
962          }
# Line 931 | Line 966 | public class JSR166TestCase extends Test
966          public volatile boolean done = false;
967          public void run() {
968              try {
969 <                Thread.sleep(SMALL_DELAY_MS);
969 >                delay(SMALL_DELAY_MS);
970                  done = true;
971              } catch (InterruptedException ok) {}
972          }
# Line 941 | Line 976 | public class JSR166TestCase extends Test
976          public volatile boolean done = false;
977          public void run() {
978              try {
979 <                Thread.sleep(MEDIUM_DELAY_MS);
979 >                delay(MEDIUM_DELAY_MS);
980                  done = true;
981              } catch (InterruptedException ok) {}
982          }
# Line 951 | Line 986 | public class JSR166TestCase extends Test
986          public volatile boolean done = false;
987          public void run() {
988              try {
989 <                Thread.sleep(LONG_DELAY_MS);
989 >                delay(LONG_DELAY_MS);
990                  done = true;
991              } catch (InterruptedException ok) {}
992          }
# Line 968 | Line 1003 | public class JSR166TestCase extends Test
1003          public volatile boolean done = false;
1004          public Object call() {
1005              try {
1006 <                Thread.sleep(SMALL_DELAY_MS);
1006 >                delay(SMALL_DELAY_MS);
1007                  done = true;
1008              } catch (InterruptedException ok) {}
1009              return Boolean.TRUE;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines