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)); |
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)); |
301 |
|
fail("Unexpected exception"); |
302 |
|
} |
303 |
|
|
304 |
+ |
/** |
305 |
+ |
* threadFail with message "Unexpected exception", with argument |
306 |
+ |
*/ |
307 |
+ |
public void threadUnexpectedException(Throwable ex) { |
308 |
+ |
threadFailed = true; |
309 |
+ |
ex.printStackTrace(); |
310 |
+ |
fail("Unexpected exception: " + ex); |
311 |
+ |
} |
312 |
|
|
313 |
|
/** |
314 |
|
* Wait out termination of a thread pool or fail doing so |
316 |
|
public void joinPool(ExecutorService exec) { |
317 |
|
try { |
318 |
|
exec.shutdown(); |
319 |
< |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
320 |
< |
} catch(SecurityException ok) { |
319 |
> |
assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
320 |
> |
} catch (SecurityException ok) { |
321 |
|
// Allowed in case test doesn't have privs |
322 |
< |
} catch(InterruptedException ie) { |
322 |
> |
} catch (InterruptedException ie) { |
323 |
|
fail("Unexpected exception"); |
324 |
|
} |
325 |
|
} |
339 |
|
fail("Unexpected exception"); |
340 |
|
} |
341 |
|
|
342 |
+ |
/** |
343 |
+ |
* fail with message "Unexpected exception", with argument |
344 |
+ |
*/ |
345 |
+ |
public void unexpectedException(Throwable ex) { |
346 |
+ |
ex.printStackTrace(); |
347 |
+ |
fail("Unexpected exception: " + ex); |
348 |
+ |
} |
349 |
+ |
|
350 |
|
|
351 |
|
/** |
352 |
|
* The number of elements to place in collections, arrays, etc. |
370 |
|
static final Integer m3 = new Integer(-3); |
371 |
|
static final Integer m4 = new Integer(-4); |
372 |
|
static final Integer m5 = new Integer(-5); |
373 |
+ |
static final Integer m6 = new Integer(-6); |
374 |
|
static final Integer m10 = new Integer(-10); |
375 |
|
|
376 |
|
|
395 |
|
public void refresh() {} |
396 |
|
} |
397 |
|
|
398 |
+ |
/** |
399 |
+ |
* Sleep until the timeout has elapsed, or interrupted. |
400 |
+ |
* Does <em>NOT</em> throw InterruptedException. |
401 |
+ |
*/ |
402 |
+ |
void sleepTillInterrupted(long timeoutMillis) { |
403 |
+ |
try { |
404 |
+ |
Thread.sleep(timeoutMillis); |
405 |
+ |
} catch (InterruptedException wakeup) { |
406 |
+ |
} |
407 |
+ |
} |
408 |
+ |
|
409 |
+ |
/** |
410 |
+ |
* Returns a new started Thread running the given runnable. |
411 |
+ |
*/ |
412 |
+ |
Thread newStartedThread(Runnable runnable) { |
413 |
+ |
Thread t = new Thread(runnable); |
414 |
+ |
t.start(); |
415 |
+ |
return t; |
416 |
+ |
} |
417 |
|
|
418 |
|
// Some convenient Runnable classes |
419 |
|
|
420 |
+ |
abstract class CheckedRunnable implements Runnable { |
421 |
+ |
abstract void realRun() throws Throwable; |
422 |
+ |
|
423 |
+ |
public final void run() { |
424 |
+ |
try { |
425 |
+ |
realRun(); |
426 |
+ |
} catch (Throwable t) { |
427 |
+ |
threadUnexpectedException(t); |
428 |
+ |
} |
429 |
+ |
} |
430 |
+ |
} |
431 |
+ |
|
432 |
+ |
abstract class CheckedInterruptedRunnable implements Runnable { |
433 |
+ |
abstract void realRun() throws Throwable; |
434 |
+ |
|
435 |
+ |
public final void run() { |
436 |
+ |
try { |
437 |
+ |
realRun(); |
438 |
+ |
threadShouldThrow(); |
439 |
+ |
} catch (InterruptedException success) { |
440 |
+ |
} catch (Throwable t) { |
441 |
+ |
threadUnexpectedException(t); |
442 |
+ |
} |
443 |
+ |
} |
444 |
+ |
} |
445 |
+ |
|
446 |
+ |
abstract class CheckedCallable<T> implements Callable<T> { |
447 |
+ |
abstract T realCall() throws Throwable; |
448 |
+ |
|
449 |
+ |
public final T call() { |
450 |
+ |
try { |
451 |
+ |
return realCall(); |
452 |
+ |
} catch (Throwable t) { |
453 |
+ |
threadUnexpectedException(t); |
454 |
+ |
return null; |
455 |
+ |
} |
456 |
+ |
} |
457 |
+ |
} |
458 |
+ |
|
459 |
|
static class NoOpRunnable implements Runnable { |
460 |
|
public void run() {} |
461 |
|
} |
478 |
|
public Integer call() { return one; } |
479 |
|
} |
480 |
|
|
481 |
< |
class ShortRunnable implements Runnable { |
482 |
< |
public void run() { |
483 |
< |
try { |
398 |
< |
Thread.sleep(SHORT_DELAY_MS); |
399 |
< |
} |
400 |
< |
catch(Exception e) { |
401 |
< |
threadUnexpectedException(); |
402 |
< |
} |
481 |
> |
class ShortRunnable extends CheckedRunnable { |
482 |
> |
void realRun() throws Throwable { |
483 |
> |
Thread.sleep(SHORT_DELAY_MS); |
484 |
|
} |
485 |
|
} |
486 |
|
|
487 |
< |
class ShortInterruptedRunnable implements Runnable { |
488 |
< |
public void run() { |
489 |
< |
try { |
409 |
< |
Thread.sleep(SHORT_DELAY_MS); |
410 |
< |
threadShouldThrow(); |
411 |
< |
} |
412 |
< |
catch(InterruptedException success) { |
413 |
< |
} |
487 |
> |
class ShortInterruptedRunnable extends CheckedInterruptedRunnable { |
488 |
> |
void realRun() throws InterruptedException { |
489 |
> |
Thread.sleep(SHORT_DELAY_MS); |
490 |
|
} |
491 |
|
} |
492 |
|
|
493 |
< |
class SmallRunnable implements Runnable { |
494 |
< |
public void run() { |
495 |
< |
try { |
420 |
< |
Thread.sleep(SMALL_DELAY_MS); |
421 |
< |
} |
422 |
< |
catch(Exception e) { |
423 |
< |
threadUnexpectedException(); |
424 |
< |
} |
493 |
> |
class SmallRunnable extends CheckedRunnable { |
494 |
> |
void realRun() throws Throwable { |
495 |
> |
Thread.sleep(SMALL_DELAY_MS); |
496 |
|
} |
497 |
|
} |
498 |
|
|
499 |
< |
class SmallPossiblyInterruptedRunnable implements Runnable { |
500 |
< |
public void run() { |
499 |
> |
class SmallPossiblyInterruptedRunnable extends CheckedRunnable { |
500 |
> |
void realRun() { |
501 |
|
try { |
502 |
|
Thread.sleep(SMALL_DELAY_MS); |
503 |
|
} |
504 |
< |
catch(Exception e) { |
504 |
> |
catch (InterruptedException ok) { |
505 |
|
} |
506 |
|
} |
507 |
|
} |
508 |
|
|
509 |
< |
class SmallCallable implements Callable { |
510 |
< |
public Object call() { |
511 |
< |
try { |
441 |
< |
Thread.sleep(SMALL_DELAY_MS); |
442 |
< |
} |
443 |
< |
catch(Exception e) { |
444 |
< |
threadUnexpectedException(); |
445 |
< |
} |
509 |
> |
class SmallCallable extends CheckedCallable { |
510 |
> |
Object realCall() throws Throwable { |
511 |
> |
Thread.sleep(SMALL_DELAY_MS); |
512 |
|
return Boolean.TRUE; |
513 |
|
} |
514 |
|
} |
515 |
|
|
516 |
< |
class SmallInterruptedRunnable implements Runnable { |
517 |
< |
public void run() { |
518 |
< |
try { |
453 |
< |
Thread.sleep(SMALL_DELAY_MS); |
454 |
< |
threadShouldThrow(); |
455 |
< |
} |
456 |
< |
catch(InterruptedException success) { |
457 |
< |
} |
516 |
> |
class SmallInterruptedRunnable extends CheckedInterruptedRunnable { |
517 |
> |
void realRun() throws InterruptedException { |
518 |
> |
Thread.sleep(SMALL_DELAY_MS); |
519 |
|
} |
520 |
|
} |
521 |
|
|
522 |
< |
|
523 |
< |
class MediumRunnable implements Runnable { |
524 |
< |
public void run() { |
464 |
< |
try { |
465 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
466 |
< |
} |
467 |
< |
catch(Exception e) { |
468 |
< |
threadUnexpectedException(); |
469 |
< |
} |
522 |
> |
class MediumRunnable extends CheckedRunnable { |
523 |
> |
void realRun() throws Throwable { |
524 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
525 |
|
} |
526 |
|
} |
527 |
|
|
528 |
< |
class MediumInterruptedRunnable implements Runnable { |
529 |
< |
public void run() { |
530 |
< |
try { |
476 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
477 |
< |
threadShouldThrow(); |
478 |
< |
} |
479 |
< |
catch(InterruptedException success) { |
480 |
< |
} |
528 |
> |
class MediumInterruptedRunnable extends CheckedInterruptedRunnable { |
529 |
> |
void realRun() throws InterruptedException { |
530 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
531 |
|
} |
532 |
|
} |
533 |
|
|
534 |
< |
class MediumPossiblyInterruptedRunnable implements Runnable { |
535 |
< |
public void run() { |
534 |
> |
class MediumPossiblyInterruptedRunnable extends CheckedRunnable { |
535 |
> |
void realRun() { |
536 |
|
try { |
537 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
538 |
|
} |
539 |
< |
catch(InterruptedException success) { |
539 |
> |
catch (InterruptedException ok) { |
540 |
|
} |
541 |
|
} |
542 |
|
} |
543 |
|
|
544 |
< |
class LongPossiblyInterruptedRunnable implements Runnable { |
545 |
< |
public void run() { |
544 |
> |
class LongPossiblyInterruptedRunnable extends CheckedRunnable { |
545 |
> |
void realRun() { |
546 |
|
try { |
547 |
|
Thread.sleep(LONG_DELAY_MS); |
548 |
|
} |
549 |
< |
catch(InterruptedException success) { |
549 |
> |
catch (InterruptedException ok) { |
550 |
|
} |
551 |
|
} |
552 |
|
} |
554 |
|
/** |
555 |
|
* For use as ThreadFactory in constructors |
556 |
|
*/ |
557 |
< |
static class SimpleThreadFactory implements ThreadFactory{ |
558 |
< |
public Thread newThread(Runnable r){ |
557 |
> |
static class SimpleThreadFactory implements ThreadFactory { |
558 |
> |
public Thread newThread(Runnable r) { |
559 |
|
return new Thread(r); |
560 |
|
} |
561 |
|
} |
566 |
|
try { |
567 |
|
Thread.sleep(SMALL_DELAY_MS); |
568 |
|
done = true; |
569 |
< |
} catch(Exception e){ |
569 |
> |
} catch (InterruptedException ok) { |
570 |
|
} |
571 |
|
} |
572 |
|
} |
577 |
|
try { |
578 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
579 |
|
done = true; |
580 |
< |
} catch(Exception e){ |
580 |
> |
} catch (InterruptedException ok) { |
581 |
|
} |
582 |
|
} |
583 |
|
} |
588 |
|
try { |
589 |
|
Thread.sleep(LONG_DELAY_MS); |
590 |
|
done = true; |
591 |
< |
} catch(Exception e){ |
591 |
> |
} catch (InterruptedException ok) { |
592 |
|
} |
593 |
|
} |
594 |
|
} |
606 |
|
try { |
607 |
|
Thread.sleep(SMALL_DELAY_MS); |
608 |
|
done = true; |
609 |
< |
} catch(Exception e){ |
609 |
> |
} catch (InterruptedException ok) { |
610 |
|
} |
611 |
|
return Boolean.TRUE; |
612 |
|
} |
616 |
|
/** |
617 |
|
* For use as RejectedExecutionHandler in constructors |
618 |
|
*/ |
619 |
< |
static class NoOpREHandler implements RejectedExecutionHandler{ |
620 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
619 |
> |
static class NoOpREHandler implements RejectedExecutionHandler { |
620 |
> |
public void rejectedExecution(Runnable r, |
621 |
> |
ThreadPoolExecutor executor) {} |
622 |
|
} |
623 |
|
|
573 |
– |
|
624 |
|
} |