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()); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
< |
* After invoking a single task, isQuiescent is true, |
255 |
< |
* queues are empty, threads are not active, and |
256 |
< |
* construction parameters continue to hold |
254 |
> |
* After invoking a single task, isQuiescent eventually becomes |
255 |
> |
* true, at which time queues are empty, threads are not active, |
256 |
> |
* the task has completed successfully, and construction |
257 |
> |
* parameters continue to hold |
258 |
|
*/ |
259 |
< |
public void testisQuiescent() throws InterruptedException { |
259 |
> |
public void testisQuiescent() throws Exception { |
260 |
|
ForkJoinPool p = new ForkJoinPool(2); |
261 |
|
try { |
262 |
|
assertTrue(p.isQuiescent()); |
263 |
< |
p.invoke(new FibTask(20)); |
263 |
> |
long startTime = System.nanoTime(); |
264 |
> |
FibTask f = new FibTask(20); |
265 |
> |
p.invoke(f); |
266 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
267 |
|
p.getFactory()); |
268 |
< |
Thread.sleep(SMALL_DELAY_MS); |
268 |
> |
while (! p.isQuiescent()) { |
269 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
270 |
> |
throw new AssertionFailedError("timed out"); |
271 |
> |
assertFalse(p.getAsyncMode()); |
272 |
> |
assertFalse(p.isShutdown()); |
273 |
> |
assertFalse(p.isTerminating()); |
274 |
> |
assertFalse(p.isTerminated()); |
275 |
> |
Thread.yield(); |
276 |
> |
} |
277 |
> |
|
278 |
|
assertTrue(p.isQuiescent()); |
279 |
|
assertFalse(p.getAsyncMode()); |
280 |
|
assertEquals(0, p.getActiveThreadCount()); |
284 |
|
assertFalse(p.isShutdown()); |
285 |
|
assertFalse(p.isTerminating()); |
286 |
|
assertFalse(p.isTerminated()); |
287 |
+ |
assertTrue(f.isDone()); |
288 |
+ |
assertEquals(6765, (int) f.get()); |
289 |
|
} finally { |
290 |
|
joinPool(p); |
291 |
|
} |
329 |
|
try { |
330 |
|
ReentrantLock lock = new ReentrantLock(); |
331 |
|
ManagedLocker locker = new ManagedLocker(lock); |
332 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
332 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
333 |
|
p.execute(f); |
334 |
< |
assertEquals(832040, (int) f.get()); |
334 |
> |
assertEquals(6765, (int) f.get()); |
335 |
|
} finally { |
336 |
|
p.shutdownNow(); // don't wait out shutdown |
337 |
|
} |
341 |
|
* pollSubmission returns unexecuted submitted task, if present |
342 |
|
*/ |
343 |
|
public void testPollSubmission() { |
344 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
345 |
|
SubFJP p = new SubFJP(); |
346 |
|
try { |
347 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
348 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
349 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
347 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
348 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
349 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
350 |
|
ForkJoinTask r = p.pollSubmission(); |
351 |
|
assertTrue(r == a || r == b || r == c); |
352 |
|
assertFalse(r.isDone()); |
353 |
|
} finally { |
354 |
+ |
done.countDown(); |
355 |
|
joinPool(p); |
356 |
|
} |
357 |
|
} |
360 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
361 |
|
*/ |
362 |
|
public void testDrainTasksTo() { |
363 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
364 |
|
SubFJP p = new SubFJP(); |
365 |
|
try { |
366 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
367 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
368 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
366 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
367 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
368 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
369 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
370 |
|
p.drainTasksTo(al); |
371 |
|
assertTrue(al.size() > 0); |
374 |
|
assertFalse(r.isDone()); |
375 |
|
} |
376 |
|
} finally { |
377 |
+ |
done.countDown(); |
378 |
|
joinPool(p); |
379 |
|
} |
380 |
|
} |
388 |
|
public void testExecuteRunnable() throws Throwable { |
389 |
|
ExecutorService e = new ForkJoinPool(1); |
390 |
|
try { |
391 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
392 |
< |
assertFalse(task.done); |
391 |
> |
TrackedRunnable task = trackedRunnable(SHORT_DELAY_MS); |
392 |
> |
assertFalse(task.isDone()); |
393 |
|
Future<?> future = e.submit(task); |
394 |
< |
future.get(); |
395 |
< |
assertTrue(task.done); |
394 |
> |
assertNull(future.get()); |
395 |
> |
assertNull(future.get(MEDIUM_DELAY_MS, MILLISECONDS)); |
396 |
> |
assertTrue(task.isDone()); |
397 |
> |
assertTrue(future.isDone()); |
398 |
> |
assertFalse(future.isCancelled()); |
399 |
|
} finally { |
400 |
|
joinPool(e); |
401 |
|
} |
409 |
|
ExecutorService e = new ForkJoinPool(1); |
410 |
|
try { |
411 |
|
Future<String> future = e.submit(new StringTask()); |
412 |
< |
String result = future.get(); |
413 |
< |
assertSame(TEST_STRING, result); |
412 |
> |
assertSame(TEST_STRING, future.get()); |
413 |
> |
assertTrue(future.isDone()); |
414 |
> |
assertFalse(future.isCancelled()); |
415 |
|
} finally { |
416 |
|
joinPool(e); |
417 |
|
} |
424 |
|
ExecutorService e = new ForkJoinPool(1); |
425 |
|
try { |
426 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
427 |
< |
future.get(); |
427 |
> |
assertNull(future.get()); |
428 |
|
assertTrue(future.isDone()); |
429 |
+ |
assertFalse(future.isCancelled()); |
430 |
|
} finally { |
431 |
|
joinPool(e); |
432 |
|
} |
439 |
|
ExecutorService e = new ForkJoinPool(1); |
440 |
|
try { |
441 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
442 |
< |
String result = future.get(); |
443 |
< |
assertSame(TEST_STRING, result); |
442 |
> |
assertSame(TEST_STRING, future.get()); |
443 |
> |
assertTrue(future.isDone()); |
444 |
> |
assertFalse(future.isCancelled()); |
445 |
|
} finally { |
446 |
|
joinPool(e); |
447 |
|
} |
448 |
|
} |
449 |
|
|
427 |
– |
|
450 |
|
/** |
451 |
< |
* A submitted privileged action to completion |
451 |
> |
* A submitted privileged action runs to completion |
452 |
|
*/ |
453 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
454 |
< |
Policy savedPolicy = null; |
455 |
< |
try { |
456 |
< |
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 { |
453 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
454 |
> |
Runnable r = new CheckedRunnable() { |
455 |
> |
public void realRun() throws Exception { |
456 |
> |
ExecutorService e = new ForkJoinPool(1); |
457 |
|
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
458 |
|
public Object run() { |
459 |
|
return TEST_STRING; |
460 |
|
}})); |
461 |
|
|
462 |
< |
Object result = future.get(); |
463 |
< |
assertSame(TEST_STRING, result); |
464 |
< |
} finally { |
465 |
< |
joinPool(e); |
466 |
< |
} |
456 |
< |
} finally { |
457 |
< |
Policy.setPolicy(savedPolicy); |
458 |
< |
} |
462 |
> |
assertSame(TEST_STRING, future.get()); |
463 |
> |
}}; |
464 |
> |
|
465 |
> |
runWithPermissions(r, |
466 |
> |
new RuntimePermission("modifyThread")); |
467 |
|
} |
468 |
|
|
469 |
|
/** |
470 |
< |
* A submitted a privileged exception action runs to completion |
470 |
> |
* A submitted privileged exception action runs to completion |
471 |
|
*/ |
472 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
473 |
< |
Policy savedPolicy = null; |
474 |
< |
try { |
475 |
< |
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 { |
472 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
473 |
> |
Runnable r = new CheckedRunnable() { |
474 |
> |
public void realRun() throws Exception { |
475 |
> |
ExecutorService e = new ForkJoinPool(1); |
476 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
477 |
|
public Object run() { |
478 |
|
return TEST_STRING; |
479 |
|
}})); |
480 |
|
|
481 |
< |
Object result = future.get(); |
482 |
< |
assertSame(TEST_STRING, result); |
483 |
< |
} finally { |
484 |
< |
joinPool(e); |
488 |
< |
} |
489 |
< |
} finally { |
490 |
< |
Policy.setPolicy(savedPolicy); |
491 |
< |
} |
481 |
> |
assertSame(TEST_STRING, future.get()); |
482 |
> |
}}; |
483 |
> |
|
484 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
|
* A submitted failed privileged exception action reports exception |
489 |
|
*/ |
490 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
491 |
< |
Policy savedPolicy = null; |
492 |
< |
try { |
493 |
< |
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 { |
490 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
491 |
> |
Runnable r = new CheckedRunnable() { |
492 |
> |
public void realRun() throws Exception { |
493 |
> |
ExecutorService e = new ForkJoinPool(1); |
494 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
495 |
|
public Object run() throws Exception { |
496 |
|
throw new IndexOutOfBoundsException(); |
497 |
|
}})); |
498 |
|
|
499 |
< |
Object result = future.get(); |
500 |
< |
shouldThrow(); |
501 |
< |
} catch (ExecutionException success) { |
502 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
503 |
< |
} finally { |
504 |
< |
joinPool(e); |
505 |
< |
} |
506 |
< |
} finally { |
525 |
< |
Policy.setPolicy(savedPolicy); |
526 |
< |
} |
499 |
> |
try { |
500 |
> |
future.get(); |
501 |
> |
shouldThrow(); |
502 |
> |
} catch (ExecutionException success) { |
503 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
504 |
> |
}}}; |
505 |
> |
|
506 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
507 |
|
} |
508 |
|
|
509 |
|
/** |