25 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.locks.ReentrantLock; |
27 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
29 |
|
import java.security.AccessControlException; |
30 |
|
import java.security.Policy; |
31 |
|
import java.security.PrivilegedAction; |
40 |
|
return new TestSuite(ForkJoinPoolTest.class); |
41 |
|
} |
42 |
|
|
43 |
< |
/** |
43 |
> |
/* |
44 |
|
* Testing coverage notes: |
45 |
|
* |
46 |
|
* 1. shutdown and related methods are tested via super.joinPool. |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
+ |
* awaitTermination on a non-shutdown pool times out |
231 |
+ |
*/ |
232 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
233 |
+ |
ForkJoinPool p = new ForkJoinPool(1); |
234 |
+ |
assertFalse(p.isTerminated()); |
235 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
236 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
237 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
238 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
239 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
240 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
241 |
+ |
long timeoutNanos = 999999L; |
242 |
+ |
long startTime = System.nanoTime(); |
243 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
244 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
245 |
+ |
assertFalse(p.isTerminated()); |
246 |
+ |
startTime = System.nanoTime(); |
247 |
+ |
long timeoutMillis = timeoutMillis(); |
248 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
249 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
250 |
+ |
assertFalse(p.isTerminated()); |
251 |
+ |
p.shutdown(); |
252 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
253 |
+ |
assertTrue(p.isTerminated()); |
254 |
+ |
} |
255 |
+ |
|
256 |
+ |
/** |
257 |
|
* setUncaughtExceptionHandler changes handler for uncaught exceptions. |
258 |
|
* |
259 |
|
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
272 |
|
assertSame(eh, p.getUncaughtExceptionHandler()); |
273 |
|
try { |
274 |
|
p.execute(new FibTask(8)); |
275 |
< |
assertTrue(uehInvoked.await(10000, MILLISECONDS)); |
276 |
< |
} catch(RejectedExecutionException ok) { |
275 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
276 |
> |
} catch (RejectedExecutionException ok) { |
277 |
|
} |
278 |
|
} finally { |
279 |
|
p.shutdownNow(); // failure might have prevented processing task |
483 |
|
* A submitted privileged action runs to completion |
484 |
|
*/ |
485 |
|
public void testSubmitPrivilegedAction() throws Exception { |
486 |
+ |
final Callable callable = Executors.callable(new PrivilegedAction() { |
487 |
+ |
public Object run() { return TEST_STRING; }}); |
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 PrivilegedAction() { |
492 |
< |
public Object run() { |
463 |
< |
return TEST_STRING; |
464 |
< |
}})); |
465 |
< |
|
489 |
> |
public void realRun() throws Exception { |
490 |
> |
ExecutorService e = new ForkJoinPool(1); |
491 |
> |
try { |
492 |
> |
Future future = e.submit(callable); |
493 |
|
assertSame(TEST_STRING, future.get()); |
494 |
< |
}}; |
494 |
> |
} finally { |
495 |
> |
joinPool(e); |
496 |
> |
} |
497 |
> |
}}; |
498 |
|
|
499 |
< |
runWithPermissions(r, |
470 |
< |
new RuntimePermission("modifyThread")); |
499 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
|
* A submitted privileged exception action runs to completion |
504 |
|
*/ |
505 |
|
public void testSubmitPrivilegedExceptionAction() throws Exception { |
506 |
+ |
final Callable callable = |
507 |
+ |
Executors.callable(new PrivilegedExceptionAction() { |
508 |
+ |
public Object run() { return TEST_STRING; }}); |
509 |
|
Runnable r = new CheckedRunnable() { |
510 |
< |
public void realRun() throws Exception { |
511 |
< |
ExecutorService e = new ForkJoinPool(1); |
512 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
513 |
< |
public Object run() { |
482 |
< |
return TEST_STRING; |
483 |
< |
}})); |
484 |
< |
|
510 |
> |
public void realRun() throws Exception { |
511 |
> |
ExecutorService e = new ForkJoinPool(1); |
512 |
> |
try { |
513 |
> |
Future future = e.submit(callable); |
514 |
|
assertSame(TEST_STRING, future.get()); |
515 |
< |
}}; |
515 |
> |
} finally { |
516 |
> |
joinPool(e); |
517 |
> |
} |
518 |
> |
}}; |
519 |
|
|
520 |
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
521 |
|
} |
524 |
|
* A submitted failed privileged exception action reports exception |
525 |
|
*/ |
526 |
|
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
527 |
+ |
final Callable callable = |
528 |
+ |
Executors.callable(new PrivilegedExceptionAction() { |
529 |
+ |
public Object run() { throw new IndexOutOfBoundsException(); }}); |
530 |
|
Runnable r = new CheckedRunnable() { |
531 |
< |
public void realRun() throws Exception { |
532 |
< |
ExecutorService e = new ForkJoinPool(1); |
533 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
534 |
< |
public Object run() throws Exception { |
500 |
< |
throw new IndexOutOfBoundsException(); |
501 |
< |
}})); |
502 |
< |
|
531 |
> |
public void realRun() throws Exception { |
532 |
> |
ExecutorService e = new ForkJoinPool(1); |
533 |
> |
try { |
534 |
> |
Future future = e.submit(callable); |
535 |
|
try { |
536 |
|
future.get(); |
537 |
|
shouldThrow(); |
538 |
|
} catch (ExecutionException success) { |
539 |
|
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
540 |
< |
}}}; |
540 |
> |
} |
541 |
> |
} finally { |
542 |
> |
joinPool(e); |
543 |
> |
} |
544 |
> |
}}; |
545 |
|
|
546 |
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
547 |
|
} |
611 |
|
ForkJoinPool p = new ForkJoinPool(1); |
612 |
|
try { |
613 |
|
p.submit(new Callable() { |
614 |
< |
public Object call() { |
615 |
< |
int i = 5/0; |
580 |
< |
return Boolean.TRUE; |
581 |
< |
}}).get(); |
614 |
> |
public Object call() { throw new ArithmeticException(); }}) |
615 |
> |
.get(); |
616 |
|
shouldThrow(); |
617 |
|
} catch (ExecutionException success) { |
618 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |