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.60 by jsr166, Wed Oct 6 07:49:22 2010 UTC vs.
Revision 1.72 by jsr166, Sun Nov 28 08:43:53 2010 UTC

# Line 7 | Line 7
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;
15   import static java.util.concurrent.TimeUnit.MILLISECONDS;
16 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
17   import java.security.CodeSource;
18   import java.security.Permission;
19   import java.security.PermissionCollection;
# Line 96 | Line 99 | public class JSR166TestCase extends Test
99      private static final boolean useSecurityManager =
100          Boolean.getBoolean("jsr166.useSecurityManager");
101  
102 +    protected static final boolean expensiveTests =
103 +        Boolean.getBoolean("jsr166.expensiveTests");
104 +
105 +    /**
106 +     * If true, report on stdout all "slow" tests, that is, ones that
107 +     * take more than profileThreshold milliseconds to execute.
108 +     */
109 +    private static final boolean profileTests =
110 +        Boolean.getBoolean("jsr166.profileTests");
111 +
112 +    /**
113 +     * The number of milliseconds that tests are permitted for
114 +     * execution without being reported, when profileTests is set.
115 +     */
116 +    private static final long profileThreshold =
117 +        Long.getLong("jsr166.profileThreshold", 100);
118 +
119 +    protected void runTest() throws Throwable {
120 +        if (profileTests)
121 +            runTestProfiled();
122 +        else
123 +            super.runTest();
124 +    }
125 +
126 +    protected void runTestProfiled() throws Throwable {
127 +        long t0 = System.nanoTime();
128 +        try {
129 +            super.runTest();
130 +        } finally {
131 +            long elapsedMillis =
132 +                (System.nanoTime() - t0) / (1000L * 1000L);
133 +            if (elapsedMillis >= profileThreshold)
134 +                System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
135 +        }
136 +    }
137 +
138      /**
139       * Runs all JSR166 unit tests using junit.textui.TestRunner
140       */
# Line 223 | 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 252 | 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 410 | Line 449 | public class JSR166TestCase extends Test
449          try {
450              exec.shutdown();
451              assertTrue("ExecutorService did not terminate in a timely manner",
452 <                       exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
452 >                       exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
453          } catch (SecurityException ok) {
454              // Allowed in case test doesn't have privs
455          } catch (InterruptedException ie) {
# Line 569 | Line 608 | public class JSR166TestCase extends Test
608      }
609  
610      /**
611 +     * Waits up to the specified number of milliseconds for the given
612 +     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
613 +     */
614 +    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
615 +        long timeoutNanos = timeoutMillis * 1000L * 1000L;
616 +        long t0 = System.nanoTime();
617 +        for (;;) {
618 +            Thread.State s = thread.getState();
619 +            if (s == Thread.State.BLOCKED ||
620 +                s == Thread.State.WAITING ||
621 +                s == Thread.State.TIMED_WAITING)
622 +                return;
623 +            else if (s == Thread.State.TERMINATED)
624 +                fail("Unexpected thread termination");
625 +            else if (System.nanoTime() - t0 > timeoutNanos) {
626 +                threadAssertTrue(thread.isAlive());
627 +                return;
628 +            }
629 +            Thread.yield();
630 +        }
631 +    }
632 +
633 +    /**
634 +     * Returns the number of milliseconds since time given by
635 +     * startNanoTime, which must have been previously returned from a
636 +     * call to {@link System.nanoTime()}.
637 +     */
638 +    long millisElapsedSince(long startNanoTime) {
639 +        return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
640 +    }
641 +
642 +    /**
643       * Returns a new started daemon Thread running the given runnable.
644       */
645      Thread newStartedThread(Runnable runnable) {
# Line 710 | Line 781 | public class JSR166TestCase extends Test
781  
782      public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
783          return new CheckedCallable<String>() {
784 <            public String realCall() {
784 >            protected String realCall() {
785                  try {
786                      latch.await();
787                  } catch (InterruptedException quittingTime) {}
# Line 771 | Line 842 | public class JSR166TestCase extends Test
842          }
843      }
844  
845 +    public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
846 +        return new CheckedRunnable() {
847 +            protected void realRun() {
848 +                try {
849 +                    Thread.sleep(timeoutMillis);
850 +                } catch (InterruptedException ok) {}
851 +            }};
852 +    }
853 +
854      public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
855          protected void realRun() {
856              try {
# Line 796 | Line 876 | public class JSR166TestCase extends Test
876          }
877      }
878  
879 +    public interface TrackedRunnable extends Runnable {
880 +        boolean isDone();
881 +    }
882 +
883 +    public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
884 +        return new TrackedRunnable() {
885 +                private volatile boolean done = false;
886 +                public boolean isDone() { return done; }
887 +                public void run() {
888 +                    try {
889 +                        Thread.sleep(timeoutMillis);
890 +                        done = true;
891 +                    } catch (InterruptedException ok) {}
892 +                }
893 +            };
894 +    }
895 +
896      public static class TrackedShortRunnable implements Runnable {
897          public volatile boolean done = false;
898          public void run() {
899              try {
900 +                Thread.sleep(SHORT_DELAY_MS);
901 +                done = true;
902 +            } catch (InterruptedException ok) {}
903 +        }
904 +    }
905 +
906 +    public static class TrackedSmallRunnable implements Runnable {
907 +        public volatile boolean done = false;
908 +        public void run() {
909 +            try {
910                  Thread.sleep(SMALL_DELAY_MS);
911                  done = true;
912              } catch (InterruptedException ok) {}
# Line 902 | Line 1009 | public class JSR166TestCase extends Test
1009          }
1010      }
1011  
1012 +    public void checkEmpty(BlockingQueue q) {
1013 +        try {
1014 +            assertTrue(q.isEmpty());
1015 +            assertEquals(0, q.size());
1016 +            assertNull(q.peek());
1017 +            assertNull(q.poll());
1018 +            assertNull(q.poll(0, MILLISECONDS));
1019 +            assertEquals(q.toString(), "[]");
1020 +            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
1021 +            assertFalse(q.iterator().hasNext());
1022 +            try {
1023 +                q.element();
1024 +                shouldThrow();
1025 +            } catch (NoSuchElementException success) {}
1026 +            try {
1027 +                q.iterator().next();
1028 +                shouldThrow();
1029 +            } catch (NoSuchElementException success) {}
1030 +            try {
1031 +                q.remove();
1032 +                shouldThrow();
1033 +            } catch (NoSuchElementException success) {}
1034 +        } catch (InterruptedException ie) {
1035 +            threadUnexpectedException(ie);
1036 +        }
1037 +    }
1038 +
1039   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines