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 |
|
|
90 |
|
/** |
91 |
|
* Runs all JSR166 unit tests using junit.textui.TestRunner |
92 |
|
*/ |
93 |
< |
public static void main (String[] args) { |
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); |
99 |
> |
junit.textui.TestRunner.run(s); |
100 |
|
System.gc(); |
101 |
|
System.runFinalization(); |
102 |
|
} |
106 |
|
/** |
107 |
|
* Collects all JSR166 unit tests as one suite |
108 |
|
*/ |
109 |
< |
public static Test suite ( ) { |
109 |
> |
public static Test suite() { |
110 |
|
TestSuite suite = new TestSuite("JSR166 Unit Tests"); |
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)); |
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)); |
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 |
|
suite.addTest(new TestSuite(TreeMapTest.class)); |
195 |
|
/** |
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; |
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(SecurityException ok) { |
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) { |
330 |
> |
} catch (InterruptedException ie) { |
331 |
|
fail("Unexpected exception"); |
332 |
|
} |
333 |
|
} |
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. |
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 m6 = new Integer(-6); |
389 |
|
static final Integer m10 = new Integer(-10); |
390 |
|
|
391 |
|
|
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 |
+ |
/** |
425 |
+ |
* Returns a new started Thread running the given runnable. |
426 |
+ |
*/ |
427 |
+ |
Thread newStartedThread(Runnable runnable) { |
428 |
+ |
Thread t = new Thread(runnable); |
429 |
+ |
t.start(); |
430 |
+ |
return t; |
431 |
+ |
} |
432 |
|
|
433 |
|
// Some convenient Runnable classes |
434 |
|
|
435 |
+ |
abstract class CheckedRunnable implements Runnable { |
436 |
+ |
abstract void realRun() throws Throwable; |
437 |
+ |
|
438 |
+ |
public final void run() { |
439 |
+ |
try { |
440 |
+ |
realRun(); |
441 |
+ |
} catch (Throwable t) { |
442 |
+ |
threadUnexpectedException(t); |
443 |
+ |
} |
444 |
+ |
} |
445 |
+ |
} |
446 |
+ |
|
447 |
+ |
abstract class RunnableShouldThrow implements Runnable { |
448 |
+ |
abstract void realRun() throws Throwable; |
449 |
+ |
|
450 |
+ |
final Class<?> exceptionClass; |
451 |
+ |
|
452 |
+ |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
453 |
+ |
this.exceptionClass = exceptionClass; |
454 |
+ |
} |
455 |
+ |
|
456 |
+ |
public final void run() { |
457 |
+ |
try { |
458 |
+ |
realRun(); |
459 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
460 |
+ |
} catch (InterruptedException success) { |
461 |
+ |
} catch (Throwable t) { |
462 |
+ |
if (! exceptionClass.isInstance(t)) |
463 |
+ |
threadUnexpectedException(t); |
464 |
+ |
} |
465 |
+ |
} |
466 |
+ |
} |
467 |
+ |
|
468 |
+ |
abstract class ThreadShouldThrow extends Thread { |
469 |
+ |
abstract void realRun() throws Throwable; |
470 |
+ |
|
471 |
+ |
final Class<?> exceptionClass; |
472 |
+ |
|
473 |
+ |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
474 |
+ |
this.exceptionClass = exceptionClass; |
475 |
+ |
} |
476 |
+ |
|
477 |
+ |
public final void run() { |
478 |
+ |
try { |
479 |
+ |
realRun(); |
480 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
481 |
+ |
} catch (InterruptedException success) { |
482 |
+ |
} catch (Throwable t) { |
483 |
+ |
if (! exceptionClass.isInstance(t)) |
484 |
+ |
threadUnexpectedException(t); |
485 |
+ |
} |
486 |
+ |
} |
487 |
+ |
} |
488 |
+ |
|
489 |
+ |
abstract class CheckedInterruptedRunnable implements Runnable { |
490 |
+ |
abstract void realRun() throws Throwable; |
491 |
+ |
|
492 |
+ |
public final void run() { |
493 |
+ |
try { |
494 |
+ |
realRun(); |
495 |
+ |
threadShouldThrow("InterruptedException"); |
496 |
+ |
} catch (InterruptedException success) { |
497 |
+ |
} catch (Throwable t) { |
498 |
+ |
threadUnexpectedException(t); |
499 |
+ |
} |
500 |
+ |
} |
501 |
+ |
} |
502 |
+ |
|
503 |
+ |
abstract class CheckedCallable<T> implements Callable<T> { |
504 |
+ |
abstract T realCall() throws Throwable; |
505 |
+ |
|
506 |
+ |
public final T call() { |
507 |
+ |
try { |
508 |
+ |
return realCall(); |
509 |
+ |
} catch (Throwable t) { |
510 |
+ |
threadUnexpectedException(t); |
511 |
+ |
} |
512 |
+ |
return null; |
513 |
+ |
} |
514 |
+ |
} |
515 |
+ |
|
516 |
+ |
abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
517 |
+ |
abstract T realCall() throws Throwable; |
518 |
+ |
|
519 |
+ |
public final T call() { |
520 |
+ |
try { |
521 |
+ |
T result = realCall(); |
522 |
+ |
threadShouldThrow("InterruptedException"); |
523 |
+ |
return result; |
524 |
+ |
} catch (InterruptedException success) { |
525 |
+ |
} catch (Throwable t) { |
526 |
+ |
threadUnexpectedException(t); |
527 |
+ |
} |
528 |
+ |
return null; |
529 |
+ |
} |
530 |
+ |
} |
531 |
+ |
|
532 |
|
static class NoOpRunnable implements Runnable { |
533 |
|
public void run() {} |
534 |
|
} |
551 |
|
public Integer call() { return one; } |
552 |
|
} |
553 |
|
|
554 |
< |
class ShortRunnable implements Runnable { |
555 |
< |
public void run() { |
556 |
< |
try { |
398 |
< |
Thread.sleep(SHORT_DELAY_MS); |
399 |
< |
} |
400 |
< |
catch(Exception e) { |
401 |
< |
threadUnexpectedException(); |
402 |
< |
} |
554 |
> |
class ShortRunnable extends CheckedRunnable { |
555 |
> |
void realRun() throws Throwable { |
556 |
> |
Thread.sleep(SHORT_DELAY_MS); |
557 |
|
} |
558 |
|
} |
559 |
|
|
560 |
< |
class ShortInterruptedRunnable implements Runnable { |
561 |
< |
public void run() { |
562 |
< |
try { |
409 |
< |
Thread.sleep(SHORT_DELAY_MS); |
410 |
< |
threadShouldThrow(); |
411 |
< |
} |
412 |
< |
catch(InterruptedException success) { |
413 |
< |
} |
560 |
> |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
561 |
> |
void realRun() throws InterruptedException { |
562 |
> |
Thread.sleep(SHORT_DELAY_MS); |
563 |
|
} |
564 |
|
} |
565 |
|
|
566 |
< |
class SmallRunnable implements Runnable { |
567 |
< |
public void run() { |
568 |
< |
try { |
420 |
< |
Thread.sleep(SMALL_DELAY_MS); |
421 |
< |
} |
422 |
< |
catch(Exception e) { |
423 |
< |
threadUnexpectedException(); |
424 |
< |
} |
566 |
> |
class SmallRunnable extends CheckedRunnable { |
567 |
> |
void realRun() throws Throwable { |
568 |
> |
Thread.sleep(SMALL_DELAY_MS); |
569 |
|
} |
570 |
|
} |
571 |
|
|
572 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
573 |
< |
public void run() { |
572 |
> |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
573 |
> |
void realRun() { |
574 |
|
try { |
575 |
|
Thread.sleep(SMALL_DELAY_MS); |
576 |
|
} |
577 |
< |
catch(Exception e) { |
577 |
> |
catch (InterruptedException ok) { |
578 |
|
} |
579 |
|
} |
580 |
|
} |
581 |
|
|
582 |
< |
class SmallCallable implements Callable { |
583 |
< |
public Object call() { |
584 |
< |
try { |
441 |
< |
Thread.sleep(SMALL_DELAY_MS); |
442 |
< |
} |
443 |
< |
catch(Exception e) { |
444 |
< |
threadUnexpectedException(); |
445 |
< |
} |
582 |
> |
class SmallCallable extends CheckedCallable { |
583 |
> |
Object realCall() throws Throwable { |
584 |
> |
Thread.sleep(SMALL_DELAY_MS); |
585 |
|
return Boolean.TRUE; |
586 |
|
} |
587 |
|
} |
588 |
|
|
589 |
< |
class SmallInterruptedRunnable implements Runnable { |
590 |
< |
public void run() { |
591 |
< |
try { |
453 |
< |
Thread.sleep(SMALL_DELAY_MS); |
454 |
< |
threadShouldThrow(); |
455 |
< |
} |
456 |
< |
catch(InterruptedException success) { |
457 |
< |
} |
589 |
> |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
590 |
> |
void realRun() throws InterruptedException { |
591 |
> |
Thread.sleep(SMALL_DELAY_MS); |
592 |
|
} |
593 |
|
} |
594 |
|
|
595 |
< |
|
596 |
< |
class MediumRunnable implements Runnable { |
597 |
< |
public void run() { |
464 |
< |
try { |
465 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
466 |
< |
} |
467 |
< |
catch(Exception e) { |
468 |
< |
threadUnexpectedException(); |
469 |
< |
} |
595 |
> |
class MediumRunnable extends CheckedRunnable { |
596 |
> |
void realRun() throws Throwable { |
597 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
598 |
|
} |
599 |
|
} |
600 |
|
|
601 |
< |
class MediumInterruptedRunnable implements Runnable { |
602 |
< |
public void run() { |
603 |
< |
try { |
476 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
477 |
< |
threadShouldThrow(); |
478 |
< |
} |
479 |
< |
catch(InterruptedException success) { |
480 |
< |
} |
601 |
> |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
602 |
> |
void realRun() throws InterruptedException { |
603 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
604 |
|
} |
605 |
|
} |
606 |
|
|
607 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
608 |
< |
public void run() { |
607 |
> |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
608 |
> |
void realRun() { |
609 |
|
try { |
610 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
611 |
|
} |
612 |
< |
catch(InterruptedException success) { |
612 |
> |
catch (InterruptedException ok) { |
613 |
|
} |
614 |
|
} |
615 |
|
} |
616 |
|
|
617 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
618 |
< |
public void run() { |
617 |
> |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
618 |
> |
void realRun() { |
619 |
|
try { |
620 |
|
Thread.sleep(LONG_DELAY_MS); |
621 |
|
} |
622 |
< |
catch(InterruptedException success) { |
622 |
> |
catch (InterruptedException ok) { |
623 |
|
} |
624 |
|
} |
625 |
|
} |
627 |
|
/** |
628 |
|
* For use as ThreadFactory in constructors |
629 |
|
*/ |
630 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
631 |
< |
public Thread newThread(Runnable r){ |
630 |
> |
static class SimpleThreadFactory implements ThreadFactory { |
631 |
> |
public Thread newThread(Runnable r) { |
632 |
|
return new Thread(r); |
633 |
|
} |
634 |
|
} |
639 |
|
try { |
640 |
|
Thread.sleep(SMALL_DELAY_MS); |
641 |
|
done = true; |
642 |
< |
} catch(Exception e){ |
642 |
> |
} catch (InterruptedException ok) { |
643 |
|
} |
644 |
|
} |
645 |
|
} |
650 |
|
try { |
651 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
652 |
|
done = true; |
653 |
< |
} catch(Exception e){ |
653 |
> |
} catch (InterruptedException ok) { |
654 |
|
} |
655 |
|
} |
656 |
|
} |
661 |
|
try { |
662 |
|
Thread.sleep(LONG_DELAY_MS); |
663 |
|
done = true; |
664 |
< |
} catch(Exception e){ |
664 |
> |
} catch (InterruptedException ok) { |
665 |
|
} |
666 |
|
} |
667 |
|
} |
679 |
|
try { |
680 |
|
Thread.sleep(SMALL_DELAY_MS); |
681 |
|
done = true; |
682 |
< |
} catch(Exception e){ |
682 |
> |
} catch (InterruptedException ok) { |
683 |
|
} |
684 |
|
return Boolean.TRUE; |
685 |
|
} |
689 |
|
/** |
690 |
|
* For use as RejectedExecutionHandler in constructors |
691 |
|
*/ |
692 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
693 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
692 |
> |
static class NoOpREHandler implements RejectedExecutionHandler { |
693 |
> |
public void rejectedExecution(Runnable r, |
694 |
> |
ThreadPoolExecutor executor) {} |
695 |
|
} |
696 |
|
|
573 |
– |
|
697 |
|
} |