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 |
|
|
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 |
29 |
< |
* <tt>fail</tt>, <tt>assertTrue</tt>, etc.) It is OK (but not |
29 |
> |
* {@code fail}, {@code assertTrue}, 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 |
35 |
< |
* to invoke <tt>super.setUp</tt> and <tt>super.tearDown</tt> within |
35 |
> |
* to invoke {@code super.setUp} and {@code super.tearDown} within |
36 |
|
* them. These methods are used to clear and check for thread |
37 |
|
* assertion failures.</li> |
38 |
|
* |
39 |
< |
* <li>All delays and timeouts must use one of the constants <tt> |
40 |
< |
* SHORT_DELAY_MS</tt>, <tt> SMALL_DELAY_MS</tt>, <tt> MEDIUM_DELAY_MS</tt>, |
41 |
< |
* <tt> LONG_DELAY_MS</tt>. The idea here is that a SHORT is always |
39 |
> |
* <li>All delays and timeouts must use one of the constants {@code |
40 |
> |
* SHORT_DELAY_MS}, {@code SMALL_DELAY_MS}, {@code MEDIUM_DELAY_MS}, |
41 |
> |
* {@code LONG_DELAY_MS}. The idea here is that a SHORT is always |
42 |
|
* discriminable from zero time, and always allows enough time for the |
43 |
|
* small amounts of computation (creating a thread, calling a few |
44 |
|
* methods, etc) needed to reach a timeout point. Similarly, a SMALL |
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 |
52 |
< |
* method. The <tt> joinPool</tt> method can be used to do this when |
51 |
> |
* method (or {@code fail} to do so) before returning from the |
52 |
> |
* method. The {@code joinPool} method can be used to do this when |
53 |
|
* using Executors.</li> |
54 |
|
* |
55 |
|
* </ol> |
81 |
|
* any particular package to simplify things for people integrating |
82 |
|
* them in TCK test suites.</li> |
83 |
|
* |
84 |
< |
* <li> As a convenience, the <tt>main</tt> of this class (JSR166TestCase) |
84 |
> |
* <li> As a convenience, the {@code main} of this class (JSR166TestCase) |
85 |
|
* runs all JSR166 unit tests.</li> |
86 |
|
* |
87 |
|
* </ul> |
88 |
|
*/ |
89 |
|
public class JSR166TestCase extends TestCase { |
90 |
+ |
private static final boolean useSecurityManager = |
91 |
+ |
Boolean.getBoolean("jsr166.useSecurityManager"); |
92 |
+ |
|
93 |
|
/** |
94 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
95 |
|
*/ |
96 |
< |
public static void main (String[] args) { |
96 |
> |
public static void main(String[] args) { |
97 |
> |
if (useSecurityManager) { |
98 |
> |
System.err.println("Setting a permissive security manager"); |
99 |
> |
Policy.setPolicy(permissivePolicy()); |
100 |
> |
System.setSecurityManager(new SecurityManager()); |
101 |
> |
} |
102 |
|
int iters = 1; |
103 |
|
if (args.length > 0) |
104 |
|
iters = Integer.parseInt(args[0]); |
105 |
|
Test s = suite(); |
106 |
|
for (int i = 0; i < iters; ++i) { |
107 |
< |
junit.textui.TestRunner.run (s); |
107 |
> |
junit.textui.TestRunner.run(s); |
108 |
|
System.gc(); |
109 |
|
System.runFinalization(); |
110 |
|
} |
114 |
|
/** |
115 |
|
* Collects all JSR166 unit tests as one suite |
116 |
|
*/ |
117 |
< |
public static Test suite ( ) { |
117 |
> |
public static Test suite() { |
118 |
|
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
119 |
|
|
120 |
+ |
suite.addTest(new TestSuite(ForkJoinPoolTest.class)); |
121 |
+ |
suite.addTest(new TestSuite(ForkJoinTaskTest.class)); |
122 |
+ |
suite.addTest(new TestSuite(RecursiveActionTest.class)); |
123 |
+ |
suite.addTest(new TestSuite(RecursiveTaskTest.class)); |
124 |
+ |
suite.addTest(new TestSuite(LinkedTransferQueueTest.class)); |
125 |
+ |
suite.addTest(new TestSuite(PhaserTest.class)); |
126 |
+ |
suite.addTest(new TestSuite(ThreadLocalRandomTest.class)); |
127 |
|
suite.addTest(new TestSuite(AbstractExecutorServiceTest.class)); |
128 |
|
suite.addTest(new TestSuite(AbstractQueueTest.class)); |
129 |
|
suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class)); |
143 |
|
suite.addTest(new TestSuite(AtomicReferenceTest.class)); |
144 |
|
suite.addTest(new TestSuite(AtomicStampedReferenceTest.class)); |
145 |
|
suite.addTest(new TestSuite(ConcurrentHashMapTest.class)); |
146 |
+ |
suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class)); |
147 |
|
suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class)); |
148 |
|
suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class)); |
149 |
|
suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class)); |
204 |
|
/** |
205 |
|
* Sets delays as multiples of SHORT_DELAY. |
206 |
|
*/ |
207 |
< |
protected void setDelays() { |
207 |
> |
protected void setDelays() { |
208 |
|
SHORT_DELAY_MS = getShortDelay(); |
209 |
|
SMALL_DELAY_MS = SHORT_DELAY_MS * 5; |
210 |
|
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10; |
296 |
|
|
297 |
|
/** |
298 |
|
* threadFail with message "should throw exception" |
299 |
< |
*/ |
299 |
> |
*/ |
300 |
|
public void threadShouldThrow() { |
301 |
< |
try { |
302 |
< |
threadFailed = true; |
303 |
< |
fail("should throw exception"); |
304 |
< |
} catch (AssertionFailedError e) { |
305 |
< |
e.printStackTrace(); |
306 |
< |
throw e; |
307 |
< |
} |
301 |
> |
threadFailed = true; |
302 |
> |
fail("should throw exception"); |
303 |
> |
} |
304 |
> |
|
305 |
> |
/** |
306 |
> |
* threadFail with message "should throw" + exceptionName |
307 |
> |
*/ |
308 |
> |
public void threadShouldThrow(String exceptionName) { |
309 |
> |
threadFailed = true; |
310 |
> |
fail("should throw " + exceptionName); |
311 |
|
} |
312 |
|
|
313 |
|
/** |
333 |
|
public void joinPool(ExecutorService exec) { |
334 |
|
try { |
335 |
|
exec.shutdown(); |
336 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
337 |
< |
} catch(SecurityException ok) { |
336 |
> |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
337 |
> |
} catch (SecurityException ok) { |
338 |
|
// Allowed in case test doesn't have privs |
339 |
< |
} catch(InterruptedException ie) { |
340 |
< |
fail("Unexpected exception"); |
339 |
> |
} catch (InterruptedException ie) { |
340 |
> |
fail("Unexpected InterruptedException"); |
341 |
|
} |
342 |
|
} |
343 |
|
|
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
+ |
* fail with message "should throw " + exceptionName |
354 |
+ |
*/ |
355 |
+ |
public void shouldThrow(String exceptionName) { |
356 |
+ |
fail("Should throw " + exceptionName); |
357 |
+ |
} |
358 |
+ |
|
359 |
+ |
/** |
360 |
|
* fail with message "Unexpected exception" |
361 |
|
*/ |
362 |
|
public void unexpectedException() { |
363 |
|
fail("Unexpected exception"); |
364 |
|
} |
365 |
|
|
366 |
+ |
/** |
367 |
+ |
* fail with message "Unexpected exception", with argument |
368 |
+ |
*/ |
369 |
+ |
public void unexpectedException(Throwable ex) { |
370 |
+ |
ex.printStackTrace(); |
371 |
+ |
fail("Unexpected exception: " + ex); |
372 |
+ |
} |
373 |
+ |
|
374 |
|
|
375 |
|
/** |
376 |
|
* The number of elements to place in collections, arrays, etc. |
377 |
|
*/ |
378 |
< |
static final int SIZE = 20; |
378 |
> |
public static final int SIZE = 20; |
379 |
|
|
380 |
|
// Some convenient Integer constants |
381 |
|
|
382 |
< |
static final Integer zero = new Integer(0); |
383 |
< |
static final Integer one = new Integer(1); |
384 |
< |
static final Integer two = new Integer(2); |
385 |
< |
static final Integer three = new Integer(3); |
386 |
< |
static final Integer four = new Integer(4); |
387 |
< |
static final Integer five = new Integer(5); |
388 |
< |
static final Integer six = new Integer(6); |
389 |
< |
static final Integer seven = new Integer(7); |
390 |
< |
static final Integer eight = new Integer(8); |
391 |
< |
static final Integer nine = new Integer(9); |
392 |
< |
static final Integer m1 = new Integer(-1); |
393 |
< |
static final Integer m2 = new Integer(-2); |
394 |
< |
static final Integer m3 = new Integer(-3); |
395 |
< |
static final Integer m4 = new Integer(-4); |
396 |
< |
static final Integer m5 = new Integer(-5); |
397 |
< |
static final Integer m6 = new Integer(-6); |
398 |
< |
static final Integer m10 = new Integer(-10); |
382 |
> |
public static final Integer zero = new Integer(0); |
383 |
> |
public static final Integer one = new Integer(1); |
384 |
> |
public static final Integer two = new Integer(2); |
385 |
> |
public static final Integer three = new Integer(3); |
386 |
> |
public static final Integer four = new Integer(4); |
387 |
> |
public static final Integer five = new Integer(5); |
388 |
> |
public static final Integer six = new Integer(6); |
389 |
> |
public static final Integer seven = new Integer(7); |
390 |
> |
public static final Integer eight = new Integer(8); |
391 |
> |
public static final Integer nine = new Integer(9); |
392 |
> |
public static final Integer m1 = new Integer(-1); |
393 |
> |
public static final Integer m2 = new Integer(-2); |
394 |
> |
public static final Integer m3 = new Integer(-3); |
395 |
> |
public static final Integer m4 = new Integer(-4); |
396 |
> |
public static final Integer m5 = new Integer(-5); |
397 |
> |
public static final Integer m6 = new Integer(-6); |
398 |
> |
public static final Integer m10 = new Integer(-10); |
399 |
|
|
400 |
|
|
401 |
|
/** |
402 |
+ |
* Runs Runnable r with a security policy that permits precisely |
403 |
+ |
* the specified permissions. If there is no current security |
404 |
+ |
* manager, the runnable is run twice, both with and without a |
405 |
+ |
* security manager. We require that any security manager permit |
406 |
+ |
* getPolicy/setPolicy. |
407 |
+ |
*/ |
408 |
+ |
public void runWithPermissions(Runnable r, Permission... permissions) { |
409 |
+ |
SecurityManager sm = System.getSecurityManager(); |
410 |
+ |
if (sm == null) { |
411 |
+ |
r.run(); |
412 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
413 |
+ |
try { |
414 |
+ |
Policy.setPolicy(permissivePolicy()); |
415 |
+ |
System.setSecurityManager(new SecurityManager()); |
416 |
+ |
runWithPermissions(r, permissions); |
417 |
+ |
} finally { |
418 |
+ |
System.setSecurityManager(null); |
419 |
+ |
Policy.setPolicy(savedPolicy); |
420 |
+ |
} |
421 |
+ |
} else { |
422 |
+ |
Policy savedPolicy = Policy.getPolicy(); |
423 |
+ |
AdjustablePolicy policy = new AdjustablePolicy(permissions); |
424 |
+ |
Policy.setPolicy(policy); |
425 |
+ |
|
426 |
+ |
try { |
427 |
+ |
r.run(); |
428 |
+ |
} finally { |
429 |
+ |
policy.addPermission(new SecurityPermission("setPolicy")); |
430 |
+ |
Policy.setPolicy(savedPolicy); |
431 |
+ |
} |
432 |
+ |
} |
433 |
+ |
} |
434 |
+ |
|
435 |
+ |
/** |
436 |
+ |
* Runs a runnable without any permissions. |
437 |
+ |
*/ |
438 |
+ |
public void runWithoutPermissions(Runnable r) { |
439 |
+ |
runWithPermissions(r); |
440 |
+ |
} |
441 |
+ |
|
442 |
+ |
/** |
443 |
|
* A security policy where new permissions can be dynamically added |
444 |
|
* or all cleared. |
445 |
|
*/ |
446 |
< |
static class AdjustablePolicy extends java.security.Policy { |
446 |
> |
public static class AdjustablePolicy extends java.security.Policy { |
447 |
|
Permissions perms = new Permissions(); |
448 |
< |
AdjustablePolicy() { } |
448 |
> |
AdjustablePolicy(Permission... permissions) { |
449 |
> |
for (Permission permission : permissions) |
450 |
> |
perms.add(permission); |
451 |
> |
} |
452 |
|
void addPermission(Permission perm) { perms.add(perm); } |
453 |
|
void clearPermissions() { perms = new Permissions(); } |
454 |
< |
public PermissionCollection getPermissions(CodeSource cs) { |
455 |
< |
return perms; |
456 |
< |
} |
457 |
< |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
458 |
< |
return perms; |
459 |
< |
} |
460 |
< |
public boolean implies(ProtectionDomain pd, Permission p) { |
461 |
< |
return perms.implies(p); |
462 |
< |
} |
463 |
< |
public void refresh() {} |
454 |
> |
public PermissionCollection getPermissions(CodeSource cs) { |
455 |
> |
return perms; |
456 |
> |
} |
457 |
> |
public PermissionCollection getPermissions(ProtectionDomain pd) { |
458 |
> |
return perms; |
459 |
> |
} |
460 |
> |
public boolean implies(ProtectionDomain pd, Permission p) { |
461 |
> |
return perms.implies(p); |
462 |
> |
} |
463 |
> |
public void refresh() {} |
464 |
|
} |
465 |
|
|
466 |
+ |
/** |
467 |
+ |
* Returns a policy containing all the permissions we ever need. |
468 |
+ |
*/ |
469 |
+ |
public static Policy permissivePolicy() { |
470 |
+ |
return new AdjustablePolicy |
471 |
+ |
// Permissions j.u.c. needs directly |
472 |
+ |
(new RuntimePermission("modifyThread"), |
473 |
+ |
new RuntimePermission("getClassLoader"), |
474 |
+ |
new RuntimePermission("setContextClassLoader"), |
475 |
+ |
// Permissions needed to change permissions! |
476 |
+ |
new SecurityPermission("getPolicy"), |
477 |
+ |
new SecurityPermission("setPolicy"), |
478 |
+ |
new RuntimePermission("setSecurityManager"), |
479 |
+ |
// Permissions needed by the junit test harness |
480 |
+ |
new RuntimePermission("accessDeclaredMembers"), |
481 |
+ |
new PropertyPermission("*", "read"), |
482 |
+ |
new java.io.FilePermission("<<ALL FILES>>", "read")); |
483 |
+ |
} |
484 |
|
|
485 |
< |
// Some convenient Runnable classes |
486 |
< |
|
487 |
< |
static class NoOpRunnable implements Runnable { |
488 |
< |
public void run() {} |
485 |
> |
/** |
486 |
> |
* Sleep until the timeout has elapsed, or interrupted. |
487 |
> |
* Does <em>NOT</em> throw InterruptedException. |
488 |
> |
*/ |
489 |
> |
void sleepTillInterrupted(long timeoutMillis) { |
490 |
> |
try { |
491 |
> |
Thread.sleep(timeoutMillis); |
492 |
> |
} catch (InterruptedException wakeup) {} |
493 |
|
} |
494 |
|
|
495 |
< |
static class NoOpCallable implements Callable { |
496 |
< |
public Object call() { return Boolean.TRUE; } |
495 |
> |
/** |
496 |
> |
* Returns a new started Thread running the given runnable. |
497 |
> |
*/ |
498 |
> |
Thread newStartedThread(Runnable runnable) { |
499 |
> |
Thread t = new Thread(runnable); |
500 |
> |
t.start(); |
501 |
> |
return t; |
502 |
|
} |
503 |
|
|
504 |
< |
static final String TEST_STRING = "a test string"; |
504 |
> |
// Some convenient Runnable classes |
505 |
|
|
506 |
< |
static class StringTask implements Callable<String> { |
507 |
< |
public String call() { return TEST_STRING; } |
402 |
< |
} |
506 |
> |
public abstract class CheckedRunnable implements Runnable { |
507 |
> |
protected abstract void realRun() throws Throwable; |
508 |
|
|
509 |
< |
static class NPETask implements Callable<String> { |
510 |
< |
public String call() { throw new NullPointerException(); } |
509 |
> |
public final void run() { |
510 |
> |
try { |
511 |
> |
realRun(); |
512 |
> |
} catch (Throwable t) { |
513 |
> |
threadUnexpectedException(t); |
514 |
> |
} |
515 |
> |
} |
516 |
|
} |
517 |
|
|
518 |
< |
static class CallableOne implements Callable<Integer> { |
519 |
< |
public Integer call() { return one; } |
410 |
< |
} |
518 |
> |
public abstract class RunnableShouldThrow implements Runnable { |
519 |
> |
protected abstract void realRun() throws Throwable; |
520 |
|
|
521 |
< |
class ShortRunnable implements Runnable { |
522 |
< |
public void run() { |
521 |
> |
final Class<?> exceptionClass; |
522 |
> |
|
523 |
> |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
524 |
> |
this.exceptionClass = exceptionClass; |
525 |
> |
} |
526 |
> |
|
527 |
> |
public final void run() { |
528 |
|
try { |
529 |
< |
Thread.sleep(SHORT_DELAY_MS); |
530 |
< |
} |
531 |
< |
catch(Exception e) { |
532 |
< |
threadUnexpectedException(e); |
529 |
> |
realRun(); |
530 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
531 |
> |
} catch (Throwable t) { |
532 |
> |
if (! exceptionClass.isInstance(t)) |
533 |
> |
threadUnexpectedException(t); |
534 |
|
} |
535 |
|
} |
536 |
|
} |
537 |
|
|
538 |
< |
class ShortInterruptedRunnable implements Runnable { |
539 |
< |
public void run() { |
538 |
> |
public abstract class ThreadShouldThrow extends Thread { |
539 |
> |
protected abstract void realRun() throws Throwable; |
540 |
> |
|
541 |
> |
final Class<?> exceptionClass; |
542 |
> |
|
543 |
> |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
544 |
> |
this.exceptionClass = exceptionClass; |
545 |
> |
} |
546 |
> |
|
547 |
> |
public final void run() { |
548 |
|
try { |
549 |
< |
Thread.sleep(SHORT_DELAY_MS); |
550 |
< |
threadShouldThrow(); |
551 |
< |
} |
552 |
< |
catch(InterruptedException success) { |
549 |
> |
realRun(); |
550 |
> |
threadShouldThrow(exceptionClass.getSimpleName()); |
551 |
> |
} catch (Throwable t) { |
552 |
> |
if (! exceptionClass.isInstance(t)) |
553 |
> |
threadUnexpectedException(t); |
554 |
|
} |
555 |
|
} |
556 |
|
} |
557 |
|
|
558 |
< |
class SmallRunnable implements Runnable { |
559 |
< |
public void run() { |
558 |
> |
public abstract class CheckedInterruptedRunnable implements Runnable { |
559 |
> |
protected abstract void realRun() throws Throwable; |
560 |
> |
|
561 |
> |
public final void run() { |
562 |
|
try { |
563 |
< |
Thread.sleep(SMALL_DELAY_MS); |
564 |
< |
} |
565 |
< |
catch(Exception e) { |
566 |
< |
threadUnexpectedException(e); |
563 |
> |
realRun(); |
564 |
> |
threadShouldThrow("InterruptedException"); |
565 |
> |
} catch (InterruptedException success) { |
566 |
> |
} catch (Throwable t) { |
567 |
> |
threadUnexpectedException(t); |
568 |
|
} |
569 |
|
} |
570 |
|
} |
571 |
|
|
572 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
573 |
< |
public void run() { |
572 |
> |
public abstract class CheckedCallable<T> implements Callable<T> { |
573 |
> |
protected abstract T realCall() throws Throwable; |
574 |
> |
|
575 |
> |
public final T call() { |
576 |
|
try { |
577 |
< |
Thread.sleep(SMALL_DELAY_MS); |
578 |
< |
} |
579 |
< |
catch(Exception e) { |
577 |
> |
return realCall(); |
578 |
> |
} catch (Throwable t) { |
579 |
> |
threadUnexpectedException(t); |
580 |
|
} |
581 |
+ |
return null; |
582 |
|
} |
583 |
|
} |
584 |
|
|
585 |
< |
class SmallCallable implements Callable { |
586 |
< |
public Object call() { |
585 |
> |
public abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
586 |
> |
protected abstract T realCall() throws Throwable; |
587 |
> |
|
588 |
> |
public final T call() { |
589 |
|
try { |
590 |
< |
Thread.sleep(SMALL_DELAY_MS); |
591 |
< |
} |
592 |
< |
catch(Exception e) { |
593 |
< |
threadUnexpectedException(e); |
590 |
> |
T result = realCall(); |
591 |
> |
threadShouldThrow("InterruptedException"); |
592 |
> |
return result; |
593 |
> |
} catch (InterruptedException success) { |
594 |
> |
} catch (Throwable t) { |
595 |
> |
threadUnexpectedException(t); |
596 |
|
} |
597 |
< |
return Boolean.TRUE; |
597 |
> |
return null; |
598 |
|
} |
599 |
|
} |
600 |
|
|
601 |
< |
class SmallInterruptedRunnable implements Runnable { |
602 |
< |
public void run() { |
601 |
> |
public static class NoOpRunnable implements Runnable { |
602 |
> |
public void run() {} |
603 |
> |
} |
604 |
> |
|
605 |
> |
public static class NoOpCallable implements Callable { |
606 |
> |
public Object call() { return Boolean.TRUE; } |
607 |
> |
} |
608 |
> |
|
609 |
> |
public static final String TEST_STRING = "a test string"; |
610 |
> |
|
611 |
> |
public static class StringTask implements Callable<String> { |
612 |
> |
public String call() { return TEST_STRING; } |
613 |
> |
} |
614 |
> |
|
615 |
> |
public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) { |
616 |
> |
return new CheckedCallable<String>() { |
617 |
> |
public String realCall() { |
618 |
> |
try { |
619 |
> |
latch.await(); |
620 |
> |
} catch (InterruptedException quittingTime) {} |
621 |
> |
return TEST_STRING; |
622 |
> |
}}; |
623 |
> |
} |
624 |
> |
|
625 |
> |
public static class NPETask implements Callable<String> { |
626 |
> |
public String call() { throw new NullPointerException(); } |
627 |
> |
} |
628 |
> |
|
629 |
> |
public static class CallableOne implements Callable<Integer> { |
630 |
> |
public Integer call() { return one; } |
631 |
> |
} |
632 |
> |
|
633 |
> |
public class ShortRunnable extends CheckedRunnable { |
634 |
> |
protected void realRun() throws Throwable { |
635 |
> |
Thread.sleep(SHORT_DELAY_MS); |
636 |
> |
} |
637 |
> |
} |
638 |
> |
|
639 |
> |
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
640 |
> |
protected void realRun() throws InterruptedException { |
641 |
> |
Thread.sleep(SHORT_DELAY_MS); |
642 |
> |
} |
643 |
> |
} |
644 |
> |
|
645 |
> |
public class SmallRunnable extends CheckedRunnable { |
646 |
> |
protected void realRun() throws Throwable { |
647 |
> |
Thread.sleep(SMALL_DELAY_MS); |
648 |
> |
} |
649 |
> |
} |
650 |
> |
|
651 |
> |
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
652 |
> |
protected void realRun() { |
653 |
|
try { |
654 |
|
Thread.sleep(SMALL_DELAY_MS); |
655 |
< |
threadShouldThrow(); |
472 |
< |
} |
473 |
< |
catch(InterruptedException success) { |
474 |
< |
} |
655 |
> |
} catch (InterruptedException ok) {} |
656 |
|
} |
657 |
|
} |
658 |
|
|
659 |
+ |
public class SmallCallable extends CheckedCallable { |
660 |
+ |
protected Object realCall() throws InterruptedException { |
661 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
662 |
+ |
return Boolean.TRUE; |
663 |
+ |
} |
664 |
+ |
} |
665 |
|
|
666 |
< |
class MediumRunnable implements Runnable { |
667 |
< |
public void run() { |
668 |
< |
try { |
482 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
483 |
< |
} |
484 |
< |
catch(Exception e) { |
485 |
< |
threadUnexpectedException(e); |
486 |
< |
} |
666 |
> |
public class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
667 |
> |
protected void realRun() throws InterruptedException { |
668 |
> |
Thread.sleep(SMALL_DELAY_MS); |
669 |
|
} |
670 |
|
} |
671 |
|
|
672 |
< |
class MediumInterruptedRunnable implements Runnable { |
673 |
< |
public void run() { |
674 |
< |
try { |
493 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
494 |
< |
threadShouldThrow(); |
495 |
< |
} |
496 |
< |
catch(InterruptedException success) { |
497 |
< |
} |
672 |
> |
public class MediumRunnable extends CheckedRunnable { |
673 |
> |
protected void realRun() throws Throwable { |
674 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
675 |
|
} |
676 |
|
} |
677 |
|
|
678 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
679 |
< |
public void run() { |
678 |
> |
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
679 |
> |
protected void realRun() throws InterruptedException { |
680 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
681 |
> |
} |
682 |
> |
} |
683 |
> |
|
684 |
> |
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
685 |
> |
protected void realRun() { |
686 |
|
try { |
687 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
688 |
< |
} |
506 |
< |
catch(InterruptedException success) { |
507 |
< |
} |
688 |
> |
} catch (InterruptedException ok) {} |
689 |
|
} |
690 |
|
} |
691 |
|
|
692 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
693 |
< |
public void run() { |
692 |
> |
public class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
693 |
> |
protected void realRun() { |
694 |
|
try { |
695 |
|
Thread.sleep(LONG_DELAY_MS); |
696 |
< |
} |
516 |
< |
catch(InterruptedException success) { |
517 |
< |
} |
696 |
> |
} catch (InterruptedException ok) {} |
697 |
|
} |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
|
* For use as ThreadFactory in constructors |
702 |
|
*/ |
703 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
704 |
< |
public Thread newThread(Runnable r){ |
703 |
> |
public static class SimpleThreadFactory implements ThreadFactory { |
704 |
> |
public Thread newThread(Runnable r) { |
705 |
|
return new Thread(r); |
706 |
|
} |
707 |
|
} |
708 |
|
|
709 |
< |
static class TrackedShortRunnable implements Runnable { |
710 |
< |
volatile boolean done = false; |
709 |
> |
public static class TrackedShortRunnable implements Runnable { |
710 |
> |
public volatile boolean done = false; |
711 |
|
public void run() { |
712 |
|
try { |
713 |
|
Thread.sleep(SMALL_DELAY_MS); |
714 |
|
done = true; |
715 |
< |
} catch(Exception e){ |
537 |
< |
} |
715 |
> |
} catch (InterruptedException ok) {} |
716 |
|
} |
717 |
|
} |
718 |
|
|
719 |
< |
static class TrackedMediumRunnable implements Runnable { |
720 |
< |
volatile boolean done = false; |
719 |
> |
public static class TrackedMediumRunnable implements Runnable { |
720 |
> |
public volatile boolean done = false; |
721 |
|
public void run() { |
722 |
|
try { |
723 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
724 |
|
done = true; |
725 |
< |
} catch(Exception e){ |
548 |
< |
} |
725 |
> |
} catch (InterruptedException ok) {} |
726 |
|
} |
727 |
|
} |
728 |
|
|
729 |
< |
static class TrackedLongRunnable implements Runnable { |
730 |
< |
volatile boolean done = false; |
729 |
> |
public static class TrackedLongRunnable implements Runnable { |
730 |
> |
public volatile boolean done = false; |
731 |
|
public void run() { |
732 |
|
try { |
733 |
|
Thread.sleep(LONG_DELAY_MS); |
734 |
|
done = true; |
735 |
< |
} catch(Exception e){ |
559 |
< |
} |
735 |
> |
} catch (InterruptedException ok) {} |
736 |
|
} |
737 |
|
} |
738 |
|
|
739 |
< |
static class TrackedNoOpRunnable implements Runnable { |
740 |
< |
volatile boolean done = false; |
739 |
> |
public static class TrackedNoOpRunnable implements Runnable { |
740 |
> |
public volatile boolean done = false; |
741 |
|
public void run() { |
742 |
|
done = true; |
743 |
|
} |
744 |
|
} |
745 |
|
|
746 |
< |
static class TrackedCallable implements Callable { |
747 |
< |
volatile boolean done = false; |
746 |
> |
public static class TrackedCallable implements Callable { |
747 |
> |
public volatile boolean done = false; |
748 |
|
public Object call() { |
749 |
|
try { |
750 |
|
Thread.sleep(SMALL_DELAY_MS); |
751 |
|
done = true; |
752 |
< |
} catch(Exception e){ |
577 |
< |
} |
752 |
> |
} catch (InterruptedException ok) {} |
753 |
|
return Boolean.TRUE; |
754 |
|
} |
755 |
|
} |
758 |
|
/** |
759 |
|
* For use as RejectedExecutionHandler in constructors |
760 |
|
*/ |
761 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
762 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
761 |
> |
public static class NoOpREHandler implements RejectedExecutionHandler { |
762 |
> |
public void rejectedExecution(Runnable r, |
763 |
> |
ThreadPoolExecutor executor) {} |
764 |
|
} |
765 |
|
|
590 |
– |
|
766 |
|
} |