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