1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
29 |
|
p1.execute(runnable); |
30 |
|
assertFalse(runnable.done); |
31 |
|
Thread.sleep(SHORT_DELAY_MS); |
32 |
< |
p1.shutdown(); |
32 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
33 |
|
try { |
34 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
35 |
|
} catch(InterruptedException e){ |
36 |
|
unexpectedException(); |
37 |
|
} |
38 |
|
assertTrue(runnable.done); |
39 |
< |
p1.shutdown(); |
39 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
40 |
|
joinPool(p1); |
41 |
|
} |
42 |
|
catch(Exception e){ |
58 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
59 |
|
assertTrue(callable.done); |
60 |
|
assertEquals(Boolean.TRUE, f.get()); |
61 |
< |
p1.shutdown(); |
61 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
62 |
|
joinPool(p1); |
63 |
|
} catch(RejectedExecutionException e){} |
64 |
|
catch(Exception e){ |
79 |
|
assertFalse(runnable.done); |
80 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
81 |
|
assertTrue(runnable.done); |
82 |
< |
p1.shutdown(); |
82 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
83 |
|
joinPool(p1); |
84 |
|
} catch(Exception e){ |
85 |
|
unexpectedException(); |
98 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
99 |
|
assertTrue(runnable.done); |
100 |
|
h.cancel(true); |
100 |
– |
p1.shutdown(); |
101 |
|
joinPool(p1); |
102 |
|
} catch(Exception e){ |
103 |
|
unexpectedException(); |
116 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
117 |
|
assertTrue(runnable.done); |
118 |
|
h.cancel(true); |
119 |
– |
p1.shutdown(); |
119 |
|
joinPool(p1); |
120 |
|
} catch(Exception e){ |
121 |
|
unexpectedException(); |
166 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
167 |
|
shouldThrow(); |
168 |
|
} catch(RejectedExecutionException success){ |
169 |
+ |
} catch (SecurityException ok) { |
170 |
|
} |
171 |
+ |
|
172 |
|
joinPool(se); |
173 |
|
|
174 |
|
} |
184 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
185 |
|
shouldThrow(); |
186 |
|
} catch(RejectedExecutionException success){ |
187 |
+ |
} catch (SecurityException ok) { |
188 |
|
} |
189 |
|
joinPool(se); |
190 |
|
} |
200 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
201 |
|
shouldThrow(); |
202 |
|
} catch(RejectedExecutionException success){ |
203 |
< |
} |
203 |
> |
} catch (SecurityException ok) { |
204 |
> |
} |
205 |
|
joinPool(se); |
206 |
|
} |
207 |
|
|
216 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
217 |
|
shouldThrow(); |
218 |
|
} catch(RejectedExecutionException success){ |
219 |
+ |
} catch (SecurityException ok) { |
220 |
|
} |
221 |
|
joinPool(se); |
222 |
|
} |
232 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
233 |
|
shouldThrow(); |
234 |
|
} catch(RejectedExecutionException success){ |
235 |
+ |
} catch (SecurityException ok) { |
236 |
|
} |
237 |
|
joinPool(se); |
238 |
|
} |
335 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
336 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
337 |
|
assertSame(tf, p.getThreadFactory()); |
333 |
– |
p.shutdown(); |
338 |
|
joinPool(p); |
339 |
|
} |
340 |
|
|
346 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
347 |
|
p.setThreadFactory(tf); |
348 |
|
assertSame(tf, p.getThreadFactory()); |
345 |
– |
p.shutdown(); |
349 |
|
joinPool(p); |
350 |
|
} |
351 |
|
|
373 |
|
assertFalse(p1.isShutdown()); |
374 |
|
} |
375 |
|
finally { |
376 |
< |
p1.shutdown(); |
376 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
377 |
|
} |
378 |
|
assertTrue(p1.isShutdown()); |
379 |
|
} |
387 |
|
try { |
388 |
|
p1.execute(new SmallRunnable()); |
389 |
|
} finally { |
390 |
< |
p1.shutdown(); |
390 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
391 |
|
} |
392 |
|
try { |
393 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
407 |
|
p1.execute(new SmallRunnable()); |
408 |
|
assertFalse(p1.isTerminating()); |
409 |
|
} finally { |
410 |
< |
p1.shutdown(); |
410 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
411 |
|
} |
412 |
|
try { |
413 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
432 |
|
BlockingQueue<Runnable> q = p1.getQueue(); |
433 |
|
assertTrue(q.contains(tasks[4])); |
434 |
|
assertFalse(q.contains(tasks[0])); |
432 |
– |
p1.shutdownNow(); |
435 |
|
} catch(Exception e) { |
436 |
|
unexpectedException(); |
437 |
|
} finally { |
460 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
461 |
|
assertTrue(p1.remove((Runnable)tasks[3])); |
462 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
461 |
– |
p1.shutdownNow(); |
463 |
|
} catch(Exception e) { |
464 |
|
unexpectedException(); |
465 |
|
} finally { |
492 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
493 |
|
for(int i = 0; i < 5; i++) |
494 |
|
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
495 |
< |
List l = p1.shutdownNow(); |
495 |
> |
List l; |
496 |
> |
try { |
497 |
> |
l = p1.shutdownNow(); |
498 |
> |
} catch (SecurityException ok) { |
499 |
> |
return; |
500 |
> |
} |
501 |
|
assertTrue(p1.isShutdown()); |
502 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
503 |
|
joinPool(p1); |
516 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
517 |
|
for(int i = 0; i < 5; i++) |
518 |
|
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
519 |
< |
p1.shutdown(); |
519 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
520 |
|
BlockingQueue q = p1.getQueue(); |
521 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
522 |
|
ScheduledFuture t = (ScheduledFuture)it.next(); |
547 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
548 |
|
for(int i = 0; i < 5; i++) |
549 |
|
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
550 |
< |
p1.shutdown(); |
550 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
551 |
|
assertTrue(p1.isShutdown()); |
552 |
|
BlockingQueue q = p1.getQueue(); |
553 |
|
assertTrue(q.isEmpty()); |
570 |
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
571 |
|
ScheduledFuture task = |
572 |
|
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
573 |
< |
p1.shutdown(); |
573 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
574 |
|
assertTrue(p1.isShutdown()); |
575 |
|
BlockingQueue q = p1.getQueue(); |
576 |
|
assertTrue(q.isEmpty()); |
591 |
|
try { |
592 |
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
593 |
|
ScheduledFuture task = |
594 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
594 |
> |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); |
595 |
|
assertFalse(task.isCancelled()); |
596 |
< |
p1.shutdown(); |
596 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
597 |
|
assertFalse(task.isCancelled()); |
598 |
|
assertFalse(p1.isTerminated()); |
599 |
|
assertTrue(p1.isShutdown()); |
600 |
|
Thread.sleep(SHORT_DELAY_MS); |
601 |
|
assertFalse(task.isCancelled()); |
602 |
< |
task.cancel(true); |
603 |
< |
assertTrue(task.isCancelled()); |
602 |
> |
assertTrue(task.cancel(true)); |
603 |
> |
assertTrue(task.isDone()); |
604 |
|
Thread.sleep(SHORT_DELAY_MS); |
605 |
|
assertTrue(p1.isTerminated()); |
606 |
|
} |
607 |
|
catch(Exception ex) { |
608 |
|
unexpectedException(); |
609 |
|
} |
610 |
< |
finally { |
611 |
< |
p1.shutdownNow(); |
610 |
> |
finally { |
611 |
> |
joinPool(p1); |
612 |
> |
} |
613 |
> |
} |
614 |
> |
|
615 |
> |
/** |
616 |
> |
* completed submit of callable returns result |
617 |
> |
*/ |
618 |
> |
public void testSubmitCallable() { |
619 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
620 |
> |
try { |
621 |
> |
Future<String> future = e.submit(new StringTask()); |
622 |
> |
String result = future.get(); |
623 |
> |
assertSame(TEST_STRING, result); |
624 |
> |
} |
625 |
> |
catch (ExecutionException ex) { |
626 |
> |
unexpectedException(); |
627 |
> |
} |
628 |
> |
catch (InterruptedException ex) { |
629 |
> |
unexpectedException(); |
630 |
> |
} finally { |
631 |
> |
joinPool(e); |
632 |
> |
} |
633 |
> |
} |
634 |
> |
|
635 |
> |
/** |
636 |
> |
* completed submit of runnable returns successfully |
637 |
> |
*/ |
638 |
> |
public void testSubmitRunnable() { |
639 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
640 |
> |
try { |
641 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
642 |
> |
future.get(); |
643 |
> |
assertTrue(future.isDone()); |
644 |
> |
} |
645 |
> |
catch (ExecutionException ex) { |
646 |
> |
unexpectedException(); |
647 |
> |
} |
648 |
> |
catch (InterruptedException ex) { |
649 |
> |
unexpectedException(); |
650 |
> |
} finally { |
651 |
> |
joinPool(e); |
652 |
> |
} |
653 |
> |
} |
654 |
> |
|
655 |
> |
/** |
656 |
> |
* completed submit of (runnable, result) returns result |
657 |
> |
*/ |
658 |
> |
public void testSubmitRunnable2() { |
659 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
660 |
> |
try { |
661 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
662 |
> |
String result = future.get(); |
663 |
> |
assertSame(TEST_STRING, result); |
664 |
> |
} |
665 |
> |
catch (ExecutionException ex) { |
666 |
> |
unexpectedException(); |
667 |
> |
} |
668 |
> |
catch (InterruptedException ex) { |
669 |
> |
unexpectedException(); |
670 |
> |
} finally { |
671 |
> |
joinPool(e); |
672 |
> |
} |
673 |
> |
} |
674 |
> |
|
675 |
> |
|
676 |
> |
|
677 |
> |
|
678 |
> |
|
679 |
> |
/** |
680 |
> |
* invokeAny(null) throws NPE |
681 |
> |
*/ |
682 |
> |
public void testInvokeAny1() { |
683 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
684 |
> |
try { |
685 |
> |
e.invokeAny(null); |
686 |
> |
} catch (NullPointerException success) { |
687 |
> |
} catch(Exception ex) { |
688 |
> |
unexpectedException(); |
689 |
> |
} finally { |
690 |
> |
joinPool(e); |
691 |
> |
} |
692 |
> |
} |
693 |
> |
|
694 |
> |
/** |
695 |
> |
* invokeAny(empty collection) throws IAE |
696 |
> |
*/ |
697 |
> |
public void testInvokeAny2() { |
698 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
699 |
> |
try { |
700 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
701 |
> |
} catch (IllegalArgumentException success) { |
702 |
> |
} catch(Exception ex) { |
703 |
> |
unexpectedException(); |
704 |
> |
} finally { |
705 |
> |
joinPool(e); |
706 |
> |
} |
707 |
> |
} |
708 |
> |
|
709 |
> |
/** |
710 |
> |
* invokeAny(c) throws NPE if c has null elements |
711 |
> |
*/ |
712 |
> |
public void testInvokeAny3() { |
713 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
714 |
> |
try { |
715 |
> |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
716 |
> |
l.add(new StringTask()); |
717 |
> |
l.add(null); |
718 |
> |
e.invokeAny(l); |
719 |
> |
} catch (NullPointerException success) { |
720 |
> |
} catch(Exception ex) { |
721 |
> |
unexpectedException(); |
722 |
> |
} finally { |
723 |
> |
joinPool(e); |
724 |
> |
} |
725 |
> |
} |
726 |
> |
|
727 |
> |
/** |
728 |
> |
* invokeAny(c) throws ExecutionException if no task completes |
729 |
> |
*/ |
730 |
> |
public void testInvokeAny4() { |
731 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
732 |
> |
try { |
733 |
> |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
734 |
> |
l.add(new NPETask()); |
735 |
> |
e.invokeAny(l); |
736 |
> |
} catch (ExecutionException success) { |
737 |
> |
} catch(Exception ex) { |
738 |
> |
unexpectedException(); |
739 |
> |
} finally { |
740 |
> |
joinPool(e); |
741 |
> |
} |
742 |
> |
} |
743 |
> |
|
744 |
> |
/** |
745 |
> |
* invokeAny(c) returns result of some task |
746 |
> |
*/ |
747 |
> |
public void testInvokeAny5() { |
748 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
749 |
> |
try { |
750 |
> |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
751 |
> |
l.add(new StringTask()); |
752 |
> |
l.add(new StringTask()); |
753 |
> |
String result = e.invokeAny(l); |
754 |
> |
assertSame(TEST_STRING, result); |
755 |
> |
} catch (ExecutionException success) { |
756 |
> |
} catch(Exception ex) { |
757 |
> |
unexpectedException(); |
758 |
> |
} finally { |
759 |
> |
joinPool(e); |
760 |
> |
} |
761 |
> |
} |
762 |
> |
|
763 |
> |
/** |
764 |
> |
* invokeAll(null) throws NPE |
765 |
> |
*/ |
766 |
> |
public void testInvokeAll1() { |
767 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
768 |
> |
try { |
769 |
> |
e.invokeAll(null); |
770 |
> |
} catch (NullPointerException success) { |
771 |
> |
} catch(Exception ex) { |
772 |
> |
unexpectedException(); |
773 |
> |
} finally { |
774 |
> |
joinPool(e); |
775 |
> |
} |
776 |
> |
} |
777 |
> |
|
778 |
> |
/** |
779 |
> |
* invokeAll(empty collection) returns empty collection |
780 |
> |
*/ |
781 |
> |
public void testInvokeAll2() { |
782 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
783 |
> |
try { |
784 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
785 |
> |
assertTrue(r.isEmpty()); |
786 |
> |
} catch(Exception ex) { |
787 |
> |
unexpectedException(); |
788 |
> |
} finally { |
789 |
> |
joinPool(e); |
790 |
> |
} |
791 |
> |
} |
792 |
> |
|
793 |
> |
/** |
794 |
> |
* invokeAll(c) throws NPE if c has null elements |
795 |
> |
*/ |
796 |
> |
public void testInvokeAll3() { |
797 |
> |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
798 |
> |
try { |
799 |
> |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
800 |
> |
l.add(new StringTask()); |
801 |
> |
l.add(null); |
802 |
> |
e.invokeAll(l); |
803 |
> |
} catch (NullPointerException success) { |
804 |
> |
} catch(Exception ex) { |
805 |
> |
unexpectedException(); |
806 |
> |
} finally { |
807 |
> |
joinPool(e); |
808 |
|
} |
809 |
|
} |
810 |
|
|
811 |
+ |
/** |
812 |
+ |
* get of invokeAll(c) throws exception on failed task |
813 |
+ |
*/ |
814 |
+ |
public void testInvokeAll4() { |
815 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
816 |
+ |
try { |
817 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
818 |
+ |
l.add(new NPETask()); |
819 |
+ |
List<Future<String>> result = e.invokeAll(l); |
820 |
+ |
assertEquals(1, result.size()); |
821 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
822 |
+ |
it.next().get(); |
823 |
+ |
} catch(ExecutionException success) { |
824 |
+ |
} catch(Exception ex) { |
825 |
+ |
unexpectedException(); |
826 |
+ |
} finally { |
827 |
+ |
joinPool(e); |
828 |
+ |
} |
829 |
+ |
} |
830 |
+ |
|
831 |
+ |
/** |
832 |
+ |
* invokeAll(c) returns results of all completed tasks |
833 |
+ |
*/ |
834 |
+ |
public void testInvokeAll5() { |
835 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
836 |
+ |
try { |
837 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
838 |
+ |
l.add(new StringTask()); |
839 |
+ |
l.add(new StringTask()); |
840 |
+ |
List<Future<String>> result = e.invokeAll(l); |
841 |
+ |
assertEquals(2, result.size()); |
842 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
843 |
+ |
assertSame(TEST_STRING, it.next().get()); |
844 |
+ |
} catch (ExecutionException success) { |
845 |
+ |
} catch(Exception ex) { |
846 |
+ |
unexpectedException(); |
847 |
+ |
} finally { |
848 |
+ |
joinPool(e); |
849 |
+ |
} |
850 |
+ |
} |
851 |
+ |
|
852 |
+ |
/** |
853 |
+ |
* timed invokeAny(null) throws NPE |
854 |
+ |
*/ |
855 |
+ |
public void testTimedInvokeAny1() { |
856 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
857 |
+ |
try { |
858 |
+ |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
859 |
+ |
} catch (NullPointerException success) { |
860 |
+ |
} catch(Exception ex) { |
861 |
+ |
unexpectedException(); |
862 |
+ |
} finally { |
863 |
+ |
joinPool(e); |
864 |
+ |
} |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
/** |
868 |
+ |
* timed invokeAny(,,null) throws NPE |
869 |
+ |
*/ |
870 |
+ |
public void testTimedInvokeAnyNullTimeUnit() { |
871 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
872 |
+ |
try { |
873 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
874 |
+ |
l.add(new StringTask()); |
875 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
876 |
+ |
} catch (NullPointerException success) { |
877 |
+ |
} catch(Exception ex) { |
878 |
+ |
unexpectedException(); |
879 |
+ |
} finally { |
880 |
+ |
joinPool(e); |
881 |
+ |
} |
882 |
+ |
} |
883 |
+ |
|
884 |
+ |
/** |
885 |
+ |
* timed invokeAny(empty collection) throws IAE |
886 |
+ |
*/ |
887 |
+ |
public void testTimedInvokeAny2() { |
888 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
889 |
+ |
try { |
890 |
+ |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
891 |
+ |
} catch (IllegalArgumentException success) { |
892 |
+ |
} catch(Exception ex) { |
893 |
+ |
unexpectedException(); |
894 |
+ |
} finally { |
895 |
+ |
joinPool(e); |
896 |
+ |
} |
897 |
+ |
} |
898 |
+ |
|
899 |
+ |
/** |
900 |
+ |
* timed invokeAny(c) throws NPE if c has null elements |
901 |
+ |
*/ |
902 |
+ |
public void testTimedInvokeAny3() { |
903 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
904 |
+ |
try { |
905 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
906 |
+ |
l.add(new StringTask()); |
907 |
+ |
l.add(null); |
908 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
909 |
+ |
} catch (NullPointerException success) { |
910 |
+ |
} catch(Exception ex) { |
911 |
+ |
ex.printStackTrace(); |
912 |
+ |
unexpectedException(); |
913 |
+ |
} finally { |
914 |
+ |
joinPool(e); |
915 |
+ |
} |
916 |
+ |
} |
917 |
+ |
|
918 |
+ |
/** |
919 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
920 |
+ |
*/ |
921 |
+ |
public void testTimedInvokeAny4() { |
922 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
923 |
+ |
try { |
924 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
925 |
+ |
l.add(new NPETask()); |
926 |
+ |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
927 |
+ |
} catch(ExecutionException success) { |
928 |
+ |
} catch(Exception ex) { |
929 |
+ |
unexpectedException(); |
930 |
+ |
} finally { |
931 |
+ |
joinPool(e); |
932 |
+ |
} |
933 |
+ |
} |
934 |
+ |
|
935 |
+ |
/** |
936 |
+ |
* timed invokeAny(c) returns result of some task |
937 |
+ |
*/ |
938 |
+ |
public void testTimedInvokeAny5() { |
939 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
940 |
+ |
try { |
941 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
942 |
+ |
l.add(new StringTask()); |
943 |
+ |
l.add(new StringTask()); |
944 |
+ |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
945 |
+ |
assertSame(TEST_STRING, result); |
946 |
+ |
} catch (ExecutionException success) { |
947 |
+ |
} catch(Exception ex) { |
948 |
+ |
unexpectedException(); |
949 |
+ |
} finally { |
950 |
+ |
joinPool(e); |
951 |
+ |
} |
952 |
+ |
} |
953 |
+ |
|
954 |
+ |
/** |
955 |
+ |
* timed invokeAll(null) throws NPE |
956 |
+ |
*/ |
957 |
+ |
public void testTimedInvokeAll1() { |
958 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
959 |
+ |
try { |
960 |
+ |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
961 |
+ |
} catch (NullPointerException success) { |
962 |
+ |
} catch(Exception ex) { |
963 |
+ |
unexpectedException(); |
964 |
+ |
} finally { |
965 |
+ |
joinPool(e); |
966 |
+ |
} |
967 |
+ |
} |
968 |
+ |
|
969 |
+ |
/** |
970 |
+ |
* timed invokeAll(,,null) throws NPE |
971 |
+ |
*/ |
972 |
+ |
public void testTimedInvokeAllNullTimeUnit() { |
973 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
974 |
+ |
try { |
975 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
976 |
+ |
l.add(new StringTask()); |
977 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
978 |
+ |
} catch (NullPointerException success) { |
979 |
+ |
} catch(Exception ex) { |
980 |
+ |
unexpectedException(); |
981 |
+ |
} finally { |
982 |
+ |
joinPool(e); |
983 |
+ |
} |
984 |
+ |
} |
985 |
+ |
|
986 |
+ |
/** |
987 |
+ |
* timed invokeAll(empty collection) returns empty collection |
988 |
+ |
*/ |
989 |
+ |
public void testTimedInvokeAll2() { |
990 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
991 |
+ |
try { |
992 |
+ |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
993 |
+ |
assertTrue(r.isEmpty()); |
994 |
+ |
} catch(Exception ex) { |
995 |
+ |
unexpectedException(); |
996 |
+ |
} finally { |
997 |
+ |
joinPool(e); |
998 |
+ |
} |
999 |
+ |
} |
1000 |
+ |
|
1001 |
+ |
/** |
1002 |
+ |
* timed invokeAll(c) throws NPE if c has null elements |
1003 |
+ |
*/ |
1004 |
+ |
public void testTimedInvokeAll3() { |
1005 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1006 |
+ |
try { |
1007 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1008 |
+ |
l.add(new StringTask()); |
1009 |
+ |
l.add(null); |
1010 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1011 |
+ |
} catch (NullPointerException success) { |
1012 |
+ |
} catch(Exception ex) { |
1013 |
+ |
unexpectedException(); |
1014 |
+ |
} finally { |
1015 |
+ |
joinPool(e); |
1016 |
+ |
} |
1017 |
+ |
} |
1018 |
+ |
|
1019 |
+ |
/** |
1020 |
+ |
* get of element of invokeAll(c) throws exception on failed task |
1021 |
+ |
*/ |
1022 |
+ |
public void testTimedInvokeAll4() { |
1023 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1024 |
+ |
try { |
1025 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1026 |
+ |
l.add(new NPETask()); |
1027 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1028 |
+ |
assertEquals(1, result.size()); |
1029 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1030 |
+ |
it.next().get(); |
1031 |
+ |
} catch(ExecutionException success) { |
1032 |
+ |
} catch(Exception ex) { |
1033 |
+ |
unexpectedException(); |
1034 |
+ |
} finally { |
1035 |
+ |
joinPool(e); |
1036 |
+ |
} |
1037 |
+ |
} |
1038 |
+ |
|
1039 |
+ |
/** |
1040 |
+ |
* timed invokeAll(c) returns results of all completed tasks |
1041 |
+ |
*/ |
1042 |
+ |
public void testTimedInvokeAll5() { |
1043 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1044 |
+ |
try { |
1045 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1046 |
+ |
l.add(new StringTask()); |
1047 |
+ |
l.add(new StringTask()); |
1048 |
+ |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1049 |
+ |
assertEquals(2, result.size()); |
1050 |
+ |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1051 |
+ |
assertSame(TEST_STRING, it.next().get()); |
1052 |
+ |
} catch (ExecutionException success) { |
1053 |
+ |
} catch(Exception ex) { |
1054 |
+ |
unexpectedException(); |
1055 |
+ |
} finally { |
1056 |
+ |
joinPool(e); |
1057 |
+ |
} |
1058 |
+ |
} |
1059 |
+ |
|
1060 |
+ |
/** |
1061 |
+ |
* timed invokeAll(c) cancels tasks not completed by timeout |
1062 |
+ |
*/ |
1063 |
+ |
public void testTimedInvokeAll6() { |
1064 |
+ |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1065 |
+ |
try { |
1066 |
+ |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1067 |
+ |
l.add(new StringTask()); |
1068 |
+ |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1069 |
+ |
l.add(new StringTask()); |
1070 |
+ |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1071 |
+ |
assertEquals(3, result.size()); |
1072 |
+ |
Iterator<Future<String>> it = result.iterator(); |
1073 |
+ |
Future<String> f1 = it.next(); |
1074 |
+ |
Future<String> f2 = it.next(); |
1075 |
+ |
Future<String> f3 = it.next(); |
1076 |
+ |
assertTrue(f1.isDone()); |
1077 |
+ |
assertTrue(f2.isDone()); |
1078 |
+ |
assertTrue(f3.isDone()); |
1079 |
+ |
assertFalse(f1.isCancelled()); |
1080 |
+ |
assertTrue(f2.isCancelled()); |
1081 |
+ |
} catch(Exception ex) { |
1082 |
+ |
unexpectedException(); |
1083 |
+ |
} finally { |
1084 |
+ |
joinPool(e); |
1085 |
+ |
} |
1086 |
+ |
} |
1087 |
+ |
|
1088 |
+ |
|
1089 |
|
} |