1 |
tim |
1.1 |
/* |
2 |
dl |
1.10 |
* 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 |
jsr166 |
1.16 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
tim |
1.1 |
*/ |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.*; |
13 |
jsr166 |
1.20 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
tim |
1.1 |
import java.math.BigInteger; |
15 |
|
|
import java.security.*; |
16 |
|
|
|
17 |
jsr166 |
1.18 |
public class AbstractExecutorServiceTest extends JSR166TestCase { |
18 |
tim |
1.1 |
public static void main(String[] args) { |
19 |
|
|
junit.textui.TestRunner.run (suite()); |
20 |
|
|
} |
21 |
|
|
public static Test suite() { |
22 |
dl |
1.6 |
return new TestSuite(AbstractExecutorServiceTest.class); |
23 |
tim |
1.1 |
} |
24 |
|
|
|
25 |
jsr166 |
1.16 |
/** |
26 |
tim |
1.1 |
* A no-frills implementation of AbstractExecutorService, designed |
27 |
dl |
1.6 |
* to test the submit methods only. |
28 |
tim |
1.1 |
*/ |
29 |
|
|
static class DirectExecutorService extends AbstractExecutorService { |
30 |
|
|
public void execute(Runnable r) { r.run(); } |
31 |
|
|
public void shutdown() { shutdown = true; } |
32 |
tim |
1.2 |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
33 |
tim |
1.1 |
public boolean isShutdown() { return shutdown; } |
34 |
|
|
public boolean isTerminated() { return isShutdown(); } |
35 |
|
|
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
36 |
|
|
private volatile boolean shutdown = false; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
dl |
1.12 |
* execute(runnable) runs it to completion |
41 |
tim |
1.1 |
*/ |
42 |
jsr166 |
1.19 |
public void testExecuteRunnable() throws Exception { |
43 |
|
|
ExecutorService e = new DirectExecutorService(); |
44 |
|
|
TrackedShortRunnable task = new TrackedShortRunnable(); |
45 |
|
|
assertFalse(task.done); |
46 |
|
|
Future<?> future = e.submit(task); |
47 |
|
|
future.get(); |
48 |
|
|
assertTrue(task.done); |
49 |
tim |
1.1 |
} |
50 |
|
|
|
51 |
|
|
|
52 |
|
|
/** |
53 |
dl |
1.12 |
* Completed submit(callable) returns result |
54 |
tim |
1.1 |
*/ |
55 |
jsr166 |
1.19 |
public void testSubmitCallable() throws Exception { |
56 |
|
|
ExecutorService e = new DirectExecutorService(); |
57 |
|
|
Future<String> future = e.submit(new StringTask()); |
58 |
|
|
String result = future.get(); |
59 |
|
|
assertSame(TEST_STRING, result); |
60 |
tim |
1.1 |
} |
61 |
|
|
|
62 |
dl |
1.6 |
/** |
63 |
dl |
1.12 |
* Completed submit(runnable) returns successfully |
64 |
dl |
1.6 |
*/ |
65 |
jsr166 |
1.19 |
public void testSubmitRunnable() throws Exception { |
66 |
|
|
ExecutorService e = new DirectExecutorService(); |
67 |
|
|
Future<?> future = e.submit(new NoOpRunnable()); |
68 |
|
|
future.get(); |
69 |
|
|
assertTrue(future.isDone()); |
70 |
dl |
1.6 |
} |
71 |
|
|
|
72 |
|
|
/** |
73 |
dl |
1.12 |
* Completed submit(runnable, result) returns result |
74 |
dl |
1.6 |
*/ |
75 |
jsr166 |
1.19 |
public void testSubmitRunnable2() throws Exception { |
76 |
|
|
ExecutorService e = new DirectExecutorService(); |
77 |
|
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
78 |
|
|
String result = future.get(); |
79 |
|
|
assertSame(TEST_STRING, result); |
80 |
dl |
1.6 |
} |
81 |
|
|
|
82 |
tim |
1.1 |
|
83 |
|
|
/** |
84 |
dl |
1.12 |
* A submitted privileged action to completion |
85 |
tim |
1.1 |
*/ |
86 |
jsr166 |
1.19 |
public void testSubmitPrivilegedAction() throws Exception { |
87 |
dl |
1.15 |
Policy savedPolicy = null; |
88 |
|
|
try { |
89 |
|
|
savedPolicy = Policy.getPolicy(); |
90 |
|
|
AdjustablePolicy policy = new AdjustablePolicy(); |
91 |
|
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
92 |
|
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
93 |
|
|
Policy.setPolicy(policy); |
94 |
jsr166 |
1.17 |
} catch (AccessControlException ok) { |
95 |
dl |
1.15 |
return; |
96 |
|
|
} |
97 |
tim |
1.1 |
try { |
98 |
|
|
ExecutorService e = new DirectExecutorService(); |
99 |
dl |
1.5 |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
100 |
tim |
1.1 |
public Object run() { |
101 |
|
|
return TEST_STRING; |
102 |
dl |
1.5 |
}})); |
103 |
tim |
1.1 |
|
104 |
|
|
Object result = future.get(); |
105 |
|
|
assertSame(TEST_STRING, result); |
106 |
|
|
} |
107 |
|
|
finally { |
108 |
dl |
1.15 |
try { |
109 |
|
|
Policy.setPolicy(savedPolicy); |
110 |
jsr166 |
1.17 |
} catch (AccessControlException ok) { |
111 |
dl |
1.15 |
return; |
112 |
|
|
} |
113 |
tim |
1.1 |
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/** |
117 |
dl |
1.12 |
* A submitted a privileged exception action runs to completion |
118 |
tim |
1.1 |
*/ |
119 |
jsr166 |
1.19 |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
120 |
dl |
1.15 |
Policy savedPolicy = null; |
121 |
|
|
try { |
122 |
|
|
savedPolicy = Policy.getPolicy(); |
123 |
|
|
AdjustablePolicy policy = new AdjustablePolicy(); |
124 |
|
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
125 |
|
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
126 |
|
|
Policy.setPolicy(policy); |
127 |
jsr166 |
1.17 |
} catch (AccessControlException ok) { |
128 |
dl |
1.15 |
return; |
129 |
|
|
} |
130 |
|
|
|
131 |
tim |
1.1 |
try { |
132 |
|
|
ExecutorService e = new DirectExecutorService(); |
133 |
dl |
1.5 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
134 |
tim |
1.1 |
public Object run() { |
135 |
|
|
return TEST_STRING; |
136 |
dl |
1.5 |
}})); |
137 |
tim |
1.1 |
|
138 |
|
|
Object result = future.get(); |
139 |
|
|
assertSame(TEST_STRING, result); |
140 |
|
|
} |
141 |
|
|
finally { |
142 |
|
|
Policy.setPolicy(savedPolicy); |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/** |
147 |
dl |
1.12 |
* A submitted failed privileged exception action reports exception |
148 |
tim |
1.1 |
*/ |
149 |
jsr166 |
1.19 |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
150 |
dl |
1.15 |
Policy savedPolicy = null; |
151 |
|
|
try { |
152 |
|
|
savedPolicy = Policy.getPolicy(); |
153 |
|
|
AdjustablePolicy policy = new AdjustablePolicy(); |
154 |
|
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
155 |
|
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
156 |
|
|
Policy.setPolicy(policy); |
157 |
jsr166 |
1.17 |
} catch (AccessControlException ok) { |
158 |
dl |
1.15 |
return; |
159 |
|
|
} |
160 |
|
|
|
161 |
tim |
1.1 |
try { |
162 |
|
|
ExecutorService e = new DirectExecutorService(); |
163 |
dl |
1.5 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
164 |
tim |
1.1 |
public Object run() throws Exception { |
165 |
|
|
throw new IndexOutOfBoundsException(); |
166 |
dl |
1.5 |
}})); |
167 |
tim |
1.1 |
|
168 |
jsr166 |
1.19 |
future.get(); |
169 |
tim |
1.1 |
shouldThrow(); |
170 |
jsr166 |
1.19 |
} catch (ExecutionException success) { |
171 |
|
|
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
172 |
tim |
1.1 |
} |
173 |
|
|
finally { |
174 |
|
|
Policy.setPolicy(savedPolicy); |
175 |
|
|
} |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
/** |
179 |
dl |
1.12 |
* execute(null runnable) throws NPE |
180 |
tim |
1.1 |
*/ |
181 |
|
|
public void testExecuteNullRunnable() { |
182 |
|
|
try { |
183 |
|
|
ExecutorService e = new DirectExecutorService(); |
184 |
jsr166 |
1.19 |
e.submit((Runnable) null); |
185 |
tim |
1.1 |
shouldThrow(); |
186 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
187 |
tim |
1.1 |
} |
188 |
|
|
|
189 |
|
|
|
190 |
|
|
/** |
191 |
dl |
1.12 |
* submit(null callable) throws NPE |
192 |
tim |
1.1 |
*/ |
193 |
dl |
1.6 |
public void testSubmitNullCallable() { |
194 |
tim |
1.1 |
try { |
195 |
|
|
ExecutorService e = new DirectExecutorService(); |
196 |
jsr166 |
1.19 |
e.submit((Callable) null); |
197 |
tim |
1.1 |
shouldThrow(); |
198 |
jsr166 |
1.19 |
} catch (NullPointerException success) {} |
199 |
tim |
1.1 |
} |
200 |
|
|
|
201 |
|
|
/** |
202 |
dl |
1.12 |
* submit(runnable) throws RejectedExecutionException if |
203 |
|
|
* executor is saturated. |
204 |
tim |
1.1 |
*/ |
205 |
|
|
public void testExecute1() { |
206 |
jsr166 |
1.19 |
ThreadPoolExecutor p = |
207 |
|
|
new ThreadPoolExecutor(1, 1, |
208 |
|
|
60, TimeUnit.SECONDS, |
209 |
|
|
new ArrayBlockingQueue<Runnable>(1)); |
210 |
tim |
1.1 |
try { |
211 |
jsr166 |
1.19 |
for (int i = 0; i < 2; ++i) |
212 |
dl |
1.4 |
p.submit(new MediumRunnable()); |
213 |
jsr166 |
1.19 |
for (int i = 0; i < 2; ++i) { |
214 |
|
|
try { |
215 |
|
|
p.submit(new MediumRunnable()); |
216 |
|
|
shouldThrow(); |
217 |
|
|
} catch (RejectedExecutionException success) {} |
218 |
tim |
1.1 |
} |
219 |
jsr166 |
1.19 |
} finally { |
220 |
|
|
joinPool(p); |
221 |
|
|
} |
222 |
tim |
1.1 |
} |
223 |
|
|
|
224 |
|
|
/** |
225 |
dl |
1.12 |
* submit(callable) throws RejectedExecutionException |
226 |
|
|
* if executor is saturated. |
227 |
tim |
1.1 |
*/ |
228 |
|
|
public void testExecute2() { |
229 |
jsr166 |
1.19 |
ThreadPoolExecutor p = |
230 |
|
|
new ThreadPoolExecutor(1, 1, |
231 |
|
|
60, TimeUnit.SECONDS, |
232 |
|
|
new ArrayBlockingQueue<Runnable>(1)); |
233 |
tim |
1.1 |
try { |
234 |
jsr166 |
1.19 |
for (int i = 0; i < 2; ++i) |
235 |
|
|
p.submit(new MediumRunnable()); |
236 |
|
|
for (int i = 0; i < 2; ++i) { |
237 |
|
|
try { |
238 |
|
|
p.submit(new SmallCallable()); |
239 |
|
|
shouldThrow(); |
240 |
|
|
} catch (RejectedExecutionException success) {} |
241 |
tim |
1.1 |
} |
242 |
jsr166 |
1.19 |
} finally { |
243 |
|
|
joinPool(p); |
244 |
|
|
} |
245 |
tim |
1.1 |
} |
246 |
|
|
|
247 |
|
|
|
248 |
|
|
/** |
249 |
dl |
1.12 |
* Blocking on submit(callable) throws InterruptedException if |
250 |
tim |
1.1 |
* caller interrupted. |
251 |
|
|
*/ |
252 |
jsr166 |
1.19 |
public void testInterruptedSubmit() throws InterruptedException { |
253 |
dl |
1.13 |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
254 |
jsr166 |
1.19 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
255 |
|
|
public void realRun() throws Exception { |
256 |
|
|
p.submit(new CheckedCallable<Object>() { |
257 |
|
|
public Object realCall() |
258 |
|
|
throws InterruptedException { |
259 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
260 |
|
|
return null; |
261 |
|
|
}}).get(); |
262 |
|
|
}}); |
263 |
|
|
|
264 |
|
|
t.start(); |
265 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
266 |
|
|
t.interrupt(); |
267 |
tim |
1.1 |
joinPool(p); |
268 |
|
|
} |
269 |
|
|
|
270 |
|
|
/** |
271 |
jsr166 |
1.19 |
* get of submitted callable throws InterruptedException if callable |
272 |
dl |
1.6 |
* interrupted |
273 |
tim |
1.1 |
*/ |
274 |
jsr166 |
1.19 |
public void testSubmitIE() throws InterruptedException { |
275 |
|
|
final ThreadPoolExecutor p = |
276 |
|
|
new ThreadPoolExecutor(1, 1, |
277 |
|
|
60, TimeUnit.SECONDS, |
278 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
279 |
|
|
|
280 |
|
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
281 |
|
|
public void realRun() throws Exception { |
282 |
|
|
p.submit(new SmallCallable()).get(); |
283 |
|
|
}}); |
284 |
|
|
|
285 |
|
|
t.start(); |
286 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
287 |
|
|
t.interrupt(); |
288 |
|
|
t.join(); |
289 |
tim |
1.1 |
joinPool(p); |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
/** |
293 |
dl |
1.12 |
* get of submit(callable) throws ExecutionException if callable |
294 |
|
|
* throws exception |
295 |
tim |
1.1 |
*/ |
296 |
jsr166 |
1.19 |
public void testSubmitEE() throws InterruptedException { |
297 |
|
|
ThreadPoolExecutor p = |
298 |
|
|
new ThreadPoolExecutor(1, 1, |
299 |
|
|
60, TimeUnit.SECONDS, |
300 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
301 |
|
|
|
302 |
|
|
Callable c = new Callable() { |
303 |
|
|
public Object call() { return 5/0; }}; |
304 |
tim |
1.1 |
|
305 |
|
|
try { |
306 |
jsr166 |
1.19 |
p.submit(c).get(); |
307 |
tim |
1.1 |
shouldThrow(); |
308 |
jsr166 |
1.19 |
} catch (ExecutionException success) { |
309 |
|
|
assertTrue(success.getCause() instanceof ArithmeticException); |
310 |
tim |
1.1 |
} |
311 |
|
|
joinPool(p); |
312 |
dl |
1.6 |
} |
313 |
|
|
|
314 |
|
|
/** |
315 |
|
|
* invokeAny(null) throws NPE |
316 |
|
|
*/ |
317 |
jsr166 |
1.19 |
public void testInvokeAny1() |
318 |
|
|
throws InterruptedException, ExecutionException { |
319 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
320 |
|
|
try { |
321 |
|
|
e.invokeAny(null); |
322 |
jsr166 |
1.19 |
shouldThrow(); |
323 |
dl |
1.6 |
} catch (NullPointerException success) { |
324 |
|
|
} finally { |
325 |
|
|
joinPool(e); |
326 |
|
|
} |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
|
|
* invokeAny(empty collection) throws IAE |
331 |
|
|
*/ |
332 |
jsr166 |
1.19 |
public void testInvokeAny2() |
333 |
|
|
throws InterruptedException, ExecutionException { |
334 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
335 |
|
|
try { |
336 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
337 |
jsr166 |
1.19 |
shouldThrow(); |
338 |
dl |
1.6 |
} catch (IllegalArgumentException success) { |
339 |
|
|
} finally { |
340 |
|
|
joinPool(e); |
341 |
|
|
} |
342 |
|
|
} |
343 |
|
|
|
344 |
|
|
/** |
345 |
|
|
* invokeAny(c) throws NPE if c has null elements |
346 |
|
|
*/ |
347 |
jsr166 |
1.19 |
public void testInvokeAny3() throws Exception { |
348 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
349 |
jsr166 |
1.22 |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
350 |
|
|
l.add(new Callable<Integer>() { |
351 |
|
|
public Integer call() { return 5/0; }}); |
352 |
|
|
l.add(null); |
353 |
dl |
1.6 |
try { |
354 |
|
|
e.invokeAny(l); |
355 |
jsr166 |
1.19 |
shouldThrow(); |
356 |
dl |
1.6 |
} catch (NullPointerException success) { |
357 |
|
|
} finally { |
358 |
|
|
joinPool(e); |
359 |
|
|
} |
360 |
|
|
} |
361 |
|
|
|
362 |
|
|
/** |
363 |
dl |
1.12 |
* invokeAny(c) throws ExecutionException if no task in c completes |
364 |
dl |
1.6 |
*/ |
365 |
jsr166 |
1.19 |
public void testInvokeAny4() throws InterruptedException { |
366 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
367 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
368 |
|
|
l.add(new NPETask()); |
369 |
dl |
1.6 |
try { |
370 |
dl |
1.8 |
e.invokeAny(l); |
371 |
jsr166 |
1.19 |
shouldThrow(); |
372 |
jsr166 |
1.17 |
} catch (ExecutionException success) { |
373 |
jsr166 |
1.19 |
assertTrue(success.getCause() instanceof NullPointerException); |
374 |
dl |
1.6 |
} finally { |
375 |
|
|
joinPool(e); |
376 |
|
|
} |
377 |
|
|
} |
378 |
|
|
|
379 |
|
|
/** |
380 |
dl |
1.12 |
* invokeAny(c) returns result of some task in c if at least one completes |
381 |
dl |
1.6 |
*/ |
382 |
jsr166 |
1.19 |
public void testInvokeAny5() throws Exception { |
383 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
384 |
|
|
try { |
385 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
386 |
dl |
1.6 |
l.add(new StringTask()); |
387 |
|
|
l.add(new StringTask()); |
388 |
|
|
String result = e.invokeAny(l); |
389 |
|
|
assertSame(TEST_STRING, result); |
390 |
|
|
} finally { |
391 |
|
|
joinPool(e); |
392 |
|
|
} |
393 |
|
|
} |
394 |
|
|
|
395 |
|
|
/** |
396 |
|
|
* invokeAll(null) throws NPE |
397 |
|
|
*/ |
398 |
jsr166 |
1.19 |
public void testInvokeAll1() throws InterruptedException { |
399 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
400 |
|
|
try { |
401 |
|
|
e.invokeAll(null); |
402 |
jsr166 |
1.19 |
shouldThrow(); |
403 |
dl |
1.6 |
} catch (NullPointerException success) { |
404 |
|
|
} finally { |
405 |
|
|
joinPool(e); |
406 |
|
|
} |
407 |
|
|
} |
408 |
|
|
|
409 |
|
|
/** |
410 |
|
|
* invokeAll(empty collection) returns empty collection |
411 |
|
|
*/ |
412 |
jsr166 |
1.19 |
public void testInvokeAll2() throws InterruptedException { |
413 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
414 |
|
|
try { |
415 |
|
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
416 |
|
|
assertTrue(r.isEmpty()); |
417 |
|
|
} finally { |
418 |
|
|
joinPool(e); |
419 |
|
|
} |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
/** |
423 |
|
|
* invokeAll(c) throws NPE if c has null elements |
424 |
|
|
*/ |
425 |
jsr166 |
1.19 |
public void testInvokeAll3() throws InterruptedException { |
426 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
427 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
428 |
|
|
l.add(new StringTask()); |
429 |
|
|
l.add(null); |
430 |
dl |
1.6 |
try { |
431 |
|
|
e.invokeAll(l); |
432 |
jsr166 |
1.19 |
shouldThrow(); |
433 |
dl |
1.6 |
} catch (NullPointerException success) { |
434 |
|
|
} finally { |
435 |
|
|
joinPool(e); |
436 |
|
|
} |
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
/** |
440 |
dl |
1.12 |
* get of returned element of invokeAll(c) throws exception on failed task |
441 |
dl |
1.6 |
*/ |
442 |
jsr166 |
1.19 |
public void testInvokeAll4() throws Exception { |
443 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
444 |
|
|
try { |
445 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
446 |
dl |
1.6 |
l.add(new NPETask()); |
447 |
jsr166 |
1.22 |
List<Future<String>> futures = e.invokeAll(l); |
448 |
|
|
assertEquals(1, futures.size()); |
449 |
|
|
try { |
450 |
|
|
futures.get(0).get(); |
451 |
|
|
shouldThrow(); |
452 |
|
|
} catch (ExecutionException success) { |
453 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
454 |
jsr166 |
1.19 |
} |
455 |
dl |
1.6 |
} finally { |
456 |
|
|
joinPool(e); |
457 |
|
|
} |
458 |
|
|
} |
459 |
|
|
|
460 |
|
|
/** |
461 |
dl |
1.12 |
* invokeAll(c) returns results of all completed tasks in c |
462 |
dl |
1.6 |
*/ |
463 |
jsr166 |
1.19 |
public void testInvokeAll5() throws Exception { |
464 |
dl |
1.6 |
ExecutorService e = new DirectExecutorService(); |
465 |
|
|
try { |
466 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
467 |
dl |
1.6 |
l.add(new StringTask()); |
468 |
|
|
l.add(new StringTask()); |
469 |
jsr166 |
1.22 |
List<Future<String>> futures = e.invokeAll(l); |
470 |
|
|
assertEquals(2, futures.size()); |
471 |
|
|
for (Future<String> future : futures) |
472 |
jsr166 |
1.19 |
assertSame(TEST_STRING, future.get()); |
473 |
dl |
1.8 |
} finally { |
474 |
|
|
joinPool(e); |
475 |
|
|
} |
476 |
|
|
} |
477 |
|
|
|
478 |
|
|
|
479 |
|
|
/** |
480 |
|
|
* timed invokeAny(null) throws NPE |
481 |
|
|
*/ |
482 |
jsr166 |
1.19 |
public void testTimedInvokeAny1() throws Exception { |
483 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
484 |
|
|
try { |
485 |
jsr166 |
1.20 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
486 |
jsr166 |
1.19 |
shouldThrow(); |
487 |
dl |
1.8 |
} catch (NullPointerException success) { |
488 |
|
|
} finally { |
489 |
|
|
joinPool(e); |
490 |
|
|
} |
491 |
|
|
} |
492 |
|
|
|
493 |
|
|
/** |
494 |
dl |
1.12 |
* timed invokeAny(null time unit) throws NPE |
495 |
dl |
1.8 |
*/ |
496 |
jsr166 |
1.19 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
497 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
498 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
499 |
|
|
l.add(new StringTask()); |
500 |
dl |
1.8 |
try { |
501 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
502 |
jsr166 |
1.19 |
shouldThrow(); |
503 |
dl |
1.8 |
} catch (NullPointerException success) { |
504 |
|
|
} finally { |
505 |
|
|
joinPool(e); |
506 |
|
|
} |
507 |
|
|
} |
508 |
|
|
|
509 |
|
|
/** |
510 |
|
|
* timed invokeAny(empty collection) throws IAE |
511 |
|
|
*/ |
512 |
jsr166 |
1.19 |
public void testTimedInvokeAny2() throws Exception { |
513 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
514 |
|
|
try { |
515 |
jsr166 |
1.20 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
516 |
jsr166 |
1.19 |
shouldThrow(); |
517 |
dl |
1.8 |
} catch (IllegalArgumentException success) { |
518 |
|
|
} finally { |
519 |
|
|
joinPool(e); |
520 |
|
|
} |
521 |
|
|
} |
522 |
|
|
|
523 |
|
|
/** |
524 |
|
|
* timed invokeAny(c) throws NPE if c has null elements |
525 |
|
|
*/ |
526 |
jsr166 |
1.19 |
public void testTimedInvokeAny3() throws Exception { |
527 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
528 |
jsr166 |
1.22 |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
529 |
|
|
l.add(new Callable<Integer>() { |
530 |
|
|
public Integer call() { return 5/0; }}); |
531 |
|
|
l.add(null); |
532 |
dl |
1.8 |
try { |
533 |
jsr166 |
1.20 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
534 |
jsr166 |
1.19 |
shouldThrow(); |
535 |
dl |
1.8 |
} catch (NullPointerException success) { |
536 |
|
|
} finally { |
537 |
|
|
joinPool(e); |
538 |
|
|
} |
539 |
|
|
} |
540 |
|
|
|
541 |
|
|
/** |
542 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
543 |
|
|
*/ |
544 |
jsr166 |
1.19 |
public void testTimedInvokeAny4() throws Exception { |
545 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
546 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
547 |
|
|
l.add(new NPETask()); |
548 |
dl |
1.8 |
try { |
549 |
jsr166 |
1.20 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
550 |
jsr166 |
1.19 |
shouldThrow(); |
551 |
jsr166 |
1.17 |
} catch (ExecutionException success) { |
552 |
jsr166 |
1.19 |
assertTrue(success.getCause() instanceof NullPointerException); |
553 |
dl |
1.8 |
} finally { |
554 |
|
|
joinPool(e); |
555 |
|
|
} |
556 |
|
|
} |
557 |
|
|
|
558 |
|
|
/** |
559 |
dl |
1.12 |
* timed invokeAny(c) returns result of some task in c |
560 |
dl |
1.8 |
*/ |
561 |
jsr166 |
1.19 |
public void testTimedInvokeAny5() throws Exception { |
562 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
563 |
|
|
try { |
564 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
565 |
dl |
1.8 |
l.add(new StringTask()); |
566 |
|
|
l.add(new StringTask()); |
567 |
jsr166 |
1.20 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
568 |
dl |
1.8 |
assertSame(TEST_STRING, result); |
569 |
|
|
} finally { |
570 |
|
|
joinPool(e); |
571 |
|
|
} |
572 |
|
|
} |
573 |
|
|
|
574 |
|
|
/** |
575 |
|
|
* timed invokeAll(null) throws NPE |
576 |
|
|
*/ |
577 |
jsr166 |
1.19 |
public void testTimedInvokeAll1() throws InterruptedException { |
578 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
579 |
|
|
try { |
580 |
jsr166 |
1.20 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
581 |
jsr166 |
1.19 |
shouldThrow(); |
582 |
dl |
1.8 |
} catch (NullPointerException success) { |
583 |
|
|
} finally { |
584 |
|
|
joinPool(e); |
585 |
|
|
} |
586 |
|
|
} |
587 |
|
|
|
588 |
|
|
/** |
589 |
dl |
1.12 |
* timed invokeAll(null time unit) throws NPE |
590 |
dl |
1.8 |
*/ |
591 |
jsr166 |
1.19 |
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
592 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
593 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
594 |
|
|
l.add(new StringTask()); |
595 |
dl |
1.8 |
try { |
596 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
597 |
jsr166 |
1.19 |
shouldThrow(); |
598 |
dl |
1.8 |
} catch (NullPointerException success) { |
599 |
|
|
} finally { |
600 |
|
|
joinPool(e); |
601 |
|
|
} |
602 |
|
|
} |
603 |
|
|
|
604 |
|
|
/** |
605 |
|
|
* timed invokeAll(empty collection) returns empty collection |
606 |
|
|
*/ |
607 |
jsr166 |
1.19 |
public void testTimedInvokeAll2() throws InterruptedException { |
608 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
609 |
|
|
try { |
610 |
jsr166 |
1.20 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
611 |
dl |
1.8 |
assertTrue(r.isEmpty()); |
612 |
|
|
} finally { |
613 |
|
|
joinPool(e); |
614 |
|
|
} |
615 |
|
|
} |
616 |
|
|
|
617 |
|
|
/** |
618 |
|
|
* timed invokeAll(c) throws NPE if c has null elements |
619 |
|
|
*/ |
620 |
jsr166 |
1.19 |
public void testTimedInvokeAll3() throws InterruptedException { |
621 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
622 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
623 |
|
|
l.add(new StringTask()); |
624 |
|
|
l.add(null); |
625 |
dl |
1.8 |
try { |
626 |
jsr166 |
1.20 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
627 |
jsr166 |
1.19 |
shouldThrow(); |
628 |
dl |
1.8 |
} catch (NullPointerException success) { |
629 |
|
|
} finally { |
630 |
|
|
joinPool(e); |
631 |
|
|
} |
632 |
|
|
} |
633 |
|
|
|
634 |
|
|
/** |
635 |
dl |
1.12 |
* get of returned element of invokeAll(c) throws exception on failed task |
636 |
dl |
1.8 |
*/ |
637 |
jsr166 |
1.19 |
public void testTimedInvokeAll4() throws Exception { |
638 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
639 |
|
|
try { |
640 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
641 |
dl |
1.8 |
l.add(new NPETask()); |
642 |
jsr166 |
1.22 |
List<Future<String>> futures = |
643 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
644 |
|
|
assertEquals(1, futures.size()); |
645 |
|
|
try { |
646 |
|
|
futures.get(0).get(); |
647 |
|
|
shouldThrow(); |
648 |
|
|
} catch (ExecutionException success) { |
649 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
650 |
jsr166 |
1.19 |
} |
651 |
dl |
1.8 |
} finally { |
652 |
|
|
joinPool(e); |
653 |
|
|
} |
654 |
|
|
} |
655 |
|
|
|
656 |
|
|
/** |
657 |
dl |
1.12 |
* timed invokeAll(c) returns results of all completed tasks in c |
658 |
dl |
1.8 |
*/ |
659 |
jsr166 |
1.19 |
public void testTimedInvokeAll5() throws Exception { |
660 |
dl |
1.8 |
ExecutorService e = new DirectExecutorService(); |
661 |
|
|
try { |
662 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
663 |
dl |
1.8 |
l.add(new StringTask()); |
664 |
|
|
l.add(new StringTask()); |
665 |
jsr166 |
1.22 |
List<Future<String>> futures = |
666 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
667 |
|
|
assertEquals(2, futures.size()); |
668 |
|
|
for (Future<String> future : futures) |
669 |
jsr166 |
1.19 |
assertSame(TEST_STRING, future.get()); |
670 |
dl |
1.8 |
} finally { |
671 |
|
|
joinPool(e); |
672 |
|
|
} |
673 |
|
|
} |
674 |
|
|
|
675 |
|
|
/** |
676 |
dl |
1.12 |
* timed invokeAll cancels tasks not completed by timeout |
677 |
dl |
1.8 |
*/ |
678 |
jsr166 |
1.19 |
public void testTimedInvokeAll6() throws InterruptedException { |
679 |
dl |
1.9 |
ExecutorService e = new DirectExecutorService(); |
680 |
dl |
1.8 |
try { |
681 |
jsr166 |
1.22 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
682 |
dl |
1.8 |
l.add(new StringTask()); |
683 |
dl |
1.11 |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
684 |
|
|
l.add(new StringTask()); |
685 |
jsr166 |
1.22 |
List<Future<String>> futures = |
686 |
|
|
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
687 |
|
|
assertEquals(3, futures.size()); |
688 |
|
|
Iterator<Future<String>> it = futures.iterator(); |
689 |
dl |
1.8 |
Future<String> f1 = it.next(); |
690 |
|
|
Future<String> f2 = it.next(); |
691 |
dl |
1.11 |
Future<String> f3 = it.next(); |
692 |
dl |
1.8 |
assertTrue(f1.isDone()); |
693 |
|
|
assertFalse(f1.isCancelled()); |
694 |
|
|
assertTrue(f2.isDone()); |
695 |
dl |
1.11 |
assertTrue(f3.isDone()); |
696 |
|
|
assertTrue(f3.isCancelled()); |
697 |
dl |
1.6 |
} finally { |
698 |
|
|
joinPool(e); |
699 |
|
|
} |
700 |
tim |
1.1 |
} |
701 |
|
|
|
702 |
dl |
1.3 |
} |