1 |
/* |
2 |
* 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 |
* http://creativecommons.org/licenses/publicdomain |
5 |
*/ |
6 |
|
7 |
import junit.framework.*; |
8 |
import java.util.*; |
9 |
import java.util.concurrent.Executors; |
10 |
import java.util.concurrent.ExecutorService; |
11 |
import java.util.concurrent.AbstractExecutorService; |
12 |
import java.util.concurrent.CountDownLatch; |
13 |
import java.util.concurrent.Callable; |
14 |
import java.util.concurrent.Future; |
15 |
import java.util.concurrent.ExecutionException; |
16 |
import java.util.concurrent.CancellationException; |
17 |
import java.util.concurrent.RejectedExecutionException; |
18 |
import java.util.concurrent.ForkJoinPool; |
19 |
import java.util.concurrent.ForkJoinTask; |
20 |
import java.util.concurrent.ForkJoinWorkerThread; |
21 |
import java.util.concurrent.RecursiveTask; |
22 |
import java.util.concurrent.TimeUnit; |
23 |
import java.util.concurrent.locks.ReentrantLock; |
24 |
import java.security.AccessControlException; |
25 |
import java.security.Policy; |
26 |
import java.security.PrivilegedAction; |
27 |
import java.security.PrivilegedExceptionAction; |
28 |
|
29 |
public class ForkJoinPoolTest extends JSR166TestCase { |
30 |
public static void main(String[] args) { |
31 |
junit.textui.TestRunner.run(suite()); |
32 |
} |
33 |
|
34 |
public static Test suite() { |
35 |
return new TestSuite(ForkJoinPoolTest.class); |
36 |
} |
37 |
|
38 |
/** |
39 |
* Testing coverage notes: |
40 |
* |
41 |
* 1. shutdown and related methods are tested via super.joinPool. |
42 |
* |
43 |
* 2. newTaskFor and adapters are tested in submit/invoke tests |
44 |
* |
45 |
* 3. We cannot portably test monitoring methods such as |
46 |
* getStealCount() since they rely ultimately on random task |
47 |
* stealing that may cause tasks not to be stolen/propagated |
48 |
* across threads, especially on uniprocessors. |
49 |
* |
50 |
* 4. There are no independently testable ForkJoinWorkerThread |
51 |
* methods, but they are covered here and in task tests. |
52 |
*/ |
53 |
|
54 |
// Some classes to test extension and factory methods |
55 |
|
56 |
static class MyHandler implements Thread.UncaughtExceptionHandler { |
57 |
volatile int catches = 0; |
58 |
public void uncaughtException(Thread t, Throwable e) { |
59 |
++catches; |
60 |
} |
61 |
} |
62 |
|
63 |
// to test handlers |
64 |
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
65 |
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
66 |
protected void onStart() { super.onStart(); throw new Error(); } |
67 |
} |
68 |
|
69 |
static class FailingThreadFactory |
70 |
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
71 |
volatile int calls = 0; |
72 |
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
73 |
if (++calls > 1) return null; |
74 |
return new FailingFJWSubclass(p); |
75 |
} |
76 |
} |
77 |
|
78 |
static class SubFJP extends ForkJoinPool { // to expose protected |
79 |
SubFJP() { super(1); } |
80 |
public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
81 |
return super.drainTasksTo(c); |
82 |
} |
83 |
public ForkJoinTask<?> pollSubmission() { |
84 |
return super.pollSubmission(); |
85 |
} |
86 |
} |
87 |
|
88 |
static class ManagedLocker implements ForkJoinPool.ManagedBlocker { |
89 |
final ReentrantLock lock; |
90 |
boolean hasLock = false; |
91 |
ManagedLocker(ReentrantLock lock) { this.lock = lock; } |
92 |
public boolean block() { |
93 |
if (!hasLock) |
94 |
lock.lock(); |
95 |
return true; |
96 |
} |
97 |
public boolean isReleasable() { |
98 |
return hasLock || (hasLock = lock.tryLock()); |
99 |
} |
100 |
} |
101 |
|
102 |
// A simple recursive task for testing |
103 |
static final class FibTask extends RecursiveTask<Integer> { |
104 |
final int number; |
105 |
FibTask(int n) { number = n; } |
106 |
public Integer compute() { |
107 |
int n = number; |
108 |
if (n <= 1) |
109 |
return n; |
110 |
FibTask f1 = new FibTask(n - 1); |
111 |
f1.fork(); |
112 |
return (new FibTask(n - 2)).compute() + f1.join(); |
113 |
} |
114 |
} |
115 |
|
116 |
// A failing task for testing |
117 |
static final class FailingTask extends ForkJoinTask<Void> { |
118 |
public final Void getRawResult() { return null; } |
119 |
protected final void setRawResult(Void mustBeNull) { } |
120 |
protected final boolean exec() { throw new Error(); } |
121 |
FailingTask() {} |
122 |
} |
123 |
|
124 |
// Fib needlessly using locking to test ManagedBlockers |
125 |
static final class LockingFibTask extends RecursiveTask<Integer> { |
126 |
final int number; |
127 |
final ManagedLocker locker; |
128 |
final ReentrantLock lock; |
129 |
LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) { |
130 |
number = n; |
131 |
this.locker = locker; |
132 |
this.lock = lock; |
133 |
} |
134 |
public Integer compute() { |
135 |
int n; |
136 |
LockingFibTask f1 = null; |
137 |
LockingFibTask f2 = null; |
138 |
locker.block(); |
139 |
n = number; |
140 |
if (n > 1) { |
141 |
f1 = new LockingFibTask(n - 1, locker, lock); |
142 |
f2 = new LockingFibTask(n - 2, locker, lock); |
143 |
} |
144 |
lock.unlock(); |
145 |
if (n <= 1) |
146 |
return n; |
147 |
else { |
148 |
f1.fork(); |
149 |
return f2.compute() + f1.join(); |
150 |
} |
151 |
} |
152 |
} |
153 |
|
154 |
/** |
155 |
* Successfully constructed pool reports default factory, |
156 |
* parallelism and async mode policies, no active threads or |
157 |
* tasks, and quiescent running state. |
158 |
*/ |
159 |
public void testDefaultInitialState() { |
160 |
ForkJoinPool p = new ForkJoinPool(1); |
161 |
try { |
162 |
assertTrue(p.getFactory() == |
163 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
164 |
assertTrue(p.isQuiescent()); |
165 |
assertFalse(p.getAsyncMode()); |
166 |
assertTrue(p.getActiveThreadCount() == 0); |
167 |
assertTrue(p.getStealCount() == 0); |
168 |
assertTrue(p.getQueuedTaskCount() == 0); |
169 |
assertTrue(p.getQueuedSubmissionCount() == 0); |
170 |
assertFalse(p.hasQueuedSubmissions()); |
171 |
assertFalse(p.isShutdown()); |
172 |
assertFalse(p.isTerminating()); |
173 |
assertFalse(p.isTerminated()); |
174 |
} finally { |
175 |
joinPool(p); |
176 |
} |
177 |
} |
178 |
|
179 |
/** |
180 |
* Constructor throws if size argument is less than zero |
181 |
*/ |
182 |
public void testConstructor1() { |
183 |
try { |
184 |
new ForkJoinPool(-1); |
185 |
shouldThrow(); |
186 |
} catch (IllegalArgumentException success) {} |
187 |
} |
188 |
|
189 |
/** |
190 |
* Constructor throws if factory argument is null |
191 |
*/ |
192 |
public void testConstructor2() { |
193 |
try { |
194 |
new ForkJoinPool(1, null, null, false); |
195 |
shouldThrow(); |
196 |
} catch (NullPointerException success) {} |
197 |
} |
198 |
|
199 |
|
200 |
/** |
201 |
* getParallelism returns size set in constructor |
202 |
*/ |
203 |
public void testGetParallelism() { |
204 |
ForkJoinPool p = new ForkJoinPool(1); |
205 |
try { |
206 |
assertTrue(p.getParallelism() == 1); |
207 |
} finally { |
208 |
joinPool(p); |
209 |
} |
210 |
} |
211 |
|
212 |
/** |
213 |
* getPoolSize returns number of started workers. |
214 |
*/ |
215 |
public void testGetPoolSize() { |
216 |
ForkJoinPool p = new ForkJoinPool(1); |
217 |
try { |
218 |
assertTrue(p.getActiveThreadCount() == 0); |
219 |
Future<String> future = p.submit(new StringTask()); |
220 |
assertTrue(p.getPoolSize() == 1); |
221 |
} finally { |
222 |
joinPool(p); |
223 |
} |
224 |
} |
225 |
|
226 |
/** |
227 |
* setUncaughtExceptionHandler changes handler for uncaught exceptions. |
228 |
* |
229 |
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
230 |
* performs its defined action |
231 |
*/ |
232 |
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
233 |
MyHandler eh = new MyHandler(); |
234 |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
235 |
try { |
236 |
assert(eh == p.getUncaughtExceptionHandler()); |
237 |
p.execute(new FailingTask()); |
238 |
Thread.sleep(MEDIUM_DELAY_MS); |
239 |
assertTrue(eh.catches > 0); |
240 |
} finally { |
241 |
p.shutdownNow(); |
242 |
joinPool(p); |
243 |
} |
244 |
} |
245 |
|
246 |
/** |
247 |
* After invoking a single task, isQuiescent is true, |
248 |
* queues are empty, threads are not active, and |
249 |
* construction parameters continue to hold |
250 |
*/ |
251 |
public void testisQuiescent() throws InterruptedException { |
252 |
ForkJoinPool p = new ForkJoinPool(2); |
253 |
try { |
254 |
p.invoke(new FibTask(20)); |
255 |
assertTrue(p.getFactory() == |
256 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
257 |
Thread.sleep(MEDIUM_DELAY_MS); |
258 |
assertTrue(p.isQuiescent()); |
259 |
assertFalse(p.getAsyncMode()); |
260 |
assertTrue(p.getActiveThreadCount() == 0); |
261 |
assertTrue(p.getQueuedTaskCount() == 0); |
262 |
assertTrue(p.getQueuedSubmissionCount() == 0); |
263 |
assertFalse(p.hasQueuedSubmissions()); |
264 |
assertFalse(p.isShutdown()); |
265 |
assertFalse(p.isTerminating()); |
266 |
assertFalse(p.isTerminated()); |
267 |
} finally { |
268 |
joinPool(p); |
269 |
} |
270 |
} |
271 |
|
272 |
/** |
273 |
* Completed submit(ForkJoinTask) returns result |
274 |
*/ |
275 |
public void testSubmitForkJoinTask() throws Throwable { |
276 |
ForkJoinPool p = new ForkJoinPool(1); |
277 |
try { |
278 |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
279 |
int r = f.get(); |
280 |
assertTrue(r == 21); |
281 |
} finally { |
282 |
joinPool(p); |
283 |
} |
284 |
} |
285 |
|
286 |
/** |
287 |
* A task submitted after shutdown is rejected |
288 |
*/ |
289 |
public void testSubmitAfterShutdown() { |
290 |
ForkJoinPool p = new ForkJoinPool(1); |
291 |
try { |
292 |
p.shutdown(); |
293 |
assertTrue(p.isShutdown()); |
294 |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
295 |
shouldThrow(); |
296 |
} catch (RejectedExecutionException success) { |
297 |
} finally { |
298 |
joinPool(p); |
299 |
} |
300 |
} |
301 |
|
302 |
/** |
303 |
* Pool maintains parallelism when using ManagedBlocker |
304 |
*/ |
305 |
public void testBlockingForkJoinTask() throws Throwable { |
306 |
ForkJoinPool p = new ForkJoinPool(4); |
307 |
try { |
308 |
ReentrantLock lock = new ReentrantLock(); |
309 |
ManagedLocker locker = new ManagedLocker(lock); |
310 |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
311 |
p.execute(f); |
312 |
int r = f.get(); |
313 |
assertTrue(r == 832040); |
314 |
} finally { |
315 |
p.shutdownNow(); // don't wait out shutdown |
316 |
} |
317 |
} |
318 |
|
319 |
/** |
320 |
* pollSubmission returns unexecuted submitted task, if present |
321 |
*/ |
322 |
public void testPollSubmission() { |
323 |
SubFJP p = new SubFJP(); |
324 |
try { |
325 |
ForkJoinTask a = p.submit(new MediumRunnable()); |
326 |
ForkJoinTask b = p.submit(new MediumRunnable()); |
327 |
ForkJoinTask c = p.submit(new MediumRunnable()); |
328 |
ForkJoinTask r = p.pollSubmission(); |
329 |
assertTrue(r == a || r == b || r == c); |
330 |
assertFalse(r.isDone()); |
331 |
} finally { |
332 |
joinPool(p); |
333 |
} |
334 |
} |
335 |
|
336 |
/** |
337 |
* drainTasksTo transfers unexecuted submitted tasks, if present |
338 |
*/ |
339 |
public void testDrainTasksTo() { |
340 |
SubFJP p = new SubFJP(); |
341 |
try { |
342 |
ForkJoinTask a = p.submit(new MediumRunnable()); |
343 |
ForkJoinTask b = p.submit(new MediumRunnable()); |
344 |
ForkJoinTask c = p.submit(new MediumRunnable()); |
345 |
ArrayList<ForkJoinTask> al = new ArrayList(); |
346 |
p.drainTasksTo(al); |
347 |
assertTrue(al.size() > 0); |
348 |
for (ForkJoinTask r : al) { |
349 |
assertTrue(r == a || r == b || r == c); |
350 |
assertFalse(r.isDone()); |
351 |
} |
352 |
} finally { |
353 |
joinPool(p); |
354 |
} |
355 |
} |
356 |
|
357 |
|
358 |
// FJ Versions of AbstractExecutorService tests |
359 |
|
360 |
/** |
361 |
* execute(runnable) runs it to completion |
362 |
*/ |
363 |
public void testExecuteRunnable() throws Throwable { |
364 |
ExecutorService e = new ForkJoinPool(1); |
365 |
try { |
366 |
TrackedShortRunnable task = new TrackedShortRunnable(); |
367 |
assertFalse(task.done); |
368 |
Future<?> future = e.submit(task); |
369 |
future.get(); |
370 |
assertTrue(task.done); |
371 |
} finally { |
372 |
joinPool(e); |
373 |
} |
374 |
} |
375 |
|
376 |
|
377 |
/** |
378 |
* Completed submit(callable) returns result |
379 |
*/ |
380 |
public void testSubmitCallable() throws Throwable { |
381 |
ExecutorService e = new ForkJoinPool(1); |
382 |
try { |
383 |
Future<String> future = e.submit(new StringTask()); |
384 |
String result = future.get(); |
385 |
assertSame(TEST_STRING, result); |
386 |
} finally { |
387 |
joinPool(e); |
388 |
} |
389 |
} |
390 |
|
391 |
/** |
392 |
* Completed submit(runnable) returns successfully |
393 |
*/ |
394 |
public void testSubmitRunnable() throws Throwable { |
395 |
ExecutorService e = new ForkJoinPool(1); |
396 |
try { |
397 |
Future<?> future = e.submit(new NoOpRunnable()); |
398 |
future.get(); |
399 |
assertTrue(future.isDone()); |
400 |
} finally { |
401 |
joinPool(e); |
402 |
} |
403 |
} |
404 |
|
405 |
/** |
406 |
* Completed submit(runnable, result) returns result |
407 |
*/ |
408 |
public void testSubmitRunnable2() throws Throwable { |
409 |
ExecutorService e = new ForkJoinPool(1); |
410 |
try { |
411 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
412 |
String result = future.get(); |
413 |
assertSame(TEST_STRING, result); |
414 |
} finally { |
415 |
joinPool(e); |
416 |
} |
417 |
} |
418 |
|
419 |
|
420 |
/** |
421 |
* A submitted privileged action to completion |
422 |
*/ |
423 |
public void testSubmitPrivilegedAction() throws Throwable { |
424 |
Policy savedPolicy = null; |
425 |
try { |
426 |
savedPolicy = Policy.getPolicy(); |
427 |
AdjustablePolicy policy = new AdjustablePolicy(); |
428 |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
429 |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
430 |
Policy.setPolicy(policy); |
431 |
} catch (AccessControlException ok) { |
432 |
return; |
433 |
} |
434 |
|
435 |
try { |
436 |
ExecutorService e = new ForkJoinPool(1); |
437 |
try { |
438 |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
439 |
public Object run() { |
440 |
return TEST_STRING; |
441 |
}})); |
442 |
|
443 |
Object result = future.get(); |
444 |
assertSame(TEST_STRING, result); |
445 |
} finally { |
446 |
joinPool(e); |
447 |
} |
448 |
} finally { |
449 |
Policy.setPolicy(savedPolicy); |
450 |
} |
451 |
} |
452 |
|
453 |
/** |
454 |
* A submitted a privileged exception action runs to completion |
455 |
*/ |
456 |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
457 |
Policy savedPolicy = null; |
458 |
try { |
459 |
savedPolicy = Policy.getPolicy(); |
460 |
AdjustablePolicy policy = new AdjustablePolicy(); |
461 |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
462 |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
463 |
Policy.setPolicy(policy); |
464 |
} catch (AccessControlException ok) { |
465 |
return; |
466 |
} |
467 |
|
468 |
try { |
469 |
ExecutorService e = new ForkJoinPool(1); |
470 |
try { |
471 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
472 |
public Object run() { |
473 |
return TEST_STRING; |
474 |
}})); |
475 |
|
476 |
Object result = future.get(); |
477 |
assertSame(TEST_STRING, result); |
478 |
} finally { |
479 |
joinPool(e); |
480 |
} |
481 |
} finally { |
482 |
Policy.setPolicy(savedPolicy); |
483 |
} |
484 |
} |
485 |
|
486 |
/** |
487 |
* A submitted failed privileged exception action reports exception |
488 |
*/ |
489 |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
490 |
Policy savedPolicy = null; |
491 |
try { |
492 |
savedPolicy = Policy.getPolicy(); |
493 |
AdjustablePolicy policy = new AdjustablePolicy(); |
494 |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
495 |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
496 |
Policy.setPolicy(policy); |
497 |
} catch (AccessControlException ok) { |
498 |
return; |
499 |
} |
500 |
|
501 |
try { |
502 |
ExecutorService e = new ForkJoinPool(1); |
503 |
try { |
504 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
505 |
public Object run() throws Exception { |
506 |
throw new IndexOutOfBoundsException(); |
507 |
}})); |
508 |
|
509 |
Object result = future.get(); |
510 |
shouldThrow(); |
511 |
} catch (ExecutionException success) { |
512 |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
513 |
} finally { |
514 |
joinPool(e); |
515 |
} |
516 |
} finally { |
517 |
Policy.setPolicy(savedPolicy); |
518 |
} |
519 |
} |
520 |
|
521 |
/** |
522 |
* execute(null runnable) throws NullPointerException |
523 |
*/ |
524 |
public void testExecuteNullRunnable() { |
525 |
ExecutorService e = new ForkJoinPool(1); |
526 |
try { |
527 |
TrackedShortRunnable task = null; |
528 |
Future<?> future = e.submit(task); |
529 |
shouldThrow(); |
530 |
} catch (NullPointerException success) { |
531 |
} finally { |
532 |
joinPool(e); |
533 |
} |
534 |
} |
535 |
|
536 |
|
537 |
/** |
538 |
* submit(null callable) throws NullPointerException |
539 |
*/ |
540 |
public void testSubmitNullCallable() { |
541 |
ExecutorService e = new ForkJoinPool(1); |
542 |
try { |
543 |
StringTask t = null; |
544 |
Future<String> future = e.submit(t); |
545 |
shouldThrow(); |
546 |
} catch (NullPointerException success) { |
547 |
} finally { |
548 |
joinPool(e); |
549 |
} |
550 |
} |
551 |
|
552 |
|
553 |
/** |
554 |
* Blocking on submit(callable) throws InterruptedException if |
555 |
* caller interrupted. |
556 |
*/ |
557 |
public void testInterruptedSubmit() throws InterruptedException { |
558 |
final ForkJoinPool p = new ForkJoinPool(1); |
559 |
|
560 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
561 |
public void realRun() throws Throwable { |
562 |
p.submit(new CheckedCallable<Object>() { |
563 |
public Object realCall() throws Throwable { |
564 |
try { |
565 |
Thread.sleep(MEDIUM_DELAY_MS); |
566 |
} catch (InterruptedException ok) { |
567 |
} |
568 |
return null; |
569 |
}}).get(); |
570 |
}}); |
571 |
|
572 |
t.start(); |
573 |
Thread.sleep(SHORT_DELAY_MS); |
574 |
t.interrupt(); |
575 |
t.join(); |
576 |
p.shutdownNow(); |
577 |
joinPool(p); |
578 |
} |
579 |
|
580 |
/** |
581 |
* get of submit(callable) throws ExecutionException if callable |
582 |
* throws exception |
583 |
*/ |
584 |
public void testSubmitEE() throws Throwable { |
585 |
ForkJoinPool p = new ForkJoinPool(1); |
586 |
try { |
587 |
p.submit(new Callable() { |
588 |
public Object call() { |
589 |
int i = 5/0; |
590 |
return Boolean.TRUE; |
591 |
}}).get(); |
592 |
shouldThrow(); |
593 |
} catch (ExecutionException success) { |
594 |
assertTrue(success.getCause() instanceof ArithmeticException); |
595 |
} finally { |
596 |
joinPool(p); |
597 |
} |
598 |
} |
599 |
|
600 |
/** |
601 |
* invokeAny(null) throws NullPointerException |
602 |
*/ |
603 |
public void testInvokeAny1() throws Throwable { |
604 |
ExecutorService e = new ForkJoinPool(1); |
605 |
try { |
606 |
e.invokeAny(null); |
607 |
shouldThrow(); |
608 |
} catch (NullPointerException success) { |
609 |
} finally { |
610 |
joinPool(e); |
611 |
} |
612 |
} |
613 |
|
614 |
/** |
615 |
* invokeAny(empty collection) throws IllegalArgumentException |
616 |
*/ |
617 |
public void testInvokeAny2() throws Throwable { |
618 |
ExecutorService e = new ForkJoinPool(1); |
619 |
try { |
620 |
e.invokeAny(new ArrayList<Callable<String>>()); |
621 |
shouldThrow(); |
622 |
} catch (IllegalArgumentException success) { |
623 |
} finally { |
624 |
joinPool(e); |
625 |
} |
626 |
} |
627 |
|
628 |
/** |
629 |
* invokeAny(c) throws NullPointerException if c has a single null element |
630 |
*/ |
631 |
public void testInvokeAny3() throws Throwable { |
632 |
ExecutorService e = new ForkJoinPool(1); |
633 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
634 |
l.add(null); |
635 |
try { |
636 |
e.invokeAny(l); |
637 |
shouldThrow(); |
638 |
} catch (NullPointerException success) { |
639 |
} finally { |
640 |
joinPool(e); |
641 |
} |
642 |
} |
643 |
|
644 |
/** |
645 |
* invokeAny(c) throws NullPointerException if c has null elements |
646 |
*/ |
647 |
public void testInvokeAny4() throws Throwable { |
648 |
CountDownLatch latch = new CountDownLatch(1); |
649 |
ExecutorService e = new ForkJoinPool(1); |
650 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
651 |
l.add(latchAwaitingStringTask(latch)); |
652 |
l.add(null); |
653 |
try { |
654 |
e.invokeAny(l); |
655 |
shouldThrow(); |
656 |
} catch (NullPointerException success) { |
657 |
} finally { |
658 |
latch.countDown(); |
659 |
joinPool(e); |
660 |
} |
661 |
} |
662 |
|
663 |
/** |
664 |
* invokeAny(c) throws ExecutionException if no task in c completes |
665 |
*/ |
666 |
public void testInvokeAny5() throws Throwable { |
667 |
ExecutorService e = new ForkJoinPool(1); |
668 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
669 |
l.add(new NPETask()); |
670 |
try { |
671 |
e.invokeAny(l); |
672 |
shouldThrow(); |
673 |
} catch (ExecutionException success) { |
674 |
assertTrue(success.getCause() instanceof NullPointerException); |
675 |
} finally { |
676 |
joinPool(e); |
677 |
} |
678 |
} |
679 |
|
680 |
/** |
681 |
* invokeAny(c) returns result of some task in c if at least one completes |
682 |
*/ |
683 |
public void testInvokeAny6() throws Throwable { |
684 |
ExecutorService e = new ForkJoinPool(1); |
685 |
try { |
686 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
687 |
l.add(new StringTask()); |
688 |
l.add(new StringTask()); |
689 |
String result = e.invokeAny(l); |
690 |
assertSame(TEST_STRING, result); |
691 |
} finally { |
692 |
joinPool(e); |
693 |
} |
694 |
} |
695 |
|
696 |
/** |
697 |
* invokeAll(null) throws NullPointerException |
698 |
*/ |
699 |
public void testInvokeAll1() throws Throwable { |
700 |
ExecutorService e = new ForkJoinPool(1); |
701 |
try { |
702 |
e.invokeAll(null); |
703 |
shouldThrow(); |
704 |
} catch (NullPointerException success) { |
705 |
} finally { |
706 |
joinPool(e); |
707 |
} |
708 |
} |
709 |
|
710 |
/** |
711 |
* invokeAll(empty collection) returns empty collection |
712 |
*/ |
713 |
public void testInvokeAll2() throws InterruptedException { |
714 |
ExecutorService e = new ForkJoinPool(1); |
715 |
try { |
716 |
List<Future<String>> r |
717 |
= e.invokeAll(new ArrayList<Callable<String>>()); |
718 |
assertTrue(r.isEmpty()); |
719 |
} finally { |
720 |
joinPool(e); |
721 |
} |
722 |
} |
723 |
|
724 |
/** |
725 |
* invokeAll(c) throws NullPointerException if c has null elements |
726 |
*/ |
727 |
public void testInvokeAll3() throws InterruptedException { |
728 |
ExecutorService e = new ForkJoinPool(1); |
729 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
730 |
l.add(new StringTask()); |
731 |
l.add(null); |
732 |
try { |
733 |
e.invokeAll(l); |
734 |
shouldThrow(); |
735 |
} catch (NullPointerException success) { |
736 |
} finally { |
737 |
joinPool(e); |
738 |
} |
739 |
} |
740 |
|
741 |
/** |
742 |
* get of returned element of invokeAll(c) throws |
743 |
* ExecutionException on failed task |
744 |
*/ |
745 |
public void testInvokeAll4() throws Throwable { |
746 |
ExecutorService e = new ForkJoinPool(1); |
747 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
748 |
l.add(new NPETask()); |
749 |
List<Future<String>> futures = e.invokeAll(l); |
750 |
assertEquals(1, futures.size()); |
751 |
try { |
752 |
futures.get(0).get(); |
753 |
shouldThrow(); |
754 |
} catch (ExecutionException success) { |
755 |
assertTrue(success.getCause() instanceof NullPointerException); |
756 |
} finally { |
757 |
joinPool(e); |
758 |
} |
759 |
} |
760 |
|
761 |
/** |
762 |
* invokeAll(c) returns results of all completed tasks in c |
763 |
*/ |
764 |
public void testInvokeAll5() throws Throwable { |
765 |
ExecutorService e = new ForkJoinPool(1); |
766 |
try { |
767 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
768 |
l.add(new StringTask()); |
769 |
l.add(new StringTask()); |
770 |
List<Future<String>> futures = e.invokeAll(l); |
771 |
assertEquals(2, futures.size()); |
772 |
for (Future<String> future : futures) |
773 |
assertSame(TEST_STRING, future.get()); |
774 |
} finally { |
775 |
joinPool(e); |
776 |
} |
777 |
} |
778 |
|
779 |
|
780 |
/** |
781 |
* timed invokeAny(null) throws NullPointerException |
782 |
*/ |
783 |
public void testTimedInvokeAny1() throws Throwable { |
784 |
ExecutorService e = new ForkJoinPool(1); |
785 |
try { |
786 |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
787 |
shouldThrow(); |
788 |
} catch (NullPointerException success) { |
789 |
} finally { |
790 |
joinPool(e); |
791 |
} |
792 |
} |
793 |
|
794 |
/** |
795 |
* timed invokeAny(null time unit) throws NullPointerException |
796 |
*/ |
797 |
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
798 |
ExecutorService e = new ForkJoinPool(1); |
799 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
800 |
l.add(new StringTask()); |
801 |
try { |
802 |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
803 |
shouldThrow(); |
804 |
} catch (NullPointerException success) { |
805 |
} finally { |
806 |
joinPool(e); |
807 |
} |
808 |
} |
809 |
|
810 |
/** |
811 |
* timed invokeAny(empty collection) throws IllegalArgumentException |
812 |
*/ |
813 |
public void testTimedInvokeAny2() throws Throwable { |
814 |
ExecutorService e = new ForkJoinPool(1); |
815 |
try { |
816 |
e.invokeAny(new ArrayList<Callable<String>>(), |
817 |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
818 |
shouldThrow(); |
819 |
} catch (IllegalArgumentException success) { |
820 |
} finally { |
821 |
joinPool(e); |
822 |
} |
823 |
} |
824 |
|
825 |
/** |
826 |
* timed invokeAny(c) throws NullPointerException if c has null elements |
827 |
*/ |
828 |
public void testTimedInvokeAny3() throws Throwable { |
829 |
CountDownLatch latch = new CountDownLatch(1); |
830 |
ExecutorService e = new ForkJoinPool(1); |
831 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
832 |
l.add(latchAwaitingStringTask(latch)); |
833 |
l.add(null); |
834 |
try { |
835 |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
836 |
shouldThrow(); |
837 |
} catch (NullPointerException success) { |
838 |
} finally { |
839 |
latch.countDown(); |
840 |
joinPool(e); |
841 |
} |
842 |
} |
843 |
|
844 |
/** |
845 |
* timed invokeAny(c) throws ExecutionException if no task completes |
846 |
*/ |
847 |
public void testTimedInvokeAny4() throws Throwable { |
848 |
ExecutorService e = new ForkJoinPool(1); |
849 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
850 |
l.add(new NPETask()); |
851 |
try { |
852 |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
853 |
shouldThrow(); |
854 |
} catch (ExecutionException success) { |
855 |
assertTrue(success.getCause() instanceof NullPointerException); |
856 |
} finally { |
857 |
joinPool(e); |
858 |
} |
859 |
} |
860 |
|
861 |
/** |
862 |
* timed invokeAny(c) returns result of some task in c |
863 |
*/ |
864 |
public void testTimedInvokeAny5() throws Throwable { |
865 |
ExecutorService e = new ForkJoinPool(1); |
866 |
try { |
867 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
868 |
l.add(new StringTask()); |
869 |
l.add(new StringTask()); |
870 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
871 |
assertSame(TEST_STRING, result); |
872 |
} finally { |
873 |
joinPool(e); |
874 |
} |
875 |
} |
876 |
|
877 |
/** |
878 |
* timed invokeAll(null) throws NullPointerException |
879 |
*/ |
880 |
public void testTimedInvokeAll1() throws Throwable { |
881 |
ExecutorService e = new ForkJoinPool(1); |
882 |
try { |
883 |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
884 |
shouldThrow(); |
885 |
} catch (NullPointerException success) { |
886 |
} finally { |
887 |
joinPool(e); |
888 |
} |
889 |
} |
890 |
|
891 |
/** |
892 |
* timed invokeAll(null time unit) throws NullPointerException |
893 |
*/ |
894 |
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
895 |
ExecutorService e = new ForkJoinPool(1); |
896 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
897 |
l.add(new StringTask()); |
898 |
try { |
899 |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
900 |
shouldThrow(); |
901 |
} catch (NullPointerException success) { |
902 |
} finally { |
903 |
joinPool(e); |
904 |
} |
905 |
} |
906 |
|
907 |
/** |
908 |
* timed invokeAll(empty collection) returns empty collection |
909 |
*/ |
910 |
public void testTimedInvokeAll2() throws InterruptedException { |
911 |
ExecutorService e = new ForkJoinPool(1); |
912 |
try { |
913 |
List<Future<String>> r |
914 |
= e.invokeAll(new ArrayList<Callable<String>>(), |
915 |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
916 |
assertTrue(r.isEmpty()); |
917 |
} finally { |
918 |
joinPool(e); |
919 |
} |
920 |
} |
921 |
|
922 |
/** |
923 |
* timed invokeAll(c) throws NullPointerException if c has null elements |
924 |
*/ |
925 |
public void testTimedInvokeAll3() throws InterruptedException { |
926 |
ExecutorService e = new ForkJoinPool(1); |
927 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
928 |
l.add(new StringTask()); |
929 |
l.add(null); |
930 |
try { |
931 |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
932 |
shouldThrow(); |
933 |
} catch (NullPointerException success) { |
934 |
} finally { |
935 |
joinPool(e); |
936 |
} |
937 |
} |
938 |
|
939 |
/** |
940 |
* get of returned element of invokeAll(c) throws exception on failed task |
941 |
*/ |
942 |
public void testTimedInvokeAll4() throws Throwable { |
943 |
ExecutorService e = new ForkJoinPool(1); |
944 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
945 |
l.add(new NPETask()); |
946 |
List<Future<String>> futures |
947 |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
948 |
assertEquals(1, futures.size()); |
949 |
try { |
950 |
futures.get(0).get(); |
951 |
shouldThrow(); |
952 |
} catch (ExecutionException success) { |
953 |
assertTrue(success.getCause() instanceof NullPointerException); |
954 |
} finally { |
955 |
joinPool(e); |
956 |
} |
957 |
} |
958 |
|
959 |
/** |
960 |
* timed invokeAll(c) returns results of all completed tasks in c |
961 |
*/ |
962 |
public void testTimedInvokeAll5() throws Throwable { |
963 |
ExecutorService e = new ForkJoinPool(1); |
964 |
try { |
965 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
966 |
l.add(new StringTask()); |
967 |
l.add(new StringTask()); |
968 |
List<Future<String>> futures |
969 |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
970 |
assertEquals(2, futures.size()); |
971 |
for (Future<String> future : futures) |
972 |
assertSame(TEST_STRING, future.get()); |
973 |
} finally { |
974 |
joinPool(e); |
975 |
} |
976 |
} |
977 |
|
978 |
} |