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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
|
7 |
|
import junit.framework.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.Executor; |
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; |
20 |
|
import java.util.concurrent.ForkJoinPool; |
21 |
|
import java.util.concurrent.ForkJoinTask; |
22 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
– |
import java.util.concurrent.RecursiveAction; |
23 |
|
import java.util.concurrent.RecursiveTask; |
24 |
|
import java.util.concurrent.TimeUnit; |
25 |
< |
import java.util.concurrent.locks.*; |
26 |
< |
import java.security.*; |
25 |
> |
import java.util.concurrent.locks.ReentrantLock; |
26 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
27 |
> |
import java.security.AccessControlException; |
28 |
> |
import java.security.Policy; |
29 |
> |
import java.security.PrivilegedAction; |
30 |
> |
import java.security.PrivilegedExceptionAction; |
31 |
|
|
32 |
|
public class ForkJoinPoolTest extends JSR166TestCase { |
33 |
|
public static void main(String[] args) { |
34 |
|
junit.textui.TestRunner.run(suite()); |
35 |
|
} |
36 |
+ |
|
37 |
|
public static Test suite() { |
38 |
|
return new TestSuite(ForkJoinPoolTest.class); |
39 |
|
} |
160 |
|
* tasks, and quiescent running state. |
161 |
|
*/ |
162 |
|
public void testDefaultInitialState() { |
163 |
< |
ForkJoinPool p = null; |
163 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
164 |
|
try { |
165 |
< |
p = new ForkJoinPool(1); |
166 |
< |
assertTrue(p.getFactory() == |
163 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
164 |
< |
assertTrue(p.isQuiescent()); |
165 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
166 |
> |
p.getFactory()); |
167 |
|
assertFalse(p.getAsyncMode()); |
168 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
169 |
< |
assertTrue(p.getStealCount() == 0); |
170 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
171 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
168 |
> |
assertEquals(0, p.getActiveThreadCount()); |
169 |
> |
assertEquals(0, p.getStealCount()); |
170 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
171 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
172 |
|
assertFalse(p.hasQueuedSubmissions()); |
173 |
|
assertFalse(p.isShutdown()); |
174 |
|
assertFalse(p.isTerminating()); |
198 |
|
} catch (NullPointerException success) {} |
199 |
|
} |
200 |
|
|
199 |
– |
|
201 |
|
/** |
202 |
|
* getParallelism returns size set in constructor |
203 |
|
*/ |
204 |
|
public void testGetParallelism() { |
205 |
< |
ForkJoinPool p = null; |
205 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
206 |
|
try { |
207 |
< |
p = new ForkJoinPool(1); |
207 |
< |
assertTrue(p.getParallelism() == 1); |
207 |
> |
assertEquals(1, p.getParallelism()); |
208 |
|
} finally { |
209 |
|
joinPool(p); |
210 |
|
} |
214 |
|
* getPoolSize returns number of started workers. |
215 |
|
*/ |
216 |
|
public void testGetPoolSize() { |
217 |
< |
ForkJoinPool p = null; |
217 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
218 |
|
try { |
219 |
< |
p = new ForkJoinPool(1); |
220 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
219 |
> |
assertEquals(0, p.getActiveThreadCount()); |
220 |
|
Future<String> future = p.submit(new StringTask()); |
221 |
< |
assertTrue(p.getPoolSize() == 1); |
223 |
< |
|
221 |
> |
assertEquals(1, p.getPoolSize()); |
222 |
|
} finally { |
223 |
|
joinPool(p); |
224 |
|
} |
231 |
|
* performs its defined action |
232 |
|
*/ |
233 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
234 |
< |
ForkJoinPool p = null; |
235 |
< |
try { |
236 |
< |
MyHandler eh = new MyHandler(); |
237 |
< |
p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
238 |
< |
assert(eh == p.getUncaughtExceptionHandler()); |
239 |
< |
p.execute(new FailingTask()); |
240 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
241 |
< |
assertTrue(eh.catches > 0); |
234 |
> |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
235 |
> |
final Thread.UncaughtExceptionHandler eh = |
236 |
> |
new Thread.UncaughtExceptionHandler() { |
237 |
> |
public void uncaughtException(Thread t, Throwable e) { |
238 |
> |
uehInvoked.countDown(); |
239 |
> |
}}; |
240 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
241 |
> |
eh, false); |
242 |
> |
try { |
243 |
> |
assertSame(eh, p.getUncaughtExceptionHandler()); |
244 |
> |
p.execute(new FibTask(8)); |
245 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
246 |
|
} finally { |
247 |
< |
p.shutdownNow(); |
247 |
> |
p.shutdownNow(); // failure might have prevented processing task |
248 |
|
joinPool(p); |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* After invoking a single task, isQuiescent is true, |
254 |
< |
* queues are empty, threads are not active, and |
255 |
< |
* construction parameters continue to hold |
253 |
> |
* After invoking a single task, isQuiescent eventually becomes |
254 |
> |
* true, at which time queues are empty, threads are not active, |
255 |
> |
* the task has completed successfully, and construction |
256 |
> |
* parameters continue to hold |
257 |
|
*/ |
258 |
< |
public void testisQuiescent() throws InterruptedException { |
259 |
< |
ForkJoinPool p = null; |
258 |
> |
public void testisQuiescent() throws Exception { |
259 |
> |
ForkJoinPool p = new ForkJoinPool(2); |
260 |
|
try { |
261 |
< |
p = new ForkJoinPool(2); |
262 |
< |
p.invoke(new FibTask(20)); |
263 |
< |
assertTrue(p.getFactory() == |
264 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
265 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
261 |
> |
assertTrue(p.isQuiescent()); |
262 |
> |
long startTime = System.nanoTime(); |
263 |
> |
FibTask f = new FibTask(20); |
264 |
> |
p.invoke(f); |
265 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
266 |
> |
p.getFactory()); |
267 |
> |
while (! p.isQuiescent()) { |
268 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
269 |
> |
throw new AssertionFailedError("timed out"); |
270 |
> |
assertFalse(p.getAsyncMode()); |
271 |
> |
assertFalse(p.isShutdown()); |
272 |
> |
assertFalse(p.isTerminating()); |
273 |
> |
assertFalse(p.isTerminated()); |
274 |
> |
Thread.yield(); |
275 |
> |
} |
276 |
> |
|
277 |
|
assertTrue(p.isQuiescent()); |
278 |
|
assertFalse(p.getAsyncMode()); |
279 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
280 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
281 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
279 |
> |
assertEquals(0, p.getActiveThreadCount()); |
280 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
281 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
282 |
|
assertFalse(p.hasQueuedSubmissions()); |
283 |
|
assertFalse(p.isShutdown()); |
284 |
|
assertFalse(p.isTerminating()); |
285 |
|
assertFalse(p.isTerminated()); |
286 |
+ |
assertTrue(f.isDone()); |
287 |
+ |
assertEquals(6765, (int) f.get()); |
288 |
|
} finally { |
289 |
|
joinPool(p); |
290 |
|
} |
294 |
|
* Completed submit(ForkJoinTask) returns result |
295 |
|
*/ |
296 |
|
public void testSubmitForkJoinTask() throws Throwable { |
297 |
< |
ForkJoinPool p = null; |
297 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
298 |
|
try { |
283 |
– |
p = new ForkJoinPool(1); |
299 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
300 |
< |
int r = f.get(); |
286 |
< |
assertTrue(r == 21); |
300 |
> |
assertEquals(21, (int) f.get()); |
301 |
|
} finally { |
302 |
|
joinPool(p); |
303 |
|
} |
307 |
|
* A task submitted after shutdown is rejected |
308 |
|
*/ |
309 |
|
public void testSubmitAfterShutdown() { |
310 |
< |
ForkJoinPool p = null; |
310 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
311 |
|
try { |
298 |
– |
p = new ForkJoinPool(1); |
312 |
|
p.shutdown(); |
313 |
|
assertTrue(p.isShutdown()); |
314 |
< |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
315 |
< |
shouldThrow(); |
316 |
< |
} catch (RejectedExecutionException success) { |
314 |
> |
try { |
315 |
> |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
316 |
> |
shouldThrow(); |
317 |
> |
} catch (RejectedExecutionException success) {} |
318 |
|
} finally { |
319 |
|
joinPool(p); |
320 |
|
} |
324 |
|
* Pool maintains parallelism when using ManagedBlocker |
325 |
|
*/ |
326 |
|
public void testBlockingForkJoinTask() throws Throwable { |
327 |
< |
ForkJoinPool p = null; |
327 |
> |
ForkJoinPool p = new ForkJoinPool(4); |
328 |
|
try { |
315 |
– |
p = new ForkJoinPool(4); |
329 |
|
ReentrantLock lock = new ReentrantLock(); |
330 |
|
ManagedLocker locker = new ManagedLocker(lock); |
331 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
331 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
332 |
|
p.execute(f); |
333 |
< |
int r = f.get(); |
321 |
< |
assertTrue(r == 832040); |
333 |
> |
assertEquals(6765, (int) f.get()); |
334 |
|
} finally { |
335 |
|
p.shutdownNow(); // don't wait out shutdown |
336 |
|
} |
340 |
|
* pollSubmission returns unexecuted submitted task, if present |
341 |
|
*/ |
342 |
|
public void testPollSubmission() { |
343 |
< |
SubFJP p = null; |
343 |
> |
final CountDownLatch done = new CountDownLatch(1); |
344 |
> |
SubFJP p = new SubFJP(); |
345 |
|
try { |
346 |
< |
p = new SubFJP(); |
347 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
348 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
336 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
346 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
347 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
348 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
349 |
|
ForkJoinTask r = p.pollSubmission(); |
350 |
|
assertTrue(r == a || r == b || r == c); |
351 |
|
assertFalse(r.isDone()); |
352 |
|
} finally { |
353 |
+ |
done.countDown(); |
354 |
|
joinPool(p); |
355 |
|
} |
356 |
|
} |
359 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
360 |
|
*/ |
361 |
|
public void testDrainTasksTo() { |
362 |
< |
SubFJP p = null; |
362 |
> |
final CountDownLatch done = new CountDownLatch(1); |
363 |
> |
SubFJP p = new SubFJP(); |
364 |
|
try { |
365 |
< |
p = new SubFJP(); |
366 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
367 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
354 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
365 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
366 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
367 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
368 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
369 |
|
p.drainTasksTo(al); |
370 |
|
assertTrue(al.size() > 0); |
373 |
|
assertFalse(r.isDone()); |
374 |
|
} |
375 |
|
} finally { |
376 |
+ |
done.countDown(); |
377 |
|
joinPool(p); |
378 |
|
} |
379 |
|
} |
380 |
|
|
367 |
– |
|
381 |
|
// FJ Versions of AbstractExecutorService tests |
382 |
|
|
383 |
|
/** |
385 |
|
*/ |
386 |
|
public void testExecuteRunnable() throws Throwable { |
387 |
|
ExecutorService e = new ForkJoinPool(1); |
388 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
389 |
< |
assertFalse(task.done); |
390 |
< |
Future<?> future = e.submit(task); |
391 |
< |
future.get(); |
392 |
< |
assertTrue(task.done); |
388 |
> |
try { |
389 |
> |
TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); |
390 |
> |
assertFalse(task.isDone()); |
391 |
> |
Future<?> future = e.submit(task); |
392 |
> |
assertNull(future.get()); |
393 |
> |
assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
394 |
> |
assertTrue(task.isDone()); |
395 |
> |
assertTrue(future.isDone()); |
396 |
> |
assertFalse(future.isCancelled()); |
397 |
> |
} finally { |
398 |
> |
joinPool(e); |
399 |
> |
} |
400 |
|
} |
401 |
|
|
382 |
– |
|
402 |
|
/** |
403 |
|
* Completed submit(callable) returns result |
404 |
|
*/ |
405 |
|
public void testSubmitCallable() throws Throwable { |
406 |
|
ExecutorService e = new ForkJoinPool(1); |
407 |
< |
Future<String> future = e.submit(new StringTask()); |
408 |
< |
String result = future.get(); |
409 |
< |
assertSame(TEST_STRING, result); |
407 |
> |
try { |
408 |
> |
Future<String> future = e.submit(new StringTask()); |
409 |
> |
assertSame(TEST_STRING, future.get()); |
410 |
> |
assertTrue(future.isDone()); |
411 |
> |
assertFalse(future.isCancelled()); |
412 |
> |
} finally { |
413 |
> |
joinPool(e); |
414 |
> |
} |
415 |
|
} |
416 |
|
|
417 |
|
/** |
419 |
|
*/ |
420 |
|
public void testSubmitRunnable() throws Throwable { |
421 |
|
ExecutorService e = new ForkJoinPool(1); |
422 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
423 |
< |
future.get(); |
424 |
< |
assertTrue(future.isDone()); |
422 |
> |
try { |
423 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
424 |
> |
assertNull(future.get()); |
425 |
> |
assertTrue(future.isDone()); |
426 |
> |
assertFalse(future.isCancelled()); |
427 |
> |
} finally { |
428 |
> |
joinPool(e); |
429 |
> |
} |
430 |
|
} |
431 |
|
|
432 |
|
/** |
434 |
|
*/ |
435 |
|
public void testSubmitRunnable2() throws Throwable { |
436 |
|
ExecutorService e = new ForkJoinPool(1); |
437 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
438 |
< |
String result = future.get(); |
439 |
< |
assertSame(TEST_STRING, result); |
437 |
> |
try { |
438 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
439 |
> |
assertSame(TEST_STRING, future.get()); |
440 |
> |
assertTrue(future.isDone()); |
441 |
> |
assertFalse(future.isCancelled()); |
442 |
> |
} finally { |
443 |
> |
joinPool(e); |
444 |
> |
} |
445 |
|
} |
446 |
|
|
413 |
– |
|
447 |
|
/** |
448 |
< |
* A submitted privileged action to completion |
448 |
> |
* A submitted privileged action runs to completion |
449 |
|
*/ |
450 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
451 |
< |
Policy savedPolicy = null; |
452 |
< |
try { |
453 |
< |
savedPolicy = Policy.getPolicy(); |
454 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
422 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
423 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
424 |
< |
Policy.setPolicy(policy); |
425 |
< |
} catch (AccessControlException ok) { |
426 |
< |
return; |
427 |
< |
} |
428 |
< |
try { |
429 |
< |
ExecutorService e = new ForkJoinPool(1); |
430 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
450 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
451 |
> |
Runnable r = new CheckedRunnable() { |
452 |
> |
public void realRun() throws Exception { |
453 |
> |
ExecutorService e = new ForkJoinPool(1); |
454 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
455 |
|
public Object run() { |
456 |
|
return TEST_STRING; |
457 |
|
}})); |
458 |
|
|
459 |
< |
Object result = future.get(); |
460 |
< |
assertSame(TEST_STRING, result); |
461 |
< |
} |
462 |
< |
finally { |
463 |
< |
Policy.setPolicy(savedPolicy); |
440 |
< |
} |
459 |
> |
assertSame(TEST_STRING, future.get()); |
460 |
> |
}}; |
461 |
> |
|
462 |
> |
runWithPermissions(r, |
463 |
> |
new RuntimePermission("modifyThread")); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
< |
* A submitted a privileged exception action runs to completion |
467 |
> |
* A submitted privileged exception action runs to completion |
468 |
|
*/ |
469 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
470 |
< |
Policy savedPolicy = null; |
471 |
< |
try { |
472 |
< |
savedPolicy = Policy.getPolicy(); |
473 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
451 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
452 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
453 |
< |
Policy.setPolicy(policy); |
454 |
< |
} catch (AccessControlException ok) { |
455 |
< |
return; |
456 |
< |
} |
457 |
< |
|
458 |
< |
try { |
459 |
< |
ExecutorService e = new ForkJoinPool(1); |
460 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
469 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
470 |
> |
Runnable r = new CheckedRunnable() { |
471 |
> |
public void realRun() throws Exception { |
472 |
> |
ExecutorService e = new ForkJoinPool(1); |
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 { |
469 |
< |
Policy.setPolicy(savedPolicy); |
470 |
< |
} |
478 |
> |
assertSame(TEST_STRING, future.get()); |
479 |
> |
}}; |
480 |
> |
|
481 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
|
* A submitted failed privileged exception action reports exception |
486 |
|
*/ |
487 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
488 |
< |
Policy savedPolicy = null; |
489 |
< |
try { |
490 |
< |
savedPolicy = Policy.getPolicy(); |
491 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
481 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
482 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
483 |
< |
Policy.setPolicy(policy); |
484 |
< |
} catch (AccessControlException ok) { |
485 |
< |
return; |
486 |
< |
} |
487 |
< |
|
488 |
< |
|
489 |
< |
try { |
490 |
< |
ExecutorService e = new ForkJoinPool(1); |
491 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
487 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
488 |
> |
Runnable r = new CheckedRunnable() { |
489 |
> |
public void realRun() throws Exception { |
490 |
> |
ExecutorService e = new ForkJoinPool(1); |
491 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
492 |
|
public Object run() throws Exception { |
493 |
|
throw new IndexOutOfBoundsException(); |
494 |
|
}})); |
495 |
|
|
496 |
< |
Object result = future.get(); |
497 |
< |
shouldThrow(); |
498 |
< |
} catch (ExecutionException success) { |
499 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
500 |
< |
} finally { |
501 |
< |
Policy.setPolicy(savedPolicy); |
502 |
< |
} |
496 |
> |
try { |
497 |
> |
future.get(); |
498 |
> |
shouldThrow(); |
499 |
> |
} catch (ExecutionException success) { |
500 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
501 |
> |
}}}; |
502 |
> |
|
503 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
504 |
|
} |
505 |
|
|
506 |
|
/** |
507 |
|
* execute(null runnable) throws NullPointerException |
508 |
|
*/ |
509 |
|
public void testExecuteNullRunnable() { |
510 |
+ |
ExecutorService e = new ForkJoinPool(1); |
511 |
|
try { |
512 |
< |
ExecutorService e = new ForkJoinPool(1); |
511 |
< |
TrackedShortRunnable task = null; |
512 |
< |
Future<?> future = e.submit(task); |
512 |
> |
Future<?> future = e.submit((Runnable) null); |
513 |
|
shouldThrow(); |
514 |
< |
} catch (NullPointerException success) {} |
514 |
> |
} catch (NullPointerException success) { |
515 |
> |
} finally { |
516 |
> |
joinPool(e); |
517 |
> |
} |
518 |
|
} |
519 |
|
|
517 |
– |
|
520 |
|
/** |
521 |
|
* submit(null callable) throws NullPointerException |
522 |
|
*/ |
523 |
|
public void testSubmitNullCallable() { |
524 |
+ |
ExecutorService e = new ForkJoinPool(1); |
525 |
|
try { |
526 |
< |
ExecutorService e = new ForkJoinPool(1); |
524 |
< |
StringTask t = null; |
525 |
< |
Future<String> future = e.submit(t); |
526 |
> |
Future<String> future = e.submit((Callable) null); |
527 |
|
shouldThrow(); |
528 |
< |
} catch (NullPointerException success) {} |
528 |
> |
} catch (NullPointerException success) { |
529 |
> |
} finally { |
530 |
> |
joinPool(e); |
531 |
> |
} |
532 |
|
} |
533 |
|
|
530 |
– |
|
534 |
|
/** |
535 |
< |
* Blocking on submit(callable) throws InterruptedException if |
533 |
< |
* caller interrupted. |
535 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
536 |
|
*/ |
537 |
|
public void testInterruptedSubmit() throws InterruptedException { |
538 |
< |
final ForkJoinPool p = new ForkJoinPool(1); |
539 |
< |
|
540 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
541 |
< |
public void realRun() throws Throwable { |
542 |
< |
p.submit(new CheckedCallable<Object>() { |
543 |
< |
public Object realCall() throws Throwable { |
544 |
< |
try { |
545 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
546 |
< |
} catch (InterruptedException ok) { |
547 |
< |
} |
548 |
< |
return null; |
549 |
< |
}}).get(); |
550 |
< |
}}); |
551 |
< |
|
552 |
< |
t.start(); |
553 |
< |
Thread.sleep(SHORT_DELAY_MS); |
554 |
< |
t.interrupt(); |
555 |
< |
t.join(); |
556 |
< |
p.shutdownNow(); |
557 |
< |
joinPool(p); |
538 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
539 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
540 |
> |
final ExecutorService p = new ForkJoinPool(1); |
541 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
542 |
> |
public Void realCall() throws InterruptedException { |
543 |
> |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
544 |
> |
return null; |
545 |
> |
}}; |
546 |
> |
try { |
547 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
548 |
> |
public void realRun() throws Exception { |
549 |
> |
Future<Void> future = p.submit(awaiter); |
550 |
> |
submitted.countDown(); |
551 |
> |
future.get(); |
552 |
> |
}}); |
553 |
> |
t.start(); |
554 |
> |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
555 |
> |
t.interrupt(); |
556 |
> |
t.join(); |
557 |
> |
} finally { |
558 |
> |
quittingTime.countDown(); |
559 |
> |
joinPool(p); |
560 |
> |
} |
561 |
|
} |
562 |
|
|
563 |
|
/** |
575 |
|
shouldThrow(); |
576 |
|
} catch (ExecutionException success) { |
577 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
578 |
+ |
} finally { |
579 |
+ |
joinPool(p); |
580 |
|
} |
574 |
– |
|
575 |
– |
joinPool(p); |
581 |
|
} |
582 |
|
|
583 |
|
/** |
759 |
|
} |
760 |
|
} |
761 |
|
|
757 |
– |
|
762 |
|
/** |
763 |
|
* timed invokeAny(null) throws NullPointerException |
764 |
|
*/ |
765 |
|
public void testTimedInvokeAny1() throws Throwable { |
766 |
|
ExecutorService e = new ForkJoinPool(1); |
767 |
|
try { |
768 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
768 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
769 |
|
shouldThrow(); |
770 |
|
} catch (NullPointerException success) { |
771 |
|
} finally { |
796 |
|
ExecutorService e = new ForkJoinPool(1); |
797 |
|
try { |
798 |
|
e.invokeAny(new ArrayList<Callable<String>>(), |
799 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
799 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
800 |
|
shouldThrow(); |
801 |
|
} catch (IllegalArgumentException success) { |
802 |
|
} finally { |
814 |
|
l.add(latchAwaitingStringTask(latch)); |
815 |
|
l.add(null); |
816 |
|
try { |
817 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
817 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
818 |
|
shouldThrow(); |
819 |
|
} catch (NullPointerException success) { |
820 |
|
} finally { |
831 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
832 |
|
l.add(new NPETask()); |
833 |
|
try { |
834 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
834 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
835 |
|
shouldThrow(); |
836 |
|
} catch (ExecutionException success) { |
837 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
849 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
850 |
|
l.add(new StringTask()); |
851 |
|
l.add(new StringTask()); |
852 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
852 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
853 |
|
assertSame(TEST_STRING, result); |
854 |
|
} finally { |
855 |
|
joinPool(e); |
862 |
|
public void testTimedInvokeAll1() throws Throwable { |
863 |
|
ExecutorService e = new ForkJoinPool(1); |
864 |
|
try { |
865 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
865 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
866 |
|
shouldThrow(); |
867 |
|
} catch (NullPointerException success) { |
868 |
|
} finally { |
894 |
|
try { |
895 |
|
List<Future<String>> r |
896 |
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
897 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
897 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
898 |
|
assertTrue(r.isEmpty()); |
899 |
|
} finally { |
900 |
|
joinPool(e); |
910 |
|
l.add(new StringTask()); |
911 |
|
l.add(null); |
912 |
|
try { |
913 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
913 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
914 |
|
shouldThrow(); |
915 |
|
} catch (NullPointerException success) { |
916 |
|
} finally { |
926 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
927 |
|
l.add(new NPETask()); |
928 |
|
List<Future<String>> futures |
929 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
929 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
930 |
|
assertEquals(1, futures.size()); |
931 |
|
try { |
932 |
|
futures.get(0).get(); |
948 |
|
l.add(new StringTask()); |
949 |
|
l.add(new StringTask()); |
950 |
|
List<Future<String>> futures |
951 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
951 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
952 |
|
assertEquals(2, futures.size()); |
953 |
|
for (Future<String> future : futures) |
954 |
|
assertSame(TEST_STRING, future.get()); |