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