4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
|
|
7 |
– |
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
10 |
– |
import java.util.concurrent.Executor; |
9 |
|
import java.util.concurrent.Executors; |
10 |
|
import java.util.concurrent.ExecutorService; |
11 |
|
import java.util.concurrent.AbstractExecutorService; |
18 |
|
import java.util.concurrent.ForkJoinPool; |
19 |
|
import java.util.concurrent.ForkJoinTask; |
20 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
– |
import java.util.concurrent.RecursiveAction; |
21 |
|
import java.util.concurrent.RecursiveTask; |
22 |
|
import java.util.concurrent.TimeUnit; |
23 |
< |
import java.util.concurrent.locks.*; |
24 |
< |
import java.security.*; |
23 |
> |
import java.util.concurrent.locks.ReentrantLock; |
24 |
> |
import java.security.AccessControlException; |
25 |
> |
import java.security.Policy; |
26 |
> |
import java.security.PrivilegedAction; |
27 |
> |
import java.security.PrivilegedExceptionAction; |
28 |
|
|
29 |
|
public class ForkJoinPoolTest extends JSR166TestCase { |
30 |
|
public static void main(String[] args) { |
31 |
< |
junit.textui.TestRunner.run (suite()); |
31 |
> |
junit.textui.TestRunner.run(suite()); |
32 |
|
} |
33 |
+ |
|
34 |
|
public static Test suite() { |
35 |
|
return new TestSuite(ForkJoinPoolTest.class); |
36 |
|
} |
157 |
|
* tasks, and quiescent running state. |
158 |
|
*/ |
159 |
|
public void testDefaultInitialState() { |
160 |
< |
ForkJoinPool p = null; |
160 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
161 |
|
try { |
161 |
– |
p = new ForkJoinPool(1); |
162 |
|
assertTrue(p.getFactory() == |
163 |
|
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
164 |
|
assertTrue(p.isQuiescent()); |
201 |
|
* getParallelism returns size set in constructor |
202 |
|
*/ |
203 |
|
public void testGetParallelism() { |
204 |
< |
ForkJoinPool p = null; |
204 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
205 |
|
try { |
206 |
– |
p = new ForkJoinPool(1); |
206 |
|
assertTrue(p.getParallelism() == 1); |
207 |
|
} finally { |
208 |
|
joinPool(p); |
213 |
|
* getPoolSize returns number of started workers. |
214 |
|
*/ |
215 |
|
public void testGetPoolSize() { |
216 |
< |
ForkJoinPool p = null; |
216 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
217 |
|
try { |
219 |
– |
p = new ForkJoinPool(1); |
218 |
|
assertTrue(p.getActiveThreadCount() == 0); |
219 |
|
Future<String> future = p.submit(new StringTask()); |
220 |
|
assertTrue(p.getPoolSize() == 1); |
223 |
– |
|
221 |
|
} finally { |
222 |
|
joinPool(p); |
223 |
|
} |
230 |
|
* performs its defined action |
231 |
|
*/ |
232 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
233 |
< |
ForkJoinPool p = null; |
233 |
> |
MyHandler eh = new MyHandler(); |
234 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
235 |
|
try { |
238 |
– |
MyHandler eh = new MyHandler(); |
239 |
– |
p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
236 |
|
assert(eh == p.getUncaughtExceptionHandler()); |
237 |
|
p.execute(new FailingTask()); |
238 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
249 |
|
* construction parameters continue to hold |
250 |
|
*/ |
251 |
|
public void testisQuiescent() throws InterruptedException { |
252 |
< |
ForkJoinPool p = null; |
252 |
> |
ForkJoinPool p = new ForkJoinPool(2); |
253 |
|
try { |
258 |
– |
p = new ForkJoinPool(2); |
254 |
|
p.invoke(new FibTask(20)); |
255 |
|
assertTrue(p.getFactory() == |
256 |
|
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
273 |
|
* Completed submit(ForkJoinTask) returns result |
274 |
|
*/ |
275 |
|
public void testSubmitForkJoinTask() throws Throwable { |
276 |
< |
ForkJoinPool p = null; |
276 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
277 |
|
try { |
283 |
– |
p = new ForkJoinPool(1); |
278 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
279 |
|
int r = f.get(); |
280 |
|
assertTrue(r == 21); |
287 |
|
* A task submitted after shutdown is rejected |
288 |
|
*/ |
289 |
|
public void testSubmitAfterShutdown() { |
290 |
< |
ForkJoinPool p = null; |
290 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
291 |
|
try { |
298 |
– |
p = new ForkJoinPool(1); |
292 |
|
p.shutdown(); |
293 |
|
assertTrue(p.isShutdown()); |
294 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
303 |
|
* Pool maintains parallelism when using ManagedBlocker |
304 |
|
*/ |
305 |
|
public void testBlockingForkJoinTask() throws Throwable { |
306 |
< |
ForkJoinPool p = null; |
306 |
> |
ForkJoinPool p = new ForkJoinPool(4); |
307 |
|
try { |
315 |
– |
p = new ForkJoinPool(4); |
308 |
|
ReentrantLock lock = new ReentrantLock(); |
309 |
|
ManagedLocker locker = new ManagedLocker(lock); |
310 |
|
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
320 |
|
* pollSubmission returns unexecuted submitted task, if present |
321 |
|
*/ |
322 |
|
public void testPollSubmission() { |
323 |
< |
SubFJP p = null; |
323 |
> |
SubFJP p = new SubFJP(); |
324 |
|
try { |
333 |
– |
p = new SubFJP(); |
325 |
|
ForkJoinTask a = p.submit(new MediumRunnable()); |
326 |
|
ForkJoinTask b = p.submit(new MediumRunnable()); |
327 |
|
ForkJoinTask c = p.submit(new MediumRunnable()); |
337 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
338 |
|
*/ |
339 |
|
public void testDrainTasksTo() { |
340 |
< |
SubFJP p = null; |
340 |
> |
SubFJP p = new SubFJP(); |
341 |
|
try { |
351 |
– |
p = new SubFJP(); |
342 |
|
ForkJoinTask a = p.submit(new MediumRunnable()); |
343 |
|
ForkJoinTask b = p.submit(new MediumRunnable()); |
344 |
|
ForkJoinTask c = p.submit(new MediumRunnable()); |
362 |
|
*/ |
363 |
|
public void testExecuteRunnable() throws Throwable { |
364 |
|
ExecutorService e = new ForkJoinPool(1); |
365 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
366 |
< |
assertFalse(task.done); |
367 |
< |
Future<?> future = e.submit(task); |
368 |
< |
future.get(); |
369 |
< |
assertTrue(task.done); |
365 |
> |
try { |
366 |
> |
TrackedShortRunnable task = new TrackedShortRunnable(); |
367 |
> |
assertFalse(task.done); |
368 |
> |
Future<?> future = e.submit(task); |
369 |
> |
future.get(); |
370 |
> |
assertTrue(task.done); |
371 |
> |
} finally { |
372 |
> |
joinPool(e); |
373 |
> |
} |
374 |
|
} |
375 |
|
|
376 |
|
|
379 |
|
*/ |
380 |
|
public void testSubmitCallable() throws Throwable { |
381 |
|
ExecutorService e = new ForkJoinPool(1); |
382 |
< |
Future<String> future = e.submit(new StringTask()); |
383 |
< |
String result = future.get(); |
384 |
< |
assertSame(TEST_STRING, result); |
382 |
> |
try { |
383 |
> |
Future<String> future = e.submit(new StringTask()); |
384 |
> |
String result = future.get(); |
385 |
> |
assertSame(TEST_STRING, result); |
386 |
> |
} finally { |
387 |
> |
joinPool(e); |
388 |
> |
} |
389 |
|
} |
390 |
|
|
391 |
|
/** |
393 |
|
*/ |
394 |
|
public void testSubmitRunnable() throws Throwable { |
395 |
|
ExecutorService e = new ForkJoinPool(1); |
396 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
397 |
< |
future.get(); |
398 |
< |
assertTrue(future.isDone()); |
396 |
> |
try { |
397 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
398 |
> |
future.get(); |
399 |
> |
assertTrue(future.isDone()); |
400 |
> |
} finally { |
401 |
> |
joinPool(e); |
402 |
> |
} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
407 |
|
*/ |
408 |
|
public void testSubmitRunnable2() throws Throwable { |
409 |
|
ExecutorService e = new ForkJoinPool(1); |
410 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
411 |
< |
String result = future.get(); |
412 |
< |
assertSame(TEST_STRING, result); |
410 |
> |
try { |
411 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
412 |
> |
String result = future.get(); |
413 |
> |
assertSame(TEST_STRING, result); |
414 |
> |
} finally { |
415 |
> |
joinPool(e); |
416 |
> |
} |
417 |
|
} |
418 |
|
|
419 |
|
|
431 |
|
} catch (AccessControlException ok) { |
432 |
|
return; |
433 |
|
} |
434 |
+ |
|
435 |
|
try { |
436 |
|
ExecutorService e = new ForkJoinPool(1); |
437 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
437 |
> |
try { |
438 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
439 |
|
public Object run() { |
440 |
|
return TEST_STRING; |
441 |
|
}})); |
442 |
|
|
443 |
< |
Object result = future.get(); |
444 |
< |
assertSame(TEST_STRING, result); |
445 |
< |
} |
446 |
< |
finally { |
443 |
> |
Object result = future.get(); |
444 |
> |
assertSame(TEST_STRING, result); |
445 |
> |
} finally { |
446 |
> |
joinPool(e); |
447 |
> |
} |
448 |
> |
} finally { |
449 |
|
Policy.setPolicy(savedPolicy); |
450 |
|
} |
451 |
|
} |
467 |
|
|
468 |
|
try { |
469 |
|
ExecutorService e = new ForkJoinPool(1); |
470 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
470 |
> |
try { |
471 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
472 |
|
public Object run() { |
473 |
|
return TEST_STRING; |
474 |
|
}})); |
475 |
|
|
476 |
< |
Object result = future.get(); |
477 |
< |
assertSame(TEST_STRING, result); |
478 |
< |
} |
479 |
< |
finally { |
476 |
> |
Object result = future.get(); |
477 |
> |
assertSame(TEST_STRING, result); |
478 |
> |
} finally { |
479 |
> |
joinPool(e); |
480 |
> |
} |
481 |
> |
} finally { |
482 |
|
Policy.setPolicy(savedPolicy); |
483 |
|
} |
484 |
|
} |
498 |
|
return; |
499 |
|
} |
500 |
|
|
488 |
– |
|
501 |
|
try { |
502 |
|
ExecutorService e = new ForkJoinPool(1); |
503 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
503 |
> |
try { |
504 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
505 |
|
public Object run() throws Exception { |
506 |
|
throw new IndexOutOfBoundsException(); |
507 |
|
}})); |
508 |
|
|
509 |
< |
Object result = future.get(); |
510 |
< |
shouldThrow(); |
511 |
< |
} catch (ExecutionException success) { |
512 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
509 |
> |
Object result = future.get(); |
510 |
> |
shouldThrow(); |
511 |
> |
} catch (ExecutionException success) { |
512 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
513 |
> |
} finally { |
514 |
> |
joinPool(e); |
515 |
> |
} |
516 |
|
} finally { |
517 |
|
Policy.setPolicy(savedPolicy); |
518 |
|
} |
522 |
|
* execute(null runnable) throws NullPointerException |
523 |
|
*/ |
524 |
|
public void testExecuteNullRunnable() { |
525 |
+ |
ExecutorService e = new ForkJoinPool(1); |
526 |
|
try { |
510 |
– |
ExecutorService e = new ForkJoinPool(1); |
527 |
|
TrackedShortRunnable task = null; |
528 |
|
Future<?> future = e.submit(task); |
529 |
|
shouldThrow(); |
530 |
< |
} catch (NullPointerException success) {} |
530 |
> |
} catch (NullPointerException success) { |
531 |
> |
} finally { |
532 |
> |
joinPool(e); |
533 |
> |
} |
534 |
|
} |
535 |
|
|
536 |
|
|
538 |
|
* submit(null callable) throws NullPointerException |
539 |
|
*/ |
540 |
|
public void testSubmitNullCallable() { |
541 |
+ |
ExecutorService e = new ForkJoinPool(1); |
542 |
|
try { |
523 |
– |
ExecutorService e = new ForkJoinPool(1); |
543 |
|
StringTask t = null; |
544 |
|
Future<String> future = e.submit(t); |
545 |
|
shouldThrow(); |
546 |
< |
} catch (NullPointerException success) {} |
546 |
> |
} catch (NullPointerException success) { |
547 |
> |
} finally { |
548 |
> |
joinPool(e); |
549 |
> |
} |
550 |
|
} |
551 |
|
|
552 |
|
|
592 |
|
shouldThrow(); |
593 |
|
} catch (ExecutionException success) { |
594 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
595 |
+ |
} finally { |
596 |
+ |
joinPool(p); |
597 |
|
} |
574 |
– |
|
575 |
– |
joinPool(p); |
598 |
|
} |
599 |
|
|
600 |
|
/** |