14 |
|
|
15 |
|
public class ThreadPoolExecutorTest 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); |
48 |
|
|
49 |
|
|
50 |
|
/** |
51 |
< |
* execute successfully executes a runnable |
51 |
> |
* execute successfully executes a runnable |
52 |
|
*/ |
53 |
|
public void testExecute() throws InterruptedException { |
54 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
55 |
|
try { |
56 |
|
p1.execute(new ShortRunnable()); |
57 |
< |
Thread.sleep(SMALL_DELAY_MS); |
57 |
> |
Thread.sleep(SMALL_DELAY_MS); |
58 |
|
} finally { |
59 |
|
joinPool(p1); |
60 |
|
} |
61 |
|
} |
62 |
|
|
63 |
|
/** |
64 |
< |
* getActiveCount increases but doesn't overestimate, when a |
65 |
< |
* thread becomes active |
64 |
> |
* getActiveCount increases but doesn't overestimate, when a |
65 |
> |
* thread becomes active |
66 |
|
*/ |
67 |
|
public void testGetActiveCount() throws InterruptedException { |
68 |
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
77 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
78 |
|
*/ |
79 |
|
public void testPrestartCoreThread() { |
80 |
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* prestartAllCoreThreads starts all corePoolSize threads |
92 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
93 |
|
*/ |
94 |
|
public void testPrestartAllCoreThreads() { |
95 |
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
106 |
< |
* when tasks complete |
105 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
106 |
> |
* when tasks complete |
107 |
|
*/ |
108 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
109 |
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
119 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
120 |
|
*/ |
121 |
|
public void testGetCorePoolSize() { |
122 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* getKeepAliveTime returns value given in constructor if not otherwise set |
128 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
129 |
|
*/ |
130 |
|
public void testGetKeepAliveTime() { |
131 |
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
209 |
|
|
210 |
|
|
211 |
|
/** |
212 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
213 |
< |
* multiple threads active |
212 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
213 |
> |
* multiple threads active |
214 |
|
*/ |
215 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
216 |
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
< |
* getMaximumPoolSize returns value given in constructor if not |
227 |
< |
* otherwise set |
226 |
> |
* getMaximumPoolSize returns value given in constructor if not |
227 |
> |
* otherwise set |
228 |
|
*/ |
229 |
|
public void testGetMaximumPoolSize() { |
230 |
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
237 |
< |
* become active |
236 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
237 |
> |
* become active |
238 |
|
*/ |
239 |
|
public void testGetPoolSize() { |
240 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
245 |
|
} |
246 |
|
|
247 |
|
/** |
248 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
248 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
249 |
|
*/ |
250 |
|
public void testGetTaskCount() throws InterruptedException { |
251 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
< |
* isShutDown is false before shutdown, true after |
260 |
> |
* isShutDown is false before shutdown, true after |
261 |
|
*/ |
262 |
|
public void testIsShutdown() { |
263 |
|
|
264 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
264 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
265 |
|
assertFalse(p1.isShutdown()); |
266 |
|
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
267 |
< |
assertTrue(p1.isShutdown()); |
267 |
> |
assertTrue(p1.isShutdown()); |
268 |
|
joinPool(p1); |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
/** |
273 |
< |
* isTerminated is false before termination, true after |
273 |
> |
* isTerminated is false before termination, true after |
274 |
|
*/ |
275 |
|
public void testIsTerminated() throws InterruptedException { |
276 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
276 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
277 |
|
assertFalse(p1.isTerminated()); |
278 |
|
try { |
279 |
|
p1.execute(new MediumRunnable()); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* isTerminating is not true when running or when terminated |
288 |
> |
* isTerminating is not true when running or when terminated |
289 |
|
*/ |
290 |
|
public void testIsTerminating() throws InterruptedException { |
291 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
291 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
292 |
|
assertFalse(p1.isTerminating()); |
293 |
|
try { |
294 |
|
p1.execute(new SmallRunnable()); |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
< |
* purge removes cancelled tasks from the queue |
357 |
> |
* purge removes cancelled tasks from the queue |
358 |
|
*/ |
359 |
|
public void testPurge() { |
360 |
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* shutDownNow returns a list containing tasks that were not run |
375 |
> |
* shutDownNow returns a list containing tasks that were not run |
376 |
|
*/ |
377 |
|
public void testShutDownNow() { |
378 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
378 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
379 |
|
List l; |
380 |
|
try { |
381 |
|
for (int i = 0; i < 5; i++) |
385 |
|
try { |
386 |
|
l = p1.shutdownNow(); |
387 |
|
} catch (SecurityException ok) { return; } |
388 |
– |
|
388 |
|
} |
389 |
< |
assertTrue(p1.isShutdown()); |
390 |
< |
assertTrue(l.size() <= 4); |
389 |
> |
assertTrue(p1.isShutdown()); |
390 |
> |
assertTrue(l.size() <= 4); |
391 |
|
} |
392 |
|
|
393 |
|
// Exception Tests |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
< |
* Constructor throws if workQueue is set to null |
653 |
> |
* Constructor throws if workQueue is null |
654 |
|
*/ |
655 |
|
public void testConstructorNullPointerException6() { |
656 |
|
try { |
660 |
|
} |
661 |
|
|
662 |
|
/** |
663 |
< |
* Constructor throws if handler is set to null |
663 |
> |
* Constructor throws if handler is null |
664 |
|
*/ |
665 |
|
public void testConstructorNullPointerException7() { |
666 |
|
try { |
671 |
|
} |
672 |
|
|
673 |
|
/** |
674 |
< |
* Constructor throws if ThreadFactory is set top null |
674 |
> |
* Constructor throws if ThreadFactory is null |
675 |
|
*/ |
676 |
|
public void testConstructorNullPointerException8() { |
677 |
|
try { |
683 |
|
|
684 |
|
|
685 |
|
/** |
686 |
< |
* execute throws RejectedExecutionException |
688 |
< |
* if saturated. |
686 |
> |
* execute throws RejectedExecutionException if saturated. |
687 |
|
*/ |
688 |
|
public void testSaturatedExecute() { |
689 |
|
ThreadPoolExecutor p = |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
708 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
709 |
|
*/ |
710 |
|
public void testSaturatedExecute2() { |
711 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
731 |
|
} |
732 |
|
|
733 |
|
/** |
734 |
< |
* executor using DiscardPolicy drops task if saturated. |
734 |
> |
* executor using DiscardPolicy drops task if saturated. |
735 |
|
*/ |
736 |
|
public void testSaturatedExecute3() { |
737 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
756 |
|
} |
757 |
|
|
758 |
|
/** |
759 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
759 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
760 |
|
*/ |
761 |
|
public void testSaturatedExecute4() { |
762 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
777 |
|
} |
778 |
|
|
779 |
|
/** |
780 |
< |
* execute throws RejectedExecutionException if shutdown |
780 |
> |
* execute throws RejectedExecutionException if shutdown |
781 |
|
*/ |
782 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
783 |
|
ThreadPoolExecutor tpe = |
784 |
|
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
785 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
786 |
< |
try { |
787 |
< |
tpe.execute(new NoOpRunnable()); |
788 |
< |
shouldThrow(); |
789 |
< |
} catch (RejectedExecutionException success) {} |
786 |
> |
try { |
787 |
> |
tpe.execute(new NoOpRunnable()); |
788 |
> |
shouldThrow(); |
789 |
> |
} catch (RejectedExecutionException success) {} |
790 |
|
|
791 |
< |
joinPool(tpe); |
791 |
> |
joinPool(tpe); |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
795 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
796 |
|
*/ |
797 |
|
public void testCallerRunsOnShutdown() { |
798 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
799 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
800 |
|
|
801 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
802 |
< |
try { |
802 |
> |
try { |
803 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
804 |
< |
p.execute(r); |
804 |
> |
p.execute(r); |
805 |
|
assertFalse(r.done); |
806 |
|
} finally { |
807 |
|
joinPool(p); |
809 |
|
} |
810 |
|
|
811 |
|
/** |
812 |
< |
* execute using DiscardPolicy drops task on shutdown |
812 |
> |
* execute using DiscardPolicy drops task on shutdown |
813 |
|
*/ |
814 |
|
public void testDiscardOnShutdown() { |
815 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
816 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
817 |
|
|
818 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
819 |
< |
try { |
819 |
> |
try { |
820 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
821 |
< |
p.execute(r); |
821 |
> |
p.execute(r); |
822 |
|
assertFalse(r.done); |
823 |
|
} finally { |
824 |
|
joinPool(p); |
827 |
|
|
828 |
|
|
829 |
|
/** |
830 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
830 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
831 |
|
*/ |
832 |
|
public void testDiscardOldestOnShutdown() { |
833 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
834 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
835 |
|
|
836 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
837 |
< |
try { |
837 |
> |
try { |
838 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
839 |
< |
p.execute(r); |
839 |
> |
p.execute(r); |
840 |
|
assertFalse(r.done); |
841 |
|
} finally { |
842 |
|
joinPool(p); |
845 |
|
|
846 |
|
|
847 |
|
/** |
848 |
< |
* execute (null) throws NPE |
848 |
> |
* execute(null) throws NPE |
849 |
|
*/ |
850 |
|
public void testExecuteNull() { |
851 |
|
ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
852 |
|
try { |
853 |
< |
tpe.execute(null); |
853 |
> |
tpe.execute(null); |
854 |
|
shouldThrow(); |
855 |
< |
} catch (NullPointerException success) {} |
855 |
> |
} catch (NullPointerException success) {} |
856 |
|
|
857 |
< |
joinPool(tpe); |
857 |
> |
joinPool(tpe); |
858 |
|
} |
859 |
|
|
860 |
|
/** |
861 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
861 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
862 |
|
*/ |
863 |
|
public void testCorePoolSizeIllegalArgumentException() { |
864 |
< |
ThreadPoolExecutor tpe = |
864 |
> |
ThreadPoolExecutor tpe = |
865 |
|
new ThreadPoolExecutor(1, 2, |
866 |
|
LONG_DELAY_MS, MILLISECONDS, |
867 |
|
new ArrayBlockingQueue<Runnable>(10)); |
868 |
< |
try { |
869 |
< |
tpe.setCorePoolSize(-1); |
870 |
< |
shouldThrow(); |
871 |
< |
} catch (IllegalArgumentException success) { |
868 |
> |
try { |
869 |
> |
tpe.setCorePoolSize(-1); |
870 |
> |
shouldThrow(); |
871 |
> |
} catch (IllegalArgumentException success) { |
872 |
|
} finally { |
873 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
874 |
|
} |
876 |
|
} |
877 |
|
|
878 |
|
/** |
879 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
880 |
< |
* given a value less the core pool size |
879 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
880 |
> |
* given a value less the core pool size |
881 |
|
*/ |
882 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
883 |
|
ThreadPoolExecutor tpe = |
895 |
|
} |
896 |
|
|
897 |
|
/** |
898 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
899 |
< |
* if given a negative value |
898 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
899 |
> |
* if given a negative value |
900 |
|
*/ |
901 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
902 |
|
ThreadPoolExecutor tpe = |
915 |
|
|
916 |
|
|
917 |
|
/** |
918 |
< |
* setKeepAliveTime throws IllegalArgumentException |
919 |
< |
* when given a negative value |
918 |
> |
* setKeepAliveTime throws IllegalArgumentException |
919 |
> |
* when given a negative value |
920 |
|
*/ |
921 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
922 |
< |
ThreadPoolExecutor tpe = |
922 |
> |
ThreadPoolExecutor tpe = |
923 |
|
new ThreadPoolExecutor(2, 3, |
924 |
|
LONG_DELAY_MS, MILLISECONDS, |
925 |
|
new ArrayBlockingQueue<Runnable>(10)); |
926 |
< |
try { |
926 |
> |
try { |
927 |
|
tpe.setKeepAliveTime(-1,MILLISECONDS); |
928 |
|
shouldThrow(); |
929 |
|
} catch (IllegalArgumentException success) { |
1036 |
|
* invokeAny(c) throws NPE if c has null elements |
1037 |
|
*/ |
1038 |
|
public void testInvokeAny3() throws Exception { |
1039 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
1039 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1040 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1041 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1042 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1043 |
+ |
l.add(null); |
1044 |
|
try { |
1044 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1045 |
– |
l.add(new Callable<String>() { |
1046 |
– |
public String call() { |
1047 |
– |
try { |
1048 |
– |
latch.await(); |
1049 |
– |
} catch (InterruptedException ok) {} |
1050 |
– |
return TEST_STRING; |
1051 |
– |
}}); |
1052 |
– |
l.add(null); |
1045 |
|
e.invokeAny(l); |
1046 |
|
shouldThrow(); |
1047 |
|
} catch (NullPointerException success) { |
1056 |
|
*/ |
1057 |
|
public void testInvokeAny4() throws Exception { |
1058 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1059 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1060 |
+ |
l.add(new NPETask()); |
1061 |
|
try { |
1068 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1069 |
– |
l.add(new NPETask()); |
1062 |
|
e.invokeAny(l); |
1063 |
|
shouldThrow(); |
1064 |
|
} catch (ExecutionException success) { |
1074 |
|
public void testInvokeAny5() throws Exception { |
1075 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1076 |
|
try { |
1077 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1077 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1078 |
|
l.add(new StringTask()); |
1079 |
|
l.add(new StringTask()); |
1080 |
|
String result = e.invokeAny(l); |
1116 |
|
*/ |
1117 |
|
public void testInvokeAll3() throws Exception { |
1118 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1119 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1120 |
+ |
l.add(new StringTask()); |
1121 |
+ |
l.add(null); |
1122 |
|
try { |
1128 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1129 |
– |
l.add(new StringTask()); |
1130 |
– |
l.add(null); |
1123 |
|
e.invokeAll(l); |
1124 |
|
shouldThrow(); |
1125 |
|
} catch (NullPointerException success) { |
1134 |
|
public void testInvokeAll4() throws Exception { |
1135 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1136 |
|
try { |
1137 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1137 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1138 |
|
l.add(new NPETask()); |
1139 |
< |
List<Future<String>> result = e.invokeAll(l); |
1140 |
< |
assertEquals(1, result.size()); |
1141 |
< |
for (Future<String> future : result) { |
1142 |
< |
try { |
1143 |
< |
future.get(); |
1144 |
< |
shouldThrow(); |
1145 |
< |
} catch (ExecutionException success) { |
1154 |
< |
Throwable cause = success.getCause(); |
1155 |
< |
assertTrue(cause instanceof NullPointerException); |
1156 |
< |
} |
1139 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1140 |
> |
assertEquals(1, futures.size()); |
1141 |
> |
try { |
1142 |
> |
futures.get(0).get(); |
1143 |
> |
shouldThrow(); |
1144 |
> |
} catch (ExecutionException success) { |
1145 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1146 |
|
} |
1147 |
|
} finally { |
1148 |
|
joinPool(e); |
1155 |
|
public void testInvokeAll5() throws Exception { |
1156 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1157 |
|
try { |
1158 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1158 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1159 |
|
l.add(new StringTask()); |
1160 |
|
l.add(new StringTask()); |
1161 |
< |
List<Future<String>> result = e.invokeAll(l); |
1162 |
< |
assertEquals(2, result.size()); |
1163 |
< |
for (Future<String> future : result) |
1161 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1162 |
> |
assertEquals(2, futures.size()); |
1163 |
> |
for (Future<String> future : futures) |
1164 |
|
assertSame(TEST_STRING, future.get()); |
1165 |
|
} finally { |
1166 |
|
joinPool(e); |
1188 |
|
*/ |
1189 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1190 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1191 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1192 |
+ |
l.add(new StringTask()); |
1193 |
|
try { |
1203 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1204 |
– |
l.add(new StringTask()); |
1194 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1195 |
|
shouldThrow(); |
1196 |
|
} catch (NullPointerException success) { |
1217 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1218 |
|
*/ |
1219 |
|
public void testTimedInvokeAny3() throws Exception { |
1220 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
1221 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1222 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1223 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1224 |
+ |
l.add(null); |
1225 |
|
try { |
1233 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1234 |
– |
l.add(new StringTask()); |
1235 |
– |
l.add(null); |
1226 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1227 |
|
shouldThrow(); |
1228 |
|
} catch (NullPointerException success) { |
1229 |
|
} finally { |
1230 |
+ |
latch.countDown(); |
1231 |
|
joinPool(e); |
1232 |
|
} |
1233 |
|
} |
1237 |
|
*/ |
1238 |
|
public void testTimedInvokeAny4() throws Exception { |
1239 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1240 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1241 |
+ |
l.add(new NPETask()); |
1242 |
|
try { |
1250 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1251 |
– |
l.add(new NPETask()); |
1243 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1244 |
|
shouldThrow(); |
1245 |
|
} catch (ExecutionException success) { |
1255 |
|
public void testTimedInvokeAny5() throws Exception { |
1256 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1257 |
|
try { |
1258 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1258 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1259 |
|
l.add(new StringTask()); |
1260 |
|
l.add(new StringTask()); |
1261 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1284 |
|
*/ |
1285 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1286 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1287 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1288 |
+ |
l.add(new StringTask()); |
1289 |
|
try { |
1297 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1298 |
– |
l.add(new StringTask()); |
1290 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1291 |
|
shouldThrow(); |
1292 |
|
} catch (NullPointerException success) { |
1313 |
|
*/ |
1314 |
|
public void testTimedInvokeAll3() throws Exception { |
1315 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1316 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1317 |
+ |
l.add(new StringTask()); |
1318 |
+ |
l.add(null); |
1319 |
|
try { |
1326 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1327 |
– |
l.add(new StringTask()); |
1328 |
– |
l.add(null); |
1320 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1321 |
|
shouldThrow(); |
1322 |
|
} catch (NullPointerException success) { |
1330 |
|
*/ |
1331 |
|
public void testTimedInvokeAll4() throws Exception { |
1332 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1333 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1334 |
+ |
l.add(new NPETask()); |
1335 |
+ |
List<Future<String>> futures = |
1336 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1337 |
+ |
assertEquals(1, futures.size()); |
1338 |
|
try { |
1339 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1344 |
< |
l.add(new NPETask()); |
1345 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1346 |
< |
assertEquals(1, result.size()); |
1347 |
< |
for (Future<String> future : result) |
1348 |
< |
future.get(); |
1339 |
> |
futures.get(0).get(); |
1340 |
|
shouldThrow(); |
1341 |
|
} catch (ExecutionException success) { |
1342 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1351 |
|
public void testTimedInvokeAll5() throws Exception { |
1352 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1353 |
|
try { |
1354 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1354 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1355 |
|
l.add(new StringTask()); |
1356 |
|
l.add(new StringTask()); |
1357 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1358 |
< |
assertEquals(2, result.size()); |
1359 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1360 |
< |
assertSame(TEST_STRING, it.next().get()); |
1357 |
> |
List<Future<String>> futures = |
1358 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1359 |
> |
assertEquals(2, futures.size()); |
1360 |
> |
for (Future<String> future : futures) |
1361 |
> |
assertSame(TEST_STRING, future.get()); |
1362 |
|
} finally { |
1363 |
|
joinPool(e); |
1364 |
|
} |
1370 |
|
public void testTimedInvokeAll6() throws Exception { |
1371 |
|
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1372 |
|
try { |
1373 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1373 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1374 |
|
l.add(new StringTask()); |
1375 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1376 |
|
l.add(new StringTask()); |
1377 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1378 |
< |
assertEquals(3, result.size()); |
1379 |
< |
Iterator<Future<String>> it = result.iterator(); |
1377 |
> |
List<Future<String>> futures = |
1378 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1379 |
> |
assertEquals(3, futures.size()); |
1380 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1381 |
|
Future<String> f1 = it.next(); |
1382 |
|
Future<String> f2 = it.next(); |
1383 |
|
Future<String> f3 = it.next(); |
1398 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1399 |
|
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1400 |
|
try { |
1401 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1401 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1402 |
|
for (int k = 0; k < 100; ++k) { |
1403 |
|
e.execute(new NoOpRunnable()); |
1404 |
|
} |