1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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 |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
|
import java.io.*; |
14 |
|
import java.security.*; |
15 |
|
|
18 |
|
* utility methods and classes, as well as a simple framework for |
19 |
|
* helping to make sure that assertions failing in generated threads |
20 |
|
* cause the associated test that generated them to itself fail (which |
21 |
< |
* JUnit doe not otherwise arrange). The rules for creating such |
21 |
> |
* JUnit does not otherwise arrange). The rules for creating such |
22 |
|
* tests are: |
23 |
|
* |
24 |
|
* <ol> |
25 |
|
* |
26 |
|
* <li> All assertions in code running in generated threads must use |
27 |
< |
* the forms {@link threadFail} , {@link threadAssertTrue} {@link |
28 |
< |
* threadAssertEquals}, or {@link threadAssertNull}, (not |
27 |
> |
* the forms {@link #threadFail}, {@link #threadAssertTrue}, {@link |
28 |
> |
* #threadAssertEquals}, or {@link #threadAssertNull}, (not |
29 |
|
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
30 |
|
* particularly recommended) for other code to use these forms too. |
31 |
|
* Only the most typically used JUnit assertion methods are defined |
32 |
|
* this way, but enough to live with.</li> |
33 |
|
* |
34 |
< |
* <li> If you override {@link setUp} or {@link tearDown}, make sure |
34 |
> |
* <li> If you override {@link #setUp} or {@link #tearDown}, make sure |
35 |
|
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
36 |
|
* them. These methods are used to clear and check for thread |
37 |
|
* assertion failures.</li> |
45 |
|
* is always discriminable as larger than SHORT and smaller than |
46 |
|
* MEDIUM. And so on. These constants are set to conservative values, |
47 |
|
* but even so, if there is ever any doubt, they can all be increased |
48 |
< |
* in one spot to rerun tests on slower platforms</li> |
48 |
> |
* in one spot to rerun tests on slower platforms.</li> |
49 |
|
* |
50 |
|
* <li> All threads generated must be joined inside each test case |
51 |
|
* method (or <tt>fail</tt> to do so) before returning from the |
64 |
|
* "normal" behaviors differ significantly. And sometimes testcases |
65 |
|
* cover multiple methods when they cannot be tested in |
66 |
|
* isolation.</li> |
67 |
< |
* |
67 |
> |
* |
68 |
|
* <li> The documentation style for testcases is to provide as javadoc |
69 |
|
* a simple sentence or two describing the property that the testcase |
70 |
|
* method purports to test. The javadocs do not say anything about how |
89 |
|
public class JSR166TestCase extends TestCase { |
90 |
|
/** |
91 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
92 |
< |
*/ |
93 |
< |
public static void main (String[] args) { |
94 |
< |
junit.textui.TestRunner.run (suite()); |
92 |
> |
*/ |
93 |
> |
public static void main(String[] args) { |
94 |
> |
int iters = 1; |
95 |
> |
if (args.length > 0) |
96 |
> |
iters = Integer.parseInt(args[0]); |
97 |
> |
Test s = suite(); |
98 |
> |
for (int i = 0; i < iters; ++i) { |
99 |
> |
junit.textui.TestRunner.run(s); |
100 |
> |
System.gc(); |
101 |
> |
System.runFinalization(); |
102 |
> |
} |
103 |
> |
System.exit(0); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
|
* Collects all JSR166 unit tests as one suite |
108 |
< |
*/ |
109 |
< |
public static Test suite ( ) { |
108 |
> |
*/ |
109 |
> |
public static Test suite() { |
110 |
|
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
111 |
< |
|
111 |
> |
|
112 |
> |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
113 |
> |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
114 |
> |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
115 |
> |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
116 |
> |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
117 |
> |
suite.addTest(new TestSuite(PhaserTest.class)); |
118 |
> |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
119 |
|
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
120 |
+ |
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
121 |
+ |
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
122 |
+ |
suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class)); |
123 |
|
suite.addTest(new TestSuite(ArrayBlockingQueueTest.class)); |
124 |
< |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
125 |
< |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
126 |
< |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
127 |
< |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
128 |
< |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
129 |
< |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
130 |
< |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
131 |
< |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
132 |
< |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
133 |
< |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
134 |
< |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
135 |
< |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
124 |
> |
suite.addTest(new TestSuite(ArrayDequeTest.class)); |
125 |
> |
suite.addTest(new TestSuite(AtomicBooleanTest.class)); |
126 |
> |
suite.addTest(new TestSuite(AtomicIntegerArrayTest.class)); |
127 |
> |
suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class)); |
128 |
> |
suite.addTest(new TestSuite(AtomicIntegerTest.class)); |
129 |
> |
suite.addTest(new TestSuite(AtomicLongArrayTest.class)); |
130 |
> |
suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class)); |
131 |
> |
suite.addTest(new TestSuite(AtomicLongTest.class)); |
132 |
> |
suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class)); |
133 |
> |
suite.addTest(new TestSuite(AtomicReferenceArrayTest.class)); |
134 |
> |
suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class)); |
135 |
> |
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
136 |
> |
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
137 |
|
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
138 |
|
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
139 |
+ |
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
140 |
+ |
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
141 |
+ |
suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class)); |
142 |
+ |
suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class)); |
143 |
|
suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class)); |
144 |
|
suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class)); |
145 |
|
suite.addTest(new TestSuite(CountDownLatchTest.class)); |
146 |
|
suite.addTest(new TestSuite(CyclicBarrierTest.class)); |
147 |
|
suite.addTest(new TestSuite(DelayQueueTest.class)); |
148 |
+ |
suite.addTest(new TestSuite(EntryTest.class)); |
149 |
|
suite.addTest(new TestSuite(ExchangerTest.class)); |
150 |
|
suite.addTest(new TestSuite(ExecutorsTest.class)); |
151 |
+ |
suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class)); |
152 |
|
suite.addTest(new TestSuite(FutureTaskTest.class)); |
153 |
+ |
suite.addTest(new TestSuite(LinkedBlockingDequeTest.class)); |
154 |
|
suite.addTest(new TestSuite(LinkedBlockingQueueTest.class)); |
155 |
|
suite.addTest(new TestSuite(LinkedListTest.class)); |
156 |
|
suite.addTest(new TestSuite(LockSupportTest.class)); |
159 |
|
suite.addTest(new TestSuite(ReentrantLockTest.class)); |
160 |
|
suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class)); |
161 |
|
suite.addTest(new TestSuite(ScheduledExecutorTest.class)); |
162 |
+ |
suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class)); |
163 |
|
suite.addTest(new TestSuite(SemaphoreTest.class)); |
164 |
|
suite.addTest(new TestSuite(SynchronousQueueTest.class)); |
165 |
|
suite.addTest(new TestSuite(SystemTest.class)); |
166 |
|
suite.addTest(new TestSuite(ThreadLocalTest.class)); |
167 |
|
suite.addTest(new TestSuite(ThreadPoolExecutorTest.class)); |
168 |
+ |
suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class)); |
169 |
|
suite.addTest(new TestSuite(ThreadTest.class)); |
170 |
|
suite.addTest(new TestSuite(TimeUnitTest.class)); |
171 |
< |
|
171 |
> |
suite.addTest(new TestSuite(TreeMapTest.class)); |
172 |
> |
suite.addTest(new TestSuite(TreeSetTest.class)); |
173 |
> |
suite.addTest(new TestSuite(TreeSubMapTest.class)); |
174 |
> |
suite.addTest(new TestSuite(TreeSubSetTest.class)); |
175 |
> |
|
176 |
|
return suite; |
177 |
|
} |
178 |
|
|
184 |
|
|
185 |
|
|
186 |
|
/** |
187 |
< |
* Return the shortest timed delay. This could |
188 |
< |
* be reimplmented to use for example a Property. |
189 |
< |
*/ |
187 |
> |
* Returns the shortest timed delay. This could |
188 |
> |
* be reimplemented to use for example a Property. |
189 |
> |
*/ |
190 |
|
protected long getShortDelay() { |
191 |
< |
return 100; |
191 |
> |
return 50; |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
/** |
196 |
< |
* Set delays as multiples of SHORT_DELAY. |
196 |
> |
* Sets delays as multiples of SHORT_DELAY. |
197 |
|
*/ |
198 |
< |
protected void setDelays() { |
198 |
> |
protected void setDelays() { |
199 |
|
SHORT_DELAY_MS = getShortDelay(); |
200 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
201 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
208 |
|
volatile boolean threadFailed; |
209 |
|
|
210 |
|
/** |
211 |
< |
* Initialize test to indicate that no thread assertions have failed |
211 |
> |
* Initializes test to indicate that no thread assertions have failed |
212 |
|
*/ |
213 |
< |
public void setUp() { |
213 |
> |
public void setUp() { |
214 |
|
setDelays(); |
215 |
< |
threadFailed = false; |
215 |
> |
threadFailed = false; |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* Trigger test case failure if any thread assertions have failed |
219 |
> |
* Triggers test case failure if any thread assertions have failed |
220 |
|
*/ |
221 |
< |
public void tearDown() { |
222 |
< |
assertFalse(threadFailed); |
221 |
> |
public void tearDown() { |
222 |
> |
assertFalse(threadFailed); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
|
* Fail, also setting status to indicate current testcase should fail |
227 |
< |
*/ |
227 |
> |
*/ |
228 |
|
public void threadFail(String reason) { |
229 |
|
threadFailed = true; |
230 |
|
fail(reason); |
233 |
|
/** |
234 |
|
* If expression not true, set status to indicate current testcase |
235 |
|
* should fail |
236 |
< |
*/ |
236 |
> |
*/ |
237 |
|
public void threadAssertTrue(boolean b) { |
238 |
|
if (!b) { |
239 |
|
threadFailed = true; |
244 |
|
/** |
245 |
|
* If expression not false, set status to indicate current testcase |
246 |
|
* should fail |
247 |
< |
*/ |
247 |
> |
*/ |
248 |
|
public void threadAssertFalse(boolean b) { |
249 |
|
if (b) { |
250 |
|
threadFailed = true; |
255 |
|
/** |
256 |
|
* If argument not null, set status to indicate current testcase |
257 |
|
* should fail |
258 |
< |
*/ |
258 |
> |
*/ |
259 |
|
public void threadAssertNull(Object x) { |
260 |
|
if (x != null) { |
261 |
|
threadFailed = true; |
266 |
|
/** |
267 |
|
* If arguments not equal, set status to indicate current testcase |
268 |
|
* should fail |
269 |
< |
*/ |
269 |
> |
*/ |
270 |
|
public void threadAssertEquals(long x, long y) { |
271 |
|
if (x != y) { |
272 |
|
threadFailed = true; |
277 |
|
/** |
278 |
|
* If arguments not equal, set status to indicate current testcase |
279 |
|
* should fail |
280 |
< |
*/ |
280 |
> |
*/ |
281 |
|
public void threadAssertEquals(Object x, Object y) { |
282 |
|
if (x != y && (x == null || !x.equals(y))) { |
283 |
|
threadFailed = true; |
287 |
|
|
288 |
|
/** |
289 |
|
* threadFail with message "should throw exception" |
290 |
< |
*/ |
290 |
> |
*/ |
291 |
|
public void threadShouldThrow() { |
292 |
|
threadFailed = true; |
293 |
|
fail("should throw exception"); |
294 |
|
} |
295 |
|
|
296 |
|
/** |
297 |
+ |
* threadFail with message "should throw" + exceptionName |
298 |
+ |
*/ |
299 |
+ |
public void threadShouldThrow(String exceptionName) { |
300 |
+ |
threadFailed = true; |
301 |
+ |
fail("should throw " + exceptionName); |
302 |
+ |
} |
303 |
+ |
|
304 |
+ |
/** |
305 |
|
* threadFail with message "Unexpected exception" |
306 |
|
*/ |
307 |
|
public void threadUnexpectedException() { |
309 |
|
fail("Unexpected exception"); |
310 |
|
} |
311 |
|
|
312 |
+ |
/** |
313 |
+ |
* threadFail with message "Unexpected exception", with argument |
314 |
+ |
*/ |
315 |
+ |
public void threadUnexpectedException(Throwable ex) { |
316 |
+ |
threadFailed = true; |
317 |
+ |
ex.printStackTrace(); |
318 |
+ |
fail("Unexpected exception: " + ex); |
319 |
+ |
} |
320 |
|
|
321 |
|
/** |
322 |
|
* Wait out termination of a thread pool or fail doing so |
324 |
|
public void joinPool(ExecutorService exec) { |
325 |
|
try { |
326 |
|
exec.shutdown(); |
327 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
328 |
< |
} catch(InterruptedException ie) { |
329 |
< |
fail("Unexpected exception"); |
327 |
> |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
328 |
> |
} catch (SecurityException ok) { |
329 |
> |
// Allowed in case test doesn't have privs |
330 |
> |
} catch (InterruptedException ie) { |
331 |
> |
fail("Unexpected InterruptedException"); |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
/** |
337 |
|
* fail with message "should throw exception" |
338 |
< |
*/ |
338 |
> |
*/ |
339 |
|
public void shouldThrow() { |
340 |
|
fail("Should throw exception"); |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
+ |
* fail with message "should throw " + exceptionName |
345 |
+ |
*/ |
346 |
+ |
public void shouldThrow(String exceptionName) { |
347 |
+ |
fail("Should throw " + exceptionName); |
348 |
+ |
} |
349 |
+ |
|
350 |
+ |
/** |
351 |
|
* fail with message "Unexpected exception" |
352 |
|
*/ |
353 |
|
public void unexpectedException() { |
354 |
|
fail("Unexpected exception"); |
355 |
|
} |
356 |
|
|
357 |
+ |
/** |
358 |
+ |
* fail with message "Unexpected exception", with argument |
359 |
+ |
*/ |
360 |
+ |
public void unexpectedException(Throwable ex) { |
361 |
+ |
ex.printStackTrace(); |
362 |
+ |
fail("Unexpected exception: " + ex); |
363 |
+ |
} |
364 |
+ |
|
365 |
|
|
366 |
|
/** |
367 |
|
* The number of elements to place in collections, arrays, etc. |
368 |
|
*/ |
369 |
< |
static final int SIZE = 20; |
369 |
> |
public static final int SIZE = 20; |
370 |
|
|
371 |
|
// Some convenient Integer constants |
372 |
|
|
373 |
< |
static final Integer zero = new Integer(0); |
374 |
< |
static final Integer one = new Integer(1); |
375 |
< |
static final Integer two = new Integer(2); |
376 |
< |
static final Integer three = new Integer(3); |
377 |
< |
static final Integer four = new Integer(4); |
378 |
< |
static final Integer five = new Integer(5); |
379 |
< |
static final Integer six = new Integer(6); |
380 |
< |
static final Integer seven = new Integer(7); |
381 |
< |
static final Integer eight = new Integer(8); |
382 |
< |
static final Integer nine = new Integer(9); |
383 |
< |
static final Integer m1 = new Integer(-1); |
384 |
< |
static final Integer m2 = new Integer(-2); |
385 |
< |
static final Integer m3 = new Integer(-3); |
386 |
< |
static final Integer m4 = new Integer(-4); |
387 |
< |
static final Integer m5 = new Integer(-5); |
388 |
< |
static final Integer m10 = new Integer(-10); |
373 |
> |
public static final Integer zero = new Integer(0); |
374 |
> |
public static final Integer one = new Integer(1); |
375 |
> |
public static final Integer two = new Integer(2); |
376 |
> |
public static final Integer three = new Integer(3); |
377 |
> |
public static final Integer four = new Integer(4); |
378 |
> |
public static final Integer five = new Integer(5); |
379 |
> |
public static final Integer six = new Integer(6); |
380 |
> |
public static final Integer seven = new Integer(7); |
381 |
> |
public static final Integer eight = new Integer(8); |
382 |
> |
public static final Integer nine = new Integer(9); |
383 |
> |
public static final Integer m1 = new Integer(-1); |
384 |
> |
public static final Integer m2 = new Integer(-2); |
385 |
> |
public static final Integer m3 = new Integer(-3); |
386 |
> |
public static final Integer m4 = new Integer(-4); |
387 |
> |
public static final Integer m5 = new Integer(-5); |
388 |
> |
public static final Integer m6 = new Integer(-6); |
389 |
> |
public static final Integer m10 = new Integer(-10); |
390 |
|
|
391 |
|
|
392 |
|
/** |
393 |
|
* A security policy where new permissions can be dynamically added |
394 |
|
* or all cleared. |
395 |
|
*/ |
396 |
< |
static class AdjustablePolicy extends java.security.Policy { |
396 |
> |
public static class AdjustablePolicy extends java.security.Policy { |
397 |
|
Permissions perms = new Permissions(); |
398 |
|
AdjustablePolicy() { } |
399 |
|
void addPermission(Permission perm) { perms.add(perm); } |
400 |
|
void clearPermissions() { perms = new Permissions(); } |
401 |
< |
public PermissionCollection getPermissions(CodeSource cs) { |
402 |
< |
return perms; |
403 |
< |
} |
404 |
< |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
405 |
< |
return perms; |
406 |
< |
} |
407 |
< |
public boolean implies(ProtectionDomain pd, Permission p) { |
408 |
< |
return perms.implies(p); |
409 |
< |
} |
410 |
< |
public void refresh() {} |
401 |
> |
public PermissionCollection getPermissions(CodeSource cs) { |
402 |
> |
return perms; |
403 |
> |
} |
404 |
> |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
405 |
> |
return perms; |
406 |
> |
} |
407 |
> |
public boolean implies(ProtectionDomain pd, Permission p) { |
408 |
> |
return perms.implies(p); |
409 |
> |
} |
410 |
> |
public void refresh() {} |
411 |
|
} |
412 |
|
|
413 |
+ |
/** |
414 |
+ |
* Sleep until the timeout has elapsed, or interrupted. |
415 |
+ |
* Does <em>NOT</em> throw InterruptedException. |
416 |
+ |
*/ |
417 |
+ |
void sleepTillInterrupted(long timeoutMillis) { |
418 |
+ |
try { |
419 |
+ |
Thread.sleep(timeoutMillis); |
420 |
+ |
} catch (InterruptedException wakeup) {} |
421 |
+ |
} |
422 |
+ |
|
423 |
+ |
/** |
424 |
+ |
* Returns a new started Thread running the given runnable. |
425 |
+ |
*/ |
426 |
+ |
Thread newStartedThread(Runnable runnable) { |
427 |
+ |
Thread t = new Thread(runnable); |
428 |
+ |
t.start(); |
429 |
+ |
return t; |
430 |
+ |
} |
431 |
|
|
432 |
|
// Some convenient Runnable classes |
433 |
|
|
434 |
< |
static class NoOpRunnable implements Runnable { |
435 |
< |
public void run() {} |
349 |
< |
} |
434 |
> |
public abstract class CheckedRunnable implements Runnable { |
435 |
> |
protected abstract void realRun() throws Throwable; |
436 |
|
|
437 |
< |
static class NoOpCallable implements Callable { |
438 |
< |
public Object call() { return Boolean.TRUE; } |
437 |
> |
public final void run() { |
438 |
> |
try { |
439 |
> |
realRun(); |
440 |
> |
} catch (Throwable t) { |
441 |
> |
threadUnexpectedException(t); |
442 |
> |
} |
443 |
> |
} |
444 |
|
} |
445 |
|
|
446 |
< |
static final String TEST_STRING = "a test string"; |
446 |
> |
public abstract class RunnableShouldThrow implements Runnable { |
447 |
> |
protected abstract void realRun() throws Throwable; |
448 |
|
|
449 |
< |
static class StringTask implements Callable<String> { |
358 |
< |
public String call() { return TEST_STRING; } |
359 |
< |
} |
449 |
> |
final Class<?> exceptionClass; |
450 |
|
|
451 |
< |
static class NPETask implements Callable<String> { |
452 |
< |
public String call() { throw new NullPointerException(); } |
453 |
< |
} |
451 |
> |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
452 |
> |
this.exceptionClass = exceptionClass; |
453 |
> |
} |
454 |
|
|
455 |
< |
static class CallableOne implements Callable<Integer> { |
456 |
< |
public Integer call() { return one; } |
455 |
> |
public final void run() { |
456 |
> |
try { |
457 |
> |
realRun(); |
458 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
459 |
> |
} catch (Throwable t) { |
460 |
> |
if (! exceptionClass.isInstance(t)) |
461 |
> |
threadUnexpectedException(t); |
462 |
> |
} |
463 |
> |
} |
464 |
|
} |
465 |
|
|
466 |
< |
class ShortRunnable implements Runnable { |
467 |
< |
public void run() { |
466 |
> |
public abstract class ThreadShouldThrow extends Thread { |
467 |
> |
protected abstract void realRun() throws Throwable; |
468 |
> |
|
469 |
> |
final Class<?> exceptionClass; |
470 |
> |
|
471 |
> |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
472 |
> |
this.exceptionClass = exceptionClass; |
473 |
> |
} |
474 |
> |
|
475 |
> |
public final void run() { |
476 |
|
try { |
477 |
< |
Thread.sleep(SHORT_DELAY_MS); |
478 |
< |
} |
479 |
< |
catch(Exception e) { |
480 |
< |
threadUnexpectedException(); |
477 |
> |
realRun(); |
478 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
479 |
> |
} catch (Throwable t) { |
480 |
> |
if (! exceptionClass.isInstance(t)) |
481 |
> |
threadUnexpectedException(t); |
482 |
|
} |
483 |
|
} |
484 |
|
} |
485 |
|
|
486 |
< |
class ShortInterruptedRunnable implements Runnable { |
487 |
< |
public void run() { |
486 |
> |
public abstract class CheckedInterruptedRunnable implements Runnable { |
487 |
> |
protected abstract void realRun() throws Throwable; |
488 |
> |
|
489 |
> |
public final void run() { |
490 |
|
try { |
491 |
< |
Thread.sleep(SHORT_DELAY_MS); |
492 |
< |
threadShouldThrow(); |
493 |
< |
} |
494 |
< |
catch(InterruptedException success) { |
491 |
> |
realRun(); |
492 |
> |
threadShouldThrow("InterruptedException"); |
493 |
> |
} catch (InterruptedException success) { |
494 |
> |
} catch (Throwable t) { |
495 |
> |
threadUnexpectedException(t); |
496 |
|
} |
497 |
|
} |
498 |
|
} |
499 |
|
|
500 |
< |
class SmallRunnable implements Runnable { |
501 |
< |
public void run() { |
500 |
> |
public abstract class CheckedCallable<T> implements Callable<T> { |
501 |
> |
protected abstract T realCall() throws Throwable; |
502 |
> |
|
503 |
> |
public final T call() { |
504 |
|
try { |
505 |
< |
Thread.sleep(SMALL_DELAY_MS); |
506 |
< |
} |
507 |
< |
catch(Exception e) { |
397 |
< |
threadUnexpectedException(); |
505 |
> |
return realCall(); |
506 |
> |
} catch (Throwable t) { |
507 |
> |
threadUnexpectedException(t); |
508 |
|
} |
509 |
+ |
return null; |
510 |
|
} |
511 |
|
} |
512 |
|
|
513 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
514 |
< |
public void run() { |
513 |
> |
public abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
514 |
> |
protected abstract T realCall() throws Throwable; |
515 |
> |
|
516 |
> |
public final T call() { |
517 |
|
try { |
518 |
< |
Thread.sleep(SMALL_DELAY_MS); |
519 |
< |
} |
520 |
< |
catch(Exception e) { |
518 |
> |
T result = realCall(); |
519 |
> |
threadShouldThrow("InterruptedException"); |
520 |
> |
return result; |
521 |
> |
} catch (InterruptedException success) { |
522 |
> |
} catch (Throwable t) { |
523 |
> |
threadUnexpectedException(t); |
524 |
|
} |
525 |
+ |
return null; |
526 |
|
} |
527 |
|
} |
528 |
|
|
529 |
< |
class SmallCallable implements Callable { |
530 |
< |
public Object call() { |
529 |
> |
public static class NoOpRunnable implements Runnable { |
530 |
> |
public void run() {} |
531 |
> |
} |
532 |
> |
|
533 |
> |
public static class NoOpCallable implements Callable { |
534 |
> |
public Object call() { return Boolean.TRUE; } |
535 |
> |
} |
536 |
> |
|
537 |
> |
public static final String TEST_STRING = "a test string"; |
538 |
> |
|
539 |
> |
public static class StringTask implements Callable<String> { |
540 |
> |
public String call() { return TEST_STRING; } |
541 |
> |
} |
542 |
> |
|
543 |
> |
public static class NPETask implements Callable<String> { |
544 |
> |
public String call() { throw new NullPointerException(); } |
545 |
> |
} |
546 |
> |
|
547 |
> |
public static class CallableOne implements Callable<Integer> { |
548 |
> |
public Integer call() { return one; } |
549 |
> |
} |
550 |
> |
|
551 |
> |
public class ShortRunnable extends CheckedRunnable { |
552 |
> |
protected void realRun() throws Throwable { |
553 |
> |
Thread.sleep(SHORT_DELAY_MS); |
554 |
> |
} |
555 |
> |
} |
556 |
> |
|
557 |
> |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
558 |
> |
protected void realRun() throws InterruptedException { |
559 |
> |
Thread.sleep(SHORT_DELAY_MS); |
560 |
> |
} |
561 |
> |
} |
562 |
> |
|
563 |
> |
public class SmallRunnable extends CheckedRunnable { |
564 |
> |
protected void realRun() throws Throwable { |
565 |
> |
Thread.sleep(SMALL_DELAY_MS); |
566 |
> |
} |
567 |
> |
} |
568 |
> |
|
569 |
> |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
570 |
> |
protected void realRun() { |
571 |
|
try { |
572 |
|
Thread.sleep(SMALL_DELAY_MS); |
573 |
< |
} |
574 |
< |
catch(Exception e) { |
575 |
< |
threadUnexpectedException(); |
576 |
< |
} |
573 |
> |
} catch (InterruptedException ok) {} |
574 |
> |
} |
575 |
> |
} |
576 |
> |
|
577 |
> |
public class SmallCallable extends CheckedCallable { |
578 |
> |
protected Object realCall() throws InterruptedException { |
579 |
> |
Thread.sleep(SMALL_DELAY_MS); |
580 |
|
return Boolean.TRUE; |
581 |
|
} |
582 |
|
} |
583 |
|
|
584 |
< |
class SmallInterruptedRunnable implements Runnable { |
585 |
< |
public void run() { |
586 |
< |
try { |
427 |
< |
Thread.sleep(SMALL_DELAY_MS); |
428 |
< |
threadShouldThrow(); |
429 |
< |
} |
430 |
< |
catch(InterruptedException success) { |
431 |
< |
} |
584 |
> |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
585 |
> |
protected void realRun() throws InterruptedException { |
586 |
> |
Thread.sleep(SMALL_DELAY_MS); |
587 |
|
} |
588 |
|
} |
589 |
|
|
590 |
+ |
public class MediumRunnable extends CheckedRunnable { |
591 |
+ |
protected void realRun() throws Throwable { |
592 |
+ |
Thread.sleep(MEDIUM_DELAY_MS); |
593 |
+ |
} |
594 |
+ |
} |
595 |
|
|
596 |
< |
class MediumRunnable implements Runnable { |
597 |
< |
public void run() { |
598 |
< |
try { |
439 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
440 |
< |
} |
441 |
< |
catch(Exception e) { |
442 |
< |
threadUnexpectedException(); |
443 |
< |
} |
596 |
> |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
597 |
> |
protected void realRun() throws InterruptedException { |
598 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
599 |
|
} |
600 |
|
} |
601 |
|
|
602 |
< |
class MediumInterruptedRunnable implements Runnable { |
603 |
< |
public void run() { |
602 |
> |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
603 |
> |
protected void realRun() { |
604 |
|
try { |
605 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
606 |
< |
threadShouldThrow(); |
452 |
< |
} |
453 |
< |
catch(InterruptedException success) { |
454 |
< |
} |
606 |
> |
} catch (InterruptedException ok) {} |
607 |
|
} |
608 |
|
} |
609 |
|
|
610 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
611 |
< |
public void run() { |
610 |
> |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
611 |
> |
protected void realRun() { |
612 |
|
try { |
613 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
614 |
< |
} |
463 |
< |
catch(InterruptedException success) { |
464 |
< |
} |
613 |
> |
Thread.sleep(LONG_DELAY_MS); |
614 |
> |
} catch (InterruptedException ok) {} |
615 |
|
} |
616 |
|
} |
617 |
|
|
618 |
|
/** |
619 |
|
* For use as ThreadFactory in constructors |
620 |
|
*/ |
621 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
622 |
< |
public Thread newThread(Runnable r){ |
621 |
> |
public static class SimpleThreadFactory implements ThreadFactory { |
622 |
> |
public Thread newThread(Runnable r) { |
623 |
|
return new Thread(r); |
624 |
< |
} |
624 |
> |
} |
625 |
|
} |
626 |
|
|
627 |
< |
static class TrackedShortRunnable implements Runnable { |
628 |
< |
volatile boolean done = false; |
627 |
> |
public static class TrackedShortRunnable implements Runnable { |
628 |
> |
public volatile boolean done = false; |
629 |
|
public void run() { |
630 |
|
try { |
631 |
|
Thread.sleep(SMALL_DELAY_MS); |
632 |
|
done = true; |
633 |
< |
} catch(Exception e){ |
484 |
< |
} |
633 |
> |
} catch (InterruptedException ok) {} |
634 |
|
} |
635 |
|
} |
636 |
|
|
637 |
< |
static class TrackedMediumRunnable implements Runnable { |
638 |
< |
volatile boolean done = false; |
637 |
> |
public static class TrackedMediumRunnable implements Runnable { |
638 |
> |
public volatile boolean done = false; |
639 |
|
public void run() { |
640 |
|
try { |
641 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
642 |
|
done = true; |
643 |
< |
} catch(Exception e){ |
495 |
< |
} |
643 |
> |
} catch (InterruptedException ok) {} |
644 |
|
} |
645 |
|
} |
646 |
|
|
647 |
< |
static class TrackedLongRunnable implements Runnable { |
648 |
< |
volatile boolean done = false; |
647 |
> |
public static class TrackedLongRunnable implements Runnable { |
648 |
> |
public volatile boolean done = false; |
649 |
|
public void run() { |
650 |
|
try { |
651 |
|
Thread.sleep(LONG_DELAY_MS); |
652 |
|
done = true; |
653 |
< |
} catch(Exception e){ |
506 |
< |
} |
653 |
> |
} catch (InterruptedException ok) {} |
654 |
|
} |
655 |
|
} |
656 |
|
|
657 |
< |
static class TrackedNoOpRunnable implements Runnable { |
658 |
< |
volatile boolean done = false; |
657 |
> |
public static class TrackedNoOpRunnable implements Runnable { |
658 |
> |
public volatile boolean done = false; |
659 |
|
public void run() { |
660 |
|
done = true; |
661 |
|
} |
662 |
|
} |
663 |
|
|
664 |
< |
static class TrackedCallable implements Callable { |
665 |
< |
volatile boolean done = false; |
664 |
> |
public static class TrackedCallable implements Callable { |
665 |
> |
public volatile boolean done = false; |
666 |
|
public Object call() { |
667 |
|
try { |
668 |
|
Thread.sleep(SMALL_DELAY_MS); |
669 |
|
done = true; |
670 |
< |
} catch(Exception e){ |
524 |
< |
} |
670 |
> |
} catch (InterruptedException ok) {} |
671 |
|
return Boolean.TRUE; |
672 |
|
} |
673 |
|
} |
674 |
|
|
675 |
+ |
|
676 |
|
/** |
677 |
|
* For use as RejectedExecutionHandler in constructors |
678 |
|
*/ |
679 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
680 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
679 |
> |
public static class NoOpREHandler implements RejectedExecutionHandler { |
680 |
> |
public void rejectedExecution(Runnable r, |
681 |
> |
ThreadPoolExecutor executor) {} |
682 |
|
} |
683 |
< |
|
536 |
< |
|
683 |
> |
|
684 |
|
} |