11 |
|
import java.security.PrivilegedAction; |
12 |
|
import java.security.PrivilegedExceptionAction; |
13 |
|
import java.util.ArrayList; |
14 |
+ |
import java.util.Collection; |
15 |
|
import java.util.Collections; |
16 |
|
import java.util.List; |
17 |
|
import java.util.concurrent.AbstractExecutorService; |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* execute(null runnable) throws NPE |
169 |
> |
* Submitting null tasks throws NullPointerException. |
170 |
|
*/ |
171 |
< |
public void testExecuteNullRunnable() { |
172 |
< |
ExecutorService e = new DirectExecutorService(); |
173 |
< |
try { |
174 |
< |
e.submit((Runnable) null); |
175 |
< |
shouldThrow(); |
176 |
< |
} catch (NullPointerException success) {} |
177 |
< |
} |
178 |
< |
|
179 |
< |
/** |
180 |
< |
* submit(null callable) throws NPE |
181 |
< |
*/ |
182 |
< |
public void testSubmitNullCallable() { |
183 |
< |
ExecutorService e = new DirectExecutorService(); |
184 |
< |
try { |
185 |
< |
e.submit((Callable) null); |
186 |
< |
shouldThrow(); |
187 |
< |
} catch (NullPointerException success) {} |
171 |
> |
@SuppressWarnings("FutureReturnValueIgnored") |
172 |
> |
public void testNullTaskSubmission() throws Exception { |
173 |
> |
final ExecutorService e = new DirectExecutorService(); |
174 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
175 |
> |
try { |
176 |
> |
e.execute((Runnable) null); |
177 |
> |
shouldThrow(); |
178 |
> |
} catch (NullPointerException success) {} |
179 |
> |
try { |
180 |
> |
e.submit((Runnable) null); |
181 |
> |
shouldThrow(); |
182 |
> |
} catch (NullPointerException success) {} |
183 |
> |
try { |
184 |
> |
e.submit((Callable) null); |
185 |
> |
shouldThrow(); |
186 |
> |
} catch (NullPointerException success) {} |
187 |
> |
} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
195 |
|
final CountDownLatch quittingTime = new CountDownLatch(1); |
196 |
|
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
197 |
|
public Void realCall() throws InterruptedException { |
198 |
< |
quittingTime.await(); |
198 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
199 |
|
return null; |
200 |
|
}}; |
201 |
|
final ExecutorService p |
202 |
|
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
203 |
|
new ArrayBlockingQueue<Runnable>(10)); |
204 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
205 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
204 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
205 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
206 |
|
public void realRun() throws Exception { |
207 |
|
Future<Void> future = p.submit(awaiter); |
208 |
|
submitted.countDown(); |
209 |
|
future.get(); |
210 |
|
}}); |
211 |
< |
t.start(); |
212 |
< |
submitted.await(); |
211 |
> |
|
212 |
> |
await(submitted); |
213 |
|
t.interrupt(); |
214 |
< |
t.join(); |
214 |
< |
quittingTime.countDown(); |
214 |
> |
awaitTermination(t); |
215 |
|
} |
216 |
|
} |
217 |
|
|
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* invokeAny(empty collection) throws IAE |
253 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
254 |
|
*/ |
255 |
|
public void testInvokeAny2() throws Exception { |
256 |
|
final ExecutorService e = new DirectExecutorService(); |
257 |
+ |
final Collection<Callable<String>> emptyCollection |
258 |
+ |
= Collections.emptyList(); |
259 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
260 |
|
try { |
261 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
261 |
> |
e.invokeAny(emptyCollection); |
262 |
|
shouldThrow(); |
263 |
|
} catch (IllegalArgumentException success) {} |
264 |
|
} |
270 |
|
public void testInvokeAny3() throws Exception { |
271 |
|
final ExecutorService e = new DirectExecutorService(); |
272 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
273 |
< |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
273 |
> |
List<Callable<Long>> l = new ArrayList<>(); |
274 |
|
l.add(new Callable<Long>() { |
275 |
|
public Long call() { throw new ArithmeticException(); }}); |
276 |
|
l.add(null); |
287 |
|
public void testInvokeAny4() throws InterruptedException { |
288 |
|
final ExecutorService e = new DirectExecutorService(); |
289 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
290 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
290 |
> |
List<Callable<String>> l = new ArrayList<>(); |
291 |
|
l.add(new NPETask()); |
292 |
|
try { |
293 |
|
e.invokeAny(l); |
304 |
|
public void testInvokeAny5() throws Exception { |
305 |
|
final ExecutorService e = new DirectExecutorService(); |
306 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
307 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
307 |
> |
List<Callable<String>> l = new ArrayList<>(); |
308 |
|
l.add(new StringTask()); |
309 |
|
l.add(new StringTask()); |
310 |
|
String result = e.invokeAny(l); |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* invokeAll(empty collection) returns empty collection |
329 |
> |
* invokeAll(empty collection) returns empty list |
330 |
|
*/ |
331 |
|
public void testInvokeAll2() throws InterruptedException { |
332 |
|
final ExecutorService e = new DirectExecutorService(); |
333 |
+ |
final Collection<Callable<String>> emptyCollection |
334 |
+ |
= Collections.emptyList(); |
335 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
336 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
336 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
337 |
|
assertTrue(r.isEmpty()); |
338 |
|
} |
339 |
|
} |
344 |
|
public void testInvokeAll3() throws InterruptedException { |
345 |
|
final ExecutorService e = new DirectExecutorService(); |
346 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
347 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
347 |
> |
List<Callable<String>> l = new ArrayList<>(); |
348 |
|
l.add(new StringTask()); |
349 |
|
l.add(null); |
350 |
|
try { |
360 |
|
public void testInvokeAll4() throws Exception { |
361 |
|
final ExecutorService e = new DirectExecutorService(); |
362 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
363 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
363 |
> |
List<Callable<String>> l = new ArrayList<>(); |
364 |
|
l.add(new NPETask()); |
365 |
|
List<Future<String>> futures = e.invokeAll(l); |
366 |
|
assertEquals(1, futures.size()); |
379 |
|
public void testInvokeAll5() throws Exception { |
380 |
|
final ExecutorService e = new DirectExecutorService(); |
381 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
382 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
382 |
> |
List<Callable<String>> l = new ArrayList<>(); |
383 |
|
l.add(new StringTask()); |
384 |
|
l.add(new StringTask()); |
385 |
|
List<Future<String>> futures = e.invokeAll(l); |
396 |
|
final ExecutorService e = new DirectExecutorService(); |
397 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
398 |
|
try { |
399 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
399 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
400 |
|
shouldThrow(); |
401 |
|
} catch (NullPointerException success) {} |
402 |
|
} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* timed invokeAny(null time unit) throws NPE |
406 |
> |
* timed invokeAny(null time unit) throws NullPointerException |
407 |
|
*/ |
408 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
409 |
|
final ExecutorService e = new DirectExecutorService(); |
410 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
411 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
411 |
> |
List<Callable<String>> l = new ArrayList<>(); |
412 |
|
l.add(new StringTask()); |
413 |
|
try { |
414 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
414 |
> |
e.invokeAny(l, randomTimeout(), null); |
415 |
|
shouldThrow(); |
416 |
|
} catch (NullPointerException success) {} |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* timed invokeAny(empty collection) throws IAE |
421 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
422 |
|
*/ |
423 |
|
public void testTimedInvokeAny2() throws Exception { |
424 |
|
final ExecutorService e = new DirectExecutorService(); |
425 |
+ |
final Collection<Callable<String>> emptyCollection |
426 |
+ |
= Collections.emptyList(); |
427 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
428 |
|
try { |
429 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), |
424 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
429 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
430 |
|
shouldThrow(); |
431 |
|
} catch (IllegalArgumentException success) {} |
432 |
|
} |
438 |
|
public void testTimedInvokeAny3() throws Exception { |
439 |
|
final ExecutorService e = new DirectExecutorService(); |
440 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
441 |
< |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
441 |
> |
List<Callable<Long>> l = new ArrayList<>(); |
442 |
|
l.add(new Callable<Long>() { |
443 |
|
public Long call() { throw new ArithmeticException(); }}); |
444 |
|
l.add(null); |
445 |
|
try { |
446 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
446 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
447 |
|
shouldThrow(); |
448 |
|
} catch (NullPointerException success) {} |
449 |
|
} |
455 |
|
public void testTimedInvokeAny4() throws Exception { |
456 |
|
final ExecutorService e = new DirectExecutorService(); |
457 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
458 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
458 |
> |
long startTime = System.nanoTime(); |
459 |
> |
List<Callable<String>> l = new ArrayList<>(); |
460 |
|
l.add(new NPETask()); |
461 |
|
try { |
462 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
462 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
463 |
|
shouldThrow(); |
464 |
|
} catch (ExecutionException success) { |
465 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
466 |
|
} |
467 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
468 |
|
} |
469 |
|
} |
470 |
|
|
474 |
|
public void testTimedInvokeAny5() throws Exception { |
475 |
|
final ExecutorService e = new DirectExecutorService(); |
476 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
477 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
477 |
> |
long startTime = System.nanoTime(); |
478 |
> |
List<Callable<String>> l = new ArrayList<>(); |
479 |
|
l.add(new StringTask()); |
480 |
|
l.add(new StringTask()); |
481 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
481 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
482 |
|
assertSame(TEST_STRING, result); |
483 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
484 |
|
} |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
< |
* timed invokeAll(null) throws NPE |
488 |
> |
* timed invokeAll(null) throws NullPointerException |
489 |
|
*/ |
490 |
|
public void testTimedInvokeAll1() throws InterruptedException { |
491 |
|
final ExecutorService e = new DirectExecutorService(); |
492 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
493 |
|
try { |
494 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
494 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
495 |
|
shouldThrow(); |
496 |
|
} catch (NullPointerException success) {} |
497 |
|
} |
503 |
|
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
504 |
|
final ExecutorService e = new DirectExecutorService(); |
505 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
506 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
506 |
> |
List<Callable<String>> l = new ArrayList<>(); |
507 |
|
l.add(new StringTask()); |
508 |
|
try { |
509 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
509 |
> |
e.invokeAll(l, randomTimeout(), null); |
510 |
|
shouldThrow(); |
511 |
|
} catch (NullPointerException success) {} |
512 |
|
} |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
< |
* timed invokeAll(empty collection) returns empty collection |
516 |
> |
* timed invokeAll(empty collection) returns empty list |
517 |
|
*/ |
518 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
519 |
|
final ExecutorService e = new DirectExecutorService(); |
520 |
+ |
final Collection<Callable<String>> emptyCollection |
521 |
+ |
= Collections.emptyList(); |
522 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
523 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
523 |
> |
List<Future<String>> r = |
524 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
525 |
|
assertTrue(r.isEmpty()); |
526 |
|
} |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* timed invokeAll(c) throws NPE if c has null elements |
530 |
> |
* timed invokeAll(c) throws NullPointerException if c has null elements |
531 |
|
*/ |
532 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
533 |
|
final ExecutorService e = new DirectExecutorService(); |
534 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
535 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
535 |
> |
List<Callable<String>> l = new ArrayList<>(); |
536 |
|
l.add(new StringTask()); |
537 |
|
l.add(null); |
538 |
|
try { |
539 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
539 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
540 |
|
shouldThrow(); |
541 |
|
} catch (NullPointerException success) {} |
542 |
|
} |
548 |
|
public void testTimedInvokeAll4() throws Exception { |
549 |
|
final ExecutorService e = new DirectExecutorService(); |
550 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
551 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
551 |
> |
List<Callable<String>> l = new ArrayList<>(); |
552 |
|
l.add(new NPETask()); |
553 |
|
List<Future<String>> futures = |
554 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
554 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
555 |
|
assertEquals(1, futures.size()); |
556 |
|
try { |
557 |
|
futures.get(0).get(); |
568 |
|
public void testTimedInvokeAll5() throws Exception { |
569 |
|
final ExecutorService e = new DirectExecutorService(); |
570 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
571 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
571 |
> |
List<Callable<String>> l = new ArrayList<>(); |
572 |
|
l.add(new StringTask()); |
573 |
|
l.add(new StringTask()); |
574 |
|
List<Future<String>> futures = |