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