14 |
|
|
15 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
< |
return new TestSuite(ThreadPoolExecutorTest.class); |
20 |
> |
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
21 |
|
} |
22 |
|
|
23 |
|
static class CustomTask<V> implements RunnableFuture<V> { |
29 |
|
V result; |
30 |
|
Thread thread; |
31 |
|
Exception exception; |
32 |
< |
CustomTask(Callable<V> c) { callable = c; } |
33 |
< |
CustomTask(final Runnable r, final V res) { callable = new Callable<V>() { |
32 |
> |
CustomTask(Callable<V> c) { |
33 |
> |
if (c == null) throw new NullPointerException(); |
34 |
> |
callable = c; |
35 |
> |
} |
36 |
> |
CustomTask(final Runnable r, final V res) { |
37 |
> |
if (r == null) throw new NullPointerException(); |
38 |
> |
callable = new Callable<V>() { |
39 |
|
public V call() throws Exception { r.run(); return res; }}; |
40 |
|
} |
41 |
|
public boolean isDone() { |
193 |
|
/** |
194 |
|
* execute successfully executes a runnable |
195 |
|
*/ |
196 |
< |
public void testExecute() { |
196 |
> |
public void testExecute() throws InterruptedException { |
197 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
198 |
|
try { |
199 |
< |
p1.execute(new Runnable() { |
195 |
< |
public void run() { |
196 |
< |
try { |
197 |
< |
Thread.sleep(SHORT_DELAY_MS); |
198 |
< |
} catch (InterruptedException e) { |
199 |
< |
threadUnexpectedException(); |
200 |
< |
} |
201 |
< |
} |
202 |
< |
}); |
199 |
> |
p1.execute(new ShortRunnable()); |
200 |
|
Thread.sleep(SMALL_DELAY_MS); |
201 |
< |
} catch (InterruptedException e) { |
202 |
< |
unexpectedException(); |
201 |
> |
} finally { |
202 |
> |
joinPool(p1); |
203 |
|
} |
207 |
– |
joinPool(p1); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
|
* getActiveCount increases but doesn't overestimate, when a |
208 |
|
* thread becomes active |
209 |
|
*/ |
210 |
< |
public void testGetActiveCount() { |
210 |
> |
public void testGetActiveCount() throws InterruptedException { |
211 |
|
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
212 |
|
assertEquals(0, p2.getActiveCount()); |
213 |
|
p2.execute(new MediumRunnable()); |
214 |
< |
try { |
219 |
< |
Thread.sleep(SHORT_DELAY_MS); |
220 |
< |
} catch (Exception e) { |
221 |
< |
unexpectedException(); |
222 |
< |
} |
214 |
> |
Thread.sleep(SHORT_DELAY_MS); |
215 |
|
assertEquals(1, p2.getActiveCount()); |
216 |
|
joinPool(p2); |
217 |
|
} |
248 |
|
* getCompletedTaskCount increases, but doesn't overestimate, |
249 |
|
* when tasks complete |
250 |
|
*/ |
251 |
< |
public void testGetCompletedTaskCount() { |
251 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
252 |
|
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
253 |
|
assertEquals(0, p2.getCompletedTaskCount()); |
254 |
|
p2.execute(new ShortRunnable()); |
255 |
< |
try { |
264 |
< |
Thread.sleep(SMALL_DELAY_MS); |
265 |
< |
} catch (Exception e) { |
266 |
< |
unexpectedException(); |
267 |
< |
} |
255 |
> |
Thread.sleep(SMALL_DELAY_MS); |
256 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
257 |
|
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
258 |
|
joinPool(p2); |
355 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
356 |
|
* multiple threads active |
357 |
|
*/ |
358 |
< |
public void testGetLargestPoolSize() { |
358 |
> |
public void testGetLargestPoolSize() throws InterruptedException { |
359 |
|
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
360 |
< |
try { |
361 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
362 |
< |
p2.execute(new MediumRunnable()); |
363 |
< |
p2.execute(new MediumRunnable()); |
364 |
< |
Thread.sleep(SHORT_DELAY_MS); |
377 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
378 |
< |
} catch (Exception e) { |
379 |
< |
unexpectedException(); |
380 |
< |
} |
360 |
> |
assertEquals(0, p2.getLargestPoolSize()); |
361 |
> |
p2.execute(new MediumRunnable()); |
362 |
> |
p2.execute(new MediumRunnable()); |
363 |
> |
Thread.sleep(SHORT_DELAY_MS); |
364 |
> |
assertEquals(2, p2.getLargestPoolSize()); |
365 |
|
joinPool(p2); |
366 |
|
} |
367 |
|
|
390 |
|
/** |
391 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
392 |
|
*/ |
393 |
< |
public void testGetTaskCount() { |
393 |
> |
public void testGetTaskCount() throws InterruptedException { |
394 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
395 |
< |
try { |
396 |
< |
assertEquals(0, p1.getTaskCount()); |
397 |
< |
p1.execute(new MediumRunnable()); |
398 |
< |
Thread.sleep(SHORT_DELAY_MS); |
415 |
< |
assertEquals(1, p1.getTaskCount()); |
416 |
< |
} catch (Exception e) { |
417 |
< |
unexpectedException(); |
418 |
< |
} |
395 |
> |
assertEquals(0, p1.getTaskCount()); |
396 |
> |
p1.execute(new MediumRunnable()); |
397 |
> |
Thread.sleep(SHORT_DELAY_MS); |
398 |
> |
assertEquals(1, p1.getTaskCount()); |
399 |
|
joinPool(p1); |
400 |
|
} |
401 |
|
|
415 |
|
/** |
416 |
|
* isTerminated is false before termination, true after |
417 |
|
*/ |
418 |
< |
public void testIsTerminated() { |
418 |
> |
public void testIsTerminated() throws InterruptedException { |
419 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
420 |
|
assertFalse(p1.isTerminated()); |
421 |
|
try { |
423 |
|
} finally { |
424 |
|
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
425 |
|
} |
426 |
< |
try { |
427 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
448 |
< |
assertTrue(p1.isTerminated()); |
449 |
< |
} catch (Exception e) { |
450 |
< |
unexpectedException(); |
451 |
< |
} |
426 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
427 |
> |
assertTrue(p1.isTerminated()); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
|
* isTerminating is not true when running or when terminated |
432 |
|
*/ |
433 |
< |
public void testIsTerminating() { |
433 |
> |
public void testIsTerminating() throws InterruptedException { |
434 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
435 |
|
assertFalse(p1.isTerminating()); |
436 |
|
try { |
439 |
|
} finally { |
440 |
|
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
441 |
|
} |
442 |
< |
try { |
443 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
444 |
< |
assertTrue(p1.isTerminated()); |
469 |
< |
assertFalse(p1.isTerminating()); |
470 |
< |
} catch (Exception e) { |
471 |
< |
unexpectedException(); |
472 |
< |
} |
442 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
443 |
> |
assertTrue(p1.isTerminated()); |
444 |
> |
assertFalse(p1.isTerminating()); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
|
* getQueue returns the work queue, which contains queued tasks |
449 |
|
*/ |
450 |
< |
public void testGetQueue() { |
450 |
> |
public void testGetQueue() throws InterruptedException { |
451 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
452 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
453 |
|
FutureTask[] tasks = new FutureTask[5]; |
464 |
|
for (int i = 1; i < 5; ++i) |
465 |
|
tasks[i].cancel(true); |
466 |
|
p1.shutdownNow(); |
495 |
– |
} catch (Exception e) { |
496 |
– |
unexpectedException(); |
467 |
|
} finally { |
468 |
|
joinPool(p1); |
469 |
|
} |
472 |
|
/** |
473 |
|
* remove(task) removes queued task, and fails to remove active task |
474 |
|
*/ |
475 |
< |
public void testRemove() { |
475 |
> |
public void testRemove() throws InterruptedException { |
476 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
477 |
|
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
478 |
|
FutureTask[] tasks = new FutureTask[5]; |
491 |
|
assertTrue(q.contains(tasks[3])); |
492 |
|
assertTrue(p1.remove(tasks[3])); |
493 |
|
assertFalse(q.contains(tasks[3])); |
524 |
– |
} catch (Exception e) { |
525 |
– |
unexpectedException(); |
494 |
|
} finally { |
495 |
|
joinPool(p1); |
496 |
|
} |
544 |
|
try { |
545 |
|
new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
546 |
|
shouldThrow(); |
547 |
< |
} |
580 |
< |
catch (IllegalArgumentException success) {} |
547 |
> |
} catch (IllegalArgumentException success) {} |
548 |
|
} |
549 |
|
|
550 |
|
/** |
554 |
|
try { |
555 |
|
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
556 |
|
shouldThrow(); |
557 |
< |
} |
591 |
< |
catch (IllegalArgumentException success) {} |
557 |
> |
} catch (IllegalArgumentException success) {} |
558 |
|
} |
559 |
|
|
560 |
|
/** |
564 |
|
try { |
565 |
|
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
566 |
|
shouldThrow(); |
567 |
< |
} |
602 |
< |
catch (IllegalArgumentException success) {} |
567 |
> |
} catch (IllegalArgumentException success) {} |
568 |
|
} |
569 |
|
|
570 |
|
/** |
574 |
|
try { |
575 |
|
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
576 |
|
shouldThrow(); |
577 |
< |
} |
613 |
< |
catch (IllegalArgumentException success) {} |
577 |
> |
} catch (IllegalArgumentException success) {} |
578 |
|
} |
579 |
|
|
580 |
|
/** |
584 |
|
try { |
585 |
|
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
586 |
|
shouldThrow(); |
587 |
< |
} |
624 |
< |
catch (IllegalArgumentException success) {} |
587 |
> |
} catch (IllegalArgumentException success) {} |
588 |
|
} |
589 |
|
|
590 |
|
/** |
594 |
|
try { |
595 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); |
596 |
|
shouldThrow(); |
597 |
< |
} |
635 |
< |
catch (NullPointerException success) {} |
597 |
> |
} catch (NullPointerException success) {} |
598 |
|
} |
599 |
|
|
600 |
|
|
616 |
|
try { |
617 |
|
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
618 |
|
shouldThrow(); |
619 |
< |
} |
658 |
< |
catch (IllegalArgumentException success) {} |
619 |
> |
} catch (IllegalArgumentException success) {} |
620 |
|
} |
621 |
|
|
622 |
|
/** |
626 |
|
try { |
627 |
|
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
628 |
|
shouldThrow(); |
629 |
< |
} |
669 |
< |
catch (IllegalArgumentException success) {} |
629 |
> |
} catch (IllegalArgumentException success) {} |
630 |
|
} |
631 |
|
|
632 |
|
/** |
636 |
|
try { |
637 |
|
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
638 |
|
shouldThrow(); |
639 |
< |
} |
680 |
< |
catch (IllegalArgumentException success) {} |
639 |
> |
} catch (IllegalArgumentException success) {} |
640 |
|
} |
641 |
|
|
642 |
|
/** |
646 |
|
try { |
647 |
|
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
648 |
|
shouldThrow(); |
649 |
< |
} |
691 |
< |
catch (IllegalArgumentException success) {} |
649 |
> |
} catch (IllegalArgumentException success) {} |
650 |
|
} |
651 |
|
|
652 |
|
/** |
656 |
|
try { |
657 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
658 |
|
shouldThrow(); |
659 |
< |
} |
702 |
< |
catch (NullPointerException success) {} |
659 |
> |
} catch (NullPointerException success) {} |
660 |
|
} |
661 |
|
|
662 |
|
/** |
667 |
|
ThreadFactory f = null; |
668 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
669 |
|
shouldThrow(); |
670 |
< |
} |
714 |
< |
catch (NullPointerException success) {} |
670 |
> |
} catch (NullPointerException success) {} |
671 |
|
} |
672 |
|
|
673 |
|
|
678 |
|
try { |
679 |
|
new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
680 |
|
shouldThrow(); |
681 |
< |
} |
726 |
< |
catch (IllegalArgumentException success) {} |
681 |
> |
} catch (IllegalArgumentException success) {} |
682 |
|
} |
683 |
|
|
684 |
|
/** |
688 |
|
try { |
689 |
|
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
690 |
|
shouldThrow(); |
691 |
< |
} |
737 |
< |
catch (IllegalArgumentException success) {} |
691 |
> |
} catch (IllegalArgumentException success) {} |
692 |
|
} |
693 |
|
|
694 |
|
/** |
698 |
|
try { |
699 |
|
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
700 |
|
shouldThrow(); |
701 |
< |
} |
748 |
< |
catch (IllegalArgumentException success) {} |
701 |
> |
} catch (IllegalArgumentException success) {} |
702 |
|
} |
703 |
|
|
704 |
|
/** |
708 |
|
try { |
709 |
|
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
710 |
|
shouldThrow(); |
711 |
< |
} |
759 |
< |
catch (IllegalArgumentException success) {} |
711 |
> |
} catch (IllegalArgumentException success) {} |
712 |
|
} |
713 |
|
|
714 |
|
/** |
718 |
|
try { |
719 |
|
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
720 |
|
shouldThrow(); |
721 |
< |
} |
770 |
< |
catch (IllegalArgumentException success) {} |
721 |
> |
} catch (IllegalArgumentException success) {} |
722 |
|
} |
723 |
|
|
724 |
|
/** |
728 |
|
try { |
729 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
730 |
|
shouldThrow(); |
731 |
< |
} |
781 |
< |
catch (NullPointerException success) {} |
731 |
> |
} catch (NullPointerException success) {} |
732 |
|
} |
733 |
|
|
734 |
|
/** |
739 |
|
RejectedExecutionHandler r = null; |
740 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
741 |
|
shouldThrow(); |
742 |
< |
} |
793 |
< |
catch (NullPointerException success) {} |
742 |
> |
} catch (NullPointerException success) {} |
743 |
|
} |
744 |
|
|
745 |
|
|
750 |
|
try { |
751 |
|
new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
752 |
|
shouldThrow(); |
753 |
< |
} |
805 |
< |
catch (IllegalArgumentException success) {} |
753 |
> |
} catch (IllegalArgumentException success) {} |
754 |
|
} |
755 |
|
|
756 |
|
/** |
760 |
|
try { |
761 |
|
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
762 |
|
shouldThrow(); |
763 |
< |
} |
816 |
< |
catch (IllegalArgumentException success) {} |
763 |
> |
} catch (IllegalArgumentException success) {} |
764 |
|
} |
765 |
|
|
766 |
|
/** |
770 |
|
try { |
771 |
|
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
772 |
|
shouldThrow(); |
773 |
< |
} |
827 |
< |
catch (IllegalArgumentException success) {} |
773 |
> |
} catch (IllegalArgumentException success) {} |
774 |
|
} |
775 |
|
|
776 |
|
/** |
780 |
|
try { |
781 |
|
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
782 |
|
shouldThrow(); |
783 |
< |
} |
838 |
< |
catch (IllegalArgumentException success) {} |
783 |
> |
} catch (IllegalArgumentException success) {} |
784 |
|
} |
785 |
|
|
786 |
|
/** |
790 |
|
try { |
791 |
|
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
792 |
|
shouldThrow(); |
793 |
< |
} |
849 |
< |
catch (IllegalArgumentException success) {} |
793 |
> |
} catch (IllegalArgumentException success) {} |
794 |
|
} |
795 |
|
|
796 |
|
/** |
800 |
|
try { |
801 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
802 |
|
shouldThrow(); |
803 |
< |
} |
860 |
< |
catch (NullPointerException success) {} |
803 |
> |
} catch (NullPointerException success) {} |
804 |
|
} |
805 |
|
|
806 |
|
/** |
811 |
|
RejectedExecutionHandler r = null; |
812 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
813 |
|
shouldThrow(); |
814 |
< |
} |
872 |
< |
catch (NullPointerException success) {} |
814 |
> |
} catch (NullPointerException success) {} |
815 |
|
} |
816 |
|
|
817 |
|
/** |
822 |
|
ThreadFactory f = null; |
823 |
|
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
824 |
|
shouldThrow(); |
825 |
< |
} |
884 |
< |
catch (NullPointerException successdn8) {} |
825 |
> |
} catch (NullPointerException success) {} |
826 |
|
} |
827 |
|
|
828 |
|
|
863 |
|
assertTrue(tasks[i].done); |
864 |
|
} |
865 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
925 |
– |
} catch (RejectedExecutionException ex) { |
926 |
– |
unexpectedException(); |
866 |
|
} finally { |
867 |
|
joinPool(p); |
868 |
|
} |
888 |
|
assertFalse(tasks[i].done); |
889 |
|
} |
890 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
952 |
– |
} catch (RejectedExecutionException ex) { |
953 |
– |
unexpectedException(); |
891 |
|
} finally { |
892 |
|
joinPool(p); |
893 |
|
} |
909 |
|
assertFalse(p.getQueue().contains(r2)); |
910 |
|
assertTrue(p.getQueue().contains(r3)); |
911 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
975 |
– |
} catch (RejectedExecutionException ex) { |
976 |
– |
unexpectedException(); |
912 |
|
} finally { |
913 |
|
joinPool(p); |
914 |
|
} |
941 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
942 |
|
p.execute(r); |
943 |
|
assertFalse(r.done); |
1009 |
– |
} catch (RejectedExecutionException success) { |
1010 |
– |
unexpectedException(); |
944 |
|
} finally { |
945 |
|
joinPool(p); |
946 |
|
} |
958 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
959 |
|
p.execute(r); |
960 |
|
assertFalse(r.done); |
1028 |
– |
} catch (RejectedExecutionException success) { |
1029 |
– |
unexpectedException(); |
961 |
|
} finally { |
962 |
|
joinPool(p); |
963 |
|
} |
976 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
977 |
|
p.execute(r); |
978 |
|
assertFalse(r.done); |
1048 |
– |
} catch (RejectedExecutionException success) { |
1049 |
– |
unexpectedException(); |
979 |
|
} finally { |
980 |
|
joinPool(p); |
981 |
|
} |
1000 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1001 |
|
*/ |
1002 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1003 |
< |
ThreadPoolExecutor tpe = null; |
1004 |
< |
try { |
1076 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1077 |
< |
} catch (Exception e) {} |
1003 |
> |
ThreadPoolExecutor tpe = |
1004 |
> |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1005 |
|
try { |
1006 |
|
tpe.setCorePoolSize(-1); |
1007 |
|
shouldThrow(); |
1084 |
|
/** |
1085 |
|
* beforeExecute and afterExecute are called when executing task |
1086 |
|
*/ |
1087 |
< |
public void testBeforeAfter() { |
1087 |
> |
public void testBeforeAfter() throws InterruptedException { |
1088 |
|
CustomTPE tpe = new CustomTPE(); |
1089 |
|
try { |
1090 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1094 |
|
assertTrue(tpe.beforeCalled); |
1095 |
|
assertTrue(tpe.afterCalled); |
1096 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1170 |
– |
} |
1171 |
– |
catch (Exception ex) { |
1172 |
– |
unexpectedException(); |
1097 |
|
} finally { |
1098 |
|
joinPool(tpe); |
1099 |
|
} |
1102 |
|
/** |
1103 |
|
* completed submit of callable returns result |
1104 |
|
*/ |
1105 |
< |
public void testSubmitCallable() { |
1105 |
> |
public void testSubmitCallable() throws Exception { |
1106 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1107 |
|
try { |
1108 |
|
Future<String> future = e.submit(new StringTask()); |
1109 |
|
String result = future.get(); |
1110 |
|
assertSame(TEST_STRING, result); |
1187 |
– |
} |
1188 |
– |
catch (ExecutionException ex) { |
1189 |
– |
unexpectedException(); |
1190 |
– |
} |
1191 |
– |
catch (InterruptedException ex) { |
1192 |
– |
unexpectedException(); |
1111 |
|
} finally { |
1112 |
|
joinPool(e); |
1113 |
|
} |
1116 |
|
/** |
1117 |
|
* completed submit of runnable returns successfully |
1118 |
|
*/ |
1119 |
< |
public void testSubmitRunnable() { |
1119 |
> |
public void testSubmitRunnable() throws Exception { |
1120 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1121 |
|
try { |
1122 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1123 |
|
future.get(); |
1124 |
|
assertTrue(future.isDone()); |
1207 |
– |
} |
1208 |
– |
catch (ExecutionException ex) { |
1209 |
– |
unexpectedException(); |
1210 |
– |
} |
1211 |
– |
catch (InterruptedException ex) { |
1212 |
– |
unexpectedException(); |
1125 |
|
} finally { |
1126 |
|
joinPool(e); |
1127 |
|
} |
1130 |
|
/** |
1131 |
|
* completed submit of (runnable, result) returns result |
1132 |
|
*/ |
1133 |
< |
public void testSubmitRunnable2() { |
1133 |
> |
public void testSubmitRunnable2() throws Exception { |
1134 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1135 |
|
try { |
1136 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1137 |
|
String result = future.get(); |
1138 |
|
assertSame(TEST_STRING, result); |
1227 |
– |
} |
1228 |
– |
catch (ExecutionException ex) { |
1229 |
– |
unexpectedException(); |
1230 |
– |
} |
1231 |
– |
catch (InterruptedException ex) { |
1232 |
– |
unexpectedException(); |
1139 |
|
} finally { |
1140 |
|
joinPool(e); |
1141 |
|
} |
1142 |
|
} |
1143 |
|
|
1144 |
|
|
1239 |
– |
|
1240 |
– |
|
1241 |
– |
|
1145 |
|
/** |
1146 |
|
* invokeAny(null) throws NPE |
1147 |
|
*/ |
1148 |
< |
public void testInvokeAny1() { |
1148 |
> |
public void testInvokeAny1() throws Exception { |
1149 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1150 |
|
try { |
1151 |
|
e.invokeAny(null); |
1152 |
+ |
shouldThrow(); |
1153 |
|
} catch (NullPointerException success) { |
1250 |
– |
} catch (Exception ex) { |
1251 |
– |
unexpectedException(); |
1154 |
|
} finally { |
1155 |
|
joinPool(e); |
1156 |
|
} |
1159 |
|
/** |
1160 |
|
* invokeAny(empty collection) throws IAE |
1161 |
|
*/ |
1162 |
< |
public void testInvokeAny2() { |
1162 |
> |
public void testInvokeAny2() throws Exception { |
1163 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1164 |
|
try { |
1165 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1166 |
+ |
shouldThrow(); |
1167 |
|
} catch (IllegalArgumentException success) { |
1265 |
– |
} catch (Exception ex) { |
1266 |
– |
unexpectedException(); |
1168 |
|
} finally { |
1169 |
|
joinPool(e); |
1170 |
|
} |
1173 |
|
/** |
1174 |
|
* invokeAny(c) throws NPE if c has null elements |
1175 |
|
*/ |
1176 |
< |
public void testInvokeAny3() { |
1176 |
> |
public void testInvokeAny3() throws Exception { |
1177 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
1178 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1179 |
|
try { |
1180 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1181 |
< |
l.add(new StringTask()); |
1181 |
> |
l.add(new Callable<String>() { |
1182 |
> |
public String call() { |
1183 |
> |
try { |
1184 |
> |
latch.await(); |
1185 |
> |
} catch (InterruptedException ok) {} |
1186 |
> |
return TEST_STRING; |
1187 |
> |
}}); |
1188 |
|
l.add(null); |
1189 |
|
e.invokeAny(l); |
1190 |
+ |
shouldThrow(); |
1191 |
|
} catch (NullPointerException success) { |
1283 |
– |
} catch (Exception ex) { |
1284 |
– |
unexpectedException(); |
1192 |
|
} finally { |
1193 |
+ |
latch.countDown(); |
1194 |
|
joinPool(e); |
1195 |
|
} |
1196 |
|
} |
1198 |
|
/** |
1199 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1200 |
|
*/ |
1201 |
< |
public void testInvokeAny4() { |
1201 |
> |
public void testInvokeAny4() throws Exception { |
1202 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1203 |
|
try { |
1204 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1205 |
|
l.add(new NPETask()); |
1206 |
|
e.invokeAny(l); |
1207 |
+ |
shouldThrow(); |
1208 |
|
} catch (ExecutionException success) { |
1300 |
– |
} catch (Exception ex) { |
1301 |
– |
unexpectedException(); |
1209 |
|
} finally { |
1210 |
|
joinPool(e); |
1211 |
|
} |
1214 |
|
/** |
1215 |
|
* invokeAny(c) returns result of some task |
1216 |
|
*/ |
1217 |
< |
public void testInvokeAny5() { |
1217 |
> |
public void testInvokeAny5() throws Exception { |
1218 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1219 |
|
try { |
1220 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1222 |
|
l.add(new StringTask()); |
1223 |
|
String result = e.invokeAny(l); |
1224 |
|
assertSame(TEST_STRING, result); |
1318 |
– |
} catch (ExecutionException success) { |
1319 |
– |
} catch (Exception ex) { |
1320 |
– |
unexpectedException(); |
1225 |
|
} finally { |
1226 |
|
joinPool(e); |
1227 |
|
} |
1230 |
|
/** |
1231 |
|
* invokeAll(null) throws NPE |
1232 |
|
*/ |
1233 |
< |
public void testInvokeAll1() { |
1233 |
> |
public void testInvokeAll1() throws Exception { |
1234 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1235 |
|
try { |
1236 |
|
e.invokeAll(null); |
1237 |
+ |
shouldThrow(); |
1238 |
|
} catch (NullPointerException success) { |
1334 |
– |
} catch (Exception ex) { |
1335 |
– |
unexpectedException(); |
1239 |
|
} finally { |
1240 |
|
joinPool(e); |
1241 |
|
} |
1244 |
|
/** |
1245 |
|
* invokeAll(empty collection) returns empty collection |
1246 |
|
*/ |
1247 |
< |
public void testInvokeAll2() { |
1247 |
> |
public void testInvokeAll2() throws Exception { |
1248 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1249 |
|
try { |
1250 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1251 |
|
assertTrue(r.isEmpty()); |
1349 |
– |
} catch (Exception ex) { |
1350 |
– |
unexpectedException(); |
1252 |
|
} finally { |
1253 |
|
joinPool(e); |
1254 |
|
} |
1257 |
|
/** |
1258 |
|
* invokeAll(c) throws NPE if c has null elements |
1259 |
|
*/ |
1260 |
< |
public void testInvokeAll3() { |
1260 |
> |
public void testInvokeAll3() throws Exception { |
1261 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1262 |
|
try { |
1263 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1264 |
|
l.add(new StringTask()); |
1265 |
|
l.add(null); |
1266 |
|
e.invokeAll(l); |
1267 |
+ |
shouldThrow(); |
1268 |
|
} catch (NullPointerException success) { |
1367 |
– |
} catch (Exception ex) { |
1368 |
– |
unexpectedException(); |
1269 |
|
} finally { |
1270 |
|
joinPool(e); |
1271 |
|
} |
1274 |
|
/** |
1275 |
|
* get of element of invokeAll(c) throws exception on failed task |
1276 |
|
*/ |
1277 |
< |
public void testInvokeAll4() { |
1277 |
> |
public void testInvokeAll4() throws Exception { |
1278 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1279 |
|
try { |
1280 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1281 |
|
l.add(new NPETask()); |
1282 |
|
List<Future<String>> result = e.invokeAll(l); |
1283 |
|
assertEquals(1, result.size()); |
1284 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1285 |
< |
it.next().get(); |
1284 |
> |
for (Future<String> future : result) |
1285 |
> |
future.get(); |
1286 |
> |
shouldThrow(); |
1287 |
|
} catch (ExecutionException success) { |
1387 |
– |
} catch (Exception ex) { |
1388 |
– |
unexpectedException(); |
1288 |
|
} finally { |
1289 |
|
joinPool(e); |
1290 |
|
} |
1293 |
|
/** |
1294 |
|
* invokeAll(c) returns results of all completed tasks |
1295 |
|
*/ |
1296 |
< |
public void testInvokeAll5() { |
1296 |
> |
public void testInvokeAll5() throws Exception { |
1297 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1298 |
|
try { |
1299 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1301 |
|
l.add(new StringTask()); |
1302 |
|
List<Future<String>> result = e.invokeAll(l); |
1303 |
|
assertEquals(2, result.size()); |
1304 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1305 |
< |
assertSame(TEST_STRING, it.next().get()); |
1407 |
< |
} catch (ExecutionException success) { |
1408 |
< |
} catch (Exception ex) { |
1409 |
< |
unexpectedException(); |
1304 |
> |
for (Future<String> future : result) |
1305 |
> |
assertSame(TEST_STRING, future.get()); |
1306 |
|
} finally { |
1307 |
|
joinPool(e); |
1308 |
|
} |
1313 |
|
/** |
1314 |
|
* timed invokeAny(null) throws NPE |
1315 |
|
*/ |
1316 |
< |
public void testTimedInvokeAny1() { |
1316 |
> |
public void testTimedInvokeAny1() throws Exception { |
1317 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1318 |
|
try { |
1319 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1320 |
+ |
shouldThrow(); |
1321 |
|
} catch (NullPointerException success) { |
1425 |
– |
} catch (Exception ex) { |
1426 |
– |
unexpectedException(); |
1322 |
|
} finally { |
1323 |
|
joinPool(e); |
1324 |
|
} |
1327 |
|
/** |
1328 |
|
* timed invokeAny(,,null) throws NPE |
1329 |
|
*/ |
1330 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
1330 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1331 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1332 |
|
try { |
1333 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1334 |
|
l.add(new StringTask()); |
1335 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1336 |
+ |
shouldThrow(); |
1337 |
|
} catch (NullPointerException success) { |
1442 |
– |
} catch (Exception ex) { |
1443 |
– |
unexpectedException(); |
1338 |
|
} finally { |
1339 |
|
joinPool(e); |
1340 |
|
} |
1343 |
|
/** |
1344 |
|
* timed invokeAny(empty collection) throws IAE |
1345 |
|
*/ |
1346 |
< |
public void testTimedInvokeAny2() { |
1346 |
> |
public void testTimedInvokeAny2() throws Exception { |
1347 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1348 |
|
try { |
1349 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1350 |
+ |
shouldThrow(); |
1351 |
|
} catch (IllegalArgumentException success) { |
1457 |
– |
} catch (Exception ex) { |
1458 |
– |
unexpectedException(); |
1352 |
|
} finally { |
1353 |
|
joinPool(e); |
1354 |
|
} |
1357 |
|
/** |
1358 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1359 |
|
*/ |
1360 |
< |
public void testTimedInvokeAny3() { |
1360 |
> |
public void testTimedInvokeAny3() throws Exception { |
1361 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1362 |
|
try { |
1363 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1364 |
|
l.add(new StringTask()); |
1365 |
|
l.add(null); |
1366 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1367 |
+ |
shouldThrow(); |
1368 |
|
} catch (NullPointerException success) { |
1475 |
– |
} catch (Exception ex) { |
1476 |
– |
ex.printStackTrace(); |
1477 |
– |
unexpectedException(); |
1369 |
|
} finally { |
1370 |
|
joinPool(e); |
1371 |
|
} |
1374 |
|
/** |
1375 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1376 |
|
*/ |
1377 |
< |
public void testTimedInvokeAny4() { |
1377 |
> |
public void testTimedInvokeAny4() throws Exception { |
1378 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1379 |
|
try { |
1380 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1381 |
|
l.add(new NPETask()); |
1382 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1383 |
+ |
shouldThrow(); |
1384 |
|
} catch (ExecutionException success) { |
1493 |
– |
} catch (Exception ex) { |
1494 |
– |
unexpectedException(); |
1385 |
|
} finally { |
1386 |
|
joinPool(e); |
1387 |
|
} |
1390 |
|
/** |
1391 |
|
* timed invokeAny(c) returns result of some task |
1392 |
|
*/ |
1393 |
< |
public void testTimedInvokeAny5() { |
1393 |
> |
public void testTimedInvokeAny5() throws Exception { |
1394 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1395 |
|
try { |
1396 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1398 |
|
l.add(new StringTask()); |
1399 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1400 |
|
assertSame(TEST_STRING, result); |
1511 |
– |
} catch (ExecutionException success) { |
1512 |
– |
} catch (Exception ex) { |
1513 |
– |
unexpectedException(); |
1401 |
|
} finally { |
1402 |
|
joinPool(e); |
1403 |
|
} |
1406 |
|
/** |
1407 |
|
* timed invokeAll(null) throws NPE |
1408 |
|
*/ |
1409 |
< |
public void testTimedInvokeAll1() { |
1409 |
> |
public void testTimedInvokeAll1() throws Exception { |
1410 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1411 |
|
try { |
1412 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1413 |
+ |
shouldThrow(); |
1414 |
|
} catch (NullPointerException success) { |
1527 |
– |
} catch (Exception ex) { |
1528 |
– |
unexpectedException(); |
1415 |
|
} finally { |
1416 |
|
joinPool(e); |
1417 |
|
} |
1420 |
|
/** |
1421 |
|
* timed invokeAll(,,null) throws NPE |
1422 |
|
*/ |
1423 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
1423 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1424 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1425 |
|
try { |
1426 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1427 |
|
l.add(new StringTask()); |
1428 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1429 |
+ |
shouldThrow(); |
1430 |
|
} catch (NullPointerException success) { |
1544 |
– |
} catch (Exception ex) { |
1545 |
– |
unexpectedException(); |
1431 |
|
} finally { |
1432 |
|
joinPool(e); |
1433 |
|
} |
1436 |
|
/** |
1437 |
|
* timed invokeAll(empty collection) returns empty collection |
1438 |
|
*/ |
1439 |
< |
public void testTimedInvokeAll2() { |
1439 |
> |
public void testTimedInvokeAll2() throws Exception { |
1440 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1441 |
|
try { |
1442 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1443 |
|
assertTrue(r.isEmpty()); |
1559 |
– |
} catch (Exception ex) { |
1560 |
– |
unexpectedException(); |
1444 |
|
} finally { |
1445 |
|
joinPool(e); |
1446 |
|
} |
1449 |
|
/** |
1450 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1451 |
|
*/ |
1452 |
< |
public void testTimedInvokeAll3() { |
1452 |
> |
public void testTimedInvokeAll3() throws Exception { |
1453 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1454 |
|
try { |
1455 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1456 |
|
l.add(new StringTask()); |
1457 |
|
l.add(null); |
1458 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1459 |
+ |
shouldThrow(); |
1460 |
|
} catch (NullPointerException success) { |
1577 |
– |
} catch (Exception ex) { |
1578 |
– |
unexpectedException(); |
1461 |
|
} finally { |
1462 |
|
joinPool(e); |
1463 |
|
} |
1466 |
|
/** |
1467 |
|
* get of element of invokeAll(c) throws exception on failed task |
1468 |
|
*/ |
1469 |
< |
public void testTimedInvokeAll4() { |
1469 |
> |
public void testTimedInvokeAll4() throws Exception { |
1470 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1471 |
|
try { |
1472 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1473 |
|
l.add(new NPETask()); |
1474 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1475 |
|
assertEquals(1, result.size()); |
1476 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1477 |
< |
it.next().get(); |
1476 |
> |
for (Future<String> future : result) |
1477 |
> |
future.get(); |
1478 |
> |
shouldThrow(); |
1479 |
|
} catch (ExecutionException success) { |
1480 |
< |
} catch (Exception ex) { |
1598 |
< |
unexpectedException(); |
1480 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1481 |
|
} finally { |
1482 |
|
joinPool(e); |
1483 |
|
} |
1486 |
|
/** |
1487 |
|
* timed invokeAll(c) returns results of all completed tasks |
1488 |
|
*/ |
1489 |
< |
public void testTimedInvokeAll5() { |
1489 |
> |
public void testTimedInvokeAll5() throws Exception { |
1490 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1491 |
|
try { |
1492 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1494 |
|
l.add(new StringTask()); |
1495 |
|
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1496 |
|
assertEquals(2, result.size()); |
1497 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1498 |
< |
assertSame(TEST_STRING, it.next().get()); |
1497 |
> |
for (Future<String> future : result) |
1498 |
> |
assertSame(TEST_STRING, future.get()); |
1499 |
|
} catch (ExecutionException success) { |
1618 |
– |
} catch (Exception ex) { |
1619 |
– |
unexpectedException(); |
1500 |
|
} finally { |
1501 |
|
joinPool(e); |
1502 |
|
} |
1505 |
|
/** |
1506 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1507 |
|
*/ |
1508 |
< |
public void testTimedInvokeAll6() { |
1508 |
> |
public void testTimedInvokeAll6() throws Exception { |
1509 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1510 |
|
try { |
1511 |
|
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1523 |
|
assertTrue(f3.isDone()); |
1524 |
|
assertFalse(f1.isCancelled()); |
1525 |
|
assertTrue(f2.isCancelled()); |
1646 |
– |
} catch (Exception ex) { |
1647 |
– |
unexpectedException(); |
1526 |
|
} finally { |
1527 |
|
joinPool(e); |
1528 |
|
} |
1532 |
|
* Execution continues if there is at least one thread even if |
1533 |
|
* thread factory fails to create more |
1534 |
|
*/ |
1535 |
< |
public void testFailingThreadFactory() { |
1535 |
> |
public void testFailingThreadFactory() throws InterruptedException { |
1536 |
|
ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1537 |
|
try { |
1660 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1538 |
|
for (int k = 0; k < 100; ++k) { |
1539 |
|
e.execute(new NoOpRunnable()); |
1540 |
|
} |
1541 |
|
Thread.sleep(LONG_DELAY_MS); |
1665 |
– |
} catch (Exception ex) { |
1666 |
– |
unexpectedException(); |
1542 |
|
} finally { |
1543 |
|
joinPool(e); |
1544 |
|
} |
1556 |
|
/** |
1557 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1558 |
|
*/ |
1559 |
< |
public void testAllowCoreThreadTimeOut_true() { |
1559 |
> |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1560 |
|
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1561 |
|
tpe.allowCoreThreadTimeOut(true); |
1562 |
|
tpe.execute(new NoOpRunnable()); |
1563 |
|
try { |
1564 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
1565 |
|
assertEquals(0, tpe.getPoolSize()); |
1691 |
– |
} catch (InterruptedException e) { |
1692 |
– |
unexpectedException(); |
1566 |
|
} finally { |
1567 |
|
joinPool(tpe); |
1568 |
|
} |
1571 |
|
/** |
1572 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1573 |
|
*/ |
1574 |
< |
public void testAllowCoreThreadTimeOut_false() { |
1574 |
> |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1575 |
|
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1576 |
|
tpe.allowCoreThreadTimeOut(false); |
1577 |
|
tpe.execute(new NoOpRunnable()); |
1578 |
|
try { |
1579 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
1580 |
|
assertTrue(tpe.getPoolSize() >= 1); |
1708 |
– |
} catch (InterruptedException e) { |
1709 |
– |
unexpectedException(); |
1581 |
|
} finally { |
1582 |
|
joinPool(tpe); |
1583 |
|
} |