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.67 by jsr166, Fri Oct 29 06:58:56 2010 UTC vs.
Revision 1.76 by dl, Fri May 6 11:22:07 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 + import java.util.Arrays;
11 + import java.util.NoSuchElementException;
12   import java.util.PropertyPermission;
13   import java.util.concurrent.*;
14   import java.util.concurrent.atomic.AtomicReference;
# Line 260 | Line 262 | public class JSR166TestCase extends Test
262          SHORT_DELAY_MS = getShortDelay();
263          SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
264          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
265 <        LONG_DELAY_MS   = SHORT_DELAY_MS * 50;
265 >        LONG_DELAY_MS   = SHORT_DELAY_MS * 200;
266      }
267  
268      /**
# Line 289 | Line 291 | public class JSR166TestCase extends Test
291       * earlier by threadRecordFailure.
292       */
293      public void tearDown() throws Exception {
294 <        Throwable t = threadFailure.get();
294 >        Throwable t = threadFailure.getAndSet(null);
295          if (t != null) {
296              if (t instanceof Error)
297                  throw (Error) t;
# Line 441 | 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) {
470          try {
471              exec.shutdown();
472              assertTrue("ExecutorService did not terminate in a timely manner",
473 <                       exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
473 >                       exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
474          } catch (SecurityException ok) {
475              // Allowed in case test doesn't have privs
476          } catch (InterruptedException ie) {
# Line 586 | Line 609 | public class JSR166TestCase extends Test
609       */
610      void sleep(long millis) {
611          try {
612 <            Thread.sleep(millis);
612 >            delay(millis);
613          } catch (InterruptedException ie) {
614              AssertionFailedError afe =
615                  new AssertionFailedError("Unexpected InterruptedException");
# Line 629 | Line 652 | public class JSR166TestCase extends Test
652      }
653  
654      /**
655 +     * Waits up to LONG_DELAY_MS for the given thread to enter a wait
656 +     * state: BLOCKED, WAITING, or TIMED_WAITING.
657 +     */
658 +    void waitForThreadToEnterWaitState(Thread thread) {
659 +        waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
660 +    }
661 +
662 +    /**
663       * Returns the number of milliseconds since time given by
664       * startNanoTime, which must have been previously returned from a
665       * call to {@link System.nanoTime()}.
# Line 636 | Line 667 | public class JSR166TestCase extends Test
667      long millisElapsedSince(long startNanoTime) {
668          return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
669      }
670 <    
670 >
671      /**
672       * Returns a new started daemon Thread running the given runnable.
673       */
# Line 665 | Line 696 | public class JSR166TestCase extends Test
696          }
697      }
698  
699 +    /**
700 +     * Waits for LONG_DELAY_MS milliseconds for the thread to
701 +     * terminate (using {@link Thread#join(long)}), else interrupts
702 +     * the thread (in the hope that it may terminate later) and fails.
703 +     */
704 +    void awaitTermination(Thread t) {
705 +        awaitTermination(t, LONG_DELAY_MS);
706 +    }
707 +
708      // Some convenient Runnable classes
709  
710      public abstract class CheckedRunnable implements Runnable {
# Line 787 | Line 827 | public class JSR166TestCase extends Test
827              }};
828      }
829  
830 +    public Runnable awaiter(final CountDownLatch latch) {
831 +        return new CheckedRunnable() {
832 +            public void realRun() throws InterruptedException {
833 +                latch.await();
834 +            }};
835 +    }
836 +
837      public static class NPETask implements Callable<String> {
838          public String call() { throw new NullPointerException(); }
839      }
# Line 797 | Line 844 | public class JSR166TestCase extends Test
844  
845      public class ShortRunnable extends CheckedRunnable {
846          protected void realRun() throws Throwable {
847 <            Thread.sleep(SHORT_DELAY_MS);
847 >            delay(SHORT_DELAY_MS);
848          }
849      }
850  
851      public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
852          protected void realRun() throws InterruptedException {
853 <            Thread.sleep(SHORT_DELAY_MS);
853 >            delay(SHORT_DELAY_MS);
854          }
855      }
856  
857      public class SmallRunnable extends CheckedRunnable {
858          protected void realRun() throws Throwable {
859 <            Thread.sleep(SMALL_DELAY_MS);
859 >            delay(SMALL_DELAY_MS);
860          }
861      }
862  
863      public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
864          protected void realRun() {
865              try {
866 <                Thread.sleep(SMALL_DELAY_MS);
866 >                delay(SMALL_DELAY_MS);
867              } catch (InterruptedException ok) {}
868          }
869      }
870  
871      public class SmallCallable extends CheckedCallable {
872          protected Object realCall() throws InterruptedException {
873 <            Thread.sleep(SMALL_DELAY_MS);
873 >            delay(SMALL_DELAY_MS);
874              return Boolean.TRUE;
875          }
876      }
877  
878      public class MediumRunnable extends CheckedRunnable {
879          protected void realRun() throws Throwable {
880 <            Thread.sleep(MEDIUM_DELAY_MS);
880 >            delay(MEDIUM_DELAY_MS);
881          }
882      }
883  
884      public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
885          protected void realRun() throws InterruptedException {
886 <            Thread.sleep(MEDIUM_DELAY_MS);
886 >            delay(MEDIUM_DELAY_MS);
887          }
888      }
889  
# Line 844 | Line 891 | public class JSR166TestCase extends Test
891          return new CheckedRunnable() {
892              protected void realRun() {
893                  try {
894 <                    Thread.sleep(timeoutMillis);
894 >                    delay(timeoutMillis);
895                  } catch (InterruptedException ok) {}
896              }};
897      }
# Line 852 | Line 899 | public class JSR166TestCase extends Test
899      public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
900          protected void realRun() {
901              try {
902 <                Thread.sleep(MEDIUM_DELAY_MS);
902 >                delay(MEDIUM_DELAY_MS);
903              } catch (InterruptedException ok) {}
904          }
905      }
# Line 860 | Line 907 | public class JSR166TestCase extends Test
907      public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
908          protected void realRun() {
909              try {
910 <                Thread.sleep(LONG_DELAY_MS);
910 >                delay(LONG_DELAY_MS);
911              } catch (InterruptedException ok) {}
912          }
913      }
# Line 884 | Line 931 | public class JSR166TestCase extends Test
931                  public boolean isDone() { return done; }
932                  public void run() {
933                      try {
934 <                        Thread.sleep(timeoutMillis);
934 >                        delay(timeoutMillis);
935                          done = true;
936                      } catch (InterruptedException ok) {}
937                  }
# Line 895 | Line 942 | public class JSR166TestCase extends Test
942          public volatile boolean done = false;
943          public void run() {
944              try {
945 <                Thread.sleep(SHORT_DELAY_MS);
945 >                delay(SHORT_DELAY_MS);
946                  done = true;
947              } catch (InterruptedException ok) {}
948          }
# Line 905 | Line 952 | public class JSR166TestCase extends Test
952          public volatile boolean done = false;
953          public void run() {
954              try {
955 <                Thread.sleep(SMALL_DELAY_MS);
955 >                delay(SMALL_DELAY_MS);
956                  done = true;
957              } catch (InterruptedException ok) {}
958          }
# Line 915 | Line 962 | public class JSR166TestCase extends Test
962          public volatile boolean done = false;
963          public void run() {
964              try {
965 <                Thread.sleep(MEDIUM_DELAY_MS);
965 >                delay(MEDIUM_DELAY_MS);
966                  done = true;
967              } catch (InterruptedException ok) {}
968          }
# Line 925 | Line 972 | public class JSR166TestCase extends Test
972          public volatile boolean done = false;
973          public void run() {
974              try {
975 <                Thread.sleep(LONG_DELAY_MS);
975 >                delay(LONG_DELAY_MS);
976                  done = true;
977              } catch (InterruptedException ok) {}
978          }
# Line 942 | Line 989 | public class JSR166TestCase extends Test
989          public volatile boolean done = false;
990          public Object call() {
991              try {
992 <                Thread.sleep(SMALL_DELAY_MS);
992 >                delay(SMALL_DELAY_MS);
993                  done = true;
994              } catch (InterruptedException ok) {}
995              return Boolean.TRUE;
# Line 1007 | Line 1054 | public class JSR166TestCase extends Test
1054          }
1055      }
1056  
1057 +    public void checkEmpty(BlockingQueue q) {
1058 +        try {
1059 +            assertTrue(q.isEmpty());
1060 +            assertEquals(0, q.size());
1061 +            assertNull(q.peek());
1062 +            assertNull(q.poll());
1063 +            assertNull(q.poll(0, MILLISECONDS));
1064 +            assertEquals(q.toString(), "[]");
1065 +            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
1066 +            assertFalse(q.iterator().hasNext());
1067 +            try {
1068 +                q.element();
1069 +                shouldThrow();
1070 +            } catch (NoSuchElementException success) {}
1071 +            try {
1072 +                q.iterator().next();
1073 +                shouldThrow();
1074 +            } catch (NoSuchElementException success) {}
1075 +            try {
1076 +                q.remove();
1077 +                shouldThrow();
1078 +            } catch (NoSuchElementException success) {}
1079 +        } catch (InterruptedException ie) {
1080 +            threadUnexpectedException(ie);
1081 +        }
1082 +    }
1083 +
1084   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines