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.*; |
164 |
|
try { |
165 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
166 |
|
p.getFactory()); |
167 |
– |
assertTrue(p.isQuiescent()); |
167 |
|
assertFalse(p.getAsyncMode()); |
168 |
|
assertEquals(0, p.getActiveThreadCount()); |
169 |
|
assertEquals(0, p.getStealCount()); |
327 |
|
* pollSubmission returns unexecuted submitted task, if present |
328 |
|
*/ |
329 |
|
public void testPollSubmission() { |
330 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
331 |
|
SubFJP p = new SubFJP(); |
332 |
|
try { |
333 |
< |
ForkJoinTask a = p.submit(new ShortRunnable()); |
334 |
< |
ForkJoinTask b = p.submit(new ShortRunnable()); |
335 |
< |
ForkJoinTask c = p.submit(new ShortRunnable()); |
333 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
334 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
335 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
336 |
|
ForkJoinTask r = p.pollSubmission(); |
337 |
|
assertTrue(r == a || r == b || r == c); |
338 |
|
assertFalse(r.isDone()); |
339 |
|
} finally { |
340 |
+ |
done.countDown(); |
341 |
|
joinPool(p); |
342 |
|
} |
343 |
|
} |
346 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
347 |
|
*/ |
348 |
|
public void testDrainTasksTo() { |
349 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
350 |
|
SubFJP p = new SubFJP(); |
351 |
|
try { |
352 |
< |
ForkJoinTask a = p.submit(new ShortRunnable()); |
353 |
< |
ForkJoinTask b = p.submit(new ShortRunnable()); |
354 |
< |
ForkJoinTask c = p.submit(new ShortRunnable()); |
352 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
353 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
354 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
355 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
356 |
|
p.drainTasksTo(al); |
357 |
|
assertTrue(al.size() > 0); |
360 |
|
assertFalse(r.isDone()); |
361 |
|
} |
362 |
|
} finally { |
363 |
+ |
done.countDown(); |
364 |
|
joinPool(p); |
365 |
|
} |
366 |
|
} |
378 |
|
assertFalse(task.isDone()); |
379 |
|
Future<?> future = e.submit(task); |
380 |
|
assertNull(future.get()); |
381 |
+ |
assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
382 |
|
assertTrue(task.isDone()); |
383 |
+ |
assertTrue(future.isDone()); |
384 |
|
assertFalse(future.isCancelled()); |
385 |
|
} finally { |
386 |
|
joinPool(e); |
433 |
|
} |
434 |
|
} |
435 |
|
|
431 |
– |
|
436 |
|
/** |
437 |
|
* A submitted privileged action runs to completion |
438 |
|
*/ |
439 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
440 |
< |
Policy savedPolicy = null; |
441 |
< |
try { |
442 |
< |
savedPolicy = Policy.getPolicy(); |
439 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
440 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
441 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
442 |
< |
Policy.setPolicy(policy); |
443 |
< |
} catch (AccessControlException ok) { |
444 |
< |
return; |
445 |
< |
} |
446 |
< |
|
447 |
< |
try { |
448 |
< |
ExecutorService e = new ForkJoinPool(1); |
449 |
< |
try { |
439 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
440 |
> |
Runnable r = new CheckedRunnable() { |
441 |
> |
public void realRun() throws Exception { |
442 |
> |
ExecutorService e = new ForkJoinPool(1); |
443 |
|
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
444 |
|
public Object run() { |
445 |
|
return TEST_STRING; |
446 |
|
}})); |
447 |
|
|
448 |
< |
Object result = future.get(); |
449 |
< |
assertSame(TEST_STRING, result); |
450 |
< |
} finally { |
451 |
< |
joinPool(e); |
452 |
< |
} |
460 |
< |
} finally { |
461 |
< |
Policy.setPolicy(savedPolicy); |
462 |
< |
} |
448 |
> |
assertSame(TEST_STRING, future.get()); |
449 |
> |
}}; |
450 |
> |
|
451 |
> |
runWithPermissions(r, |
452 |
> |
new RuntimePermission("modifyThread")); |
453 |
|
} |
454 |
|
|
455 |
|
/** |
456 |
|
* A submitted privileged exception action runs to completion |
457 |
|
*/ |
458 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
459 |
< |
Policy savedPolicy = null; |
460 |
< |
try { |
461 |
< |
savedPolicy = Policy.getPolicy(); |
472 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
473 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
474 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
475 |
< |
Policy.setPolicy(policy); |
476 |
< |
} catch (AccessControlException ok) { |
477 |
< |
return; |
478 |
< |
} |
479 |
< |
|
480 |
< |
try { |
481 |
< |
ExecutorService e = new ForkJoinPool(1); |
482 |
< |
try { |
458 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
459 |
> |
Runnable r = new CheckedRunnable() { |
460 |
> |
public void realRun() throws Exception { |
461 |
> |
ExecutorService e = new ForkJoinPool(1); |
462 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
463 |
|
public Object run() { |
464 |
|
return TEST_STRING; |
465 |
|
}})); |
466 |
|
|
467 |
< |
Object result = future.get(); |
468 |
< |
assertSame(TEST_STRING, result); |
469 |
< |
} finally { |
470 |
< |
joinPool(e); |
492 |
< |
} |
493 |
< |
} finally { |
494 |
< |
Policy.setPolicy(savedPolicy); |
495 |
< |
} |
467 |
> |
assertSame(TEST_STRING, future.get()); |
468 |
> |
}}; |
469 |
> |
|
470 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
471 |
|
} |
472 |
|
|
473 |
|
/** |
474 |
|
* A submitted failed privileged exception action reports exception |
475 |
|
*/ |
476 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
477 |
< |
Policy savedPolicy = null; |
478 |
< |
try { |
479 |
< |
savedPolicy = Policy.getPolicy(); |
505 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
506 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
507 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
508 |
< |
Policy.setPolicy(policy); |
509 |
< |
} catch (AccessControlException ok) { |
510 |
< |
return; |
511 |
< |
} |
512 |
< |
|
513 |
< |
try { |
514 |
< |
ExecutorService e = new ForkJoinPool(1); |
515 |
< |
try { |
476 |
> |
public void testSubmitFailedPrivilegedExceptionAction() 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() throws Exception { |
482 |
|
throw new IndexOutOfBoundsException(); |
483 |
|
}})); |
484 |
|
|
485 |
< |
Object result = future.get(); |
486 |
< |
shouldThrow(); |
487 |
< |
} catch (ExecutionException success) { |
488 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
489 |
< |
} finally { |
490 |
< |
joinPool(e); |
491 |
< |
} |
492 |
< |
} finally { |
529 |
< |
Policy.setPolicy(savedPolicy); |
530 |
< |
} |
485 |
> |
try { |
486 |
> |
future.get(); |
487 |
> |
shouldThrow(); |
488 |
> |
} catch (ExecutionException success) { |
489 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
490 |
> |
}}}; |
491 |
> |
|
492 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
493 |
|
} |
494 |
|
|
495 |
|
/** |