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; |
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 |
|
*/ |
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 |
+ |
System.nanoTime() - t0 > timeoutNanos) |
621 |
+ |
return; |
622 |
+ |
Thread.yield(); |
623 |
+ |
} |
624 |
+ |
} |
625 |
+ |
|
626 |
+ |
/** |
627 |
+ |
* Returns the number of milliseconds since time given by |
628 |
+ |
* startNanoTime, which must have been previously returned from a |
629 |
+ |
* call to {@link System.nanoTime()}. |
630 |
+ |
*/ |
631 |
+ |
long millisElapsedSince(long startNanoTime) { |
632 |
+ |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
633 |
+ |
} |
634 |
+ |
|
635 |
+ |
/** |
636 |
|
* Returns a new started daemon Thread running the given runnable. |
637 |
|
*/ |
638 |
|
Thread newStartedThread(Runnable runnable) { |
774 |
|
|
775 |
|
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
776 |
|
return new CheckedCallable<String>() { |
777 |
< |
public String realCall() { |
777 |
> |
protected String realCall() { |
778 |
|
try { |
779 |
|
latch.await(); |
780 |
|
} catch (InterruptedException quittingTime) {} |
835 |
|
} |
836 |
|
} |
837 |
|
|
838 |
+ |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
839 |
+ |
return new CheckedRunnable() { |
840 |
+ |
protected void realRun() { |
841 |
+ |
try { |
842 |
+ |
Thread.sleep(timeoutMillis); |
843 |
+ |
} catch (InterruptedException ok) {} |
844 |
+ |
}}; |
845 |
+ |
} |
846 |
+ |
|
847 |
|
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
848 |
|
protected void realRun() { |
849 |
|
try { |
869 |
|
} |
870 |
|
} |
871 |
|
|
872 |
+ |
public interface TrackedRunnable extends Runnable { |
873 |
+ |
boolean isDone(); |
874 |
+ |
} |
875 |
+ |
|
876 |
+ |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
877 |
+ |
return new TrackedRunnable() { |
878 |
+ |
private volatile boolean done = false; |
879 |
+ |
public boolean isDone() { return done; } |
880 |
+ |
public void run() { |
881 |
+ |
try { |
882 |
+ |
Thread.sleep(timeoutMillis); |
883 |
+ |
done = true; |
884 |
+ |
} catch (InterruptedException ok) {} |
885 |
+ |
} |
886 |
+ |
}; |
887 |
+ |
} |
888 |
+ |
|
889 |
|
public static class TrackedShortRunnable implements Runnable { |
890 |
|
public volatile boolean done = false; |
891 |
|
public void run() { |
892 |
|
try { |
893 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
894 |
+ |
done = true; |
895 |
+ |
} catch (InterruptedException ok) {} |
896 |
+ |
} |
897 |
+ |
} |
898 |
+ |
|
899 |
+ |
public static class TrackedSmallRunnable implements Runnable { |
900 |
+ |
public volatile boolean done = false; |
901 |
+ |
public void run() { |
902 |
+ |
try { |
903 |
|
Thread.sleep(SMALL_DELAY_MS); |
904 |
|
done = true; |
905 |
|
} catch (InterruptedException ok) {} |