4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
|
|
7 |
– |
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
< |
import java.util.concurrent.locks.*; |
12 |
< |
import java.security.*; |
8 |
> |
import java.util.ArrayList; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.List; |
11 |
> |
import java.util.concurrent.Executors; |
12 |
> |
import java.util.concurrent.ExecutorService; |
13 |
> |
import java.util.concurrent.AbstractExecutorService; |
14 |
> |
import java.util.concurrent.CountDownLatch; |
15 |
> |
import java.util.concurrent.Callable; |
16 |
> |
import java.util.concurrent.Future; |
17 |
> |
import java.util.concurrent.ExecutionException; |
18 |
> |
import java.util.concurrent.CancellationException; |
19 |
> |
import java.util.concurrent.RejectedExecutionException; |
20 |
> |
import java.util.concurrent.ForkJoinPool; |
21 |
> |
import java.util.concurrent.ForkJoinTask; |
22 |
> |
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
> |
import java.util.concurrent.RecursiveTask; |
24 |
> |
import java.util.concurrent.TimeUnit; |
25 |
> |
import java.util.concurrent.locks.ReentrantLock; |
26 |
> |
import java.security.AccessControlException; |
27 |
> |
import java.security.Policy; |
28 |
> |
import java.security.PrivilegedAction; |
29 |
> |
import java.security.PrivilegedExceptionAction; |
30 |
|
|
31 |
|
public class ForkJoinPoolTest extends JSR166TestCase { |
32 |
|
public static void main(String[] args) { |
33 |
< |
junit.textui.TestRunner.run (suite()); |
33 |
> |
junit.textui.TestRunner.run(suite()); |
34 |
|
} |
35 |
+ |
|
36 |
|
public static Test suite() { |
37 |
|
return new TestSuite(ForkJoinPoolTest.class); |
38 |
|
} |
56 |
|
// Some classes to test extension and factory methods |
57 |
|
|
58 |
|
static class MyHandler implements Thread.UncaughtExceptionHandler { |
59 |
< |
int catches = 0; |
59 |
> |
volatile int catches = 0; |
60 |
|
public void uncaughtException(Thread t, Throwable e) { |
61 |
|
++catches; |
62 |
|
} |
65 |
|
// to test handlers |
66 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
67 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
68 |
< |
protected void onStart() { throw new Error(); } |
68 |
> |
protected void onStart() { super.onStart(); throw new Error(); } |
69 |
|
} |
70 |
|
|
71 |
|
static class FailingThreadFactory |
72 |
|
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
73 |
< |
int calls = 0; |
73 |
> |
volatile int calls = 0; |
74 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
75 |
|
if (++calls > 1) return null; |
76 |
|
return new FailingFJWSubclass(p); |
159 |
|
* tasks, and quiescent running state. |
160 |
|
*/ |
161 |
|
public void testDefaultInitialState() { |
162 |
< |
ForkJoinPool p = null; |
162 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
163 |
|
try { |
147 |
– |
p = new ForkJoinPool(1); |
164 |
|
assertTrue(p.getFactory() == |
165 |
|
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
166 |
|
assertTrue(p.isQuiescent()); |
151 |
– |
assertTrue(p.getMaintainsParallelism()); |
167 |
|
assertFalse(p.getAsyncMode()); |
168 |
|
assertTrue(p.getActiveThreadCount() == 0); |
169 |
|
assertTrue(p.getStealCount() == 0); |
193 |
|
*/ |
194 |
|
public void testConstructor2() { |
195 |
|
try { |
196 |
< |
new ForkJoinPool(1, null); |
196 |
> |
new ForkJoinPool(1, null, null, false); |
197 |
|
shouldThrow(); |
198 |
|
} catch (NullPointerException success) {} |
199 |
|
} |
203 |
|
* getParallelism returns size set in constructor |
204 |
|
*/ |
205 |
|
public void testGetParallelism() { |
206 |
< |
ForkJoinPool p = null; |
192 |
< |
try { |
193 |
< |
p = new ForkJoinPool(1); |
194 |
< |
assertTrue(p.getParallelism() == 1); |
195 |
< |
} finally { |
196 |
< |
joinPool(p); |
197 |
< |
} |
198 |
< |
} |
199 |
< |
|
200 |
< |
/** |
201 |
< |
* setParallelism changes reported parallelism level. |
202 |
< |
*/ |
203 |
< |
public void testSetParallelism() { |
204 |
< |
ForkJoinPool p = null; |
205 |
< |
try { |
206 |
< |
p = new ForkJoinPool(1); |
207 |
< |
assertTrue(p.getParallelism() == 1); |
208 |
< |
p.setParallelism(2); |
209 |
< |
assertTrue(p.getParallelism() == 2); |
210 |
< |
} finally { |
211 |
< |
joinPool(p); |
212 |
< |
} |
213 |
< |
} |
214 |
< |
|
215 |
< |
/** |
216 |
< |
* setParallelism with argument <= 0 throws exception |
217 |
< |
*/ |
218 |
< |
public void testSetParallelism2() { |
219 |
< |
ForkJoinPool p = null; |
206 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
207 |
|
try { |
221 |
– |
p = new ForkJoinPool(1); |
208 |
|
assertTrue(p.getParallelism() == 1); |
223 |
– |
p.setParallelism(-2); |
224 |
– |
shouldThrow(); |
225 |
– |
} catch (IllegalArgumentException success) { |
209 |
|
} finally { |
210 |
|
joinPool(p); |
211 |
|
} |
215 |
|
* getPoolSize returns number of started workers. |
216 |
|
*/ |
217 |
|
public void testGetPoolSize() { |
218 |
< |
ForkJoinPool p = null; |
218 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
219 |
|
try { |
220 |
< |
p = new ForkJoinPool(1); |
238 |
< |
assertTrue(p.getPoolSize() == 0); |
220 |
> |
assertTrue(p.getActiveThreadCount() == 0); |
221 |
|
Future<String> future = p.submit(new StringTask()); |
222 |
|
assertTrue(p.getPoolSize() == 1); |
241 |
– |
|
242 |
– |
} finally { |
243 |
– |
joinPool(p); |
244 |
– |
} |
245 |
– |
} |
246 |
– |
|
247 |
– |
/** |
248 |
– |
* setMaximumPoolSize changes size reported by getMaximumPoolSize. |
249 |
– |
*/ |
250 |
– |
public void testSetMaximumPoolSize() { |
251 |
– |
ForkJoinPool p = null; |
252 |
– |
try { |
253 |
– |
p = new ForkJoinPool(1); |
254 |
– |
p.setMaximumPoolSize(2); |
255 |
– |
assertTrue(p.getMaximumPoolSize() == 2); |
256 |
– |
} finally { |
257 |
– |
joinPool(p); |
258 |
– |
} |
259 |
– |
} |
260 |
– |
|
261 |
– |
/** |
262 |
– |
* setMaximumPoolSize with argument <= 0 throws exception |
263 |
– |
*/ |
264 |
– |
public void testSetMaximumPoolSize2() { |
265 |
– |
ForkJoinPool p = null; |
266 |
– |
try { |
267 |
– |
p = new ForkJoinPool(1); |
268 |
– |
p.setMaximumPoolSize(-2); |
269 |
– |
shouldThrow(); |
270 |
– |
} catch (IllegalArgumentException success) { |
271 |
– |
} finally { |
272 |
– |
joinPool(p); |
273 |
– |
} |
274 |
– |
} |
275 |
– |
|
276 |
– |
/** |
277 |
– |
* setMaintainsParallelism changes policy reported by |
278 |
– |
* getMaintainsParallelism. |
279 |
– |
*/ |
280 |
– |
public void testSetMaintainsParallelism() { |
281 |
– |
ForkJoinPool p = null; |
282 |
– |
try { |
283 |
– |
p = new ForkJoinPool(1); |
284 |
– |
p.setMaintainsParallelism(false); |
285 |
– |
assertFalse(p.getMaintainsParallelism()); |
286 |
– |
} finally { |
287 |
– |
joinPool(p); |
288 |
– |
} |
289 |
– |
} |
290 |
– |
|
291 |
– |
/** |
292 |
– |
* setAsyncMode changes policy reported by |
293 |
– |
* getAsyncMode. |
294 |
– |
*/ |
295 |
– |
public void testSetAsyncMode() { |
296 |
– |
ForkJoinPool p = null; |
297 |
– |
try { |
298 |
– |
p = new ForkJoinPool(1); |
299 |
– |
p.setAsyncMode(true); |
300 |
– |
assertTrue(p.getAsyncMode()); |
223 |
|
} finally { |
224 |
|
joinPool(p); |
225 |
|
} |
232 |
|
* performs its defined action |
233 |
|
*/ |
234 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
235 |
< |
ForkJoinPool p = null; |
235 |
> |
MyHandler eh = new MyHandler(); |
236 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
237 |
|
try { |
238 |
< |
p = new ForkJoinPool(1, new FailingThreadFactory()); |
316 |
< |
MyHandler eh = new MyHandler(); |
317 |
< |
p.setUncaughtExceptionHandler(eh); |
318 |
< |
assertEquals(eh, p.getUncaughtExceptionHandler()); |
238 |
> |
assert(eh == p.getUncaughtExceptionHandler()); |
239 |
|
p.execute(new FailingTask()); |
240 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
241 |
|
assertTrue(eh.catches > 0); |
242 |
|
} finally { |
243 |
+ |
p.shutdownNow(); |
244 |
|
joinPool(p); |
245 |
|
} |
246 |
|
} |
247 |
|
|
248 |
|
/** |
328 |
– |
* setUncaughtExceptionHandler of null removes handler |
329 |
– |
*/ |
330 |
– |
public void testSetUncaughtExceptionHandler2() { |
331 |
– |
ForkJoinPool p = null; |
332 |
– |
try { |
333 |
– |
p = new ForkJoinPool(1); |
334 |
– |
p.setUncaughtExceptionHandler(null); |
335 |
– |
assertNull(p.getUncaughtExceptionHandler()); |
336 |
– |
} finally { |
337 |
– |
joinPool(p); |
338 |
– |
} |
339 |
– |
} |
340 |
– |
|
341 |
– |
|
342 |
– |
/** |
249 |
|
* After invoking a single task, isQuiescent is true, |
250 |
|
* queues are empty, threads are not active, and |
251 |
|
* construction parameters continue to hold |
252 |
|
*/ |
253 |
|
public void testisQuiescent() throws InterruptedException { |
254 |
< |
ForkJoinPool p = null; |
254 |
> |
ForkJoinPool p = new ForkJoinPool(2); |
255 |
|
try { |
350 |
– |
p = new ForkJoinPool(2); |
256 |
|
p.invoke(new FibTask(20)); |
257 |
|
assertTrue(p.getFactory() == |
258 |
|
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
259 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
260 |
|
assertTrue(p.isQuiescent()); |
356 |
– |
assertTrue(p.getMaintainsParallelism()); |
261 |
|
assertFalse(p.getAsyncMode()); |
262 |
|
assertTrue(p.getActiveThreadCount() == 0); |
263 |
|
assertTrue(p.getQueuedTaskCount() == 0); |
275 |
|
* Completed submit(ForkJoinTask) returns result |
276 |
|
*/ |
277 |
|
public void testSubmitForkJoinTask() throws Throwable { |
278 |
< |
ForkJoinPool p = null; |
278 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
279 |
|
try { |
376 |
– |
p = new ForkJoinPool(1); |
280 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
281 |
|
int r = f.get(); |
282 |
|
assertTrue(r == 21); |
289 |
|
* A task submitted after shutdown is rejected |
290 |
|
*/ |
291 |
|
public void testSubmitAfterShutdown() { |
292 |
< |
ForkJoinPool p = null; |
292 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
293 |
|
try { |
391 |
– |
p = new ForkJoinPool(1); |
294 |
|
p.shutdown(); |
295 |
|
assertTrue(p.isShutdown()); |
296 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
305 |
|
* Pool maintains parallelism when using ManagedBlocker |
306 |
|
*/ |
307 |
|
public void testBlockingForkJoinTask() throws Throwable { |
308 |
< |
ForkJoinPool p = null; |
308 |
> |
ForkJoinPool p = new ForkJoinPool(4); |
309 |
|
try { |
408 |
– |
p = new ForkJoinPool(4); |
310 |
|
ReentrantLock lock = new ReentrantLock(); |
311 |
|
ManagedLocker locker = new ManagedLocker(lock); |
312 |
|
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
313 |
|
p.execute(f); |
413 |
– |
assertTrue(p.getPoolSize() >= 4); |
314 |
|
int r = f.get(); |
315 |
< |
assertTrue(r == 832040); |
315 |
> |
assertTrue(r == 832040); |
316 |
|
} finally { |
317 |
|
p.shutdownNow(); // don't wait out shutdown |
318 |
|
} |
322 |
|
* pollSubmission returns unexecuted submitted task, if present |
323 |
|
*/ |
324 |
|
public void testPollSubmission() { |
325 |
< |
SubFJP p = null; |
325 |
> |
SubFJP p = new SubFJP(); |
326 |
|
try { |
427 |
– |
p = new SubFJP(); |
327 |
|
ForkJoinTask a = p.submit(new MediumRunnable()); |
328 |
|
ForkJoinTask b = p.submit(new MediumRunnable()); |
329 |
|
ForkJoinTask c = p.submit(new MediumRunnable()); |
339 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
340 |
|
*/ |
341 |
|
public void testDrainTasksTo() { |
342 |
< |
SubFJP p = null; |
342 |
> |
SubFJP p = new SubFJP(); |
343 |
|
try { |
445 |
– |
p = new SubFJP(); |
344 |
|
ForkJoinTask a = p.submit(new MediumRunnable()); |
345 |
|
ForkJoinTask b = p.submit(new MediumRunnable()); |
346 |
|
ForkJoinTask c = p.submit(new MediumRunnable()); |
364 |
|
*/ |
365 |
|
public void testExecuteRunnable() throws Throwable { |
366 |
|
ExecutorService e = new ForkJoinPool(1); |
367 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
368 |
< |
assertFalse(task.done); |
369 |
< |
Future<?> future = e.submit(task); |
370 |
< |
future.get(); |
371 |
< |
assertTrue(task.done); |
367 |
> |
try { |
368 |
> |
TrackedShortRunnable task = new TrackedShortRunnable(); |
369 |
> |
assertFalse(task.done); |
370 |
> |
Future<?> future = e.submit(task); |
371 |
> |
future.get(); |
372 |
> |
assertTrue(task.done); |
373 |
> |
} finally { |
374 |
> |
joinPool(e); |
375 |
> |
} |
376 |
|
} |
377 |
|
|
378 |
|
|
381 |
|
*/ |
382 |
|
public void testSubmitCallable() throws Throwable { |
383 |
|
ExecutorService e = new ForkJoinPool(1); |
384 |
< |
Future<String> future = e.submit(new StringTask()); |
385 |
< |
String result = future.get(); |
386 |
< |
assertSame(TEST_STRING, result); |
384 |
> |
try { |
385 |
> |
Future<String> future = e.submit(new StringTask()); |
386 |
> |
String result = future.get(); |
387 |
> |
assertSame(TEST_STRING, result); |
388 |
> |
} finally { |
389 |
> |
joinPool(e); |
390 |
> |
} |
391 |
|
} |
392 |
|
|
393 |
|
/** |
395 |
|
*/ |
396 |
|
public void testSubmitRunnable() throws Throwable { |
397 |
|
ExecutorService e = new ForkJoinPool(1); |
398 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
399 |
< |
future.get(); |
400 |
< |
assertTrue(future.isDone()); |
398 |
> |
try { |
399 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
400 |
> |
future.get(); |
401 |
> |
assertTrue(future.isDone()); |
402 |
> |
} finally { |
403 |
> |
joinPool(e); |
404 |
> |
} |
405 |
|
} |
406 |
|
|
407 |
|
/** |
409 |
|
*/ |
410 |
|
public void testSubmitRunnable2() throws Throwable { |
411 |
|
ExecutorService e = new ForkJoinPool(1); |
412 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
413 |
< |
String result = future.get(); |
414 |
< |
assertSame(TEST_STRING, result); |
412 |
> |
try { |
413 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
414 |
> |
String result = future.get(); |
415 |
> |
assertSame(TEST_STRING, result); |
416 |
> |
} finally { |
417 |
> |
joinPool(e); |
418 |
> |
} |
419 |
|
} |
420 |
|
|
421 |
|
|
433 |
|
} catch (AccessControlException ok) { |
434 |
|
return; |
435 |
|
} |
436 |
+ |
|
437 |
|
try { |
438 |
|
ExecutorService e = new ForkJoinPool(1); |
439 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
439 |
> |
try { |
440 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
441 |
|
public Object run() { |
442 |
|
return TEST_STRING; |
443 |
|
}})); |
444 |
|
|
445 |
< |
Object result = future.get(); |
446 |
< |
assertSame(TEST_STRING, result); |
447 |
< |
} |
448 |
< |
finally { |
445 |
> |
Object result = future.get(); |
446 |
> |
assertSame(TEST_STRING, result); |
447 |
> |
} finally { |
448 |
> |
joinPool(e); |
449 |
> |
} |
450 |
> |
} finally { |
451 |
|
Policy.setPolicy(savedPolicy); |
452 |
|
} |
453 |
|
} |
469 |
|
|
470 |
|
try { |
471 |
|
ExecutorService e = new ForkJoinPool(1); |
472 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
472 |
> |
try { |
473 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
474 |
|
public Object run() { |
475 |
|
return TEST_STRING; |
476 |
|
}})); |
477 |
|
|
478 |
< |
Object result = future.get(); |
479 |
< |
assertSame(TEST_STRING, result); |
480 |
< |
} |
481 |
< |
finally { |
478 |
> |
Object result = future.get(); |
479 |
> |
assertSame(TEST_STRING, result); |
480 |
> |
} finally { |
481 |
> |
joinPool(e); |
482 |
> |
} |
483 |
> |
} finally { |
484 |
|
Policy.setPolicy(savedPolicy); |
485 |
|
} |
486 |
|
} |
500 |
|
return; |
501 |
|
} |
502 |
|
|
582 |
– |
|
503 |
|
try { |
504 |
|
ExecutorService e = new ForkJoinPool(1); |
505 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
505 |
> |
try { |
506 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
507 |
|
public Object run() throws Exception { |
508 |
|
throw new IndexOutOfBoundsException(); |
509 |
|
}})); |
510 |
|
|
511 |
< |
Object result = future.get(); |
512 |
< |
shouldThrow(); |
513 |
< |
} catch (ExecutionException success) { |
511 |
> |
Object result = future.get(); |
512 |
> |
shouldThrow(); |
513 |
> |
} catch (ExecutionException success) { |
514 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
515 |
> |
} finally { |
516 |
> |
joinPool(e); |
517 |
> |
} |
518 |
|
} finally { |
519 |
|
Policy.setPolicy(savedPolicy); |
520 |
|
} |
524 |
|
* execute(null runnable) throws NullPointerException |
525 |
|
*/ |
526 |
|
public void testExecuteNullRunnable() { |
527 |
+ |
ExecutorService e = new ForkJoinPool(1); |
528 |
|
try { |
603 |
– |
ExecutorService e = new ForkJoinPool(1); |
529 |
|
TrackedShortRunnable task = null; |
530 |
|
Future<?> future = e.submit(task); |
531 |
|
shouldThrow(); |
532 |
< |
} catch (NullPointerException success) {} |
532 |
> |
} catch (NullPointerException success) { |
533 |
> |
} finally { |
534 |
> |
joinPool(e); |
535 |
> |
} |
536 |
|
} |
537 |
|
|
538 |
|
|
540 |
|
* submit(null callable) throws NullPointerException |
541 |
|
*/ |
542 |
|
public void testSubmitNullCallable() { |
543 |
+ |
ExecutorService e = new ForkJoinPool(1); |
544 |
|
try { |
616 |
– |
ExecutorService e = new ForkJoinPool(1); |
545 |
|
StringTask t = null; |
546 |
|
Future<String> future = e.submit(t); |
547 |
|
shouldThrow(); |
548 |
< |
} catch (NullPointerException success) {} |
548 |
> |
} catch (NullPointerException success) { |
549 |
> |
} finally { |
550 |
> |
joinPool(e); |
551 |
> |
} |
552 |
|
} |
553 |
|
|
554 |
|
|
560 |
|
final ForkJoinPool p = new ForkJoinPool(1); |
561 |
|
|
562 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
563 |
< |
void realRun() throws Throwable { |
563 |
> |
public void realRun() throws Throwable { |
564 |
|
p.submit(new CheckedCallable<Object>() { |
565 |
|
public Object realCall() throws Throwable { |
566 |
|
try { |
592 |
|
return Boolean.TRUE; |
593 |
|
}}).get(); |
594 |
|
shouldThrow(); |
595 |
< |
} catch (ExecutionException success) {} |
596 |
< |
|
597 |
< |
joinPool(p); |
595 |
> |
} catch (ExecutionException success) { |
596 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
597 |
> |
} finally { |
598 |
> |
joinPool(p); |
599 |
> |
} |
600 |
|
} |
601 |
|
|
602 |
|
/** |
632 |
|
*/ |
633 |
|
public void testInvokeAny3() throws Throwable { |
634 |
|
ExecutorService e = new ForkJoinPool(1); |
635 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
636 |
+ |
l.add(null); |
637 |
|
try { |
703 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
704 |
– |
l.add(null); |
638 |
|
e.invokeAny(l); |
639 |
|
shouldThrow(); |
640 |
|
} catch (NullPointerException success) { |
647 |
|
* invokeAny(c) throws NullPointerException if c has null elements |
648 |
|
*/ |
649 |
|
public void testInvokeAny4() throws Throwable { |
650 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
651 |
|
ExecutorService e = new ForkJoinPool(1); |
652 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
653 |
+ |
l.add(latchAwaitingStringTask(latch)); |
654 |
+ |
l.add(null); |
655 |
|
try { |
719 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
720 |
– |
l.add(new Callable<String>() { |
721 |
– |
public String call() { |
722 |
– |
// The delay gives the pool a chance to notice |
723 |
– |
// the null element. |
724 |
– |
sleepTillInterrupted(SMALL_DELAY_MS); |
725 |
– |
return "foo"; |
726 |
– |
}}); |
727 |
– |
l.add(null); |
656 |
|
e.invokeAny(l); |
657 |
|
shouldThrow(); |
658 |
|
} catch (NullPointerException success) { |
659 |
|
} finally { |
660 |
+ |
latch.countDown(); |
661 |
|
joinPool(e); |
662 |
|
} |
663 |
|
} |
667 |
|
*/ |
668 |
|
public void testInvokeAny5() throws Throwable { |
669 |
|
ExecutorService e = new ForkJoinPool(1); |
670 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
671 |
+ |
l.add(new NPETask()); |
672 |
|
try { |
742 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
743 |
– |
l.add(new NPETask()); |
673 |
|
e.invokeAny(l); |
674 |
|
shouldThrow(); |
675 |
|
} catch (ExecutionException success) { |
676 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
677 |
|
} finally { |
678 |
|
joinPool(e); |
679 |
|
} |
685 |
|
public void testInvokeAny6() throws Throwable { |
686 |
|
ExecutorService e = new ForkJoinPool(1); |
687 |
|
try { |
688 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
688 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
689 |
|
l.add(new StringTask()); |
690 |
|
l.add(new StringTask()); |
691 |
|
String result = e.invokeAny(l); |
728 |
|
*/ |
729 |
|
public void testInvokeAll3() throws InterruptedException { |
730 |
|
ExecutorService e = new ForkJoinPool(1); |
731 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
732 |
+ |
l.add(new StringTask()); |
733 |
+ |
l.add(null); |
734 |
|
try { |
802 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
803 |
– |
l.add(new StringTask()); |
804 |
– |
l.add(null); |
735 |
|
e.invokeAll(l); |
736 |
|
shouldThrow(); |
737 |
|
} catch (NullPointerException success) { |
746 |
|
*/ |
747 |
|
public void testInvokeAll4() throws Throwable { |
748 |
|
ExecutorService e = new ForkJoinPool(1); |
749 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
750 |
+ |
l.add(new NPETask()); |
751 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
752 |
+ |
assertEquals(1, futures.size()); |
753 |
|
try { |
754 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
821 |
< |
l.add(new NPETask()); |
822 |
< |
List<Future<String>> result = e.invokeAll(l); |
823 |
< |
assertEquals(1, result.size()); |
824 |
< |
for (Future<String> future : result) |
825 |
< |
future.get(); |
754 |
> |
futures.get(0).get(); |
755 |
|
shouldThrow(); |
756 |
|
} catch (ExecutionException success) { |
757 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
758 |
|
} finally { |
759 |
|
joinPool(e); |
760 |
|
} |
766 |
|
public void testInvokeAll5() throws Throwable { |
767 |
|
ExecutorService e = new ForkJoinPool(1); |
768 |
|
try { |
769 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
769 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
770 |
|
l.add(new StringTask()); |
771 |
|
l.add(new StringTask()); |
772 |
< |
List<Future<String>> result = e.invokeAll(l); |
773 |
< |
assertEquals(2, result.size()); |
774 |
< |
for (Future<String> future : result) |
772 |
> |
List<Future<String>> futures = e.invokeAll(l); |
773 |
> |
assertEquals(2, futures.size()); |
774 |
> |
for (Future<String> future : futures) |
775 |
|
assertSame(TEST_STRING, future.get()); |
776 |
|
} finally { |
777 |
|
joinPool(e); |
785 |
|
public void testTimedInvokeAny1() throws Throwable { |
786 |
|
ExecutorService e = new ForkJoinPool(1); |
787 |
|
try { |
788 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
788 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
789 |
|
shouldThrow(); |
790 |
|
} catch (NullPointerException success) { |
791 |
|
} finally { |
798 |
|
*/ |
799 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
800 |
|
ExecutorService e = new ForkJoinPool(1); |
801 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
802 |
+ |
l.add(new StringTask()); |
803 |
|
try { |
872 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
873 |
– |
l.add(new StringTask()); |
804 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
805 |
|
shouldThrow(); |
806 |
|
} catch (NullPointerException success) { |
816 |
|
ExecutorService e = new ForkJoinPool(1); |
817 |
|
try { |
818 |
|
e.invokeAny(new ArrayList<Callable<String>>(), |
819 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
819 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
820 |
|
shouldThrow(); |
821 |
|
} catch (IllegalArgumentException success) { |
822 |
|
} finally { |
828 |
|
* timed invokeAny(c) throws NullPointerException if c has null elements |
829 |
|
*/ |
830 |
|
public void testTimedInvokeAny3() throws Throwable { |
831 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
832 |
|
ExecutorService e = new ForkJoinPool(1); |
833 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
834 |
+ |
l.add(latchAwaitingStringTask(latch)); |
835 |
+ |
l.add(null); |
836 |
|
try { |
837 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
904 |
< |
l.add(new StringTask()); |
905 |
< |
l.add(null); |
906 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
837 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
838 |
|
shouldThrow(); |
839 |
|
} catch (NullPointerException success) { |
840 |
|
} finally { |
841 |
+ |
latch.countDown(); |
842 |
|
joinPool(e); |
843 |
|
} |
844 |
|
} |
848 |
|
*/ |
849 |
|
public void testTimedInvokeAny4() throws Throwable { |
850 |
|
ExecutorService e = new ForkJoinPool(1); |
851 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
852 |
+ |
l.add(new NPETask()); |
853 |
|
try { |
854 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
921 |
< |
l.add(new NPETask()); |
922 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
854 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
855 |
|
shouldThrow(); |
856 |
|
} catch (ExecutionException success) { |
857 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
858 |
|
} finally { |
859 |
|
joinPool(e); |
860 |
|
} |
866 |
|
public void testTimedInvokeAny5() throws Throwable { |
867 |
|
ExecutorService e = new ForkJoinPool(1); |
868 |
|
try { |
869 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
869 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
870 |
|
l.add(new StringTask()); |
871 |
|
l.add(new StringTask()); |
872 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
872 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
873 |
|
assertSame(TEST_STRING, result); |
874 |
|
} finally { |
875 |
|
joinPool(e); |
882 |
|
public void testTimedInvokeAll1() throws Throwable { |
883 |
|
ExecutorService e = new ForkJoinPool(1); |
884 |
|
try { |
885 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
885 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
886 |
|
shouldThrow(); |
887 |
|
} catch (NullPointerException success) { |
888 |
|
} finally { |
895 |
|
*/ |
896 |
|
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
897 |
|
ExecutorService e = new ForkJoinPool(1); |
898 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
899 |
+ |
l.add(new StringTask()); |
900 |
|
try { |
966 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
967 |
– |
l.add(new StringTask()); |
901 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
902 |
|
shouldThrow(); |
903 |
|
} catch (NullPointerException success) { |
914 |
|
try { |
915 |
|
List<Future<String>> r |
916 |
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
917 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
917 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
918 |
|
assertTrue(r.isEmpty()); |
919 |
|
} finally { |
920 |
|
joinPool(e); |
926 |
|
*/ |
927 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
928 |
|
ExecutorService e = new ForkJoinPool(1); |
929 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
930 |
+ |
l.add(new StringTask()); |
931 |
+ |
l.add(null); |
932 |
|
try { |
933 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
998 |
< |
l.add(new StringTask()); |
999 |
< |
l.add(null); |
1000 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
933 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
934 |
|
shouldThrow(); |
935 |
|
} catch (NullPointerException success) { |
936 |
|
} finally { |
943 |
|
*/ |
944 |
|
public void testTimedInvokeAll4() throws Throwable { |
945 |
|
ExecutorService e = new ForkJoinPool(1); |
946 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
947 |
+ |
l.add(new NPETask()); |
948 |
+ |
List<Future<String>> futures |
949 |
+ |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
950 |
+ |
assertEquals(1, futures.size()); |
951 |
|
try { |
952 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1015 |
< |
l.add(new NPETask()); |
1016 |
< |
List<Future<String>> result |
1017 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1018 |
< |
assertEquals(1, result.size()); |
1019 |
< |
for (Future<String> future : result) |
1020 |
< |
future.get(); |
952 |
> |
futures.get(0).get(); |
953 |
|
shouldThrow(); |
954 |
|
} catch (ExecutionException success) { |
955 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
956 |
|
} finally { |
957 |
|
joinPool(e); |
958 |
|
} |
964 |
|
public void testTimedInvokeAll5() throws Throwable { |
965 |
|
ExecutorService e = new ForkJoinPool(1); |
966 |
|
try { |
967 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
967 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
968 |
|
l.add(new StringTask()); |
969 |
|
l.add(new StringTask()); |
970 |
< |
List<Future<String>> result |
971 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
972 |
< |
assertEquals(2, result.size()); |
973 |
< |
for (Future<String> future : result) |
970 |
> |
List<Future<String>> futures |
971 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
972 |
> |
assertEquals(2, futures.size()); |
973 |
> |
for (Future<String> future : futures) |
974 |
|
assertSame(TEST_STRING, future.get()); |
975 |
|
} finally { |
976 |
|
joinPool(e); |