7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
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 java.io.*; |
15 |
< |
import java.security.*; |
14 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
> |
import java.security.CodeSource; |
16 |
> |
import java.security.Permission; |
17 |
> |
import java.security.PermissionCollection; |
18 |
> |
import java.security.Permissions; |
19 |
> |
import java.security.Policy; |
20 |
> |
import java.security.ProtectionDomain; |
21 |
> |
import java.security.SecurityPermission; |
22 |
|
|
23 |
|
/** |
24 |
|
* Base class for JSR166 Junit TCK tests. Defines some constants, |
33 |
|
* <li> All assertions in code running in generated threads must use |
34 |
|
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
35 |
|
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
36 |
< |
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
36 |
> |
* {@code fail}, {@code assertTrue}, etc.) It is OK (but not |
37 |
|
* particularly recommended) for other code to use these forms too. |
38 |
|
* Only the most typically used JUnit assertion methods are defined |
39 |
|
* this way, but enough to live with.</li> |
40 |
|
* |
41 |
|
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
42 |
< |
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
42 |
> |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
43 |
|
* them. These methods are used to clear and check for thread |
44 |
|
* assertion failures.</li> |
45 |
|
* |
46 |
< |
* <li>All delays and timeouts must use one of the constants <tt> |
47 |
< |
* SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>, |
48 |
< |
* <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always |
46 |
> |
* <li>All delays and timeouts must use one of the constants {@code |
47 |
> |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
48 |
> |
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
49 |
|
* discriminable from zero time, and always allows enough time for the |
50 |
|
* small amounts of computation (creating a thread, calling a few |
51 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
55 |
|
* in one spot to rerun tests on slower platforms.</li> |
56 |
|
* |
57 |
|
* <li> All threads generated must be joined inside each test case |
58 |
< |
* method (or <tt>fail</tt> to do so) before returning from the |
59 |
< |
* method. The <tt> joinPool</tt> method can be used to do this when |
58 |
> |
* method (or {@code fail} to do so) before returning from the |
59 |
> |
* method. The {@code joinPool} method can be used to do this when |
60 |
|
* using Executors.</li> |
61 |
|
* |
62 |
|
* </ol> |
88 |
|
* any particular package to simplify things for people integrating |
89 |
|
* them in TCK test suites.</li> |
90 |
|
* |
91 |
< |
* <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase) |
91 |
> |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
92 |
|
* runs all JSR166 unit tests.</li> |
93 |
|
* |
94 |
|
* </ul> |
95 |
|
*/ |
96 |
|
public class JSR166TestCase extends TestCase { |
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 |
|
*/ |
139 |
< |
public static void main (String[] args) { |
140 |
< |
int iters = 1; |
141 |
< |
if (args.length > 0) |
142 |
< |
iters = Integer.parseInt(args[0]); |
139 |
> |
public static void main(String[] args) { |
140 |
> |
if (useSecurityManager) { |
141 |
> |
System.err.println("Setting a permissive security manager"); |
142 |
> |
Policy.setPolicy(permissivePolicy()); |
143 |
> |
System.setSecurityManager(new SecurityManager()); |
144 |
> |
} |
145 |
> |
int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]); |
146 |
> |
|
147 |
|
Test s = suite(); |
148 |
|
for (int i = 0; i < iters; ++i) { |
149 |
< |
junit.textui.TestRunner.run (s); |
149 |
> |
junit.textui.TestRunner.run(s); |
150 |
|
System.gc(); |
151 |
|
System.runFinalization(); |
152 |
|
} |
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(new TestSuite(ForkJoinPoolTest.class)); |
176 |
< |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
177 |
< |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
178 |
< |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
179 |
< |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
180 |
< |
suite.addTest(new TestSuite(PhaserTest.class)); |
181 |
< |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
182 |
< |
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
183 |
< |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
184 |
< |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
185 |
< |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
186 |
< |
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
187 |
< |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
188 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
189 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
190 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
191 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
192 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
193 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
194 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
195 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
196 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
197 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
198 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
199 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
200 |
< |
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
201 |
< |
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
202 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
203 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
204 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
205 |
< |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
206 |
< |
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
207 |
< |
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
208 |
< |
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
209 |
< |
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
210 |
< |
suite.addTest(new TestSuite(DelayQueueTest.class)); |
211 |
< |
suite.addTest(new TestSuite(EntryTest.class)); |
212 |
< |
suite.addTest(new TestSuite(ExchangerTest.class)); |
213 |
< |
suite.addTest(new TestSuite(ExecutorsTest.class)); |
214 |
< |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
215 |
< |
suite.addTest(new TestSuite(FutureTaskTest.class)); |
216 |
< |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
217 |
< |
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
218 |
< |
suite.addTest(new TestSuite(LinkedListTest.class)); |
219 |
< |
suite.addTest(new TestSuite(LockSupportTest.class)); |
220 |
< |
suite.addTest(new TestSuite(PriorityBlockingQueueTest.class)); |
221 |
< |
suite.addTest(new TestSuite(PriorityQueueTest.class)); |
222 |
< |
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
223 |
< |
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
224 |
< |
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
225 |
< |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
226 |
< |
suite.addTest(new TestSuite(SemaphoreTest.class)); |
227 |
< |
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
228 |
< |
suite.addTest(new TestSuite(SystemTest.class)); |
229 |
< |
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
230 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
231 |
< |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
232 |
< |
suite.addTest(new TestSuite(ThreadTest.class)); |
233 |
< |
suite.addTest(new TestSuite(TimeUnitTest.class)); |
234 |
< |
suite.addTest(new TestSuite(TreeMapTest.class)); |
235 |
< |
suite.addTest(new TestSuite(TreeSetTest.class)); |
236 |
< |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
237 |
< |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
175 |
< |
|
176 |
< |
return suite; |
172 |
> |
public static Test 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 |
|
|
256 |
|
/** |
257 |
|
* Sets delays as multiples of SHORT_DELAY. |
258 |
|
*/ |
259 |
< |
protected void setDelays() { |
259 |
> |
protected void setDelays() { |
260 |
|
SHORT_DELAY_MS = getShortDelay(); |
261 |
< |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
261 |
> |
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
262 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
263 |
< |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
263 |
> |
LONG_DELAY_MS = SHORT_DELAY_MS * 50; |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
< |
* Flag set true if any threadAssert methods fail |
267 |
> |
* The first exception encountered if any threadAssertXXX method fails. |
268 |
|
*/ |
269 |
< |
volatile boolean threadFailed; |
269 |
> |
private final AtomicReference<Throwable> threadFailure |
270 |
> |
= new AtomicReference<Throwable>(null); |
271 |
|
|
272 |
|
/** |
273 |
< |
* Initializes test to indicate that no thread assertions have failed |
273 |
> |
* Records an exception so that it can be rethrown later in the test |
274 |
> |
* harness thread, triggering a test case failure. Only the first |
275 |
> |
* failure is recorded; subsequent calls to this method from within |
276 |
> |
* the same test have no effect. |
277 |
|
*/ |
278 |
+ |
public void threadRecordFailure(Throwable t) { |
279 |
+ |
threadFailure.compareAndSet(null, t); |
280 |
+ |
} |
281 |
+ |
|
282 |
|
public void setUp() { |
283 |
|
setDelays(); |
215 |
– |
threadFailed = false; |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* Triggers test case failure if any thread assertions have failed |
288 |
< |
*/ |
289 |
< |
public void tearDown() { |
290 |
< |
assertFalse(threadFailed); |
287 |
> |
* Triggers test case failure if any thread assertions have failed, |
288 |
> |
* by rethrowing, in the test harness thread, any exception recorded |
289 |
> |
* earlier by threadRecordFailure. |
290 |
> |
*/ |
291 |
> |
public void tearDown() throws Exception { |
292 |
> |
Throwable t = threadFailure.get(); |
293 |
> |
if (t != null) { |
294 |
> |
if (t instanceof Error) |
295 |
> |
throw (Error) t; |
296 |
> |
else if (t instanceof RuntimeException) |
297 |
> |
throw (RuntimeException) t; |
298 |
> |
else if (t instanceof Exception) |
299 |
> |
throw (Exception) t; |
300 |
> |
else { |
301 |
> |
AssertionFailedError afe = |
302 |
> |
new AssertionFailedError(t.toString()); |
303 |
> |
afe.initCause(t); |
304 |
> |
throw afe; |
305 |
> |
} |
306 |
> |
} |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
< |
* Fail, also setting status to indicate current testcase should fail |
310 |
> |
* Just like fail(reason), but additionally recording (using |
311 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
312 |
> |
* the current testcase will fail. |
313 |
|
*/ |
314 |
|
public void threadFail(String reason) { |
315 |
< |
threadFailed = true; |
316 |
< |
fail(reason); |
315 |
> |
try { |
316 |
> |
fail(reason); |
317 |
> |
} catch (AssertionFailedError t) { |
318 |
> |
threadRecordFailure(t); |
319 |
> |
fail(reason); |
320 |
> |
} |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
< |
* If expression not true, set status to indicate current testcase |
325 |
< |
* should fail |
324 |
> |
* Just like assertTrue(b), but additionally recording (using |
325 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
326 |
> |
* the current testcase will fail. |
327 |
|
*/ |
328 |
|
public void threadAssertTrue(boolean b) { |
329 |
< |
if (!b) { |
239 |
< |
threadFailed = true; |
329 |
> |
try { |
330 |
|
assertTrue(b); |
331 |
+ |
} catch (AssertionFailedError t) { |
332 |
+ |
threadRecordFailure(t); |
333 |
+ |
throw t; |
334 |
|
} |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* If expression not false, set status to indicate current testcase |
339 |
< |
* should fail |
338 |
> |
* Just like assertFalse(b), but additionally recording (using |
339 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
340 |
> |
* the current testcase will fail. |
341 |
|
*/ |
342 |
|
public void threadAssertFalse(boolean b) { |
343 |
< |
if (b) { |
250 |
< |
threadFailed = true; |
343 |
> |
try { |
344 |
|
assertFalse(b); |
345 |
+ |
} catch (AssertionFailedError t) { |
346 |
+ |
threadRecordFailure(t); |
347 |
+ |
throw t; |
348 |
|
} |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
< |
* If argument not null, set status to indicate current testcase |
353 |
< |
* should fail |
352 |
> |
* Just like assertNull(x), but additionally recording (using |
353 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
354 |
> |
* the current testcase will fail. |
355 |
|
*/ |
356 |
|
public void threadAssertNull(Object x) { |
357 |
< |
if (x != null) { |
261 |
< |
threadFailed = true; |
357 |
> |
try { |
358 |
|
assertNull(x); |
359 |
+ |
} catch (AssertionFailedError t) { |
360 |
+ |
threadRecordFailure(t); |
361 |
+ |
throw t; |
362 |
|
} |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
< |
* If arguments not equal, set status to indicate current testcase |
367 |
< |
* should fail |
366 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
367 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
368 |
> |
* the current testcase will fail. |
369 |
|
*/ |
370 |
|
public void threadAssertEquals(long x, long y) { |
371 |
< |
if (x != y) { |
272 |
< |
threadFailed = true; |
371 |
> |
try { |
372 |
|
assertEquals(x, y); |
373 |
+ |
} catch (AssertionFailedError t) { |
374 |
+ |
threadRecordFailure(t); |
375 |
+ |
throw t; |
376 |
|
} |
377 |
|
} |
378 |
|
|
379 |
|
/** |
380 |
< |
* If arguments not equal, set status to indicate current testcase |
381 |
< |
* should fail |
380 |
> |
* Just like assertEquals(x, y), but additionally recording (using |
381 |
> |
* threadRecordFailure) any AssertionFailedError thrown, so that |
382 |
> |
* the current testcase will fail. |
383 |
|
*/ |
384 |
|
public void threadAssertEquals(Object x, Object y) { |
385 |
< |
if (x != y && (x == null || !x.equals(y))) { |
283 |
< |
threadFailed = true; |
385 |
> |
try { |
386 |
|
assertEquals(x, y); |
387 |
+ |
} catch (AssertionFailedError t) { |
388 |
+ |
threadRecordFailure(t); |
389 |
+ |
throw t; |
390 |
+ |
} catch (Throwable t) { |
391 |
+ |
threadUnexpectedException(t); |
392 |
+ |
} |
393 |
+ |
} |
394 |
+ |
|
395 |
+ |
/** |
396 |
+ |
* Just like assertSame(x, y), but additionally recording (using |
397 |
+ |
* threadRecordFailure) any AssertionFailedError thrown, so that |
398 |
+ |
* the current testcase will fail. |
399 |
+ |
*/ |
400 |
+ |
public void threadAssertSame(Object x, Object y) { |
401 |
+ |
try { |
402 |
+ |
assertSame(x, y); |
403 |
+ |
} catch (AssertionFailedError t) { |
404 |
+ |
threadRecordFailure(t); |
405 |
+ |
throw t; |
406 |
|
} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* threadFail with message "should throw exception" |
410 |
> |
* Calls threadFail with message "should throw exception". |
411 |
|
*/ |
412 |
|
public void threadShouldThrow() { |
413 |
< |
threadFailed = true; |
293 |
< |
fail("should throw exception"); |
413 |
> |
threadFail("should throw exception"); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
417 |
< |
* threadFail with message "Unexpected exception" |
417 |
> |
* Calls threadFail with message "should throw" + exceptionName. |
418 |
|
*/ |
419 |
< |
public void threadUnexpectedException() { |
420 |
< |
threadFailed = true; |
301 |
< |
fail("Unexpected exception"); |
419 |
> |
public void threadShouldThrow(String exceptionName) { |
420 |
> |
threadFail("should throw " + exceptionName); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
< |
* threadFail with message "Unexpected exception", with argument |
424 |
> |
* Records the given exception using {@link #threadRecordFailure}, |
425 |
> |
* then rethrows the exception, wrapping it in an |
426 |
> |
* AssertionFailedError if necessary. |
427 |
|
*/ |
428 |
< |
public void threadUnexpectedException(Throwable ex) { |
429 |
< |
threadFailed = true; |
430 |
< |
ex.printStackTrace(); |
431 |
< |
fail("Unexpected exception: " + ex); |
428 |
> |
public void threadUnexpectedException(Throwable t) { |
429 |
> |
threadRecordFailure(t); |
430 |
> |
t.printStackTrace(); |
431 |
> |
if (t instanceof RuntimeException) |
432 |
> |
throw (RuntimeException) t; |
433 |
> |
else if (t instanceof Error) |
434 |
> |
throw (Error) t; |
435 |
> |
else { |
436 |
> |
AssertionFailedError afe = |
437 |
> |
new AssertionFailedError("unexpected exception: " + t); |
438 |
> |
t.initCause(t); |
439 |
> |
throw afe; |
440 |
> |
} |
441 |
|
} |
442 |
|
|
443 |
|
/** |
444 |
< |
* Wait out termination of a thread pool or fail doing so |
444 |
> |
* Waits out termination of a thread pool or fails doing so. |
445 |
|
*/ |
446 |
|
public void joinPool(ExecutorService exec) { |
447 |
|
try { |
448 |
|
exec.shutdown(); |
449 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
449 |
> |
assertTrue("ExecutorService did not terminate in a timely manner", |
450 |
> |
exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
451 |
|
} catch (SecurityException ok) { |
452 |
|
// Allowed in case test doesn't have privs |
453 |
|
} catch (InterruptedException ie) { |
454 |
< |
fail("Unexpected exception"); |
454 |
> |
fail("Unexpected InterruptedException"); |
455 |
|
} |
456 |
|
} |
457 |
|
|
458 |
|
|
459 |
|
/** |
460 |
< |
* fail with message "should throw exception" |
460 |
> |
* Fails with message "should throw exception". |
461 |
|
*/ |
462 |
|
public void shouldThrow() { |
463 |
|
fail("Should throw exception"); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
< |
* fail with message "Unexpected exception" |
467 |
> |
* Fails with message "should throw " + exceptionName. |
468 |
|
*/ |
469 |
< |
public void unexpectedException() { |
470 |
< |
fail("Unexpected exception"); |
469 |
> |
public void shouldThrow(String exceptionName) { |
470 |
> |
fail("Should throw " + exceptionName); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
343 |
– |
* fail with message "Unexpected exception", with argument |
344 |
– |
*/ |
345 |
– |
public void unexpectedException(Throwable ex) { |
346 |
– |
ex.printStackTrace(); |
347 |
– |
fail("Unexpected exception: " + ex); |
348 |
– |
} |
349 |
– |
|
350 |
– |
|
351 |
– |
/** |
474 |
|
* The number of elements to place in collections, arrays, etc. |
475 |
|
*/ |
476 |
< |
static final int SIZE = 20; |
476 |
> |
public static final int SIZE = 20; |
477 |
|
|
478 |
|
// Some convenient Integer constants |
479 |
|
|
480 |
< |
static final Integer zero = new Integer(0); |
481 |
< |
static final Integer one = new Integer(1); |
482 |
< |
static final Integer two = new Integer(2); |
483 |
< |
static final Integer three = new Integer(3); |
484 |
< |
static final Integer four = new Integer(4); |
485 |
< |
static final Integer five = new Integer(5); |
486 |
< |
static final Integer six = new Integer(6); |
487 |
< |
static final Integer seven = new Integer(7); |
488 |
< |
static final Integer eight = new Integer(8); |
489 |
< |
static final Integer nine = new Integer(9); |
490 |
< |
static final Integer m1 = new Integer(-1); |
491 |
< |
static final Integer m2 = new Integer(-2); |
492 |
< |
static final Integer m3 = new Integer(-3); |
493 |
< |
static final Integer m4 = new Integer(-4); |
494 |
< |
static final Integer m5 = new Integer(-5); |
495 |
< |
static final Integer m6 = new Integer(-6); |
496 |
< |
static final Integer m10 = new Integer(-10); |
480 |
> |
public static final Integer zero = new Integer(0); |
481 |
> |
public static final Integer one = new Integer(1); |
482 |
> |
public static final Integer two = new Integer(2); |
483 |
> |
public static final Integer three = new Integer(3); |
484 |
> |
public static final Integer four = new Integer(4); |
485 |
> |
public static final Integer five = new Integer(5); |
486 |
> |
public static final Integer six = new Integer(6); |
487 |
> |
public static final Integer seven = new Integer(7); |
488 |
> |
public static final Integer eight = new Integer(8); |
489 |
> |
public static final Integer nine = new Integer(9); |
490 |
> |
public static final Integer m1 = new Integer(-1); |
491 |
> |
public static final Integer m2 = new Integer(-2); |
492 |
> |
public static final Integer m3 = new Integer(-3); |
493 |
> |
public static final Integer m4 = new Integer(-4); |
494 |
> |
public static final Integer m5 = new Integer(-5); |
495 |
> |
public static final Integer m6 = new Integer(-6); |
496 |
> |
public static final Integer m10 = new Integer(-10); |
497 |
> |
|
498 |
|
|
499 |
+ |
/** |
500 |
+ |
* Runs Runnable r with a security policy that permits precisely |
501 |
+ |
* the specified permissions. If there is no current security |
502 |
+ |
* manager, the runnable is run twice, both with and without a |
503 |
+ |
* security manager. We require that any security manager permit |
504 |
+ |
* getPolicy/setPolicy. |
505 |
+ |
*/ |
506 |
+ |
public void runWithPermissions(Runnable r, Permission... permissions) { |
507 |
+ |
SecurityManager sm = System.getSecurityManager(); |
508 |
+ |
if (sm == null) { |
509 |
+ |
r.run(); |
510 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
511 |
+ |
try { |
512 |
+ |
Policy.setPolicy(permissivePolicy()); |
513 |
+ |
System.setSecurityManager(new SecurityManager()); |
514 |
+ |
runWithPermissions(r, permissions); |
515 |
+ |
} finally { |
516 |
+ |
System.setSecurityManager(null); |
517 |
+ |
Policy.setPolicy(savedPolicy); |
518 |
+ |
} |
519 |
+ |
} else { |
520 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
521 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
522 |
+ |
Policy.setPolicy(policy); |
523 |
+ |
|
524 |
+ |
try { |
525 |
+ |
r.run(); |
526 |
+ |
} finally { |
527 |
+ |
policy.addPermission(new SecurityPermission("setPolicy")); |
528 |
+ |
Policy.setPolicy(savedPolicy); |
529 |
+ |
} |
530 |
+ |
} |
531 |
+ |
} |
532 |
+ |
|
533 |
+ |
/** |
534 |
+ |
* Runs a runnable without any permissions. |
535 |
+ |
*/ |
536 |
+ |
public void runWithoutPermissions(Runnable r) { |
537 |
+ |
runWithPermissions(r); |
538 |
+ |
} |
539 |
|
|
540 |
|
/** |
541 |
|
* A security policy where new permissions can be dynamically added |
542 |
|
* or all cleared. |
543 |
|
*/ |
544 |
< |
static class AdjustablePolicy extends java.security.Policy { |
544 |
> |
public static class AdjustablePolicy extends java.security.Policy { |
545 |
|
Permissions perms = new Permissions(); |
546 |
< |
AdjustablePolicy() { } |
546 |
> |
AdjustablePolicy(Permission... permissions) { |
547 |
> |
for (Permission permission : permissions) |
548 |
> |
perms.add(permission); |
549 |
> |
} |
550 |
|
void addPermission(Permission perm) { perms.add(perm); } |
551 |
|
void clearPermissions() { perms = new Permissions(); } |
552 |
< |
public PermissionCollection getPermissions(CodeSource cs) { |
553 |
< |
return perms; |
554 |
< |
} |
555 |
< |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
556 |
< |
return perms; |
557 |
< |
} |
558 |
< |
public boolean implies(ProtectionDomain pd, Permission p) { |
559 |
< |
return perms.implies(p); |
560 |
< |
} |
561 |
< |
public void refresh() {} |
552 |
> |
public PermissionCollection getPermissions(CodeSource cs) { |
553 |
> |
return perms; |
554 |
> |
} |
555 |
> |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
556 |
> |
return perms; |
557 |
> |
} |
558 |
> |
public boolean implies(ProtectionDomain pd, Permission p) { |
559 |
> |
return perms.implies(p); |
560 |
> |
} |
561 |
> |
public void refresh() {} |
562 |
> |
} |
563 |
> |
|
564 |
> |
/** |
565 |
> |
* Returns a policy containing all the permissions we ever need. |
566 |
> |
*/ |
567 |
> |
public static Policy permissivePolicy() { |
568 |
> |
return new AdjustablePolicy |
569 |
> |
// Permissions j.u.c. needs directly |
570 |
> |
(new RuntimePermission("modifyThread"), |
571 |
> |
new RuntimePermission("getClassLoader"), |
572 |
> |
new RuntimePermission("setContextClassLoader"), |
573 |
> |
// Permissions needed to change permissions! |
574 |
> |
new SecurityPermission("getPolicy"), |
575 |
> |
new SecurityPermission("setPolicy"), |
576 |
> |
new RuntimePermission("setSecurityManager"), |
577 |
> |
// Permissions needed by the junit test harness |
578 |
> |
new RuntimePermission("accessDeclaredMembers"), |
579 |
> |
new PropertyPermission("*", "read"), |
580 |
> |
new java.io.FilePermission("<<ALL FILES>>", "read")); |
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 |
> |
*/ |
602 |
> |
void sleepTillInterrupted(long timeoutMillis) { |
603 |
> |
try { |
604 |
> |
Thread.sleep(timeoutMillis); |
605 |
> |
} catch (InterruptedException wakeup) {} |
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) { |
639 |
+ |
Thread t = new Thread(runnable); |
640 |
+ |
t.setDaemon(true); |
641 |
+ |
t.start(); |
642 |
+ |
return t; |
643 |
+ |
} |
644 |
+ |
|
645 |
+ |
/** |
646 |
+ |
* Waits for the specified time (in milliseconds) for the thread |
647 |
+ |
* to terminate (using {@link Thread#join(long)}), else interrupts |
648 |
+ |
* the thread (in the hope that it may terminate later) and fails. |
649 |
+ |
*/ |
650 |
+ |
void awaitTermination(Thread t, long timeoutMillis) { |
651 |
+ |
try { |
652 |
+ |
t.join(timeoutMillis); |
653 |
+ |
} catch (InterruptedException ie) { |
654 |
+ |
threadUnexpectedException(ie); |
655 |
+ |
} finally { |
656 |
+ |
if (t.isAlive()) { |
657 |
+ |
t.interrupt(); |
658 |
+ |
fail("Test timed out"); |
659 |
+ |
} |
660 |
+ |
} |
661 |
+ |
} |
662 |
|
|
663 |
|
// Some convenient Runnable classes |
664 |
|
|
665 |
< |
abstract class CheckedRunnable implements Runnable { |
666 |
< |
abstract void realRun() throws Throwable; |
665 |
> |
public abstract class CheckedRunnable implements Runnable { |
666 |
> |
protected abstract void realRun() throws Throwable; |
667 |
|
|
668 |
|
public final void run() { |
669 |
|
try { |
674 |
|
} |
675 |
|
} |
676 |
|
|
677 |
< |
abstract class CheckedInterruptedRunnable implements Runnable { |
678 |
< |
abstract void realRun() throws Throwable; |
677 |
> |
public abstract class RunnableShouldThrow implements Runnable { |
678 |
> |
protected abstract void realRun() throws Throwable; |
679 |
> |
|
680 |
> |
final Class<?> exceptionClass; |
681 |
> |
|
682 |
> |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
683 |
> |
this.exceptionClass = exceptionClass; |
684 |
> |
} |
685 |
> |
|
686 |
> |
public final void run() { |
687 |
> |
try { |
688 |
> |
realRun(); |
689 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
690 |
> |
} catch (Throwable t) { |
691 |
> |
if (! exceptionClass.isInstance(t)) |
692 |
> |
threadUnexpectedException(t); |
693 |
> |
} |
694 |
> |
} |
695 |
> |
} |
696 |
> |
|
697 |
> |
public abstract class ThreadShouldThrow extends Thread { |
698 |
> |
protected abstract void realRun() throws Throwable; |
699 |
> |
|
700 |
> |
final Class<?> exceptionClass; |
701 |
> |
|
702 |
> |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
703 |
> |
this.exceptionClass = exceptionClass; |
704 |
> |
} |
705 |
> |
|
706 |
> |
public final void run() { |
707 |
> |
try { |
708 |
> |
realRun(); |
709 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
710 |
> |
} catch (Throwable t) { |
711 |
> |
if (! exceptionClass.isInstance(t)) |
712 |
> |
threadUnexpectedException(t); |
713 |
> |
} |
714 |
> |
} |
715 |
> |
} |
716 |
> |
|
717 |
> |
public abstract class CheckedInterruptedRunnable implements Runnable { |
718 |
> |
protected abstract void realRun() throws Throwable; |
719 |
|
|
720 |
|
public final void run() { |
721 |
|
try { |
722 |
|
realRun(); |
723 |
< |
threadShouldThrow(); |
723 |
> |
threadShouldThrow("InterruptedException"); |
724 |
|
} catch (InterruptedException success) { |
725 |
|
} catch (Throwable t) { |
726 |
|
threadUnexpectedException(t); |
728 |
|
} |
729 |
|
} |
730 |
|
|
731 |
< |
abstract class CheckedCallable<T> implements Callable<T> { |
732 |
< |
abstract T realCall() throws Throwable; |
731 |
> |
public abstract class CheckedCallable<T> implements Callable<T> { |
732 |
> |
protected abstract T realCall() throws Throwable; |
733 |
|
|
734 |
|
public final T call() { |
735 |
|
try { |
741 |
|
} |
742 |
|
} |
743 |
|
|
744 |
< |
static class NoOpRunnable implements Runnable { |
744 |
> |
public abstract class CheckedInterruptedCallable<T> |
745 |
> |
implements Callable<T> { |
746 |
> |
protected abstract T realCall() throws Throwable; |
747 |
> |
|
748 |
> |
public final T call() { |
749 |
> |
try { |
750 |
> |
T result = realCall(); |
751 |
> |
threadShouldThrow("InterruptedException"); |
752 |
> |
return result; |
753 |
> |
} catch (InterruptedException success) { |
754 |
> |
} catch (Throwable t) { |
755 |
> |
threadUnexpectedException(t); |
756 |
> |
} |
757 |
> |
return null; |
758 |
> |
} |
759 |
> |
} |
760 |
> |
|
761 |
> |
public static class NoOpRunnable implements Runnable { |
762 |
|
public void run() {} |
763 |
|
} |
764 |
|
|
765 |
< |
static class NoOpCallable implements Callable { |
765 |
> |
public static class NoOpCallable implements Callable { |
766 |
|
public Object call() { return Boolean.TRUE; } |
767 |
|
} |
768 |
|
|
769 |
< |
static final String TEST_STRING = "a test string"; |
769 |
> |
public static final String TEST_STRING = "a test string"; |
770 |
|
|
771 |
< |
static class StringTask implements Callable<String> { |
771 |
> |
public static class StringTask implements Callable<String> { |
772 |
|
public String call() { return TEST_STRING; } |
773 |
|
} |
774 |
|
|
775 |
< |
static class NPETask implements Callable<String> { |
775 |
> |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
776 |
> |
return new CheckedCallable<String>() { |
777 |
> |
protected String realCall() { |
778 |
> |
try { |
779 |
> |
latch.await(); |
780 |
> |
} catch (InterruptedException quittingTime) {} |
781 |
> |
return TEST_STRING; |
782 |
> |
}}; |
783 |
> |
} |
784 |
> |
|
785 |
> |
public static class NPETask implements Callable<String> { |
786 |
|
public String call() { throw new NullPointerException(); } |
787 |
|
} |
788 |
|
|
789 |
< |
static class CallableOne implements Callable<Integer> { |
789 |
> |
public static class CallableOne implements Callable<Integer> { |
790 |
|
public Integer call() { return one; } |
791 |
|
} |
792 |
|
|
793 |
< |
class ShortRunnable extends CheckedRunnable { |
794 |
< |
void realRun() throws Throwable { |
793 |
> |
public class ShortRunnable extends CheckedRunnable { |
794 |
> |
protected void realRun() throws Throwable { |
795 |
|
Thread.sleep(SHORT_DELAY_MS); |
796 |
|
} |
797 |
|
} |
798 |
|
|
799 |
< |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
800 |
< |
void realRun() throws InterruptedException { |
799 |
> |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
800 |
> |
protected void realRun() throws InterruptedException { |
801 |
|
Thread.sleep(SHORT_DELAY_MS); |
802 |
|
} |
803 |
|
} |
804 |
|
|
805 |
< |
class SmallRunnable extends CheckedRunnable { |
806 |
< |
void realRun() throws Throwable { |
805 |
> |
public class SmallRunnable extends CheckedRunnable { |
806 |
> |
protected void realRun() throws Throwable { |
807 |
|
Thread.sleep(SMALL_DELAY_MS); |
808 |
|
} |
809 |
|
} |
810 |
|
|
811 |
< |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
812 |
< |
void realRun() { |
811 |
> |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
812 |
> |
protected void realRun() { |
813 |
|
try { |
814 |
|
Thread.sleep(SMALL_DELAY_MS); |
815 |
< |
} |
485 |
< |
catch (InterruptedException ok) { |
486 |
< |
} |
815 |
> |
} catch (InterruptedException ok) {} |
816 |
|
} |
817 |
|
} |
818 |
|
|
819 |
< |
class SmallCallable extends CheckedCallable { |
820 |
< |
Object realCall() throws Throwable { |
819 |
> |
public class SmallCallable extends CheckedCallable { |
820 |
> |
protected Object realCall() throws InterruptedException { |
821 |
|
Thread.sleep(SMALL_DELAY_MS); |
822 |
|
return Boolean.TRUE; |
823 |
|
} |
824 |
|
} |
825 |
|
|
826 |
< |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
827 |
< |
void realRun() throws InterruptedException { |
828 |
< |
Thread.sleep(SMALL_DELAY_MS); |
826 |
> |
public class MediumRunnable extends CheckedRunnable { |
827 |
> |
protected void realRun() throws Throwable { |
828 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
829 |
|
} |
830 |
|
} |
831 |
|
|
832 |
< |
class MediumRunnable extends CheckedRunnable { |
833 |
< |
void realRun() throws Throwable { |
832 |
> |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
833 |
> |
protected void realRun() throws InterruptedException { |
834 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
835 |
|
} |
836 |
|
} |
837 |
|
|
838 |
< |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
839 |
< |
void realRun() throws InterruptedException { |
840 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
841 |
< |
} |
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 |
< |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
848 |
< |
void realRun() { |
847 |
> |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
848 |
> |
protected void realRun() { |
849 |
|
try { |
850 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
851 |
< |
} |
520 |
< |
catch (InterruptedException ok) { |
521 |
< |
} |
851 |
> |
} catch (InterruptedException ok) {} |
852 |
|
} |
853 |
|
} |
854 |
|
|
855 |
< |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
856 |
< |
void realRun() { |
855 |
> |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
856 |
> |
protected void realRun() { |
857 |
|
try { |
858 |
|
Thread.sleep(LONG_DELAY_MS); |
859 |
< |
} |
530 |
< |
catch (InterruptedException ok) { |
531 |
< |
} |
859 |
> |
} catch (InterruptedException ok) {} |
860 |
|
} |
861 |
|
} |
862 |
|
|
863 |
|
/** |
864 |
|
* For use as ThreadFactory in constructors |
865 |
|
*/ |
866 |
< |
static class SimpleThreadFactory implements ThreadFactory { |
866 |
> |
public static class SimpleThreadFactory implements ThreadFactory { |
867 |
|
public Thread newThread(Runnable r) { |
868 |
|
return new Thread(r); |
869 |
|
} |
870 |
|
} |
871 |
|
|
872 |
< |
static class TrackedShortRunnable implements Runnable { |
873 |
< |
volatile boolean done = false; |
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 (Exception e) { |
551 |
< |
} |
905 |
> |
} catch (InterruptedException ok) {} |
906 |
|
} |
907 |
|
} |
908 |
|
|
909 |
< |
static class TrackedMediumRunnable implements Runnable { |
910 |
< |
volatile boolean done = false; |
909 |
> |
public static class TrackedMediumRunnable implements Runnable { |
910 |
> |
public volatile boolean done = false; |
911 |
|
public void run() { |
912 |
|
try { |
913 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
914 |
|
done = true; |
915 |
< |
} catch (Exception e) { |
562 |
< |
} |
915 |
> |
} catch (InterruptedException ok) {} |
916 |
|
} |
917 |
|
} |
918 |
|
|
919 |
< |
static class TrackedLongRunnable implements Runnable { |
920 |
< |
volatile boolean done = false; |
919 |
> |
public static class TrackedLongRunnable implements Runnable { |
920 |
> |
public volatile boolean done = false; |
921 |
|
public void run() { |
922 |
|
try { |
923 |
|
Thread.sleep(LONG_DELAY_MS); |
924 |
|
done = true; |
925 |
< |
} catch (Exception e) { |
573 |
< |
} |
925 |
> |
} catch (InterruptedException ok) {} |
926 |
|
} |
927 |
|
} |
928 |
|
|
929 |
< |
static class TrackedNoOpRunnable implements Runnable { |
930 |
< |
volatile boolean done = false; |
929 |
> |
public static class TrackedNoOpRunnable implements Runnable { |
930 |
> |
public volatile boolean done = false; |
931 |
|
public void run() { |
932 |
|
done = true; |
933 |
|
} |
934 |
|
} |
935 |
|
|
936 |
< |
static class TrackedCallable implements Callable { |
937 |
< |
volatile boolean done = false; |
936 |
> |
public static class TrackedCallable implements Callable { |
937 |
> |
public volatile boolean done = false; |
938 |
|
public Object call() { |
939 |
|
try { |
940 |
|
Thread.sleep(SMALL_DELAY_MS); |
941 |
|
done = true; |
942 |
< |
} catch (Exception e) { |
591 |
< |
} |
942 |
> |
} catch (InterruptedException ok) {} |
943 |
|
return Boolean.TRUE; |
944 |
|
} |
945 |
|
} |
946 |
|
|
947 |
+ |
/** |
948 |
+ |
* Analog of CheckedRunnable for RecursiveAction |
949 |
+ |
*/ |
950 |
+ |
public abstract class CheckedRecursiveAction extends RecursiveAction { |
951 |
+ |
protected abstract void realCompute() throws Throwable; |
952 |
+ |
|
953 |
+ |
public final void compute() { |
954 |
+ |
try { |
955 |
+ |
realCompute(); |
956 |
+ |
} catch (Throwable t) { |
957 |
+ |
threadUnexpectedException(t); |
958 |
+ |
} |
959 |
+ |
} |
960 |
+ |
} |
961 |
+ |
|
962 |
+ |
/** |
963 |
+ |
* Analog of CheckedCallable for RecursiveTask |
964 |
+ |
*/ |
965 |
+ |
public abstract class CheckedRecursiveTask<T> extends RecursiveTask<T> { |
966 |
+ |
protected abstract T realCompute() throws Throwable; |
967 |
+ |
|
968 |
+ |
public final T compute() { |
969 |
+ |
try { |
970 |
+ |
return realCompute(); |
971 |
+ |
} catch (Throwable t) { |
972 |
+ |
threadUnexpectedException(t); |
973 |
+ |
return null; |
974 |
+ |
} |
975 |
+ |
} |
976 |
+ |
} |
977 |
|
|
978 |
|
/** |
979 |
|
* For use as RejectedExecutionHandler in constructors |
980 |
|
*/ |
981 |
< |
static class NoOpREHandler implements RejectedExecutionHandler { |
981 |
> |
public static class NoOpREHandler implements RejectedExecutionHandler { |
982 |
|
public void rejectedExecution(Runnable r, |
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 |
|
} |