328 |
|
* pollSubmission returns unexecuted submitted task, if present |
329 |
|
*/ |
330 |
|
public void testPollSubmission() { |
331 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
332 |
|
SubFJP p = new SubFJP(); |
333 |
|
try { |
334 |
< |
ForkJoinTask a = p.submit(new ShortRunnable()); |
335 |
< |
ForkJoinTask b = p.submit(new ShortRunnable()); |
336 |
< |
ForkJoinTask c = p.submit(new ShortRunnable()); |
334 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
335 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
336 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
337 |
|
ForkJoinTask r = p.pollSubmission(); |
338 |
|
assertTrue(r == a || r == b || r == c); |
339 |
|
assertFalse(r.isDone()); |
340 |
|
} finally { |
341 |
+ |
done.countDown(); |
342 |
|
joinPool(p); |
343 |
|
} |
344 |
|
} |
347 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
348 |
|
*/ |
349 |
|
public void testDrainTasksTo() { |
350 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
351 |
|
SubFJP p = new SubFJP(); |
352 |
|
try { |
353 |
< |
ForkJoinTask a = p.submit(new ShortRunnable()); |
354 |
< |
ForkJoinTask b = p.submit(new ShortRunnable()); |
355 |
< |
ForkJoinTask c = p.submit(new ShortRunnable()); |
353 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
354 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
355 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
356 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
357 |
|
p.drainTasksTo(al); |
358 |
|
assertTrue(al.size() > 0); |
361 |
|
assertFalse(r.isDone()); |
362 |
|
} |
363 |
|
} finally { |
364 |
+ |
done.countDown(); |
365 |
|
joinPool(p); |
366 |
|
} |
367 |
|
} |
378 |
|
TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); |
379 |
|
assertFalse(task.isDone()); |
380 |
|
Future<?> future = e.submit(task); |
381 |
< |
future.get(); |
381 |
> |
assertNull(future.get()); |
382 |
> |
assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
383 |
|
assertTrue(task.isDone()); |
384 |
+ |
assertTrue(future.isDone()); |
385 |
+ |
assertFalse(future.isCancelled()); |
386 |
|
} finally { |
387 |
|
joinPool(e); |
388 |
|
} |
396 |
|
ExecutorService e = new ForkJoinPool(1); |
397 |
|
try { |
398 |
|
Future<String> future = e.submit(new StringTask()); |
399 |
< |
String result = future.get(); |
400 |
< |
assertSame(TEST_STRING, result); |
399 |
> |
assertSame(TEST_STRING, future.get()); |
400 |
> |
assertTrue(future.isDone()); |
401 |
> |
assertFalse(future.isCancelled()); |
402 |
|
} finally { |
403 |
|
joinPool(e); |
404 |
|
} |
411 |
|
ExecutorService e = new ForkJoinPool(1); |
412 |
|
try { |
413 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
414 |
< |
future.get(); |
414 |
> |
assertNull(future.get()); |
415 |
|
assertTrue(future.isDone()); |
416 |
+ |
assertFalse(future.isCancelled()); |
417 |
|
} finally { |
418 |
|
joinPool(e); |
419 |
|
} |
426 |
|
ExecutorService e = new ForkJoinPool(1); |
427 |
|
try { |
428 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
429 |
< |
String result = future.get(); |
430 |
< |
assertSame(TEST_STRING, result); |
429 |
> |
assertSame(TEST_STRING, future.get()); |
430 |
> |
assertTrue(future.isDone()); |
431 |
> |
assertFalse(future.isCancelled()); |
432 |
|
} finally { |
433 |
|
joinPool(e); |
434 |
|
} |
435 |
|
} |
436 |
|
|
427 |
– |
|
437 |
|
/** |
438 |
< |
* A submitted privileged action to completion |
438 |
> |
* A submitted privileged action runs to completion |
439 |
|
*/ |
440 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
441 |
< |
Policy savedPolicy = null; |
442 |
< |
try { |
443 |
< |
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 { |
440 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
441 |
> |
Runnable r = new CheckedRunnable() { |
442 |
> |
public void realRun() throws Exception { |
443 |
> |
ExecutorService e = new ForkJoinPool(1); |
444 |
|
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
445 |
|
public Object run() { |
446 |
|
return TEST_STRING; |
447 |
|
}})); |
448 |
|
|
449 |
< |
Object result = future.get(); |
450 |
< |
assertSame(TEST_STRING, result); |
451 |
< |
} finally { |
452 |
< |
joinPool(e); |
453 |
< |
} |
456 |
< |
} finally { |
457 |
< |
Policy.setPolicy(savedPolicy); |
458 |
< |
} |
449 |
> |
assertSame(TEST_STRING, future.get()); |
450 |
> |
}}; |
451 |
> |
|
452 |
> |
runWithPermissions(r, |
453 |
> |
new RuntimePermission("modifyThread")); |
454 |
|
} |
455 |
|
|
456 |
|
/** |
457 |
< |
* A submitted a privileged exception action runs to completion |
457 |
> |
* A submitted privileged exception action runs to completion |
458 |
|
*/ |
459 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
460 |
< |
Policy savedPolicy = null; |
461 |
< |
try { |
462 |
< |
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 { |
459 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
460 |
> |
Runnable r = new CheckedRunnable() { |
461 |
> |
public void realRun() throws Exception { |
462 |
> |
ExecutorService e = new ForkJoinPool(1); |
463 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
464 |
|
public Object run() { |
465 |
|
return TEST_STRING; |
466 |
|
}})); |
467 |
|
|
468 |
< |
Object result = future.get(); |
469 |
< |
assertSame(TEST_STRING, result); |
470 |
< |
} finally { |
471 |
< |
joinPool(e); |
488 |
< |
} |
489 |
< |
} finally { |
490 |
< |
Policy.setPolicy(savedPolicy); |
491 |
< |
} |
468 |
> |
assertSame(TEST_STRING, future.get()); |
469 |
> |
}}; |
470 |
> |
|
471 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
472 |
|
} |
473 |
|
|
474 |
|
/** |
475 |
|
* A submitted failed privileged exception action reports exception |
476 |
|
*/ |
477 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
478 |
< |
Policy savedPolicy = null; |
479 |
< |
try { |
480 |
< |
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 { |
477 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
478 |
> |
Runnable r = new CheckedRunnable() { |
479 |
> |
public void realRun() throws Exception { |
480 |
> |
ExecutorService e = new ForkJoinPool(1); |
481 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
482 |
|
public Object run() throws Exception { |
483 |
|
throw new IndexOutOfBoundsException(); |
484 |
|
}})); |
485 |
|
|
486 |
< |
Object result = future.get(); |
487 |
< |
shouldThrow(); |
488 |
< |
} catch (ExecutionException success) { |
489 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
490 |
< |
} finally { |
491 |
< |
joinPool(e); |
492 |
< |
} |
493 |
< |
} finally { |
525 |
< |
Policy.setPolicy(savedPolicy); |
526 |
< |
} |
486 |
> |
try { |
487 |
> |
future.get(); |
488 |
> |
shouldThrow(); |
489 |
> |
} catch (ExecutionException success) { |
490 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
491 |
> |
}}}; |
492 |
> |
|
493 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
494 |
|
} |
495 |
|
|
496 |
|
/** |