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()); |
232 |
|
* performs its defined action |
233 |
|
*/ |
234 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
235 |
< |
final CountDownLatch uncaughtExceptionHappened = new CountDownLatch(1); |
235 |
> |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
236 |
|
final Thread.UncaughtExceptionHandler eh = |
237 |
|
new Thread.UncaughtExceptionHandler() { |
238 |
|
public void uncaughtException(Thread t, Throwable e) { |
239 |
< |
uncaughtExceptionHappened.countDown(); |
239 |
> |
uehInvoked.countDown(); |
240 |
|
}}; |
241 |
|
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
242 |
|
eh, false); |
243 |
|
try { |
244 |
|
assertSame(eh, p.getUncaughtExceptionHandler()); |
245 |
< |
p.execute(new FailingTask()); |
246 |
< |
uncaughtExceptionHappened.await(); |
245 |
> |
p.execute(new FibTask(8)); |
246 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
247 |
|
} finally { |
248 |
< |
//p.shutdownNow(); |
248 |
> |
p.shutdownNow(); // failure might have prevented processing task |
249 |
|
joinPool(p); |
250 |
|
} |
251 |
|
} |
258 |
|
public void testisQuiescent() throws InterruptedException { |
259 |
|
ForkJoinPool p = new ForkJoinPool(2); |
260 |
|
try { |
261 |
+ |
assertTrue(p.isQuiescent()); |
262 |
|
p.invoke(new FibTask(20)); |
263 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
264 |
|
p.getFactory()); |
265 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
265 |
> |
delay(SMALL_DELAY_MS); |
266 |
|
assertTrue(p.isQuiescent()); |
267 |
|
assertFalse(p.getAsyncMode()); |
268 |
|
assertEquals(0, p.getActiveThreadCount()); |
315 |
|
try { |
316 |
|
ReentrantLock lock = new ReentrantLock(); |
317 |
|
ManagedLocker locker = new ManagedLocker(lock); |
318 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
318 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
319 |
|
p.execute(f); |
320 |
< |
assertEquals(832040, (int) f.get()); |
320 |
> |
assertEquals(6765, (int) f.get()); |
321 |
|
} finally { |
322 |
|
p.shutdownNow(); // don't wait out shutdown |
323 |
|
} |
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 MediumRunnable()); |
334 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
335 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
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 MediumRunnable()); |
353 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
354 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
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 |
|
} |
374 |
|
public void testExecuteRunnable() throws Throwable { |
375 |
|
ExecutorService e = new ForkJoinPool(1); |
376 |
|
try { |
377 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
378 |
< |
assertFalse(task.done); |
377 |
> |
TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); |
378 |
> |
assertFalse(task.isDone()); |
379 |
|
Future<?> future = e.submit(task); |
380 |
< |
future.get(); |
381 |
< |
assertTrue(task.done); |
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); |
387 |
|
} |
395 |
|
ExecutorService e = new ForkJoinPool(1); |
396 |
|
try { |
397 |
|
Future<String> future = e.submit(new StringTask()); |
398 |
< |
String result = future.get(); |
399 |
< |
assertSame(TEST_STRING, result); |
398 |
> |
assertSame(TEST_STRING, future.get()); |
399 |
> |
assertTrue(future.isDone()); |
400 |
> |
assertFalse(future.isCancelled()); |
401 |
|
} finally { |
402 |
|
joinPool(e); |
403 |
|
} |
410 |
|
ExecutorService e = new ForkJoinPool(1); |
411 |
|
try { |
412 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
413 |
< |
future.get(); |
413 |
> |
assertNull(future.get()); |
414 |
|
assertTrue(future.isDone()); |
415 |
+ |
assertFalse(future.isCancelled()); |
416 |
|
} finally { |
417 |
|
joinPool(e); |
418 |
|
} |
425 |
|
ExecutorService e = new ForkJoinPool(1); |
426 |
|
try { |
427 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
428 |
< |
String result = future.get(); |
429 |
< |
assertSame(TEST_STRING, result); |
428 |
> |
assertSame(TEST_STRING, future.get()); |
429 |
> |
assertTrue(future.isDone()); |
430 |
> |
assertFalse(future.isCancelled()); |
431 |
|
} finally { |
432 |
|
joinPool(e); |
433 |
|
} |
434 |
|
} |
435 |
|
|
426 |
– |
|
436 |
|
/** |
437 |
< |
* A submitted privileged action to completion |
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(); |
434 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
435 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
436 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
437 |
< |
Policy.setPolicy(policy); |
438 |
< |
} catch (AccessControlException ok) { |
439 |
< |
return; |
440 |
< |
} |
441 |
< |
|
442 |
< |
try { |
443 |
< |
ExecutorService e = new ForkJoinPool(1); |
444 |
< |
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 |
< |
} |
455 |
< |
} finally { |
456 |
< |
Policy.setPolicy(savedPolicy); |
457 |
< |
} |
448 |
> |
assertSame(TEST_STRING, future.get()); |
449 |
> |
}}; |
450 |
> |
|
451 |
> |
runWithPermissions(r, |
452 |
> |
new RuntimePermission("modifyThread")); |
453 |
|
} |
454 |
|
|
455 |
|
/** |
456 |
< |
* A submitted a privileged exception action runs to completion |
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(); |
467 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
468 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
469 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
470 |
< |
Policy.setPolicy(policy); |
471 |
< |
} catch (AccessControlException ok) { |
472 |
< |
return; |
473 |
< |
} |
474 |
< |
|
475 |
< |
try { |
476 |
< |
ExecutorService e = new ForkJoinPool(1); |
477 |
< |
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); |
487 |
< |
} |
488 |
< |
} finally { |
489 |
< |
Policy.setPolicy(savedPolicy); |
490 |
< |
} |
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(); |
500 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
501 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
502 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
503 |
< |
Policy.setPolicy(policy); |
504 |
< |
} catch (AccessControlException ok) { |
505 |
< |
return; |
506 |
< |
} |
507 |
< |
|
508 |
< |
try { |
509 |
< |
ExecutorService e = new ForkJoinPool(1); |
510 |
< |
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 { |
524 |
< |
Policy.setPolicy(savedPolicy); |
525 |
< |
} |
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 |
|
/** |
497 |
|
*/ |
498 |
|
public void testExecuteNullRunnable() { |
499 |
|
ExecutorService e = new ForkJoinPool(1); |
533 |
– |
TrackedShortRunnable task = null; |
500 |
|
try { |
501 |
< |
Future<?> future = e.submit(task); |
501 |
> |
Future<?> future = e.submit((Runnable) null); |
502 |
|
shouldThrow(); |
503 |
|
} catch (NullPointerException success) { |
504 |
|
} finally { |
512 |
|
*/ |
513 |
|
public void testSubmitNullCallable() { |
514 |
|
ExecutorService e = new ForkJoinPool(1); |
549 |
– |
StringTask t = null; |
515 |
|
try { |
516 |
< |
Future<String> future = e.submit(t); |
516 |
> |
Future<String> future = e.submit((Callable) null); |
517 |
|
shouldThrow(); |
518 |
|
} catch (NullPointerException success) { |
519 |
|
} finally { |