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 |
|
*/ |
153 |
|
System.exit(0); |
154 |
|
} |
155 |
|
|
156 |
+ |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
157 |
+ |
TestSuite suite = new TestSuite(); |
158 |
+ |
for (Object suiteOrClass : suiteOrClasses) { |
159 |
+ |
if (suiteOrClass instanceof TestSuite) |
160 |
+ |
suite.addTest((TestSuite) suiteOrClass); |
161 |
+ |
else if (suiteOrClass instanceof Class) |
162 |
+ |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
163 |
+ |
else |
164 |
+ |
throw new ClassCastException("not a test suite or class"); |
165 |
+ |
} |
166 |
+ |
return suite; |
167 |
+ |
} |
168 |
+ |
|
169 |
|
/** |
170 |
< |
* Collects all JSR166 unit tests as one suite |
170 |
> |
* Collects all JSR166 unit tests as one suite. |
171 |
|
*/ |
172 |
|
public static Test suite() { |
173 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
174 |
< |
|
175 |
< |
suite.addTest(ForkJoinPoolTest.suite()); |
176 |
< |
suite.addTest(ForkJoinTaskTest.suite()); |
177 |
< |
suite.addTest(RecursiveActionTest.suite()); |
178 |
< |
suite.addTest(RecursiveTaskTest.suite()); |
179 |
< |
suite.addTest(LinkedTransferQueueTest.suite()); |
180 |
< |
suite.addTest(PhaserTest.suite()); |
181 |
< |
suite.addTest(ThreadLocalRandomTest.suite()); |
182 |
< |
suite.addTest(AbstractExecutorServiceTest.suite()); |
183 |
< |
suite.addTest(AbstractQueueTest.suite()); |
184 |
< |
suite.addTest(AbstractQueuedSynchronizerTest.suite()); |
185 |
< |
suite.addTest(AbstractQueuedLongSynchronizerTest.suite()); |
186 |
< |
suite.addTest(ArrayBlockingQueueTest.suite()); |
187 |
< |
suite.addTest(ArrayDequeTest.suite()); |
188 |
< |
suite.addTest(AtomicBooleanTest.suite()); |
189 |
< |
suite.addTest(AtomicIntegerArrayTest.suite()); |
190 |
< |
suite.addTest(AtomicIntegerFieldUpdaterTest.suite()); |
191 |
< |
suite.addTest(AtomicIntegerTest.suite()); |
192 |
< |
suite.addTest(AtomicLongArrayTest.suite()); |
193 |
< |
suite.addTest(AtomicLongFieldUpdaterTest.suite()); |
194 |
< |
suite.addTest(AtomicLongTest.suite()); |
195 |
< |
suite.addTest(AtomicMarkableReferenceTest.suite()); |
196 |
< |
suite.addTest(AtomicReferenceArrayTest.suite()); |
197 |
< |
suite.addTest(AtomicReferenceFieldUpdaterTest.suite()); |
198 |
< |
suite.addTest(AtomicReferenceTest.suite()); |
199 |
< |
suite.addTest(AtomicStampedReferenceTest.suite()); |
200 |
< |
suite.addTest(ConcurrentHashMapTest.suite()); |
201 |
< |
suite.addTest(ConcurrentLinkedDequeTest.suite()); |
202 |
< |
suite.addTest(ConcurrentLinkedQueueTest.suite()); |
203 |
< |
suite.addTest(ConcurrentSkipListMapTest.suite()); |
204 |
< |
suite.addTest(ConcurrentSkipListSubMapTest.suite()); |
205 |
< |
suite.addTest(ConcurrentSkipListSetTest.suite()); |
206 |
< |
suite.addTest(ConcurrentSkipListSubSetTest.suite()); |
207 |
< |
suite.addTest(CopyOnWriteArrayListTest.suite()); |
208 |
< |
suite.addTest(CopyOnWriteArraySetTest.suite()); |
209 |
< |
suite.addTest(CountDownLatchTest.suite()); |
210 |
< |
suite.addTest(CyclicBarrierTest.suite()); |
211 |
< |
suite.addTest(DelayQueueTest.suite()); |
212 |
< |
suite.addTest(EntryTest.suite()); |
213 |
< |
suite.addTest(ExchangerTest.suite()); |
214 |
< |
suite.addTest(ExecutorsTest.suite()); |
215 |
< |
suite.addTest(ExecutorCompletionServiceTest.suite()); |
216 |
< |
suite.addTest(FutureTaskTest.suite()); |
217 |
< |
suite.addTest(LinkedBlockingDequeTest.suite()); |
218 |
< |
suite.addTest(LinkedBlockingQueueTest.suite()); |
219 |
< |
suite.addTest(LinkedListTest.suite()); |
220 |
< |
suite.addTest(LockSupportTest.suite()); |
221 |
< |
suite.addTest(PriorityBlockingQueueTest.suite()); |
222 |
< |
suite.addTest(PriorityQueueTest.suite()); |
223 |
< |
suite.addTest(ReentrantLockTest.suite()); |
224 |
< |
suite.addTest(ReentrantReadWriteLockTest.suite()); |
225 |
< |
suite.addTest(ScheduledExecutorTest.suite()); |
226 |
< |
suite.addTest(ScheduledExecutorSubclassTest.suite()); |
227 |
< |
suite.addTest(SemaphoreTest.suite()); |
228 |
< |
suite.addTest(SynchronousQueueTest.suite()); |
229 |
< |
suite.addTest(SystemTest.suite()); |
230 |
< |
suite.addTest(ThreadLocalTest.suite()); |
231 |
< |
suite.addTest(ThreadPoolExecutorTest.suite()); |
232 |
< |
suite.addTest(ThreadPoolExecutorSubclassTest.suite()); |
233 |
< |
suite.addTest(ThreadTest.suite()); |
234 |
< |
suite.addTest(TimeUnitTest.suite()); |
235 |
< |
suite.addTest(TreeMapTest.suite()); |
236 |
< |
suite.addTest(TreeSetTest.suite()); |
237 |
< |
suite.addTest(TreeSubMapTest.suite()); |
188 |
< |
suite.addTest(TreeSubSetTest.suite()); |
189 |
< |
|
190 |
< |
return suite; |
173 |
> |
return newTestSuite( |
174 |
> |
ForkJoinPoolTest.suite(), |
175 |
> |
ForkJoinTaskTest.suite(), |
176 |
> |
RecursiveActionTest.suite(), |
177 |
> |
RecursiveTaskTest.suite(), |
178 |
> |
LinkedTransferQueueTest.suite(), |
179 |
> |
PhaserTest.suite(), |
180 |
> |
ThreadLocalRandomTest.suite(), |
181 |
> |
AbstractExecutorServiceTest.suite(), |
182 |
> |
AbstractQueueTest.suite(), |
183 |
> |
AbstractQueuedSynchronizerTest.suite(), |
184 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
185 |
> |
ArrayBlockingQueueTest.suite(), |
186 |
> |
ArrayDequeTest.suite(), |
187 |
> |
AtomicBooleanTest.suite(), |
188 |
> |
AtomicIntegerArrayTest.suite(), |
189 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
190 |
> |
AtomicIntegerTest.suite(), |
191 |
> |
AtomicLongArrayTest.suite(), |
192 |
> |
AtomicLongFieldUpdaterTest.suite(), |
193 |
> |
AtomicLongTest.suite(), |
194 |
> |
AtomicMarkableReferenceTest.suite(), |
195 |
> |
AtomicReferenceArrayTest.suite(), |
196 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
197 |
> |
AtomicReferenceTest.suite(), |
198 |
> |
AtomicStampedReferenceTest.suite(), |
199 |
> |
ConcurrentHashMapTest.suite(), |
200 |
> |
ConcurrentLinkedDequeTest.suite(), |
201 |
> |
ConcurrentLinkedQueueTest.suite(), |
202 |
> |
ConcurrentSkipListMapTest.suite(), |
203 |
> |
ConcurrentSkipListSubMapTest.suite(), |
204 |
> |
ConcurrentSkipListSetTest.suite(), |
205 |
> |
ConcurrentSkipListSubSetTest.suite(), |
206 |
> |
CopyOnWriteArrayListTest.suite(), |
207 |
> |
CopyOnWriteArraySetTest.suite(), |
208 |
> |
CountDownLatchTest.suite(), |
209 |
> |
CyclicBarrierTest.suite(), |
210 |
> |
DelayQueueTest.suite(), |
211 |
> |
EntryTest.suite(), |
212 |
> |
ExchangerTest.suite(), |
213 |
> |
ExecutorsTest.suite(), |
214 |
> |
ExecutorCompletionServiceTest.suite(), |
215 |
> |
FutureTaskTest.suite(), |
216 |
> |
LinkedBlockingDequeTest.suite(), |
217 |
> |
LinkedBlockingQueueTest.suite(), |
218 |
> |
LinkedListTest.suite(), |
219 |
> |
LockSupportTest.suite(), |
220 |
> |
PriorityBlockingQueueTest.suite(), |
221 |
> |
PriorityQueueTest.suite(), |
222 |
> |
ReentrantLockTest.suite(), |
223 |
> |
ReentrantReadWriteLockTest.suite(), |
224 |
> |
ScheduledExecutorTest.suite(), |
225 |
> |
ScheduledExecutorSubclassTest.suite(), |
226 |
> |
SemaphoreTest.suite(), |
227 |
> |
SynchronousQueueTest.suite(), |
228 |
> |
SystemTest.suite(), |
229 |
> |
ThreadLocalTest.suite(), |
230 |
> |
ThreadPoolExecutorTest.suite(), |
231 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
232 |
> |
ThreadTest.suite(), |
233 |
> |
TimeUnitTest.suite(), |
234 |
> |
TreeMapTest.suite(), |
235 |
> |
TreeSetTest.suite(), |
236 |
> |
TreeSubMapTest.suite(), |
237 |
> |
TreeSubSetTest.suite()); |
238 |
|
} |
239 |
|
|
240 |
|
|
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
+ |
* Sleeps until the given time has elapsed. |
585 |
+ |
* Throws AssertionFailedError if interrupted. |
586 |
+ |
*/ |
587 |
+ |
void sleep(long millis) { |
588 |
+ |
try { |
589 |
+ |
Thread.sleep(millis); |
590 |
+ |
} catch (InterruptedException ie) { |
591 |
+ |
AssertionFailedError afe = |
592 |
+ |
new AssertionFailedError("Unexpected InterruptedException"); |
593 |
+ |
afe.initCause(ie); |
594 |
+ |
throw afe; |
595 |
+ |
} |
596 |
+ |
} |
597 |
+ |
|
598 |
+ |
/** |
599 |
|
* Sleeps until the timeout has elapsed, or interrupted. |
600 |
|
* Does <em>NOT</em> throw InterruptedException. |
601 |
|
*/ |
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) {} |
983 |
|
ThreadPoolExecutor executor) {} |
984 |
|
} |
985 |
|
|
986 |
+ |
/** |
987 |
+ |
* A CyclicBarrier that fails with AssertionFailedErrors instead |
988 |
+ |
* of throwing checked exceptions. |
989 |
+ |
*/ |
990 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
991 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
992 |
+ |
|
993 |
+ |
public int await() { |
994 |
+ |
try { |
995 |
+ |
return super.await(); |
996 |
+ |
} catch (Exception e) { |
997 |
+ |
AssertionFailedError afe = |
998 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
999 |
+ |
afe.initCause(e); |
1000 |
+ |
throw afe; |
1001 |
+ |
} |
1002 |
+ |
} |
1003 |
+ |
} |
1004 |
+ |
|
1005 |
|
} |