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 |
|
import junit.framework.*; |
22 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
|
import java.util.concurrent.RecursiveTask; |
24 |
|
import java.util.concurrent.TimeUnit; |
25 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.locks.ReentrantLock; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
|
import java.security.AccessControlException; |
165 |
|
try { |
166 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
167 |
|
p.getFactory()); |
167 |
– |
assertTrue(p.isQuiescent()); |
168 |
|
assertFalse(p.getAsyncMode()); |
169 |
|
assertEquals(0, p.getActiveThreadCount()); |
170 |
|
assertEquals(0, p.getStealCount()); |
199 |
|
} catch (NullPointerException success) {} |
200 |
|
} |
201 |
|
|
202 |
– |
|
202 |
|
/** |
203 |
|
* getParallelism returns size set in constructor |
204 |
|
*/ |
242 |
|
eh, false); |
243 |
|
try { |
244 |
|
assertSame(eh, p.getUncaughtExceptionHandler()); |
245 |
< |
p.execute(new FibTask(8)); |
246 |
< |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
245 |
> |
try { |
246 |
> |
p.execute(new FibTask(8)); |
247 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
248 |
> |
} catch (RejectedExecutionException ok) { |
249 |
> |
} |
250 |
|
} finally { |
251 |
|
p.shutdownNow(); // failure might have prevented processing task |
252 |
|
joinPool(p); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* After invoking a single task, isQuiescent is true, |
258 |
< |
* queues are empty, threads are not active, and |
259 |
< |
* construction parameters continue to hold |
257 |
> |
* After invoking a single task, isQuiescent eventually becomes |
258 |
> |
* true, at which time queues are empty, threads are not active, |
259 |
> |
* the task has completed successfully, and construction |
260 |
> |
* parameters continue to hold |
261 |
|
*/ |
262 |
< |
public void testisQuiescent() throws InterruptedException { |
262 |
> |
public void testIsQuiescent() throws Exception { |
263 |
|
ForkJoinPool p = new ForkJoinPool(2); |
264 |
|
try { |
265 |
|
assertTrue(p.isQuiescent()); |
266 |
< |
p.invoke(new FibTask(20)); |
266 |
> |
long startTime = System.nanoTime(); |
267 |
> |
FibTask f = new FibTask(20); |
268 |
> |
p.invoke(f); |
269 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
270 |
|
p.getFactory()); |
271 |
< |
Thread.sleep(SMALL_DELAY_MS); |
271 |
> |
while (! p.isQuiescent()) { |
272 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
273 |
> |
throw new AssertionFailedError("timed out"); |
274 |
> |
assertFalse(p.getAsyncMode()); |
275 |
> |
assertFalse(p.isShutdown()); |
276 |
> |
assertFalse(p.isTerminating()); |
277 |
> |
assertFalse(p.isTerminated()); |
278 |
> |
Thread.yield(); |
279 |
> |
} |
280 |
> |
|
281 |
|
assertTrue(p.isQuiescent()); |
282 |
|
assertFalse(p.getAsyncMode()); |
283 |
|
assertEquals(0, p.getActiveThreadCount()); |
287 |
|
assertFalse(p.isShutdown()); |
288 |
|
assertFalse(p.isTerminating()); |
289 |
|
assertFalse(p.isTerminated()); |
290 |
+ |
assertTrue(f.isDone()); |
291 |
+ |
assertEquals(6765, (int) f.get()); |
292 |
|
} finally { |
293 |
|
joinPool(p); |
294 |
|
} |
332 |
|
try { |
333 |
|
ReentrantLock lock = new ReentrantLock(); |
334 |
|
ManagedLocker locker = new ManagedLocker(lock); |
335 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
335 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
336 |
|
p.execute(f); |
337 |
< |
assertEquals(832040, (int) f.get()); |
337 |
> |
assertEquals(6765, (int) f.get()); |
338 |
|
} finally { |
339 |
|
p.shutdownNow(); // don't wait out shutdown |
340 |
|
} |
344 |
|
* pollSubmission returns unexecuted submitted task, if present |
345 |
|
*/ |
346 |
|
public void testPollSubmission() { |
347 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
348 |
|
SubFJP p = new SubFJP(); |
349 |
|
try { |
350 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
351 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
352 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
350 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
351 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
352 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
353 |
|
ForkJoinTask r = p.pollSubmission(); |
354 |
|
assertTrue(r == a || r == b || r == c); |
355 |
|
assertFalse(r.isDone()); |
356 |
|
} finally { |
357 |
+ |
done.countDown(); |
358 |
|
joinPool(p); |
359 |
|
} |
360 |
|
} |
363 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
364 |
|
*/ |
365 |
|
public void testDrainTasksTo() { |
366 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
367 |
|
SubFJP p = new SubFJP(); |
368 |
|
try { |
369 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
370 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
371 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
369 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
370 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
371 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
372 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
373 |
|
p.drainTasksTo(al); |
374 |
|
assertTrue(al.size() > 0); |
377 |
|
assertFalse(r.isDone()); |
378 |
|
} |
379 |
|
} finally { |
380 |
+ |
done.countDown(); |
381 |
|
joinPool(p); |
382 |
|
} |
383 |
|
} |
384 |
|
|
365 |
– |
|
385 |
|
// FJ Versions of AbstractExecutorService tests |
386 |
|
|
387 |
|
/** |
390 |
|
public void testExecuteRunnable() throws Throwable { |
391 |
|
ExecutorService e = new ForkJoinPool(1); |
392 |
|
try { |
393 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
394 |
< |
assertFalse(task.done); |
393 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
394 |
> |
CheckedRunnable task = new CheckedRunnable() { |
395 |
> |
public void realRun() { |
396 |
> |
done.set(true); |
397 |
> |
}}; |
398 |
|
Future<?> future = e.submit(task); |
399 |
< |
future.get(); |
400 |
< |
assertTrue(task.done); |
399 |
> |
assertNull(future.get()); |
400 |
> |
assertNull(future.get(0, MILLISECONDS)); |
401 |
> |
assertTrue(done.get()); |
402 |
> |
assertTrue(future.isDone()); |
403 |
> |
assertFalse(future.isCancelled()); |
404 |
|
} finally { |
405 |
|
joinPool(e); |
406 |
|
} |
407 |
|
} |
408 |
|
|
384 |
– |
|
409 |
|
/** |
410 |
|
* Completed submit(callable) returns result |
411 |
|
*/ |
413 |
|
ExecutorService e = new ForkJoinPool(1); |
414 |
|
try { |
415 |
|
Future<String> future = e.submit(new StringTask()); |
416 |
< |
String result = future.get(); |
417 |
< |
assertSame(TEST_STRING, result); |
416 |
> |
assertSame(TEST_STRING, future.get()); |
417 |
> |
assertTrue(future.isDone()); |
418 |
> |
assertFalse(future.isCancelled()); |
419 |
|
} finally { |
420 |
|
joinPool(e); |
421 |
|
} |
428 |
|
ExecutorService e = new ForkJoinPool(1); |
429 |
|
try { |
430 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
431 |
< |
future.get(); |
431 |
> |
assertNull(future.get()); |
432 |
|
assertTrue(future.isDone()); |
433 |
+ |
assertFalse(future.isCancelled()); |
434 |
|
} finally { |
435 |
|
joinPool(e); |
436 |
|
} |
443 |
|
ExecutorService e = new ForkJoinPool(1); |
444 |
|
try { |
445 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
446 |
< |
String result = future.get(); |
447 |
< |
assertSame(TEST_STRING, result); |
446 |
> |
assertSame(TEST_STRING, future.get()); |
447 |
> |
assertTrue(future.isDone()); |
448 |
> |
assertFalse(future.isCancelled()); |
449 |
|
} finally { |
450 |
|
joinPool(e); |
451 |
|
} |
452 |
|
} |
453 |
|
|
427 |
– |
|
454 |
|
/** |
455 |
< |
* A submitted privileged action to completion |
455 |
> |
* A submitted privileged action runs to completion |
456 |
|
*/ |
457 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
458 |
< |
Policy savedPolicy = null; |
459 |
< |
try { |
460 |
< |
savedPolicy = Policy.getPolicy(); |
435 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
436 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
437 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
438 |
< |
Policy.setPolicy(policy); |
439 |
< |
} catch (AccessControlException ok) { |
440 |
< |
return; |
441 |
< |
} |
442 |
< |
|
443 |
< |
try { |
444 |
< |
ExecutorService e = new ForkJoinPool(1); |
445 |
< |
try { |
457 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
458 |
> |
Runnable r = new CheckedRunnable() { |
459 |
> |
public void realRun() throws Exception { |
460 |
> |
ExecutorService e = new ForkJoinPool(1); |
461 |
|
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
462 |
|
public Object run() { |
463 |
|
return TEST_STRING; |
464 |
|
}})); |
465 |
|
|
466 |
< |
Object result = future.get(); |
467 |
< |
assertSame(TEST_STRING, result); |
468 |
< |
} finally { |
469 |
< |
joinPool(e); |
470 |
< |
} |
456 |
< |
} finally { |
457 |
< |
Policy.setPolicy(savedPolicy); |
458 |
< |
} |
466 |
> |
assertSame(TEST_STRING, future.get()); |
467 |
> |
}}; |
468 |
> |
|
469 |
> |
runWithPermissions(r, |
470 |
> |
new RuntimePermission("modifyThread")); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
< |
* A submitted a privileged exception action runs to completion |
474 |
> |
* A submitted privileged exception action runs to completion |
475 |
|
*/ |
476 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
477 |
< |
Policy savedPolicy = null; |
478 |
< |
try { |
479 |
< |
savedPolicy = Policy.getPolicy(); |
468 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
469 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
470 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
471 |
< |
Policy.setPolicy(policy); |
472 |
< |
} catch (AccessControlException ok) { |
473 |
< |
return; |
474 |
< |
} |
475 |
< |
|
476 |
< |
try { |
477 |
< |
ExecutorService e = new ForkJoinPool(1); |
478 |
< |
try { |
476 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
477 |
> |
Runnable r = new CheckedRunnable() { |
478 |
> |
public void realRun() throws Exception { |
479 |
> |
ExecutorService e = new ForkJoinPool(1); |
480 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
481 |
|
public Object run() { |
482 |
|
return TEST_STRING; |
483 |
|
}})); |
484 |
|
|
485 |
< |
Object result = future.get(); |
486 |
< |
assertSame(TEST_STRING, result); |
487 |
< |
} finally { |
488 |
< |
joinPool(e); |
488 |
< |
} |
489 |
< |
} finally { |
490 |
< |
Policy.setPolicy(savedPolicy); |
491 |
< |
} |
485 |
> |
assertSame(TEST_STRING, future.get()); |
486 |
> |
}}; |
487 |
> |
|
488 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
489 |
|
} |
490 |
|
|
491 |
|
/** |
492 |
|
* A submitted failed privileged exception action reports exception |
493 |
|
*/ |
494 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
495 |
< |
Policy savedPolicy = null; |
496 |
< |
try { |
497 |
< |
savedPolicy = Policy.getPolicy(); |
501 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
502 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
503 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
504 |
< |
Policy.setPolicy(policy); |
505 |
< |
} catch (AccessControlException ok) { |
506 |
< |
return; |
507 |
< |
} |
508 |
< |
|
509 |
< |
try { |
510 |
< |
ExecutorService e = new ForkJoinPool(1); |
511 |
< |
try { |
494 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
495 |
> |
Runnable r = new CheckedRunnable() { |
496 |
> |
public void realRun() throws Exception { |
497 |
> |
ExecutorService e = new ForkJoinPool(1); |
498 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
499 |
|
public Object run() throws Exception { |
500 |
|
throw new IndexOutOfBoundsException(); |
501 |
|
}})); |
502 |
|
|
503 |
< |
Object result = future.get(); |
504 |
< |
shouldThrow(); |
505 |
< |
} catch (ExecutionException success) { |
506 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
507 |
< |
} finally { |
508 |
< |
joinPool(e); |
509 |
< |
} |
510 |
< |
} finally { |
525 |
< |
Policy.setPolicy(savedPolicy); |
526 |
< |
} |
503 |
> |
try { |
504 |
> |
future.get(); |
505 |
> |
shouldThrow(); |
506 |
> |
} catch (ExecutionException success) { |
507 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
508 |
> |
}}}; |
509 |
> |
|
510 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
524 |
|
} |
525 |
|
} |
526 |
|
|
543 |
– |
|
527 |
|
/** |
528 |
|
* submit(null callable) throws NullPointerException |
529 |
|
*/ |
538 |
|
} |
539 |
|
} |
540 |
|
|
558 |
– |
|
541 |
|
/** |
542 |
|
* submit(callable).get() throws InterruptedException if interrupted |
543 |
|
*/ |
575 |
|
ForkJoinPool p = new ForkJoinPool(1); |
576 |
|
try { |
577 |
|
p.submit(new Callable() { |
578 |
< |
public Object call() { |
579 |
< |
int i = 5/0; |
598 |
< |
return Boolean.TRUE; |
599 |
< |
}}).get(); |
578 |
> |
public Object call() { throw new ArithmeticException(); }}) |
579 |
> |
.get(); |
580 |
|
shouldThrow(); |
581 |
|
} catch (ExecutionException success) { |
582 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
764 |
|
} |
765 |
|
} |
766 |
|
|
787 |
– |
|
767 |
|
/** |
768 |
|
* timed invokeAny(null) throws NullPointerException |
769 |
|
*/ |