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.67 by jsr166, Fri Oct 29 06:58:56 2010 UTC

# Line 11 | Line 11 | import java.util.PropertyPermission;
11   import java.util.concurrent.*;
12   import java.util.concurrent.atomic.AtomicReference;
13   import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
15   import java.security.CodeSource;
16   import java.security.Permission;
17   import java.security.PermissionCollection;
# Line 96 | Line 97 | public class JSR166TestCase extends Test
97      private static final boolean useSecurityManager =
98          Boolean.getBoolean("jsr166.useSecurityManager");
99  
100 +    protected static final boolean expensiveTests =
101 +        Boolean.getBoolean("jsr166.expensiveTests");
102 +
103 +    /**
104 +     * If true, report on stdout all "slow" tests, that is, ones that
105 +     * take more than profileThreshold milliseconds to execute.
106 +     */
107 +    private static final boolean profileTests =
108 +        Boolean.getBoolean("jsr166.profileTests");
109 +
110 +    /**
111 +     * The number of milliseconds that tests are permitted for
112 +     * execution without being reported, when profileTests is set.
113 +     */
114 +    private static final long profileThreshold =
115 +        Long.getLong("jsr166.profileThreshold", 100);
116 +
117 +    protected void runTest() throws Throwable {
118 +        if (profileTests)
119 +            runTestProfiled();
120 +        else
121 +            super.runTest();
122 +    }
123 +
124 +    protected void runTestProfiled() throws Throwable {
125 +        long t0 = System.nanoTime();
126 +        try {
127 +            super.runTest();
128 +        } finally {
129 +            long elapsedMillis =
130 +                (System.nanoTime() - t0) / (1000L * 1000L);
131 +            if (elapsedMillis >= profileThreshold)
132 +                System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
133 +        }
134 +    }
135 +
136      /**
137       * Runs all JSR166 unit tests using junit.textui.TestRunner
138       */
# Line 569 | Line 606 | public class JSR166TestCase extends Test
606      }
607  
608      /**
609 +     * Waits up to the specified number of milliseconds for the given
610 +     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
611 +     */
612 +    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
613 +        long timeoutNanos = timeoutMillis * 1000L * 1000L;
614 +        long t0 = System.nanoTime();
615 +        for (;;) {
616 +            Thread.State s = thread.getState();
617 +            if (s == Thread.State.BLOCKED ||
618 +                s == Thread.State.WAITING ||
619 +                s == Thread.State.TIMED_WAITING)
620 +                return;
621 +            else if (s == Thread.State.TERMINATED)
622 +                fail("Unexpected thread termination");
623 +            else if (System.nanoTime() - t0 > timeoutNanos) {
624 +                threadAssertTrue(thread.isAlive());
625 +                return;
626 +            }
627 +            Thread.yield();
628 +        }
629 +    }
630 +
631 +    /**
632 +     * Returns the number of milliseconds since time given by
633 +     * startNanoTime, which must have been previously returned from a
634 +     * call to {@link System.nanoTime()}.
635 +     */
636 +    long millisElapsedSince(long startNanoTime) {
637 +        return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
638 +    }
639 +    
640 +    /**
641       * Returns a new started daemon Thread running the given runnable.
642       */
643      Thread newStartedThread(Runnable runnable) {
# Line 710 | Line 779 | public class JSR166TestCase extends Test
779  
780      public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
781          return new CheckedCallable<String>() {
782 <            public String realCall() {
782 >            protected String realCall() {
783                  try {
784                      latch.await();
785                  } catch (InterruptedException quittingTime) {}
# Line 771 | Line 840 | public class JSR166TestCase extends Test
840          }
841      }
842  
843 +    public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
844 +        return new CheckedRunnable() {
845 +            protected void realRun() {
846 +                try {
847 +                    Thread.sleep(timeoutMillis);
848 +                } catch (InterruptedException ok) {}
849 +            }};
850 +    }
851 +
852      public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
853          protected void realRun() {
854              try {
# Line 796 | Line 874 | public class JSR166TestCase extends Test
874          }
875      }
876  
877 +    public interface TrackedRunnable extends Runnable {
878 +        boolean isDone();
879 +    }
880 +
881 +    public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
882 +        return new TrackedRunnable() {
883 +                private volatile boolean done = false;
884 +                public boolean isDone() { return done; }
885 +                public void run() {
886 +                    try {
887 +                        Thread.sleep(timeoutMillis);
888 +                        done = true;
889 +                    } catch (InterruptedException ok) {}
890 +                }
891 +            };
892 +    }
893 +
894      public static class TrackedShortRunnable implements Runnable {
895          public volatile boolean done = false;
896          public void run() {
897              try {
898 +                Thread.sleep(SHORT_DELAY_MS);
899 +                done = true;
900 +            } catch (InterruptedException ok) {}
901 +        }
902 +    }
903 +
904 +    public static class TrackedSmallRunnable implements Runnable {
905 +        public volatile boolean done = false;
906 +        public void run() {
907 +            try {
908                  Thread.sleep(SMALL_DELAY_MS);
909                  done = true;
910              } catch (InterruptedException ok) {}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines