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 |
|
|
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)); |
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 |
> |
try { |
293 |
> |
threadFailed = true; |
294 |
> |
fail("should throw exception"); |
295 |
> |
} catch (AssertionFailedError e) { |
296 |
> |
e.printStackTrace(); |
297 |
> |
throw e; |
298 |
> |
} |
299 |
|
} |
300 |
|
|
301 |
|
/** |
321 |
|
public void joinPool(ExecutorService exec) { |
322 |
|
try { |
323 |
|
exec.shutdown(); |
324 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
325 |
< |
} catch(SecurityException ok) { |
324 |
> |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
325 |
> |
} catch (SecurityException ok) { |
326 |
|
// Allowed in case test doesn't have privs |
327 |
< |
} catch(InterruptedException ie) { |
327 |
> |
} catch (InterruptedException ie) { |
328 |
|
fail("Unexpected exception"); |
329 |
|
} |
330 |
|
} |
344 |
|
fail("Unexpected exception"); |
345 |
|
} |
346 |
|
|
347 |
+ |
/** |
348 |
+ |
* fail with message "Unexpected exception", with argument |
349 |
+ |
*/ |
350 |
+ |
public void unexpectedException(Throwable ex) { |
351 |
+ |
ex.printStackTrace(); |
352 |
+ |
fail("Unexpected exception: " + ex); |
353 |
+ |
} |
354 |
+ |
|
355 |
|
|
356 |
|
/** |
357 |
|
* The number of elements to place in collections, arrays, etc. |
403 |
|
|
404 |
|
// Some convenient Runnable classes |
405 |
|
|
406 |
+ |
abstract class CheckedRunnable implements Runnable { |
407 |
+ |
abstract void realRun() throws Throwable; |
408 |
+ |
|
409 |
+ |
public final void run() { |
410 |
+ |
try { |
411 |
+ |
realRun(); |
412 |
+ |
} catch (Throwable t) { |
413 |
+ |
threadUnexpectedException(t); |
414 |
+ |
} |
415 |
+ |
} |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
abstract class CheckedInterruptedRunnable implements Runnable { |
419 |
+ |
abstract void realRun() throws Throwable; |
420 |
+ |
|
421 |
+ |
public final void run() { |
422 |
+ |
try { |
423 |
+ |
realRun(); |
424 |
+ |
threadShouldThrow(); |
425 |
+ |
} catch (InterruptedException success) { |
426 |
+ |
} catch (Throwable t) { |
427 |
+ |
threadUnexpectedException(t); |
428 |
+ |
} |
429 |
+ |
} |
430 |
+ |
} |
431 |
+ |
|
432 |
+ |
abstract class CheckedCallable<T> implements Callable<T> { |
433 |
+ |
abstract T realCall() throws Throwable; |
434 |
+ |
|
435 |
+ |
public final T call() { |
436 |
+ |
try { |
437 |
+ |
return realCall(); |
438 |
+ |
} catch (Throwable t) { |
439 |
+ |
threadUnexpectedException(t); |
440 |
+ |
return null; |
441 |
+ |
} |
442 |
+ |
} |
443 |
+ |
} |
444 |
+ |
|
445 |
|
static class NoOpRunnable implements Runnable { |
446 |
|
public void run() {} |
447 |
|
} |
464 |
|
public Integer call() { return one; } |
465 |
|
} |
466 |
|
|
467 |
< |
class ShortRunnable implements Runnable { |
468 |
< |
public void run() { |
469 |
< |
try { |
415 |
< |
Thread.sleep(SHORT_DELAY_MS); |
416 |
< |
} |
417 |
< |
catch(Exception e) { |
418 |
< |
threadUnexpectedException(e); |
419 |
< |
} |
467 |
> |
class ShortRunnable extends CheckedRunnable { |
468 |
> |
void realRun() throws Throwable { |
469 |
> |
Thread.sleep(SHORT_DELAY_MS); |
470 |
|
} |
471 |
|
} |
472 |
|
|
473 |
< |
class ShortInterruptedRunnable implements Runnable { |
474 |
< |
public void run() { |
475 |
< |
try { |
426 |
< |
Thread.sleep(SHORT_DELAY_MS); |
427 |
< |
threadShouldThrow(); |
428 |
< |
} |
429 |
< |
catch(InterruptedException success) { |
430 |
< |
} |
473 |
> |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
474 |
> |
void realRun() throws InterruptedException { |
475 |
> |
Thread.sleep(SHORT_DELAY_MS); |
476 |
|
} |
477 |
|
} |
478 |
|
|
479 |
< |
class SmallRunnable implements Runnable { |
480 |
< |
public void run() { |
481 |
< |
try { |
437 |
< |
Thread.sleep(SMALL_DELAY_MS); |
438 |
< |
} |
439 |
< |
catch(Exception e) { |
440 |
< |
threadUnexpectedException(e); |
441 |
< |
} |
479 |
> |
class SmallRunnable extends CheckedRunnable { |
480 |
> |
void realRun() throws Throwable { |
481 |
> |
Thread.sleep(SMALL_DELAY_MS); |
482 |
|
} |
483 |
|
} |
484 |
|
|
485 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
486 |
< |
public void run() { |
485 |
> |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
486 |
> |
void realRun() { |
487 |
|
try { |
488 |
|
Thread.sleep(SMALL_DELAY_MS); |
489 |
|
} |
490 |
< |
catch(Exception e) { |
490 |
> |
catch (InterruptedException ok) { |
491 |
|
} |
492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
< |
class SmallCallable implements Callable { |
496 |
< |
public Object call() { |
497 |
< |
try { |
458 |
< |
Thread.sleep(SMALL_DELAY_MS); |
459 |
< |
} |
460 |
< |
catch(Exception e) { |
461 |
< |
threadUnexpectedException(e); |
462 |
< |
} |
495 |
> |
class SmallCallable extends CheckedCallable { |
496 |
> |
Object realCall() throws Throwable { |
497 |
> |
Thread.sleep(SMALL_DELAY_MS); |
498 |
|
return Boolean.TRUE; |
499 |
|
} |
500 |
|
} |
501 |
|
|
502 |
< |
class SmallInterruptedRunnable implements Runnable { |
503 |
< |
public void run() { |
504 |
< |
try { |
470 |
< |
Thread.sleep(SMALL_DELAY_MS); |
471 |
< |
threadShouldThrow(); |
472 |
< |
} |
473 |
< |
catch(InterruptedException success) { |
474 |
< |
} |
502 |
> |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
503 |
> |
void realRun() throws InterruptedException { |
504 |
> |
Thread.sleep(SMALL_DELAY_MS); |
505 |
|
} |
506 |
|
} |
507 |
|
|
508 |
< |
|
509 |
< |
class MediumRunnable implements Runnable { |
510 |
< |
public void run() { |
481 |
< |
try { |
482 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
483 |
< |
} |
484 |
< |
catch(Exception e) { |
485 |
< |
threadUnexpectedException(e); |
486 |
< |
} |
508 |
> |
class MediumRunnable extends CheckedRunnable { |
509 |
> |
void realRun() throws Throwable { |
510 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
511 |
|
} |
512 |
|
} |
513 |
|
|
514 |
< |
class MediumInterruptedRunnable implements Runnable { |
515 |
< |
public void run() { |
516 |
< |
try { |
493 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
494 |
< |
threadShouldThrow(); |
495 |
< |
} |
496 |
< |
catch(InterruptedException success) { |
497 |
< |
} |
514 |
> |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
515 |
> |
void realRun() throws InterruptedException { |
516 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
517 |
|
} |
518 |
|
} |
519 |
|
|
520 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
521 |
< |
public void run() { |
520 |
> |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
521 |
> |
void realRun() { |
522 |
|
try { |
523 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
524 |
|
} |
525 |
< |
catch(InterruptedException success) { |
525 |
> |
catch (InterruptedException ok) { |
526 |
|
} |
527 |
|
} |
528 |
|
} |
529 |
|
|
530 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
531 |
< |
public void run() { |
530 |
> |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
531 |
> |
void realRun() { |
532 |
|
try { |
533 |
|
Thread.sleep(LONG_DELAY_MS); |
534 |
|
} |
535 |
< |
catch(InterruptedException success) { |
535 |
> |
catch (InterruptedException ok) { |
536 |
|
} |
537 |
|
} |
538 |
|
} |
540 |
|
/** |
541 |
|
* For use as ThreadFactory in constructors |
542 |
|
*/ |
543 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
544 |
< |
public Thread newThread(Runnable r){ |
543 |
> |
static class SimpleThreadFactory implements ThreadFactory { |
544 |
> |
public Thread newThread(Runnable r) { |
545 |
|
return new Thread(r); |
546 |
|
} |
547 |
|
} |
552 |
|
try { |
553 |
|
Thread.sleep(SMALL_DELAY_MS); |
554 |
|
done = true; |
555 |
< |
} catch(Exception e){ |
555 |
> |
} catch (Exception e) { |
556 |
|
} |
557 |
|
} |
558 |
|
} |
563 |
|
try { |
564 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
565 |
|
done = true; |
566 |
< |
} catch(Exception e){ |
566 |
> |
} catch (Exception e) { |
567 |
|
} |
568 |
|
} |
569 |
|
} |
574 |
|
try { |
575 |
|
Thread.sleep(LONG_DELAY_MS); |
576 |
|
done = true; |
577 |
< |
} catch(Exception e){ |
577 |
> |
} catch (Exception e) { |
578 |
|
} |
579 |
|
} |
580 |
|
} |
592 |
|
try { |
593 |
|
Thread.sleep(SMALL_DELAY_MS); |
594 |
|
done = true; |
595 |
< |
} catch(Exception e){ |
595 |
> |
} catch (Exception e) { |
596 |
|
} |
597 |
|
return Boolean.TRUE; |
598 |
|
} |
602 |
|
/** |
603 |
|
* For use as RejectedExecutionHandler in constructors |
604 |
|
*/ |
605 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
606 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
605 |
> |
static class NoOpREHandler implements RejectedExecutionHandler { |
606 |
> |
public void rejectedExecution(Runnable r, |
607 |
> |
ThreadPoolExecutor executor) {} |
608 |
|
} |
609 |
|
|
590 |
– |
|
610 |
|
} |