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)); |
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; |
287 |
|
|
288 |
|
/** |
289 |
|
* threadFail with message "should throw exception" |
290 |
< |
*/ |
290 |
> |
*/ |
291 |
|
public void threadShouldThrow() { |
292 |
< |
try { |
293 |
< |
threadFailed = true; |
294 |
< |
fail("should throw exception"); |
295 |
< |
} catch (AssertionFailedError e) { |
296 |
< |
e.printStackTrace(); |
297 |
< |
throw e; |
298 |
< |
} |
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 |
|
/** |
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) { |
331 |
< |
fail("Unexpected exception"); |
330 |
> |
} catch (InterruptedException ie) { |
331 |
> |
fail("Unexpected InterruptedException"); |
332 |
|
} |
333 |
|
} |
334 |
|
|
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. |
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 |
+ |
abstract class CheckedRunnable implements Runnable { |
435 |
+ |
abstract void realRun() throws Throwable; |
436 |
+ |
|
437 |
+ |
public final void run() { |
438 |
+ |
try { |
439 |
+ |
realRun(); |
440 |
+ |
} catch (Throwable t) { |
441 |
+ |
threadUnexpectedException(t); |
442 |
+ |
} |
443 |
+ |
} |
444 |
+ |
} |
445 |
+ |
|
446 |
+ |
abstract class RunnableShouldThrow implements Runnable { |
447 |
+ |
abstract void realRun() throws Throwable; |
448 |
+ |
|
449 |
+ |
final Class<?> exceptionClass; |
450 |
+ |
|
451 |
+ |
<T extends Throwable> RunnableShouldThrow(Class<T> exceptionClass) { |
452 |
+ |
this.exceptionClass = exceptionClass; |
453 |
+ |
} |
454 |
+ |
|
455 |
+ |
public final void run() { |
456 |
+ |
try { |
457 |
+ |
realRun(); |
458 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
459 |
+ |
} catch (InterruptedException success) { |
460 |
+ |
} catch (Throwable t) { |
461 |
+ |
if (! exceptionClass.isInstance(t)) |
462 |
+ |
threadUnexpectedException(t); |
463 |
+ |
} |
464 |
+ |
} |
465 |
+ |
} |
466 |
+ |
|
467 |
+ |
abstract class ThreadShouldThrow extends Thread { |
468 |
+ |
abstract void realRun() throws Throwable; |
469 |
+ |
|
470 |
+ |
final Class<?> exceptionClass; |
471 |
+ |
|
472 |
+ |
<T extends Throwable> ThreadShouldThrow(Class<T> exceptionClass) { |
473 |
+ |
this.exceptionClass = exceptionClass; |
474 |
+ |
} |
475 |
+ |
|
476 |
+ |
public final void run() { |
477 |
+ |
try { |
478 |
+ |
realRun(); |
479 |
+ |
threadShouldThrow(exceptionClass.getSimpleName()); |
480 |
+ |
} catch (InterruptedException success) { |
481 |
+ |
} catch (Throwable t) { |
482 |
+ |
if (! exceptionClass.isInstance(t)) |
483 |
+ |
threadUnexpectedException(t); |
484 |
+ |
} |
485 |
+ |
} |
486 |
+ |
} |
487 |
+ |
|
488 |
+ |
abstract class CheckedInterruptedRunnable implements Runnable { |
489 |
+ |
abstract void realRun() throws Throwable; |
490 |
+ |
|
491 |
+ |
public final void run() { |
492 |
+ |
try { |
493 |
+ |
realRun(); |
494 |
+ |
threadShouldThrow("InterruptedException"); |
495 |
+ |
} catch (InterruptedException success) { |
496 |
+ |
} catch (Throwable t) { |
497 |
+ |
threadUnexpectedException(t); |
498 |
+ |
} |
499 |
+ |
} |
500 |
+ |
} |
501 |
+ |
|
502 |
+ |
abstract class CheckedCallable<T> implements Callable<T> { |
503 |
+ |
abstract T realCall() throws Throwable; |
504 |
+ |
|
505 |
+ |
public final T call() { |
506 |
+ |
try { |
507 |
+ |
return realCall(); |
508 |
+ |
} catch (Throwable t) { |
509 |
+ |
threadUnexpectedException(t); |
510 |
+ |
} |
511 |
+ |
return null; |
512 |
+ |
} |
513 |
+ |
} |
514 |
+ |
|
515 |
+ |
abstract class CheckedInterruptedCallable<T> implements Callable<T> { |
516 |
+ |
abstract T realCall() throws Throwable; |
517 |
+ |
|
518 |
+ |
public final T call() { |
519 |
+ |
try { |
520 |
+ |
T result = realCall(); |
521 |
+ |
threadShouldThrow("InterruptedException"); |
522 |
+ |
return result; |
523 |
+ |
} catch (InterruptedException success) { |
524 |
+ |
} catch (Throwable t) { |
525 |
+ |
threadUnexpectedException(t); |
526 |
+ |
} |
527 |
+ |
return null; |
528 |
+ |
} |
529 |
+ |
} |
530 |
+ |
|
531 |
|
static class NoOpRunnable implements Runnable { |
532 |
|
public void run() {} |
533 |
|
} |
550 |
|
public Integer call() { return one; } |
551 |
|
} |
552 |
|
|
553 |
< |
class ShortRunnable implements Runnable { |
554 |
< |
public void run() { |
555 |
< |
try { |
422 |
< |
Thread.sleep(SHORT_DELAY_MS); |
423 |
< |
} |
424 |
< |
catch(Exception e) { |
425 |
< |
threadUnexpectedException(e); |
426 |
< |
} |
553 |
> |
class ShortRunnable extends CheckedRunnable { |
554 |
> |
void realRun() throws Throwable { |
555 |
> |
Thread.sleep(SHORT_DELAY_MS); |
556 |
|
} |
557 |
|
} |
558 |
|
|
559 |
< |
class ShortInterruptedRunnable implements Runnable { |
560 |
< |
public void run() { |
561 |
< |
try { |
433 |
< |
Thread.sleep(SHORT_DELAY_MS); |
434 |
< |
threadShouldThrow(); |
435 |
< |
} |
436 |
< |
catch(InterruptedException success) { |
437 |
< |
} |
559 |
> |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
560 |
> |
void realRun() throws InterruptedException { |
561 |
> |
Thread.sleep(SHORT_DELAY_MS); |
562 |
|
} |
563 |
|
} |
564 |
|
|
565 |
< |
class SmallRunnable implements Runnable { |
566 |
< |
public void run() { |
567 |
< |
try { |
444 |
< |
Thread.sleep(SMALL_DELAY_MS); |
445 |
< |
} |
446 |
< |
catch(Exception e) { |
447 |
< |
threadUnexpectedException(e); |
448 |
< |
} |
565 |
> |
class SmallRunnable extends CheckedRunnable { |
566 |
> |
void realRun() throws Throwable { |
567 |
> |
Thread.sleep(SMALL_DELAY_MS); |
568 |
|
} |
569 |
|
} |
570 |
|
|
571 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
572 |
< |
public void run() { |
571 |
> |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
572 |
> |
void realRun() { |
573 |
|
try { |
574 |
|
Thread.sleep(SMALL_DELAY_MS); |
575 |
< |
} |
457 |
< |
catch(Exception e) { |
458 |
< |
} |
575 |
> |
} catch (InterruptedException ok) {} |
576 |
|
} |
577 |
|
} |
578 |
|
|
579 |
< |
class SmallCallable implements Callable { |
580 |
< |
public Object call() { |
581 |
< |
try { |
465 |
< |
Thread.sleep(SMALL_DELAY_MS); |
466 |
< |
} |
467 |
< |
catch(Exception e) { |
468 |
< |
threadUnexpectedException(e); |
469 |
< |
} |
579 |
> |
class SmallCallable extends CheckedCallable { |
580 |
> |
Object realCall() throws InterruptedException { |
581 |
> |
Thread.sleep(SMALL_DELAY_MS); |
582 |
|
return Boolean.TRUE; |
583 |
|
} |
584 |
|
} |
585 |
|
|
586 |
< |
class SmallInterruptedRunnable implements Runnable { |
587 |
< |
public void run() { |
588 |
< |
try { |
477 |
< |
Thread.sleep(SMALL_DELAY_MS); |
478 |
< |
threadShouldThrow(); |
479 |
< |
} |
480 |
< |
catch(InterruptedException success) { |
481 |
< |
} |
586 |
> |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
587 |
> |
void realRun() throws InterruptedException { |
588 |
> |
Thread.sleep(SMALL_DELAY_MS); |
589 |
|
} |
590 |
|
} |
591 |
|
|
592 |
< |
|
593 |
< |
class MediumRunnable implements Runnable { |
594 |
< |
public void run() { |
488 |
< |
try { |
489 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
490 |
< |
} |
491 |
< |
catch(Exception e) { |
492 |
< |
threadUnexpectedException(e); |
493 |
< |
} |
592 |
> |
class MediumRunnable extends CheckedRunnable { |
593 |
> |
void realRun() throws Throwable { |
594 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
595 |
|
} |
596 |
|
} |
597 |
|
|
598 |
< |
class MediumInterruptedRunnable implements Runnable { |
599 |
< |
public void run() { |
600 |
< |
try { |
500 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
501 |
< |
threadShouldThrow(); |
502 |
< |
} |
503 |
< |
catch(InterruptedException success) { |
504 |
< |
} |
598 |
> |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
599 |
> |
void realRun() throws InterruptedException { |
600 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
601 |
|
} |
602 |
|
} |
603 |
|
|
604 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
605 |
< |
public void run() { |
604 |
> |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
605 |
> |
void realRun() { |
606 |
|
try { |
607 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
608 |
< |
} |
513 |
< |
catch(InterruptedException success) { |
514 |
< |
} |
608 |
> |
} catch (InterruptedException ok) {} |
609 |
|
} |
610 |
|
} |
611 |
|
|
612 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
613 |
< |
public void run() { |
612 |
> |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
613 |
> |
void realRun() { |
614 |
|
try { |
615 |
|
Thread.sleep(LONG_DELAY_MS); |
616 |
< |
} |
523 |
< |
catch(InterruptedException success) { |
524 |
< |
} |
616 |
> |
} catch (InterruptedException ok) {} |
617 |
|
} |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
|
* For use as ThreadFactory in constructors |
622 |
|
*/ |
623 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
624 |
< |
public Thread newThread(Runnable r){ |
623 |
> |
static class SimpleThreadFactory implements ThreadFactory { |
624 |
> |
public Thread newThread(Runnable r) { |
625 |
|
return new Thread(r); |
626 |
|
} |
627 |
|
} |
632 |
|
try { |
633 |
|
Thread.sleep(SMALL_DELAY_MS); |
634 |
|
done = true; |
635 |
< |
} catch(Exception e){ |
544 |
< |
} |
635 |
> |
} catch (InterruptedException ok) {} |
636 |
|
} |
637 |
|
} |
638 |
|
|
642 |
|
try { |
643 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
644 |
|
done = true; |
645 |
< |
} catch(Exception e){ |
555 |
< |
} |
645 |
> |
} catch (InterruptedException ok) {} |
646 |
|
} |
647 |
|
} |
648 |
|
|
652 |
|
try { |
653 |
|
Thread.sleep(LONG_DELAY_MS); |
654 |
|
done = true; |
655 |
< |
} catch(Exception e){ |
566 |
< |
} |
655 |
> |
} catch (InterruptedException ok) {} |
656 |
|
} |
657 |
|
} |
658 |
|
|
669 |
|
try { |
670 |
|
Thread.sleep(SMALL_DELAY_MS); |
671 |
|
done = true; |
672 |
< |
} catch(Exception e){ |
584 |
< |
} |
672 |
> |
} catch (InterruptedException ok) {} |
673 |
|
return Boolean.TRUE; |
674 |
|
} |
675 |
|
} |
678 |
|
/** |
679 |
|
* For use as RejectedExecutionHandler in constructors |
680 |
|
*/ |
681 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
682 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
681 |
> |
static class NoOpREHandler implements RejectedExecutionHandler { |
682 |
> |
public void rejectedExecution(Runnable r, |
683 |
> |
ThreadPoolExecutor executor) {} |
684 |
|
} |
685 |
|
|
597 |
– |
|
686 |
|
} |