6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
10 |
– |
import junit.framework.*; |
11 |
– |
import java.util.*; |
12 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.math.BigInteger; |
11 |
< |
import java.security.*; |
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.CancellationException; |
20 |
> |
import java.util.concurrent.CountDownLatch; |
21 |
> |
import java.util.concurrent.ExecutionException; |
22 |
> |
import java.util.concurrent.Executors; |
23 |
> |
import java.util.concurrent.ExecutorService; |
24 |
> |
import java.util.concurrent.Future; |
25 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
26 |
> |
import java.util.concurrent.TimeUnit; |
27 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
28 |
> |
|
29 |
> |
import junit.framework.Test; |
30 |
> |
import junit.framework.TestSuite; |
31 |
|
|
32 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
33 |
|
public static void main(String[] args) { |
34 |
< |
junit.textui.TestRunner.run(suite()); |
34 |
> |
main(suite(), args); |
35 |
|
} |
36 |
|
public static Test suite() { |
37 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
61 |
|
*/ |
62 |
|
public void testExecuteRunnable() throws Exception { |
63 |
|
ExecutorService e = new DirectExecutorService(); |
64 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
65 |
< |
assertFalse(task.done); |
66 |
< |
Future<?> future = e.submit(task); |
67 |
< |
future.get(); |
68 |
< |
assertTrue(task.done); |
64 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
65 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
66 |
> |
public void realRun() { |
67 |
> |
done.set(true); |
68 |
> |
}}); |
69 |
> |
assertNull(future.get()); |
70 |
> |
assertNull(future.get(0, MILLISECONDS)); |
71 |
> |
assertTrue(done.get()); |
72 |
> |
assertTrue(future.isDone()); |
73 |
> |
assertFalse(future.isCancelled()); |
74 |
|
} |
75 |
|
|
56 |
– |
|
76 |
|
/** |
77 |
|
* Completed submit(callable) returns result |
78 |
|
*/ |
103 |
|
assertSame(TEST_STRING, result); |
104 |
|
} |
105 |
|
|
87 |
– |
|
106 |
|
/** |
107 |
|
* A submitted privileged action runs to completion |
108 |
|
*/ |
168 |
|
* execute(null runnable) throws NPE |
169 |
|
*/ |
170 |
|
public void testExecuteNullRunnable() { |
171 |
+ |
ExecutorService e = new DirectExecutorService(); |
172 |
|
try { |
154 |
– |
ExecutorService e = new DirectExecutorService(); |
173 |
|
e.submit((Runnable) null); |
174 |
|
shouldThrow(); |
175 |
|
} catch (NullPointerException success) {} |
176 |
|
} |
177 |
|
|
160 |
– |
|
178 |
|
/** |
179 |
|
* submit(null callable) throws NPE |
180 |
|
*/ |
181 |
|
public void testSubmitNullCallable() { |
182 |
+ |
ExecutorService e = new DirectExecutorService(); |
183 |
|
try { |
166 |
– |
ExecutorService e = new DirectExecutorService(); |
184 |
|
e.submit((Callable) null); |
185 |
|
shouldThrow(); |
186 |
|
} catch (NullPointerException success) {} |
192 |
|
public void testInterruptedSubmit() throws InterruptedException { |
193 |
|
final CountDownLatch submitted = new CountDownLatch(1); |
194 |
|
final CountDownLatch quittingTime = new CountDownLatch(1); |
178 |
– |
final ExecutorService p |
179 |
– |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
180 |
– |
new ArrayBlockingQueue<Runnable>(10)); |
195 |
|
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
196 |
|
public Void realCall() throws InterruptedException { |
197 |
< |
quittingTime.await(); |
197 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
198 |
|
return null; |
199 |
|
}}; |
200 |
< |
try { |
201 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
200 |
> |
final ExecutorService p |
201 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
202 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
203 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
204 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
205 |
|
public void realRun() throws Exception { |
206 |
|
Future<Void> future = p.submit(awaiter); |
207 |
|
submitted.countDown(); |
208 |
|
future.get(); |
209 |
|
}}); |
210 |
< |
t.start(); |
211 |
< |
submitted.await(); |
210 |
> |
|
211 |
> |
await(submitted); |
212 |
|
t.interrupt(); |
213 |
< |
t.join(); |
197 |
< |
} finally { |
198 |
< |
quittingTime.countDown(); |
199 |
< |
joinPool(p); |
213 |
> |
awaitTermination(t); |
214 |
|
} |
215 |
|
} |
216 |
|
|
219 |
|
* throws exception |
220 |
|
*/ |
221 |
|
public void testSubmitEE() throws InterruptedException { |
222 |
< |
ThreadPoolExecutor p = |
222 |
> |
final ThreadPoolExecutor p = |
223 |
|
new ThreadPoolExecutor(1, 1, |
224 |
|
60, TimeUnit.SECONDS, |
225 |
|
new ArrayBlockingQueue<Runnable>(10)); |
226 |
< |
|
227 |
< |
Callable c = new Callable() { |
228 |
< |
public Object call() { return 5/0; }}; |
229 |
< |
|
230 |
< |
try { |
231 |
< |
p.submit(c).get(); |
232 |
< |
shouldThrow(); |
233 |
< |
} catch (ExecutionException success) { |
234 |
< |
assertTrue(success.getCause() instanceof ArithmeticException); |
226 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
227 |
> |
Callable c = new Callable() { |
228 |
> |
public Object call() { throw new ArithmeticException(); }}; |
229 |
> |
try { |
230 |
> |
p.submit(c).get(); |
231 |
> |
shouldThrow(); |
232 |
> |
} catch (ExecutionException success) { |
233 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
234 |
> |
} |
235 |
|
} |
222 |
– |
joinPool(p); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
|
* invokeAny(null) throws NPE |
240 |
|
*/ |
241 |
|
public void testInvokeAny1() throws Exception { |
242 |
< |
ExecutorService e = new DirectExecutorService(); |
243 |
< |
try { |
244 |
< |
e.invokeAny(null); |
245 |
< |
shouldThrow(); |
246 |
< |
} catch (NullPointerException success) { |
247 |
< |
} finally { |
235 |
< |
joinPool(e); |
242 |
> |
final ExecutorService e = new DirectExecutorService(); |
243 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
244 |
> |
try { |
245 |
> |
e.invokeAny(null); |
246 |
> |
shouldThrow(); |
247 |
> |
} catch (NullPointerException success) {} |
248 |
|
} |
249 |
|
} |
250 |
|
|
252 |
|
* invokeAny(empty collection) throws IAE |
253 |
|
*/ |
254 |
|
public void testInvokeAny2() throws Exception { |
255 |
< |
ExecutorService e = new DirectExecutorService(); |
256 |
< |
try { |
257 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
258 |
< |
shouldThrow(); |
259 |
< |
} catch (IllegalArgumentException success) { |
260 |
< |
} finally { |
249 |
< |
joinPool(e); |
255 |
> |
final ExecutorService e = new DirectExecutorService(); |
256 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
257 |
> |
try { |
258 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
259 |
> |
shouldThrow(); |
260 |
> |
} catch (IllegalArgumentException success) {} |
261 |
|
} |
262 |
|
} |
263 |
|
|
265 |
|
* invokeAny(c) throws NPE if c has null elements |
266 |
|
*/ |
267 |
|
public void testInvokeAny3() throws Exception { |
268 |
< |
ExecutorService e = new DirectExecutorService(); |
269 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
270 |
< |
l.add(new Callable<Integer>() { |
271 |
< |
public Integer call() { return 5/0; }}); |
272 |
< |
l.add(null); |
273 |
< |
try { |
274 |
< |
e.invokeAny(l); |
275 |
< |
shouldThrow(); |
276 |
< |
} catch (NullPointerException success) { |
277 |
< |
} finally { |
267 |
< |
joinPool(e); |
268 |
> |
final ExecutorService e = new DirectExecutorService(); |
269 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
270 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
271 |
> |
l.add(new Callable<Long>() { |
272 |
> |
public Long call() { throw new ArithmeticException(); }}); |
273 |
> |
l.add(null); |
274 |
> |
try { |
275 |
> |
e.invokeAny(l); |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (NullPointerException success) {} |
278 |
|
} |
279 |
|
} |
280 |
|
|
282 |
|
* invokeAny(c) throws ExecutionException if no task in c completes |
283 |
|
*/ |
284 |
|
public void testInvokeAny4() throws InterruptedException { |
285 |
< |
ExecutorService e = new DirectExecutorService(); |
286 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
287 |
< |
l.add(new NPETask()); |
288 |
< |
try { |
289 |
< |
e.invokeAny(l); |
290 |
< |
shouldThrow(); |
291 |
< |
} catch (ExecutionException success) { |
292 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
293 |
< |
} finally { |
294 |
< |
joinPool(e); |
285 |
> |
final ExecutorService e = new DirectExecutorService(); |
286 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
287 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
288 |
> |
l.add(new NPETask()); |
289 |
> |
try { |
290 |
> |
e.invokeAny(l); |
291 |
> |
shouldThrow(); |
292 |
> |
} catch (ExecutionException success) { |
293 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
294 |
> |
} |
295 |
|
} |
296 |
|
} |
297 |
|
|
299 |
|
* invokeAny(c) returns result of some task in c if at least one completes |
300 |
|
*/ |
301 |
|
public void testInvokeAny5() throws Exception { |
302 |
< |
ExecutorService e = new DirectExecutorService(); |
303 |
< |
try { |
302 |
> |
final ExecutorService e = new DirectExecutorService(); |
303 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
304 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
305 |
|
l.add(new StringTask()); |
306 |
|
l.add(new StringTask()); |
307 |
|
String result = e.invokeAny(l); |
308 |
|
assertSame(TEST_STRING, result); |
299 |
– |
} finally { |
300 |
– |
joinPool(e); |
309 |
|
} |
310 |
|
} |
311 |
|
|
313 |
|
* invokeAll(null) throws NPE |
314 |
|
*/ |
315 |
|
public void testInvokeAll1() throws InterruptedException { |
316 |
< |
ExecutorService e = new DirectExecutorService(); |
317 |
< |
try { |
318 |
< |
e.invokeAll(null); |
319 |
< |
shouldThrow(); |
320 |
< |
} catch (NullPointerException success) { |
321 |
< |
} finally { |
314 |
< |
joinPool(e); |
316 |
> |
final ExecutorService e = new DirectExecutorService(); |
317 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
318 |
> |
try { |
319 |
> |
e.invokeAll(null); |
320 |
> |
shouldThrow(); |
321 |
> |
} catch (NullPointerException success) {} |
322 |
|
} |
323 |
|
} |
324 |
|
|
326 |
|
* invokeAll(empty collection) returns empty collection |
327 |
|
*/ |
328 |
|
public void testInvokeAll2() throws InterruptedException { |
329 |
< |
ExecutorService e = new DirectExecutorService(); |
330 |
< |
try { |
329 |
> |
final ExecutorService e = new DirectExecutorService(); |
330 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
331 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
332 |
|
assertTrue(r.isEmpty()); |
326 |
– |
} finally { |
327 |
– |
joinPool(e); |
333 |
|
} |
334 |
|
} |
335 |
|
|
337 |
|
* invokeAll(c) throws NPE if c has null elements |
338 |
|
*/ |
339 |
|
public void testInvokeAll3() throws InterruptedException { |
340 |
< |
ExecutorService e = new DirectExecutorService(); |
341 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
342 |
< |
l.add(new StringTask()); |
343 |
< |
l.add(null); |
344 |
< |
try { |
345 |
< |
e.invokeAll(l); |
346 |
< |
shouldThrow(); |
347 |
< |
} catch (NullPointerException success) { |
348 |
< |
} finally { |
344 |
< |
joinPool(e); |
340 |
> |
final ExecutorService e = new DirectExecutorService(); |
341 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
342 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
343 |
> |
l.add(new StringTask()); |
344 |
> |
l.add(null); |
345 |
> |
try { |
346 |
> |
e.invokeAll(l); |
347 |
> |
shouldThrow(); |
348 |
> |
} catch (NullPointerException success) {} |
349 |
|
} |
350 |
|
} |
351 |
|
|
353 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
354 |
|
*/ |
355 |
|
public void testInvokeAll4() throws Exception { |
356 |
< |
ExecutorService e = new DirectExecutorService(); |
357 |
< |
try { |
356 |
> |
final ExecutorService e = new DirectExecutorService(); |
357 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
358 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
359 |
|
l.add(new NPETask()); |
360 |
|
List<Future<String>> futures = e.invokeAll(l); |
365 |
|
} catch (ExecutionException success) { |
366 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
367 |
|
} |
364 |
– |
} finally { |
365 |
– |
joinPool(e); |
368 |
|
} |
369 |
|
} |
370 |
|
|
372 |
|
* invokeAll(c) returns results of all completed tasks in c |
373 |
|
*/ |
374 |
|
public void testInvokeAll5() throws Exception { |
375 |
< |
ExecutorService e = new DirectExecutorService(); |
376 |
< |
try { |
375 |
> |
final ExecutorService e = new DirectExecutorService(); |
376 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
377 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
378 |
|
l.add(new StringTask()); |
379 |
|
l.add(new StringTask()); |
381 |
|
assertEquals(2, futures.size()); |
382 |
|
for (Future<String> future : futures) |
383 |
|
assertSame(TEST_STRING, future.get()); |
382 |
– |
} finally { |
383 |
– |
joinPool(e); |
384 |
|
} |
385 |
|
} |
386 |
|
|
387 |
– |
|
387 |
|
/** |
388 |
|
* timed invokeAny(null) throws NPE |
389 |
|
*/ |
390 |
|
public void testTimedInvokeAny1() throws Exception { |
391 |
< |
ExecutorService e = new DirectExecutorService(); |
392 |
< |
try { |
393 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
394 |
< |
shouldThrow(); |
395 |
< |
} catch (NullPointerException success) { |
396 |
< |
} finally { |
398 |
< |
joinPool(e); |
391 |
> |
final ExecutorService e = new DirectExecutorService(); |
392 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
393 |
> |
try { |
394 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
395 |
> |
shouldThrow(); |
396 |
> |
} catch (NullPointerException success) {} |
397 |
|
} |
398 |
|
} |
399 |
|
|
401 |
|
* timed invokeAny(null time unit) throws NPE |
402 |
|
*/ |
403 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
404 |
< |
ExecutorService e = new DirectExecutorService(); |
405 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
406 |
< |
l.add(new StringTask()); |
407 |
< |
try { |
408 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
409 |
< |
shouldThrow(); |
410 |
< |
} catch (NullPointerException success) { |
411 |
< |
} finally { |
414 |
< |
joinPool(e); |
404 |
> |
final ExecutorService e = new DirectExecutorService(); |
405 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
406 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
407 |
> |
l.add(new StringTask()); |
408 |
> |
try { |
409 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
410 |
> |
shouldThrow(); |
411 |
> |
} catch (NullPointerException success) {} |
412 |
|
} |
413 |
|
} |
414 |
|
|
416 |
|
* timed invokeAny(empty collection) throws IAE |
417 |
|
*/ |
418 |
|
public void testTimedInvokeAny2() throws Exception { |
419 |
< |
ExecutorService e = new DirectExecutorService(); |
420 |
< |
try { |
421 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
422 |
< |
shouldThrow(); |
423 |
< |
} catch (IllegalArgumentException success) { |
424 |
< |
} finally { |
425 |
< |
joinPool(e); |
419 |
> |
final ExecutorService e = new DirectExecutorService(); |
420 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
421 |
> |
try { |
422 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
423 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
424 |
> |
shouldThrow(); |
425 |
> |
} catch (IllegalArgumentException success) {} |
426 |
|
} |
427 |
|
} |
428 |
|
|
430 |
|
* timed invokeAny(c) throws NPE if c has null elements |
431 |
|
*/ |
432 |
|
public void testTimedInvokeAny3() throws Exception { |
433 |
< |
ExecutorService e = new DirectExecutorService(); |
434 |
< |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
435 |
< |
l.add(new Callable<Integer>() { |
436 |
< |
public Integer call() { return 5/0; }}); |
437 |
< |
l.add(null); |
438 |
< |
try { |
439 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
440 |
< |
shouldThrow(); |
441 |
< |
} catch (NullPointerException success) { |
442 |
< |
} finally { |
446 |
< |
joinPool(e); |
433 |
> |
final ExecutorService e = new DirectExecutorService(); |
434 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
435 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
436 |
> |
l.add(new Callable<Long>() { |
437 |
> |
public Long call() { throw new ArithmeticException(); }}); |
438 |
> |
l.add(null); |
439 |
> |
try { |
440 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
441 |
> |
shouldThrow(); |
442 |
> |
} catch (NullPointerException success) {} |
443 |
|
} |
444 |
|
} |
445 |
|
|
447 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
448 |
|
*/ |
449 |
|
public void testTimedInvokeAny4() throws Exception { |
450 |
< |
ExecutorService e = new DirectExecutorService(); |
451 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
452 |
< |
l.add(new NPETask()); |
453 |
< |
try { |
454 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
455 |
< |
shouldThrow(); |
456 |
< |
} catch (ExecutionException success) { |
457 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
458 |
< |
} finally { |
459 |
< |
joinPool(e); |
450 |
> |
final ExecutorService e = new DirectExecutorService(); |
451 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
452 |
> |
long startTime = System.nanoTime(); |
453 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
454 |
> |
l.add(new NPETask()); |
455 |
> |
try { |
456 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
457 |
> |
shouldThrow(); |
458 |
> |
} catch (ExecutionException success) { |
459 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
460 |
> |
} |
461 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
462 |
|
} |
463 |
|
} |
464 |
|
|
466 |
|
* timed invokeAny(c) returns result of some task in c |
467 |
|
*/ |
468 |
|
public void testTimedInvokeAny5() throws Exception { |
469 |
< |
ExecutorService e = new DirectExecutorService(); |
470 |
< |
try { |
469 |
> |
final ExecutorService e = new DirectExecutorService(); |
470 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
471 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
472 |
|
l.add(new StringTask()); |
473 |
|
l.add(new StringTask()); |
474 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
475 |
|
assertSame(TEST_STRING, result); |
478 |
– |
} finally { |
479 |
– |
joinPool(e); |
476 |
|
} |
477 |
|
} |
478 |
|
|
480 |
|
* timed invokeAll(null) throws NPE |
481 |
|
*/ |
482 |
|
public void testTimedInvokeAll1() throws InterruptedException { |
483 |
< |
ExecutorService e = new DirectExecutorService(); |
484 |
< |
try { |
485 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
486 |
< |
shouldThrow(); |
487 |
< |
} catch (NullPointerException success) { |
488 |
< |
} finally { |
493 |
< |
joinPool(e); |
483 |
> |
final ExecutorService e = new DirectExecutorService(); |
484 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
485 |
> |
try { |
486 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
487 |
> |
shouldThrow(); |
488 |
> |
} catch (NullPointerException success) {} |
489 |
|
} |
490 |
|
} |
491 |
|
|
493 |
|
* timed invokeAll(null time unit) throws NPE |
494 |
|
*/ |
495 |
|
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
496 |
< |
ExecutorService e = new DirectExecutorService(); |
497 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
498 |
< |
l.add(new StringTask()); |
499 |
< |
try { |
500 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
501 |
< |
shouldThrow(); |
502 |
< |
} catch (NullPointerException success) { |
503 |
< |
} finally { |
509 |
< |
joinPool(e); |
496 |
> |
final ExecutorService e = new DirectExecutorService(); |
497 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
498 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
499 |
> |
l.add(new StringTask()); |
500 |
> |
try { |
501 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
502 |
> |
shouldThrow(); |
503 |
> |
} catch (NullPointerException success) {} |
504 |
|
} |
505 |
|
} |
506 |
|
|
508 |
|
* timed invokeAll(empty collection) returns empty collection |
509 |
|
*/ |
510 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
511 |
< |
ExecutorService e = new DirectExecutorService(); |
512 |
< |
try { |
511 |
> |
final ExecutorService e = new DirectExecutorService(); |
512 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
513 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
514 |
|
assertTrue(r.isEmpty()); |
521 |
– |
} finally { |
522 |
– |
joinPool(e); |
515 |
|
} |
516 |
|
} |
517 |
|
|
519 |
|
* timed invokeAll(c) throws NPE if c has null elements |
520 |
|
*/ |
521 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
522 |
< |
ExecutorService e = new DirectExecutorService(); |
523 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
524 |
< |
l.add(new StringTask()); |
525 |
< |
l.add(null); |
526 |
< |
try { |
527 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
528 |
< |
shouldThrow(); |
529 |
< |
} catch (NullPointerException success) { |
530 |
< |
} finally { |
539 |
< |
joinPool(e); |
522 |
> |
final ExecutorService e = new DirectExecutorService(); |
523 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
524 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
525 |
> |
l.add(new StringTask()); |
526 |
> |
l.add(null); |
527 |
> |
try { |
528 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
529 |
> |
shouldThrow(); |
530 |
> |
} catch (NullPointerException success) {} |
531 |
|
} |
532 |
|
} |
533 |
|
|
535 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
536 |
|
*/ |
537 |
|
public void testTimedInvokeAll4() throws Exception { |
538 |
< |
ExecutorService e = new DirectExecutorService(); |
539 |
< |
try { |
538 |
> |
final ExecutorService e = new DirectExecutorService(); |
539 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
540 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
541 |
|
l.add(new NPETask()); |
542 |
|
List<Future<String>> futures = |
543 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
543 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
544 |
|
assertEquals(1, futures.size()); |
545 |
|
try { |
546 |
|
futures.get(0).get(); |
548 |
|
} catch (ExecutionException success) { |
549 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
550 |
|
} |
560 |
– |
} finally { |
561 |
– |
joinPool(e); |
551 |
|
} |
552 |
|
} |
553 |
|
|
555 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
556 |
|
*/ |
557 |
|
public void testTimedInvokeAll5() throws Exception { |
558 |
< |
ExecutorService e = new DirectExecutorService(); |
559 |
< |
try { |
558 |
> |
final ExecutorService e = new DirectExecutorService(); |
559 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
560 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
561 |
|
l.add(new StringTask()); |
562 |
|
l.add(new StringTask()); |
563 |
|
List<Future<String>> futures = |
564 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
564 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
565 |
|
assertEquals(2, futures.size()); |
566 |
|
for (Future<String> future : futures) |
567 |
|
assertSame(TEST_STRING, future.get()); |
579 |
– |
} finally { |
580 |
– |
joinPool(e); |
568 |
|
} |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
|
* timed invokeAll cancels tasks not completed by timeout |
573 |
|
*/ |
574 |
< |
public void testTimedInvokeAll6() throws InterruptedException { |
575 |
< |
ExecutorService e = new DirectExecutorService(); |
576 |
< |
try { |
577 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
578 |
< |
l.add(new StringTask()); |
579 |
< |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
580 |
< |
l.add(new StringTask()); |
581 |
< |
List<Future<String>> futures = |
582 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
583 |
< |
assertEquals(3, futures.size()); |
584 |
< |
Iterator<Future<String>> it = futures.iterator(); |
585 |
< |
Future<String> f1 = it.next(); |
586 |
< |
Future<String> f2 = it.next(); |
587 |
< |
Future<String> f3 = it.next(); |
588 |
< |
assertTrue(f1.isDone()); |
589 |
< |
assertFalse(f1.isCancelled()); |
590 |
< |
assertTrue(f2.isDone()); |
591 |
< |
assertFalse(f2.isCancelled()); |
592 |
< |
assertTrue(f3.isDone()); |
593 |
< |
assertTrue(f3.isCancelled()); |
594 |
< |
} finally { |
595 |
< |
joinPool(e); |
574 |
> |
public void testTimedInvokeAll6() throws Exception { |
575 |
> |
final ExecutorService e = new DirectExecutorService(); |
576 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
577 |
> |
for (long timeout = timeoutMillis();;) { |
578 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
579 |
> |
tasks.add(new StringTask("0")); |
580 |
> |
tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout), |
581 |
> |
TEST_STRING)); |
582 |
> |
tasks.add(new StringTask("2")); |
583 |
> |
long startTime = System.nanoTime(); |
584 |
> |
List<Future<String>> futures = |
585 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
586 |
> |
assertEquals(tasks.size(), futures.size()); |
587 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
588 |
> |
for (Future future : futures) |
589 |
> |
assertTrue(future.isDone()); |
590 |
> |
try { |
591 |
> |
assertEquals("0", futures.get(0).get()); |
592 |
> |
assertEquals(TEST_STRING, futures.get(1).get()); |
593 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
594 |
> |
// unusual delay before starting second task |
595 |
> |
timeout *= 2; |
596 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
597 |
> |
fail("expected exactly one task to be cancelled"); |
598 |
> |
continue; |
599 |
> |
} |
600 |
> |
assertTrue(futures.get(2).isCancelled()); |
601 |
> |
break; |
602 |
> |
} |
603 |
|
} |
604 |
|
} |
605 |
|
|