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