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.*; |
10 |
> |
import java.io.ByteArrayInputStream; |
11 |
> |
import java.io.ByteArrayOutputStream; |
12 |
> |
import java.io.ObjectInputStream; |
13 |
> |
import java.io.ObjectOutputStream; |
14 |
> |
import java.lang.management.ManagementFactory; |
15 |
> |
import java.lang.management.ThreadInfo; |
16 |
> |
import java.lang.reflect.Method; |
17 |
> |
import java.util.ArrayList; |
18 |
> |
import java.util.Arrays; |
19 |
> |
import java.util.Date; |
20 |
> |
import java.util.Enumeration; |
21 |
> |
import java.util.List; |
22 |
> |
import java.util.NoSuchElementException; |
23 |
> |
import java.util.PropertyPermission; |
24 |
|
import java.util.concurrent.*; |
25 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
< |
import java.io.*; |
29 |
< |
import java.security.*; |
28 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
29 |
> |
import java.util.regex.Pattern; |
30 |
> |
import java.security.CodeSource; |
31 |
> |
import java.security.Permission; |
32 |
> |
import java.security.PermissionCollection; |
33 |
> |
import java.security.Permissions; |
34 |
> |
import java.security.Policy; |
35 |
> |
import java.security.ProtectionDomain; |
36 |
> |
import java.security.SecurityPermission; |
37 |
|
|
38 |
|
/** |
39 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
48 |
|
* <li> All assertions in code running in generated threads must use |
49 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
50 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
51 |
< |
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
51 |
> |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
52 |
|
* particularly recommended) for other code to use these forms too. |
53 |
|
* Only the most typically used JUnit assertion methods are defined |
54 |
|
* this way, but enough to live with.</li> |
55 |
|
* |
56 |
|
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
57 |
< |
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
57 |
> |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
58 |
|
* them. These methods are used to clear and check for thread |
59 |
|
* assertion failures.</li> |
60 |
|
* |
61 |
< |
* <li>All delays and timeouts must use one of the constants <tt> |
62 |
< |
* SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>, |
63 |
< |
* <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always |
61 |
> |
* <li>All delays and timeouts must use one of the constants {@code |
62 |
> |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
63 |
> |
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
64 |
|
* discriminable from zero time, and always allows enough time for the |
65 |
|
* small amounts of computation (creating a thread, calling a few |
66 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
70 |
|
* in one spot to rerun tests on slower platforms.</li> |
71 |
|
* |
72 |
|
* <li> All threads generated must be joined inside each test case |
73 |
< |
* method (or <tt>fail</tt> to do so) before returning from the |
74 |
< |
* method. The <tt> joinPool</tt> method can be used to do this when |
73 |
> |
* method (or {@code fail} to do so) before returning from the |
74 |
> |
* method. The {@code joinPool} method can be used to do this when |
75 |
|
* using Executors.</li> |
76 |
|
* |
77 |
|
* </ol> |
78 |
|
* |
79 |
< |
* <p> <b>Other notes</b> |
79 |
> |
* <p><b>Other notes</b> |
80 |
|
* <ul> |
81 |
|
* |
82 |
|
* <li> Usually, there is one testcase method per JSR166 method |
103 |
|
* any particular package to simplify things for people integrating |
104 |
|
* them in TCK test suites.</li> |
105 |
|
* |
106 |
< |
* <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase) |
106 |
> |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
107 |
|
* runs all JSR166 unit tests.</li> |
108 |
|
* |
109 |
|
* </ul> |
110 |
|
*/ |
111 |
|
public class JSR166TestCase extends TestCase { |
112 |
+ |
private static final boolean useSecurityManager = |
113 |
+ |
Boolean.getBoolean("jsr166.useSecurityManager"); |
114 |
+ |
|
115 |
+ |
protected static final boolean expensiveTests = |
116 |
+ |
Boolean.getBoolean("jsr166.expensiveTests"); |
117 |
+ |
|
118 |
+ |
/** |
119 |
+ |
* If true, report on stdout all "slow" tests, that is, ones that |
120 |
+ |
* take more than profileThreshold milliseconds to execute. |
121 |
+ |
*/ |
122 |
+ |
private static final boolean profileTests = |
123 |
+ |
Boolean.getBoolean("jsr166.profileTests"); |
124 |
+ |
|
125 |
+ |
/** |
126 |
+ |
* The number of milliseconds that tests are permitted for |
127 |
+ |
* execution without being reported, when profileTests is set. |
128 |
+ |
*/ |
129 |
+ |
private static final long profileThreshold = |
130 |
+ |
Long.getLong("jsr166.profileThreshold", 100); |
131 |
+ |
|
132 |
|
/** |
133 |
< |
* Runs all JSR166 unit tests using junit.textui.TestRunner |
133 |
> |
* The number of repetitions per test (for tickling rare bugs). |
134 |
|
*/ |
135 |
< |
public static void main (String[] args) { |
136 |
< |
int iters = 1; |
137 |
< |
if (args.length > 0) |
138 |
< |
iters = Integer.parseInt(args[0]); |
135 |
> |
private static final int runsPerTest = |
136 |
> |
Integer.getInteger("jsr166.runsPerTest", 1); |
137 |
> |
|
138 |
> |
/** |
139 |
> |
* A filter for tests to run, matching strings of the form |
140 |
> |
* methodName(className), e.g. "testInvokeAll5(ForkJoinPoolTest)" |
141 |
> |
* Usefully combined with jsr166.runsPerTest. |
142 |
> |
*/ |
143 |
> |
private static final Pattern methodFilter = methodFilter(); |
144 |
> |
|
145 |
> |
private static Pattern methodFilter() { |
146 |
> |
String regex = System.getProperty("jsr166.methodFilter"); |
147 |
> |
return (regex == null) ? null : Pattern.compile(regex); |
148 |
> |
} |
149 |
> |
|
150 |
> |
protected void runTest() throws Throwable { |
151 |
> |
if (methodFilter == null |
152 |
> |
|| methodFilter.matcher(toString()).find()) { |
153 |
> |
for (int i = 0; i < runsPerTest; i++) { |
154 |
> |
if (profileTests) |
155 |
> |
runTestProfiled(); |
156 |
> |
else |
157 |
> |
super.runTest(); |
158 |
> |
} |
159 |
> |
} |
160 |
> |
} |
161 |
> |
|
162 |
> |
protected void runTestProfiled() throws Throwable { |
163 |
> |
long t0 = System.nanoTime(); |
164 |
> |
try { |
165 |
> |
super.runTest(); |
166 |
> |
} finally { |
167 |
> |
long elapsedMillis = |
168 |
> |
(System.nanoTime() - t0) / (1000L * 1000L); |
169 |
> |
if (elapsedMillis >= profileThreshold) |
170 |
> |
System.out.printf("%n%s: %d%n", toString(), elapsedMillis); |
171 |
> |
} |
172 |
> |
} |
173 |
> |
|
174 |
> |
/** |
175 |
> |
* Runs all JSR166 unit tests using junit.textui.TestRunner. |
176 |
> |
* Optional command line arg provides the number of iterations to |
177 |
> |
* repeat running the tests. |
178 |
> |
*/ |
179 |
> |
public static void main(String[] args) { |
180 |
> |
if (useSecurityManager) { |
181 |
> |
System.err.println("Setting a permissive security manager"); |
182 |
> |
Policy.setPolicy(permissivePolicy()); |
183 |
> |
System.setSecurityManager(new SecurityManager()); |
184 |
> |
} |
185 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
186 |
> |
|
187 |
|
Test s = suite(); |
188 |
|
for (int i = 0; i < iters; ++i) { |
189 |
< |
junit.textui.TestRunner.run (s); |
189 |
> |
junit.textui.TestRunner.run(s); |
190 |
|
System.gc(); |
191 |
|
System.runFinalization(); |
192 |
|
} |
193 |
|
System.exit(0); |
194 |
|
} |
195 |
|
|
196 |
< |
/** |
197 |
< |
* Collects all JSR166 unit tests as one suite |
198 |
< |
*/ |
199 |
< |
public static Test suite ( ) { |
200 |
< |
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
196 |
> |
public static TestSuite newTestSuite(Object... suiteOrClasses) { |
197 |
> |
TestSuite suite = new TestSuite(); |
198 |
> |
for (Object suiteOrClass : suiteOrClasses) { |
199 |
> |
if (suiteOrClass instanceof TestSuite) |
200 |
> |
suite.addTest((TestSuite) suiteOrClass); |
201 |
> |
else if (suiteOrClass instanceof Class) |
202 |
> |
suite.addTest(new TestSuite((Class<?>) suiteOrClass)); |
203 |
> |
else |
204 |
> |
throw new ClassCastException("not a test suite or class"); |
205 |
> |
} |
206 |
> |
return suite; |
207 |
> |
} |
208 |
|
|
209 |
< |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
210 |
< |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
211 |
< |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
212 |
< |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
213 |
< |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
214 |
< |
suite.addTest(new TestSuite(PhaserTest.class)); |
215 |
< |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
216 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
217 |
< |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
218 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
219 |
< |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
220 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
221 |
< |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
222 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
223 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
224 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
225 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
226 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
227 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
228 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
229 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
230 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
231 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
232 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
233 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
234 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
235 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
236 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
237 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
238 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
239 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
240 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
241 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
242 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
243 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
244 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
245 |
< |
suite.addTest(new TestSuite(EntryTest.class)); |
246 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
247 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
248 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
249 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
250 |
< |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
251 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
252 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
253 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
254 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
255 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
256 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
257 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
258 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
259 |
< |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
260 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
261 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
262 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
263 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
264 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
265 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
266 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
267 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
268 |
< |
suite.addTest(new TestSuite(TreeMapTest.class)); |
269 |
< |
suite.addTest(new TestSuite(TreeSetTest.class)); |
270 |
< |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
271 |
< |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
209 |
> |
public static void addNamedTestClasses(TestSuite suite, |
210 |
> |
String... testClassNames) { |
211 |
> |
for (String testClassName : testClassNames) { |
212 |
> |
try { |
213 |
> |
Class<?> testClass = Class.forName(testClassName); |
214 |
> |
Method m = testClass.getDeclaredMethod("suite", |
215 |
> |
new Class<?>[0]); |
216 |
> |
suite.addTest(newTestSuite((Test)m.invoke(null))); |
217 |
> |
} catch (Exception e) { |
218 |
> |
throw new Error("Missing test class", e); |
219 |
> |
} |
220 |
> |
} |
221 |
> |
} |
222 |
> |
|
223 |
> |
public static final double JAVA_CLASS_VERSION; |
224 |
> |
static { |
225 |
> |
try { |
226 |
> |
JAVA_CLASS_VERSION = java.security.AccessController.doPrivileged( |
227 |
> |
new java.security.PrivilegedAction<Double>() { |
228 |
> |
public Double run() { |
229 |
> |
return Double.valueOf(System.getProperty("java.class.version"));}}); |
230 |
> |
} catch (Throwable t) { |
231 |
> |
throw new Error(t); |
232 |
> |
} |
233 |
> |
} |
234 |
> |
|
235 |
> |
public static boolean atLeastJava6() { return JAVA_CLASS_VERSION >= 50.0; } |
236 |
> |
public static boolean atLeastJava7() { return JAVA_CLASS_VERSION >= 51.0; } |
237 |
> |
public static boolean atLeastJava8() { return JAVA_CLASS_VERSION >= 52.0; } |
238 |
> |
|
239 |
> |
/** |
240 |
> |
* Collects all JSR166 unit tests as one suite. |
241 |
> |
*/ |
242 |
> |
public static Test suite() { |
243 |
> |
// Java7+ test classes |
244 |
> |
TestSuite suite = newTestSuite( |
245 |
> |
ForkJoinPoolTest.suite(), |
246 |
> |
ForkJoinTaskTest.suite(), |
247 |
> |
RecursiveActionTest.suite(), |
248 |
> |
RecursiveTaskTest.suite(), |
249 |
> |
LinkedTransferQueueTest.suite(), |
250 |
> |
PhaserTest.suite(), |
251 |
> |
ThreadLocalRandomTest.suite(), |
252 |
> |
AbstractExecutorServiceTest.suite(), |
253 |
> |
AbstractQueueTest.suite(), |
254 |
> |
AbstractQueuedSynchronizerTest.suite(), |
255 |
> |
AbstractQueuedLongSynchronizerTest.suite(), |
256 |
> |
ArrayBlockingQueueTest.suite(), |
257 |
> |
ArrayDequeTest.suite(), |
258 |
> |
AtomicBooleanTest.suite(), |
259 |
> |
AtomicIntegerArrayTest.suite(), |
260 |
> |
AtomicIntegerFieldUpdaterTest.suite(), |
261 |
> |
AtomicIntegerTest.suite(), |
262 |
> |
AtomicLongArrayTest.suite(), |
263 |
> |
AtomicLongFieldUpdaterTest.suite(), |
264 |
> |
AtomicLongTest.suite(), |
265 |
> |
AtomicMarkableReferenceTest.suite(), |
266 |
> |
AtomicReferenceArrayTest.suite(), |
267 |
> |
AtomicReferenceFieldUpdaterTest.suite(), |
268 |
> |
AtomicReferenceTest.suite(), |
269 |
> |
AtomicStampedReferenceTest.suite(), |
270 |
> |
ConcurrentHashMapTest.suite(), |
271 |
> |
ConcurrentLinkedDequeTest.suite(), |
272 |
> |
ConcurrentLinkedQueueTest.suite(), |
273 |
> |
ConcurrentSkipListMapTest.suite(), |
274 |
> |
ConcurrentSkipListSubMapTest.suite(), |
275 |
> |
ConcurrentSkipListSetTest.suite(), |
276 |
> |
ConcurrentSkipListSubSetTest.suite(), |
277 |
> |
CopyOnWriteArrayListTest.suite(), |
278 |
> |
CopyOnWriteArraySetTest.suite(), |
279 |
> |
CountDownLatchTest.suite(), |
280 |
> |
CyclicBarrierTest.suite(), |
281 |
> |
DelayQueueTest.suite(), |
282 |
> |
EntryTest.suite(), |
283 |
> |
ExchangerTest.suite(), |
284 |
> |
ExecutorsTest.suite(), |
285 |
> |
ExecutorCompletionServiceTest.suite(), |
286 |
> |
FutureTaskTest.suite(), |
287 |
> |
LinkedBlockingDequeTest.suite(), |
288 |
> |
LinkedBlockingQueueTest.suite(), |
289 |
> |
LinkedListTest.suite(), |
290 |
> |
LockSupportTest.suite(), |
291 |
> |
PriorityBlockingQueueTest.suite(), |
292 |
> |
PriorityQueueTest.suite(), |
293 |
> |
ReentrantLockTest.suite(), |
294 |
> |
ReentrantReadWriteLockTest.suite(), |
295 |
> |
ScheduledExecutorTest.suite(), |
296 |
> |
ScheduledExecutorSubclassTest.suite(), |
297 |
> |
SemaphoreTest.suite(), |
298 |
> |
SynchronousQueueTest.suite(), |
299 |
> |
SystemTest.suite(), |
300 |
> |
ThreadLocalTest.suite(), |
301 |
> |
ThreadPoolExecutorTest.suite(), |
302 |
> |
ThreadPoolExecutorSubclassTest.suite(), |
303 |
> |
ThreadTest.suite(), |
304 |
> |
TimeUnitTest.suite(), |
305 |
> |
TreeMapTest.suite(), |
306 |
> |
TreeSetTest.suite(), |
307 |
> |
TreeSubMapTest.suite(), |
308 |
> |
TreeSubSetTest.suite()); |
309 |
> |
|
310 |
> |
// Java8+ test classes |
311 |
> |
if (atLeastJava8()) { |
312 |
> |
String[] java8TestClassNames = { |
313 |
> |
"Atomic8Test", |
314 |
> |
"CompletableFutureTest", |
315 |
> |
"ConcurrentHashMap8Test", |
316 |
> |
"CountedCompleterTest", |
317 |
> |
"DoubleAccumulatorTest", |
318 |
> |
"DoubleAdderTest", |
319 |
> |
"ForkJoinPool8Test", |
320 |
> |
"ForkJoinTask8Test", |
321 |
> |
"LongAccumulatorTest", |
322 |
> |
"LongAdderTest", |
323 |
> |
"SplittableRandomTest", |
324 |
> |
"StampedLockTest", |
325 |
> |
"ThreadLocalRandom8Test", |
326 |
> |
}; |
327 |
> |
addNamedTestClasses(suite, java8TestClassNames); |
328 |
> |
} |
329 |
|
|
330 |
|
return suite; |
331 |
|
} |
332 |
|
|
333 |
+ |
// Delays for timing-dependent tests, in milliseconds. |
334 |
|
|
335 |
|
public static long SHORT_DELAY_MS; |
336 |
|
public static long SMALL_DELAY_MS; |
337 |
|
public static long MEDIUM_DELAY_MS; |
338 |
|
public static long LONG_DELAY_MS; |
339 |
|
|
185 |
– |
|
340 |
|
/** |
341 |
|
* Returns the shortest timed delay. This could |
342 |
|
* be reimplemented to use for example a Property. |
345 |
|
return 50; |
346 |
|
} |
347 |
|
|
194 |
– |
|
348 |
|
/** |
349 |
|
* Sets delays as multiples of SHORT_DELAY. |
350 |
|
*/ |
351 |
< |
protected void setDelays() { |
351 |
> |
protected void setDelays() { |
352 |
|
SHORT_DELAY_MS = getShortDelay(); |
353 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
353 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
354 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
355 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
355 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 200; |
356 |
> |
} |
357 |
> |
|
358 |
> |
/** |
359 |
> |
* Returns a timeout in milliseconds to be used in tests that |
360 |
> |
* verify that operations block or time out. |
361 |
> |
*/ |
362 |
> |
long timeoutMillis() { |
363 |
> |
return SHORT_DELAY_MS / 4; |
364 |
> |
} |
365 |
> |
|
366 |
> |
/** |
367 |
> |
* Returns a new Date instance representing a time delayMillis |
368 |
> |
* milliseconds in the future. |
369 |
> |
*/ |
370 |
> |
Date delayedDate(long delayMillis) { |
371 |
> |
return new Date(System.currentTimeMillis() + delayMillis); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* Flag set true if any threadAssert methods fail |
375 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
376 |
|
*/ |
377 |
< |
volatile boolean threadFailed; |
377 |
> |
private final AtomicReference<Throwable> threadFailure |
378 |
> |
= new AtomicReference<Throwable>(null); |
379 |
|
|
380 |
|
/** |
381 |
< |
* Initializes test to indicate that no thread assertions have failed |
381 |
> |
* Records an exception so that it can be rethrown later in the test |
382 |
> |
* harness thread, triggering a test case failure. Only the first |
383 |
> |
* failure is recorded; subsequent calls to this method from within |
384 |
> |
* the same test have no effect. |
385 |
|
*/ |
386 |
+ |
public void threadRecordFailure(Throwable t) { |
387 |
+ |
threadFailure.compareAndSet(null, t); |
388 |
+ |
} |
389 |
+ |
|
390 |
|
public void setUp() { |
391 |
|
setDelays(); |
215 |
– |
threadFailed = false; |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* Triggers test case failure if any thread assertions have failed |
395 |
> |
* Extra checks that get done for all test cases. |
396 |
> |
* |
397 |
> |
* Triggers test case failure if any thread assertions have failed, |
398 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
399 |
> |
* earlier by threadRecordFailure. |
400 |
> |
* |
401 |
> |
* Triggers test case failure if interrupt status is set in the main thread. |
402 |
> |
*/ |
403 |
> |
public void tearDown() throws Exception { |
404 |
> |
Throwable t = threadFailure.getAndSet(null); |
405 |
> |
if (t != null) { |
406 |
> |
if (t instanceof Error) |
407 |
> |
throw (Error) t; |
408 |
> |
else if (t instanceof RuntimeException) |
409 |
> |
throw (RuntimeException) t; |
410 |
> |
else if (t instanceof Exception) |
411 |
> |
throw (Exception) t; |
412 |
> |
else { |
413 |
> |
AssertionFailedError afe = |
414 |
> |
new AssertionFailedError(t.toString()); |
415 |
> |
afe.initCause(t); |
416 |
> |
throw afe; |
417 |
> |
} |
418 |
> |
} |
419 |
> |
|
420 |
> |
if (Thread.interrupted()) |
421 |
> |
throw new AssertionFailedError("interrupt status set in main thread"); |
422 |
> |
|
423 |
> |
checkForkJoinPoolThreadLeaks(); |
424 |
> |
} |
425 |
> |
|
426 |
> |
/** |
427 |
> |
* Find missing try { ... } finally { joinPool(e); } |
428 |
|
*/ |
429 |
< |
public void tearDown() { |
430 |
< |
assertFalse(threadFailed); |
429 |
> |
void checkForkJoinPoolThreadLeaks() throws InterruptedException { |
430 |
> |
Thread[] survivors = new Thread[5]; |
431 |
> |
int count = Thread.enumerate(survivors); |
432 |
> |
for (int i = 0; i < count; i++) { |
433 |
> |
Thread thread = survivors[i]; |
434 |
> |
String name = thread.getName(); |
435 |
> |
if (name.startsWith("ForkJoinPool-")) { |
436 |
> |
// give thread some time to terminate |
437 |
> |
thread.join(LONG_DELAY_MS); |
438 |
> |
if (!thread.isAlive()) continue; |
439 |
> |
thread.stop(); |
440 |
> |
throw new AssertionFailedError |
441 |
> |
(String.format("Found leaked ForkJoinPool thread test=%s thread=%s%n", |
442 |
> |
toString(), name)); |
443 |
> |
} |
444 |
> |
} |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
< |
* Fail, also setting status to indicate current testcase should fail |
448 |
> |
* Just like fail(reason), but additionally recording (using |
449 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
450 |
> |
* the current testcase will fail. |
451 |
|
*/ |
452 |
|
public void threadFail(String reason) { |
453 |
< |
threadFailed = true; |
454 |
< |
fail(reason); |
453 |
> |
try { |
454 |
> |
fail(reason); |
455 |
> |
} catch (AssertionFailedError t) { |
456 |
> |
threadRecordFailure(t); |
457 |
> |
fail(reason); |
458 |
> |
} |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
< |
* If expression not true, set status to indicate current testcase |
463 |
< |
* should fail |
462 |
> |
* Just like assertTrue(b), but additionally recording (using |
463 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
464 |
> |
* the current testcase will fail. |
465 |
|
*/ |
466 |
|
public void threadAssertTrue(boolean b) { |
467 |
< |
if (!b) { |
239 |
< |
threadFailed = true; |
467 |
> |
try { |
468 |
|
assertTrue(b); |
469 |
+ |
} catch (AssertionFailedError t) { |
470 |
+ |
threadRecordFailure(t); |
471 |
+ |
throw t; |
472 |
|
} |
473 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
< |
* If expression not false, set status to indicate current testcase |
477 |
< |
* should fail |
476 |
> |
* Just like assertFalse(b), but additionally recording (using |
477 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
478 |
> |
* the current testcase will fail. |
479 |
|
*/ |
480 |
|
public void threadAssertFalse(boolean b) { |
481 |
< |
if (b) { |
250 |
< |
threadFailed = true; |
481 |
> |
try { |
482 |
|
assertFalse(b); |
483 |
+ |
} catch (AssertionFailedError t) { |
484 |
+ |
threadRecordFailure(t); |
485 |
+ |
throw t; |
486 |
|
} |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
< |
* If argument not null, set status to indicate current testcase |
491 |
< |
* should fail |
490 |
> |
* Just like assertNull(x), but additionally recording (using |
491 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
492 |
> |
* the current testcase will fail. |
493 |
|
*/ |
494 |
|
public void threadAssertNull(Object x) { |
495 |
< |
if (x != null) { |
261 |
< |
threadFailed = true; |
495 |
> |
try { |
496 |
|
assertNull(x); |
497 |
+ |
} catch (AssertionFailedError t) { |
498 |
+ |
threadRecordFailure(t); |
499 |
+ |
throw t; |
500 |
|
} |
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
< |
* If arguments not equal, set status to indicate current testcase |
505 |
< |
* should fail |
504 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
505 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
506 |
> |
* the current testcase will fail. |
507 |
|
*/ |
508 |
|
public void threadAssertEquals(long x, long y) { |
509 |
< |
if (x != y) { |
272 |
< |
threadFailed = true; |
509 |
> |
try { |
510 |
|
assertEquals(x, y); |
511 |
+ |
} catch (AssertionFailedError t) { |
512 |
+ |
threadRecordFailure(t); |
513 |
+ |
throw t; |
514 |
|
} |
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
< |
* If arguments not equal, set status to indicate current testcase |
519 |
< |
* should fail |
518 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
519 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
520 |
> |
* the current testcase will fail. |
521 |
|
*/ |
522 |
|
public void threadAssertEquals(Object x, Object y) { |
523 |
< |
if (x != y && (x == null || !x.equals(y))) { |
283 |
< |
threadFailed = true; |
523 |
> |
try { |
524 |
|
assertEquals(x, y); |
525 |
+ |
} catch (AssertionFailedError t) { |
526 |
+ |
threadRecordFailure(t); |
527 |
+ |
throw t; |
528 |
+ |
} catch (Throwable t) { |
529 |
+ |
threadUnexpectedException(t); |
530 |
|
} |
531 |
|
} |
532 |
|
|
533 |
|
/** |
534 |
< |
* threadFail with message "should throw exception" |
534 |
> |
* Just like assertSame(x, y), but additionally recording (using |
535 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
536 |
> |
* the current testcase will fail. |
537 |
> |
*/ |
538 |
> |
public void threadAssertSame(Object x, Object y) { |
539 |
> |
try { |
540 |
> |
assertSame(x, y); |
541 |
> |
} catch (AssertionFailedError t) { |
542 |
> |
threadRecordFailure(t); |
543 |
> |
throw t; |
544 |
> |
} |
545 |
> |
} |
546 |
> |
|
547 |
> |
/** |
548 |
> |
* Calls threadFail with message "should throw exception". |
549 |
|
*/ |
550 |
|
public void threadShouldThrow() { |
551 |
< |
threadFailed = true; |
293 |
< |
fail("should throw exception"); |
551 |
> |
threadFail("should throw exception"); |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
< |
* threadFail with message "should throw" + exceptionName |
555 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
556 |
|
*/ |
557 |
|
public void threadShouldThrow(String exceptionName) { |
558 |
< |
threadFailed = true; |
301 |
< |
fail("should throw " + exceptionName); |
558 |
> |
threadFail("should throw " + exceptionName); |
559 |
|
} |
560 |
|
|
561 |
|
/** |
562 |
< |
* threadFail with message "Unexpected exception" |
562 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
563 |
> |
* then rethrows the exception, wrapping it in an |
564 |
> |
* AssertionFailedError if necessary. |
565 |
|
*/ |
566 |
< |
public void threadUnexpectedException() { |
567 |
< |
threadFailed = true; |
568 |
< |
fail("Unexpected exception"); |
566 |
> |
public void threadUnexpectedException(Throwable t) { |
567 |
> |
threadRecordFailure(t); |
568 |
> |
t.printStackTrace(); |
569 |
> |
if (t instanceof RuntimeException) |
570 |
> |
throw (RuntimeException) t; |
571 |
> |
else if (t instanceof Error) |
572 |
> |
throw (Error) t; |
573 |
> |
else { |
574 |
> |
AssertionFailedError afe = |
575 |
> |
new AssertionFailedError("unexpected exception: " + t); |
576 |
> |
afe.initCause(t); |
577 |
> |
throw afe; |
578 |
> |
} |
579 |
|
} |
580 |
|
|
581 |
|
/** |
582 |
< |
* threadFail with message "Unexpected exception", with argument |
582 |
> |
* Delays, via Thread.sleep, for the given millisecond delay, but |
583 |
> |
* if the sleep is shorter than specified, may re-sleep or yield |
584 |
> |
* until time elapses. |
585 |
|
*/ |
586 |
< |
public void threadUnexpectedException(Throwable ex) { |
587 |
< |
threadFailed = true; |
588 |
< |
ex.printStackTrace(); |
589 |
< |
fail("Unexpected exception: " + ex); |
586 |
> |
static void delay(long millis) throws InterruptedException { |
587 |
> |
long startTime = System.nanoTime(); |
588 |
> |
long ns = millis * 1000 * 1000; |
589 |
> |
for (;;) { |
590 |
> |
if (millis > 0L) |
591 |
> |
Thread.sleep(millis); |
592 |
> |
else // too short to sleep |
593 |
> |
Thread.yield(); |
594 |
> |
long d = ns - (System.nanoTime() - startTime); |
595 |
> |
if (d > 0L) |
596 |
> |
millis = d / (1000 * 1000); |
597 |
> |
else |
598 |
> |
break; |
599 |
> |
} |
600 |
|
} |
601 |
|
|
602 |
|
/** |
603 |
< |
* Wait out termination of a thread pool or fail doing so |
603 |
> |
* Waits out termination of a thread pool or fails doing so. |
604 |
|
*/ |
605 |
< |
public void joinPool(ExecutorService exec) { |
605 |
> |
void joinPool(ExecutorService exec) { |
606 |
|
try { |
607 |
|
exec.shutdown(); |
608 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
608 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
609 |
> |
exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS)); |
610 |
|
} catch (SecurityException ok) { |
611 |
|
// Allowed in case test doesn't have privs |
612 |
|
} catch (InterruptedException ie) { |
613 |
< |
fail("Unexpected exception"); |
613 |
> |
fail("Unexpected InterruptedException"); |
614 |
|
} |
615 |
|
} |
616 |
|
|
617 |
+ |
/** |
618 |
+ |
* A debugging tool to print all stack traces, as jstack does. |
619 |
+ |
*/ |
620 |
+ |
static void printAllStackTraces() { |
621 |
+ |
for (ThreadInfo info : |
622 |
+ |
ManagementFactory.getThreadMXBean() |
623 |
+ |
.dumpAllThreads(true, true)) |
624 |
+ |
System.err.print(info); |
625 |
+ |
} |
626 |
+ |
|
627 |
+ |
/** |
628 |
+ |
* Checks that thread does not terminate within the default |
629 |
+ |
* millisecond delay of {@code timeoutMillis()}. |
630 |
+ |
*/ |
631 |
+ |
void assertThreadStaysAlive(Thread thread) { |
632 |
+ |
assertThreadStaysAlive(thread, timeoutMillis()); |
633 |
+ |
} |
634 |
|
|
635 |
|
/** |
636 |
< |
* fail with message "should throw exception" |
636 |
> |
* Checks that thread does not terminate within the given millisecond delay. |
637 |
|
*/ |
638 |
< |
public void shouldThrow() { |
639 |
< |
fail("Should throw exception"); |
638 |
> |
void assertThreadStaysAlive(Thread thread, long millis) { |
639 |
> |
try { |
640 |
> |
// No need to optimize the failing case via Thread.join. |
641 |
> |
delay(millis); |
642 |
> |
assertTrue(thread.isAlive()); |
643 |
> |
} catch (InterruptedException ie) { |
644 |
> |
fail("Unexpected InterruptedException"); |
645 |
> |
} |
646 |
|
} |
647 |
|
|
648 |
|
/** |
649 |
< |
* fail with message "should throw " + exceptionName |
649 |
> |
* Checks that the threads do not terminate within the default |
650 |
> |
* millisecond delay of {@code timeoutMillis()}. |
651 |
|
*/ |
652 |
< |
public void shouldThrow(String exceptionName) { |
653 |
< |
fail("Should throw " + exceptionName); |
652 |
> |
void assertThreadsStayAlive(Thread... threads) { |
653 |
> |
assertThreadsStayAlive(timeoutMillis(), threads); |
654 |
> |
} |
655 |
> |
|
656 |
> |
/** |
657 |
> |
* Checks that the threads do not terminate within the given millisecond delay. |
658 |
> |
*/ |
659 |
> |
void assertThreadsStayAlive(long millis, Thread... threads) { |
660 |
> |
try { |
661 |
> |
// No need to optimize the failing case via Thread.join. |
662 |
> |
delay(millis); |
663 |
> |
for (Thread thread : threads) |
664 |
> |
assertTrue(thread.isAlive()); |
665 |
> |
} catch (InterruptedException ie) { |
666 |
> |
fail("Unexpected InterruptedException"); |
667 |
> |
} |
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
< |
* fail with message "Unexpected exception" |
671 |
> |
* Checks that future.get times out, with the default timeout of |
672 |
> |
* {@code timeoutMillis()}. |
673 |
|
*/ |
674 |
< |
public void unexpectedException() { |
675 |
< |
fail("Unexpected exception"); |
674 |
> |
void assertFutureTimesOut(Future future) { |
675 |
> |
assertFutureTimesOut(future, timeoutMillis()); |
676 |
|
} |
677 |
|
|
678 |
|
/** |
679 |
< |
* fail with message "Unexpected exception", with argument |
679 |
> |
* Checks that future.get times out, with the given millisecond timeout. |
680 |
|
*/ |
681 |
< |
public void unexpectedException(Throwable ex) { |
682 |
< |
ex.printStackTrace(); |
683 |
< |
fail("Unexpected exception: " + ex); |
681 |
> |
void assertFutureTimesOut(Future future, long timeoutMillis) { |
682 |
> |
long startTime = System.nanoTime(); |
683 |
> |
try { |
684 |
> |
future.get(timeoutMillis, MILLISECONDS); |
685 |
> |
shouldThrow(); |
686 |
> |
} catch (TimeoutException success) { |
687 |
> |
} catch (Exception e) { |
688 |
> |
threadUnexpectedException(e); |
689 |
> |
} finally { future.cancel(true); } |
690 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
691 |
|
} |
692 |
|
|
693 |
+ |
/** |
694 |
+ |
* Fails with message "should throw exception". |
695 |
+ |
*/ |
696 |
+ |
public void shouldThrow() { |
697 |
+ |
fail("Should throw exception"); |
698 |
+ |
} |
699 |
+ |
|
700 |
+ |
/** |
701 |
+ |
* Fails with message "should throw " + exceptionName. |
702 |
+ |
*/ |
703 |
+ |
public void shouldThrow(String exceptionName) { |
704 |
+ |
fail("Should throw " + exceptionName); |
705 |
+ |
} |
706 |
|
|
707 |
|
/** |
708 |
|
* The number of elements to place in collections, arrays, etc. |
709 |
|
*/ |
710 |
< |
static final int SIZE = 20; |
710 |
> |
public static final int SIZE = 20; |
711 |
|
|
712 |
|
// Some convenient Integer constants |
713 |
|
|
714 |
< |
static final Integer zero = new Integer(0); |
715 |
< |
static final Integer one = new Integer(1); |
716 |
< |
static final Integer two = new Integer(2); |
717 |
< |
static final Integer three = new Integer(3); |
718 |
< |
static final Integer four = new Integer(4); |
719 |
< |
static final Integer five = new Integer(5); |
720 |
< |
static final Integer six = new Integer(6); |
721 |
< |
static final Integer seven = new Integer(7); |
722 |
< |
static final Integer eight = new Integer(8); |
723 |
< |
static final Integer nine = new Integer(9); |
724 |
< |
static final Integer m1 = new Integer(-1); |
725 |
< |
static final Integer m2 = new Integer(-2); |
726 |
< |
static final Integer m3 = new Integer(-3); |
727 |
< |
static final Integer m4 = new Integer(-4); |
728 |
< |
static final Integer m5 = new Integer(-5); |
729 |
< |
static final Integer m6 = new Integer(-6); |
730 |
< |
static final Integer m10 = new Integer(-10); |
714 |
> |
public static final Integer zero = new Integer(0); |
715 |
> |
public static final Integer one = new Integer(1); |
716 |
> |
public static final Integer two = new Integer(2); |
717 |
> |
public static final Integer three = new Integer(3); |
718 |
> |
public static final Integer four = new Integer(4); |
719 |
> |
public static final Integer five = new Integer(5); |
720 |
> |
public static final Integer six = new Integer(6); |
721 |
> |
public static final Integer seven = new Integer(7); |
722 |
> |
public static final Integer eight = new Integer(8); |
723 |
> |
public static final Integer nine = new Integer(9); |
724 |
> |
public static final Integer m1 = new Integer(-1); |
725 |
> |
public static final Integer m2 = new Integer(-2); |
726 |
> |
public static final Integer m3 = new Integer(-3); |
727 |
> |
public static final Integer m4 = new Integer(-4); |
728 |
> |
public static final Integer m5 = new Integer(-5); |
729 |
> |
public static final Integer m6 = new Integer(-6); |
730 |
> |
public static final Integer m10 = new Integer(-10); |
731 |
> |
|
732 |
> |
/** |
733 |
> |
* Runs Runnable r with a security policy that permits precisely |
734 |
> |
* the specified permissions. If there is no current security |
735 |
> |
* manager, the runnable is run twice, both with and without a |
736 |
> |
* security manager. We require that any security manager permit |
737 |
> |
* getPolicy/setPolicy. |
738 |
> |
*/ |
739 |
> |
public void runWithPermissions(Runnable r, Permission... permissions) { |
740 |
> |
SecurityManager sm = System.getSecurityManager(); |
741 |
> |
if (sm == null) { |
742 |
> |
r.run(); |
743 |
> |
} |
744 |
> |
runWithSecurityManagerWithPermissions(r, permissions); |
745 |
> |
} |
746 |
|
|
747 |
+ |
/** |
748 |
+ |
* Runs Runnable r with a security policy that permits precisely |
749 |
+ |
* the specified permissions. If there is no current security |
750 |
+ |
* manager, a temporary one is set for the duration of the |
751 |
+ |
* Runnable. We require that any security manager permit |
752 |
+ |
* getPolicy/setPolicy. |
753 |
+ |
*/ |
754 |
+ |
public void runWithSecurityManagerWithPermissions(Runnable r, |
755 |
+ |
Permission... permissions) { |
756 |
+ |
SecurityManager sm = System.getSecurityManager(); |
757 |
+ |
if (sm == null) { |
758 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
759 |
+ |
try { |
760 |
+ |
Policy.setPolicy(permissivePolicy()); |
761 |
+ |
System.setSecurityManager(new SecurityManager()); |
762 |
+ |
runWithSecurityManagerWithPermissions(r, permissions); |
763 |
+ |
} finally { |
764 |
+ |
System.setSecurityManager(null); |
765 |
+ |
Policy.setPolicy(savedPolicy); |
766 |
+ |
} |
767 |
+ |
} else { |
768 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
769 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
770 |
+ |
Policy.setPolicy(policy); |
771 |
+ |
|
772 |
+ |
try { |
773 |
+ |
r.run(); |
774 |
+ |
} finally { |
775 |
+ |
policy.addPermission(new SecurityPermission("setPolicy")); |
776 |
+ |
Policy.setPolicy(savedPolicy); |
777 |
+ |
} |
778 |
+ |
} |
779 |
+ |
} |
780 |
+ |
|
781 |
+ |
/** |
782 |
+ |
* Runs a runnable without any permissions. |
783 |
+ |
*/ |
784 |
+ |
public void runWithoutPermissions(Runnable r) { |
785 |
+ |
runWithPermissions(r); |
786 |
+ |
} |
787 |
|
|
788 |
|
/** |
789 |
|
* A security policy where new permissions can be dynamically added |
790 |
|
* or all cleared. |
791 |
|
*/ |
792 |
< |
static class AdjustablePolicy extends java.security.Policy { |
792 |
> |
public static class AdjustablePolicy extends java.security.Policy { |
793 |
|
Permissions perms = new Permissions(); |
794 |
< |
AdjustablePolicy() { } |
794 |
> |
AdjustablePolicy(Permission... permissions) { |
795 |
> |
for (Permission permission : permissions) |
796 |
> |
perms.add(permission); |
797 |
> |
} |
798 |
|
void addPermission(Permission perm) { perms.add(perm); } |
799 |
|
void clearPermissions() { perms = new Permissions(); } |
800 |
< |
public PermissionCollection getPermissions(CodeSource cs) { |
801 |
< |
return perms; |
802 |
< |
} |
803 |
< |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
804 |
< |
return perms; |
805 |
< |
} |
806 |
< |
public boolean implies(ProtectionDomain pd, Permission p) { |
807 |
< |
return perms.implies(p); |
808 |
< |
} |
809 |
< |
public void refresh() {} |
800 |
> |
public PermissionCollection getPermissions(CodeSource cs) { |
801 |
> |
return perms; |
802 |
> |
} |
803 |
> |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
804 |
> |
return perms; |
805 |
> |
} |
806 |
> |
public boolean implies(ProtectionDomain pd, Permission p) { |
807 |
> |
return perms.implies(p); |
808 |
> |
} |
809 |
> |
public void refresh() {} |
810 |
> |
public String toString() { |
811 |
> |
List<Permission> ps = new ArrayList<Permission>(); |
812 |
> |
for (Enumeration<Permission> e = perms.elements(); e.hasMoreElements();) |
813 |
> |
ps.add(e.nextElement()); |
814 |
> |
return "AdjustablePolicy with permissions " + ps; |
815 |
> |
} |
816 |
> |
} |
817 |
> |
|
818 |
> |
/** |
819 |
> |
* Returns a policy containing all the permissions we ever need. |
820 |
> |
*/ |
821 |
> |
public static Policy permissivePolicy() { |
822 |
> |
return new AdjustablePolicy |
823 |
> |
// Permissions j.u.c. needs directly |
824 |
> |
(new RuntimePermission("modifyThread"), |
825 |
> |
new RuntimePermission("getClassLoader"), |
826 |
> |
new RuntimePermission("setContextClassLoader"), |
827 |
> |
// Permissions needed to change permissions! |
828 |
> |
new SecurityPermission("getPolicy"), |
829 |
> |
new SecurityPermission("setPolicy"), |
830 |
> |
new RuntimePermission("setSecurityManager"), |
831 |
> |
// Permissions needed by the junit test harness |
832 |
> |
new RuntimePermission("accessDeclaredMembers"), |
833 |
> |
new PropertyPermission("*", "read"), |
834 |
> |
new java.io.FilePermission("<<ALL FILES>>", "read")); |
835 |
|
} |
836 |
|
|
837 |
|
/** |
838 |
< |
* Sleep until the timeout has elapsed, or interrupted. |
839 |
< |
* Does <em>NOT</em> throw InterruptedException. |
838 |
> |
* Sleeps until the given time has elapsed. |
839 |
> |
* Throws AssertionFailedError if interrupted. |
840 |
|
*/ |
841 |
< |
void sleepTillInterrupted(long timeoutMillis) { |
841 |
> |
void sleep(long millis) { |
842 |
|
try { |
843 |
< |
Thread.sleep(timeoutMillis); |
844 |
< |
} catch (InterruptedException wakeup) { |
843 |
> |
delay(millis); |
844 |
> |
} catch (InterruptedException ie) { |
845 |
> |
AssertionFailedError afe = |
846 |
> |
new AssertionFailedError("Unexpected InterruptedException"); |
847 |
> |
afe.initCause(ie); |
848 |
> |
throw afe; |
849 |
|
} |
850 |
|
} |
851 |
|
|
852 |
|
/** |
853 |
< |
* Returns a new started Thread running the given runnable. |
853 |
> |
* Spin-waits up to the specified number of milliseconds for the given |
854 |
> |
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING. |
855 |
> |
*/ |
856 |
> |
void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) { |
857 |
> |
long startTime = System.nanoTime(); |
858 |
> |
for (;;) { |
859 |
> |
Thread.State s = thread.getState(); |
860 |
> |
if (s == Thread.State.BLOCKED || |
861 |
> |
s == Thread.State.WAITING || |
862 |
> |
s == Thread.State.TIMED_WAITING) |
863 |
> |
return; |
864 |
> |
else if (s == Thread.State.TERMINATED) |
865 |
> |
fail("Unexpected thread termination"); |
866 |
> |
else if (millisElapsedSince(startTime) > timeoutMillis) { |
867 |
> |
threadAssertTrue(thread.isAlive()); |
868 |
> |
return; |
869 |
> |
} |
870 |
> |
Thread.yield(); |
871 |
> |
} |
872 |
> |
} |
873 |
> |
|
874 |
> |
/** |
875 |
> |
* Waits up to LONG_DELAY_MS for the given thread to enter a wait |
876 |
> |
* state: BLOCKED, WAITING, or TIMED_WAITING. |
877 |
> |
*/ |
878 |
> |
void waitForThreadToEnterWaitState(Thread thread) { |
879 |
> |
waitForThreadToEnterWaitState(thread, LONG_DELAY_MS); |
880 |
> |
} |
881 |
> |
|
882 |
> |
/** |
883 |
> |
* Returns the number of milliseconds since time given by |
884 |
> |
* startNanoTime, which must have been previously returned from a |
885 |
> |
* call to {@link System.nanoTime()}. |
886 |
> |
*/ |
887 |
> |
long millisElapsedSince(long startNanoTime) { |
888 |
> |
return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime); |
889 |
> |
} |
890 |
> |
|
891 |
> |
/** |
892 |
> |
* Returns a new started daemon Thread running the given runnable. |
893 |
|
*/ |
894 |
|
Thread newStartedThread(Runnable runnable) { |
895 |
|
Thread t = new Thread(runnable); |
896 |
+ |
t.setDaemon(true); |
897 |
|
t.start(); |
898 |
|
return t; |
899 |
|
} |
900 |
|
|
901 |
+ |
/** |
902 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
903 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
904 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
905 |
+ |
*/ |
906 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
907 |
+ |
try { |
908 |
+ |
t.join(timeoutMillis); |
909 |
+ |
} catch (InterruptedException ie) { |
910 |
+ |
threadUnexpectedException(ie); |
911 |
+ |
} finally { |
912 |
+ |
if (t.getState() != Thread.State.TERMINATED) { |
913 |
+ |
t.interrupt(); |
914 |
+ |
fail("Test timed out"); |
915 |
+ |
} |
916 |
+ |
} |
917 |
+ |
} |
918 |
+ |
|
919 |
+ |
/** |
920 |
+ |
* Waits for LONG_DELAY_MS milliseconds for the thread to |
921 |
+ |
* terminate (using {@link Thread#join(long)}), else interrupts |
922 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
923 |
+ |
*/ |
924 |
+ |
void awaitTermination(Thread t) { |
925 |
+ |
awaitTermination(t, LONG_DELAY_MS); |
926 |
+ |
} |
927 |
+ |
|
928 |
|
// Some convenient Runnable classes |
929 |
|
|
930 |
< |
abstract class CheckedRunnable implements Runnable { |
931 |
< |
abstract void realRun() throws Throwable; |
930 |
> |
public abstract class CheckedRunnable implements Runnable { |
931 |
> |
protected abstract void realRun() throws Throwable; |
932 |
|
|
933 |
|
public final void run() { |
934 |
|
try { |
939 |
|
} |
940 |
|
} |
941 |
|
|
942 |
< |
abstract class RunnableShouldThrow implements Runnable { |
943 |
< |
abstract void realRun() throws Throwable; |
942 |
> |
public abstract class RunnableShouldThrow implements Runnable { |
943 |
> |
protected abstract void realRun() throws Throwable; |
944 |
|
|
945 |
|
final Class<?> exceptionClass; |
946 |
|
|
952 |
|
try { |
953 |
|
realRun(); |
954 |
|
threadShouldThrow(exceptionClass.getSimpleName()); |
460 |
– |
} catch (InterruptedException success) { |
955 |
|
} catch (Throwable t) { |
956 |
|
if (! exceptionClass.isInstance(t)) |
957 |
|
threadUnexpectedException(t); |
959 |
|
} |
960 |
|
} |
961 |
|
|
962 |
< |
abstract class ThreadShouldThrow extends Thread { |
963 |
< |
abstract void realRun() throws Throwable; |
962 |
> |
public abstract class ThreadShouldThrow extends Thread { |
963 |
> |
protected abstract void realRun() throws Throwable; |
964 |
|
|
965 |
|
final Class<?> exceptionClass; |
966 |
|
|
972 |
|
try { |
973 |
|
realRun(); |
974 |
|
threadShouldThrow(exceptionClass.getSimpleName()); |
481 |
– |
} catch (InterruptedException success) { |
975 |
|
} catch (Throwable t) { |
976 |
|
if (! exceptionClass.isInstance(t)) |
977 |
|
threadUnexpectedException(t); |
979 |
|
} |
980 |
|
} |
981 |
|
|
982 |
< |
abstract class CheckedInterruptedRunnable implements Runnable { |
983 |
< |
abstract void realRun() throws Throwable; |
982 |
> |
public abstract class CheckedInterruptedRunnable implements Runnable { |
983 |
> |
protected abstract void realRun() throws Throwable; |
984 |
|
|
985 |
|
public final void run() { |
986 |
|
try { |
987 |
|
realRun(); |
988 |
|
threadShouldThrow("InterruptedException"); |
989 |
|
} catch (InterruptedException success) { |
990 |
+ |
threadAssertFalse(Thread.interrupted()); |
991 |
|
} catch (Throwable t) { |
992 |
|
threadUnexpectedException(t); |
993 |
|
} |
994 |
|
} |
995 |
|
} |
996 |
|
|
997 |
< |
abstract class CheckedCallable<T> implements Callable<T> { |
998 |
< |
abstract T realCall() throws Throwable; |
997 |
> |
public abstract class CheckedCallable<T> implements Callable<T> { |
998 |
> |
protected abstract T realCall() throws Throwable; |
999 |
|
|
1000 |
|
public final T call() { |
1001 |
|
try { |
1002 |
|
return realCall(); |
1003 |
|
} catch (Throwable t) { |
1004 |
|
threadUnexpectedException(t); |
1005 |
+ |
return null; |
1006 |
|
} |
512 |
– |
return null; |
1007 |
|
} |
1008 |
|
} |
1009 |
|
|
1010 |
< |
abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
1011 |
< |
abstract T realCall() throws Throwable; |
1010 |
> |
public abstract class CheckedInterruptedCallable<T> |
1011 |
> |
implements Callable<T> { |
1012 |
> |
protected abstract T realCall() throws Throwable; |
1013 |
|
|
1014 |
|
public final T call() { |
1015 |
|
try { |
1017 |
|
threadShouldThrow("InterruptedException"); |
1018 |
|
return result; |
1019 |
|
} catch (InterruptedException success) { |
1020 |
+ |
threadAssertFalse(Thread.interrupted()); |
1021 |
|
} catch (Throwable t) { |
1022 |
|
threadUnexpectedException(t); |
1023 |
|
} |
1025 |
|
} |
1026 |
|
} |
1027 |
|
|
1028 |
< |
static class NoOpRunnable implements Runnable { |
1028 |
> |
public static class NoOpRunnable implements Runnable { |
1029 |
|
public void run() {} |
1030 |
|
} |
1031 |
|
|
1032 |
< |
static class NoOpCallable implements Callable { |
1032 |
> |
public static class NoOpCallable implements Callable { |
1033 |
|
public Object call() { return Boolean.TRUE; } |
1034 |
|
} |
1035 |
|
|
1036 |
< |
static final String TEST_STRING = "a test string"; |
1036 |
> |
public static final String TEST_STRING = "a test string"; |
1037 |
|
|
1038 |
< |
static class StringTask implements Callable<String> { |
1038 |
> |
public static class StringTask implements Callable<String> { |
1039 |
|
public String call() { return TEST_STRING; } |
1040 |
|
} |
1041 |
|
|
1042 |
< |
static class NPETask implements Callable<String> { |
1042 |
> |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
1043 |
> |
return new CheckedCallable<String>() { |
1044 |
> |
protected String realCall() { |
1045 |
> |
try { |
1046 |
> |
latch.await(); |
1047 |
> |
} catch (InterruptedException quittingTime) {} |
1048 |
> |
return TEST_STRING; |
1049 |
> |
}}; |
1050 |
> |
} |
1051 |
> |
|
1052 |
> |
public Runnable awaiter(final CountDownLatch latch) { |
1053 |
> |
return new CheckedRunnable() { |
1054 |
> |
public void realRun() throws InterruptedException { |
1055 |
> |
await(latch); |
1056 |
> |
}}; |
1057 |
> |
} |
1058 |
> |
|
1059 |
> |
public void await(CountDownLatch latch) { |
1060 |
> |
try { |
1061 |
> |
assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); |
1062 |
> |
} catch (Throwable t) { |
1063 |
> |
threadUnexpectedException(t); |
1064 |
> |
} |
1065 |
> |
} |
1066 |
> |
|
1067 |
> |
public void await(Semaphore semaphore) { |
1068 |
> |
try { |
1069 |
> |
assertTrue(semaphore.tryAcquire(LONG_DELAY_MS, MILLISECONDS)); |
1070 |
> |
} catch (Throwable t) { |
1071 |
> |
threadUnexpectedException(t); |
1072 |
> |
} |
1073 |
> |
} |
1074 |
> |
|
1075 |
> |
// /** |
1076 |
> |
// * Spin-waits up to LONG_DELAY_MS until flag becomes true. |
1077 |
> |
// */ |
1078 |
> |
// public void await(AtomicBoolean flag) { |
1079 |
> |
// await(flag, LONG_DELAY_MS); |
1080 |
> |
// } |
1081 |
> |
|
1082 |
> |
// /** |
1083 |
> |
// * Spin-waits up to the specified timeout until flag becomes true. |
1084 |
> |
// */ |
1085 |
> |
// public void await(AtomicBoolean flag, long timeoutMillis) { |
1086 |
> |
// long startTime = System.nanoTime(); |
1087 |
> |
// while (!flag.get()) { |
1088 |
> |
// if (millisElapsedSince(startTime) > timeoutMillis) |
1089 |
> |
// throw new AssertionFailedError("timed out"); |
1090 |
> |
// Thread.yield(); |
1091 |
> |
// } |
1092 |
> |
// } |
1093 |
> |
|
1094 |
> |
public static class NPETask implements Callable<String> { |
1095 |
|
public String call() { throw new NullPointerException(); } |
1096 |
|
} |
1097 |
|
|
1098 |
< |
static class CallableOne implements Callable<Integer> { |
1098 |
> |
public static class CallableOne implements Callable<Integer> { |
1099 |
|
public Integer call() { return one; } |
1100 |
|
} |
1101 |
|
|
1102 |
< |
class ShortRunnable extends CheckedRunnable { |
1103 |
< |
void realRun() throws Throwable { |
1104 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1102 |
> |
public class ShortRunnable extends CheckedRunnable { |
1103 |
> |
protected void realRun() throws Throwable { |
1104 |
> |
delay(SHORT_DELAY_MS); |
1105 |
|
} |
1106 |
|
} |
1107 |
|
|
1108 |
< |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1109 |
< |
void realRun() throws InterruptedException { |
1110 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1108 |
> |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
1109 |
> |
protected void realRun() throws InterruptedException { |
1110 |
> |
delay(SHORT_DELAY_MS); |
1111 |
|
} |
1112 |
|
} |
1113 |
|
|
1114 |
< |
class SmallRunnable extends CheckedRunnable { |
1115 |
< |
void realRun() throws Throwable { |
1116 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1114 |
> |
public class SmallRunnable extends CheckedRunnable { |
1115 |
> |
protected void realRun() throws Throwable { |
1116 |
> |
delay(SMALL_DELAY_MS); |
1117 |
|
} |
1118 |
|
} |
1119 |
|
|
1120 |
< |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1121 |
< |
void realRun() { |
1120 |
> |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
1121 |
> |
protected void realRun() { |
1122 |
|
try { |
1123 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1124 |
< |
} |
577 |
< |
catch (InterruptedException ok) { |
578 |
< |
} |
1123 |
> |
delay(SMALL_DELAY_MS); |
1124 |
> |
} catch (InterruptedException ok) {} |
1125 |
|
} |
1126 |
|
} |
1127 |
|
|
1128 |
< |
class SmallCallable extends CheckedCallable { |
1129 |
< |
Object realCall() throws Throwable { |
1130 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1128 |
> |
public class SmallCallable extends CheckedCallable { |
1129 |
> |
protected Object realCall() throws InterruptedException { |
1130 |
> |
delay(SMALL_DELAY_MS); |
1131 |
|
return Boolean.TRUE; |
1132 |
|
} |
1133 |
|
} |
1134 |
|
|
1135 |
< |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
1136 |
< |
void realRun() throws InterruptedException { |
1137 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1135 |
> |
public class MediumRunnable extends CheckedRunnable { |
1136 |
> |
protected void realRun() throws Throwable { |
1137 |
> |
delay(MEDIUM_DELAY_MS); |
1138 |
|
} |
1139 |
|
} |
1140 |
|
|
1141 |
< |
class MediumRunnable extends CheckedRunnable { |
1142 |
< |
void realRun() throws Throwable { |
1143 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1141 |
> |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1142 |
> |
protected void realRun() throws InterruptedException { |
1143 |
> |
delay(MEDIUM_DELAY_MS); |
1144 |
|
} |
1145 |
|
} |
1146 |
|
|
1147 |
< |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
1148 |
< |
void realRun() throws InterruptedException { |
1149 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1150 |
< |
} |
1147 |
> |
public Runnable possiblyInterruptedRunnable(final long timeoutMillis) { |
1148 |
> |
return new CheckedRunnable() { |
1149 |
> |
protected void realRun() { |
1150 |
> |
try { |
1151 |
> |
delay(timeoutMillis); |
1152 |
> |
} catch (InterruptedException ok) {} |
1153 |
> |
}}; |
1154 |
|
} |
1155 |
|
|
1156 |
< |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1157 |
< |
void realRun() { |
1156 |
> |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
1157 |
> |
protected void realRun() { |
1158 |
|
try { |
1159 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1160 |
< |
} |
612 |
< |
catch (InterruptedException ok) { |
613 |
< |
} |
1159 |
> |
delay(MEDIUM_DELAY_MS); |
1160 |
> |
} catch (InterruptedException ok) {} |
1161 |
|
} |
1162 |
|
} |
1163 |
|
|
1164 |
< |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1165 |
< |
void realRun() { |
1164 |
> |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
1165 |
> |
protected void realRun() { |
1166 |
|
try { |
1167 |
< |
Thread.sleep(LONG_DELAY_MS); |
1168 |
< |
} |
622 |
< |
catch (InterruptedException ok) { |
623 |
< |
} |
1167 |
> |
delay(LONG_DELAY_MS); |
1168 |
> |
} catch (InterruptedException ok) {} |
1169 |
|
} |
1170 |
|
} |
1171 |
|
|
1172 |
|
/** |
1173 |
|
* For use as ThreadFactory in constructors |
1174 |
|
*/ |
1175 |
< |
static class SimpleThreadFactory implements ThreadFactory { |
1175 |
> |
public static class SimpleThreadFactory implements ThreadFactory { |
1176 |
|
public Thread newThread(Runnable r) { |
1177 |
|
return new Thread(r); |
1178 |
|
} |
1179 |
|
} |
1180 |
|
|
1181 |
< |
static class TrackedShortRunnable implements Runnable { |
1182 |
< |
volatile boolean done = false; |
1181 |
> |
public interface TrackedRunnable extends Runnable { |
1182 |
> |
boolean isDone(); |
1183 |
> |
} |
1184 |
> |
|
1185 |
> |
public static TrackedRunnable trackedRunnable(final long timeoutMillis) { |
1186 |
> |
return new TrackedRunnable() { |
1187 |
> |
private volatile boolean done = false; |
1188 |
> |
public boolean isDone() { return done; } |
1189 |
> |
public void run() { |
1190 |
> |
try { |
1191 |
> |
delay(timeoutMillis); |
1192 |
> |
done = true; |
1193 |
> |
} catch (InterruptedException ok) {} |
1194 |
> |
} |
1195 |
> |
}; |
1196 |
> |
} |
1197 |
> |
|
1198 |
> |
public static class TrackedShortRunnable implements Runnable { |
1199 |
> |
public volatile boolean done = false; |
1200 |
|
public void run() { |
1201 |
|
try { |
1202 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1202 |
> |
delay(SHORT_DELAY_MS); |
1203 |
|
done = true; |
1204 |
< |
} catch (InterruptedException ok) { |
643 |
< |
} |
1204 |
> |
} catch (InterruptedException ok) {} |
1205 |
|
} |
1206 |
|
} |
1207 |
|
|
1208 |
< |
static class TrackedMediumRunnable implements Runnable { |
1209 |
< |
volatile boolean done = false; |
1208 |
> |
public static class TrackedSmallRunnable implements Runnable { |
1209 |
> |
public volatile boolean done = false; |
1210 |
|
public void run() { |
1211 |
|
try { |
1212 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1212 |
> |
delay(SMALL_DELAY_MS); |
1213 |
|
done = true; |
1214 |
< |
} catch (InterruptedException ok) { |
654 |
< |
} |
1214 |
> |
} catch (InterruptedException ok) {} |
1215 |
|
} |
1216 |
|
} |
1217 |
|
|
1218 |
< |
static class TrackedLongRunnable implements Runnable { |
1219 |
< |
volatile boolean done = false; |
1218 |
> |
public static class TrackedMediumRunnable implements Runnable { |
1219 |
> |
public volatile boolean done = false; |
1220 |
|
public void run() { |
1221 |
|
try { |
1222 |
< |
Thread.sleep(LONG_DELAY_MS); |
1222 |
> |
delay(MEDIUM_DELAY_MS); |
1223 |
|
done = true; |
1224 |
< |
} catch (InterruptedException ok) { |
1225 |
< |
} |
1224 |
> |
} catch (InterruptedException ok) {} |
1225 |
> |
} |
1226 |
> |
} |
1227 |
> |
|
1228 |
> |
public static class TrackedLongRunnable implements Runnable { |
1229 |
> |
public volatile boolean done = false; |
1230 |
> |
public void run() { |
1231 |
> |
try { |
1232 |
> |
delay(LONG_DELAY_MS); |
1233 |
> |
done = true; |
1234 |
> |
} catch (InterruptedException ok) {} |
1235 |
|
} |
1236 |
|
} |
1237 |
|
|
1238 |
< |
static class TrackedNoOpRunnable implements Runnable { |
1239 |
< |
volatile boolean done = false; |
1238 |
> |
public static class TrackedNoOpRunnable implements Runnable { |
1239 |
> |
public volatile boolean done = false; |
1240 |
|
public void run() { |
1241 |
|
done = true; |
1242 |
|
} |
1243 |
|
} |
1244 |
|
|
1245 |
< |
static class TrackedCallable implements Callable { |
1246 |
< |
volatile boolean done = false; |
1245 |
> |
public static class TrackedCallable implements Callable { |
1246 |
> |
public volatile boolean done = false; |
1247 |
|
public Object call() { |
1248 |
|
try { |
1249 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1249 |
> |
delay(SMALL_DELAY_MS); |
1250 |
|
done = true; |
1251 |
< |
} catch (InterruptedException ok) { |
683 |
< |
} |
1251 |
> |
} catch (InterruptedException ok) {} |
1252 |
|
return Boolean.TRUE; |
1253 |
|
} |
1254 |
|
} |
1255 |
|
|
1256 |
+ |
/** |
1257 |
+ |
* Analog of CheckedRunnable for RecursiveAction |
1258 |
+ |
*/ |
1259 |
+ |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
1260 |
+ |
protected abstract void realCompute() throws Throwable; |
1261 |
+ |
|
1262 |
+ |
@Override protected final void compute() { |
1263 |
+ |
try { |
1264 |
+ |
realCompute(); |
1265 |
+ |
} catch (Throwable t) { |
1266 |
+ |
threadUnexpectedException(t); |
1267 |
+ |
} |
1268 |
+ |
} |
1269 |
+ |
} |
1270 |
+ |
|
1271 |
+ |
/** |
1272 |
+ |
* Analog of CheckedCallable for RecursiveTask |
1273 |
+ |
*/ |
1274 |
+ |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
1275 |
+ |
protected abstract T realCompute() throws Throwable; |
1276 |
+ |
|
1277 |
+ |
@Override protected final T compute() { |
1278 |
+ |
try { |
1279 |
+ |
return realCompute(); |
1280 |
+ |
} catch (Throwable t) { |
1281 |
+ |
threadUnexpectedException(t); |
1282 |
+ |
return null; |
1283 |
+ |
} |
1284 |
+ |
} |
1285 |
+ |
} |
1286 |
|
|
1287 |
|
/** |
1288 |
|
* For use as RejectedExecutionHandler in constructors |
1289 |
|
*/ |
1290 |
< |
static class NoOpREHandler implements RejectedExecutionHandler { |
1290 |
> |
public static class NoOpREHandler implements RejectedExecutionHandler { |
1291 |
|
public void rejectedExecution(Runnable r, |
1292 |
|
ThreadPoolExecutor executor) {} |
1293 |
|
} |
1294 |
|
|
1295 |
+ |
/** |
1296 |
+ |
* A CyclicBarrier that uses timed await and fails with |
1297 |
+ |
* AssertionFailedErrors instead of throwing checked exceptions. |
1298 |
+ |
*/ |
1299 |
+ |
public class CheckedBarrier extends CyclicBarrier { |
1300 |
+ |
public CheckedBarrier(int parties) { super(parties); } |
1301 |
+ |
|
1302 |
+ |
public int await() { |
1303 |
+ |
try { |
1304 |
+ |
return super.await(2 * LONG_DELAY_MS, MILLISECONDS); |
1305 |
+ |
} catch (TimeoutException e) { |
1306 |
+ |
throw new AssertionFailedError("timed out"); |
1307 |
+ |
} catch (Exception e) { |
1308 |
+ |
AssertionFailedError afe = |
1309 |
+ |
new AssertionFailedError("Unexpected exception: " + e); |
1310 |
+ |
afe.initCause(e); |
1311 |
+ |
throw afe; |
1312 |
+ |
} |
1313 |
+ |
} |
1314 |
+ |
} |
1315 |
+ |
|
1316 |
+ |
void checkEmpty(BlockingQueue q) { |
1317 |
+ |
try { |
1318 |
+ |
assertTrue(q.isEmpty()); |
1319 |
+ |
assertEquals(0, q.size()); |
1320 |
+ |
assertNull(q.peek()); |
1321 |
+ |
assertNull(q.poll()); |
1322 |
+ |
assertNull(q.poll(0, MILLISECONDS)); |
1323 |
+ |
assertEquals(q.toString(), "[]"); |
1324 |
+ |
assertTrue(Arrays.equals(q.toArray(), new Object[0])); |
1325 |
+ |
assertFalse(q.iterator().hasNext()); |
1326 |
+ |
try { |
1327 |
+ |
q.element(); |
1328 |
+ |
shouldThrow(); |
1329 |
+ |
} catch (NoSuchElementException success) {} |
1330 |
+ |
try { |
1331 |
+ |
q.iterator().next(); |
1332 |
+ |
shouldThrow(); |
1333 |
+ |
} catch (NoSuchElementException success) {} |
1334 |
+ |
try { |
1335 |
+ |
q.remove(); |
1336 |
+ |
shouldThrow(); |
1337 |
+ |
} catch (NoSuchElementException success) {} |
1338 |
+ |
} catch (InterruptedException ie) { |
1339 |
+ |
threadUnexpectedException(ie); |
1340 |
+ |
} |
1341 |
+ |
} |
1342 |
+ |
|
1343 |
+ |
void assertSerialEquals(Object x, Object y) { |
1344 |
+ |
assertTrue(Arrays.equals(serialBytes(x), serialBytes(y))); |
1345 |
+ |
} |
1346 |
+ |
|
1347 |
+ |
void assertNotSerialEquals(Object x, Object y) { |
1348 |
+ |
assertFalse(Arrays.equals(serialBytes(x), serialBytes(y))); |
1349 |
+ |
} |
1350 |
+ |
|
1351 |
+ |
byte[] serialBytes(Object o) { |
1352 |
+ |
try { |
1353 |
+ |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
1354 |
+ |
ObjectOutputStream oos = new ObjectOutputStream(bos); |
1355 |
+ |
oos.writeObject(o); |
1356 |
+ |
oos.flush(); |
1357 |
+ |
oos.close(); |
1358 |
+ |
return bos.toByteArray(); |
1359 |
+ |
} catch (Throwable t) { |
1360 |
+ |
threadUnexpectedException(t); |
1361 |
+ |
return new byte[0]; |
1362 |
+ |
} |
1363 |
+ |
} |
1364 |
+ |
|
1365 |
+ |
@SuppressWarnings("unchecked") |
1366 |
+ |
<T> T serialClone(T o) { |
1367 |
+ |
try { |
1368 |
+ |
ObjectInputStream ois = new ObjectInputStream |
1369 |
+ |
(new ByteArrayInputStream(serialBytes(o))); |
1370 |
+ |
T clone = (T) ois.readObject(); |
1371 |
+ |
assertSame(o.getClass(), clone.getClass()); |
1372 |
+ |
return clone; |
1373 |
+ |
} catch (Throwable t) { |
1374 |
+ |
threadUnexpectedException(t); |
1375 |
+ |
return null; |
1376 |
+ |
} |
1377 |
+ |
} |
1378 |
+ |
|
1379 |
+ |
public void assertThrows(Class<? extends Throwable> expectedExceptionClass, |
1380 |
+ |
Runnable... throwingActions) { |
1381 |
+ |
for (Runnable throwingAction : throwingActions) { |
1382 |
+ |
boolean threw = false; |
1383 |
+ |
try { throwingAction.run(); } |
1384 |
+ |
catch (Throwable t) { |
1385 |
+ |
threw = true; |
1386 |
+ |
if (!expectedExceptionClass.isInstance(t)) { |
1387 |
+ |
AssertionFailedError afe = |
1388 |
+ |
new AssertionFailedError |
1389 |
+ |
("Expected " + expectedExceptionClass.getName() + |
1390 |
+ |
", got " + t.getClass().getName()); |
1391 |
+ |
afe.initCause(t); |
1392 |
+ |
threadUnexpectedException(afe); |
1393 |
+ |
} |
1394 |
+ |
} |
1395 |
+ |
if (!threw) |
1396 |
+ |
shouldThrow(expectedExceptionClass.getName()); |
1397 |
+ |
} |
1398 |
+ |
} |
1399 |
|
} |