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.*; |
8 |
< |
import java.util.*; |
8 |
> |
import java.util.ArrayList; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.List; |
11 |
|
import java.util.concurrent.Executors; |
12 |
|
import java.util.concurrent.ExecutorService; |
13 |
|
import java.util.concurrent.AbstractExecutorService; |
22 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
23 |
|
import java.util.concurrent.RecursiveTask; |
24 |
|
import java.util.concurrent.TimeUnit; |
25 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.locks.ReentrantLock; |
27 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
28 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
29 |
|
import java.security.AccessControlException; |
30 |
|
import java.security.Policy; |
31 |
|
import java.security.PrivilegedAction; |
40 |
|
return new TestSuite(ForkJoinPoolTest.class); |
41 |
|
} |
42 |
|
|
43 |
< |
/** |
43 |
> |
/* |
44 |
|
* Testing coverage notes: |
45 |
|
* |
46 |
|
* 1. shutdown and related methods are tested via super.joinPool. |
108 |
|
static final class FibTask extends RecursiveTask<Integer> { |
109 |
|
final int number; |
110 |
|
FibTask(int n) { number = n; } |
111 |
< |
public Integer compute() { |
111 |
> |
protected Integer compute() { |
112 |
|
int n = number; |
113 |
|
if (n <= 1) |
114 |
|
return n; |
136 |
|
this.locker = locker; |
137 |
|
this.lock = lock; |
138 |
|
} |
139 |
< |
public Integer compute() { |
139 |
> |
protected Integer compute() { |
140 |
|
int n; |
141 |
|
LockingFibTask f1 = null; |
142 |
|
LockingFibTask f2 = null; |
164 |
|
public void testDefaultInitialState() { |
165 |
|
ForkJoinPool p = new ForkJoinPool(1); |
166 |
|
try { |
167 |
< |
assertTrue(p.getFactory() == |
168 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
164 |
< |
assertTrue(p.isQuiescent()); |
167 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
168 |
> |
p.getFactory()); |
169 |
|
assertFalse(p.getAsyncMode()); |
170 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
171 |
< |
assertTrue(p.getStealCount() == 0); |
172 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
173 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
170 |
> |
assertEquals(0, p.getActiveThreadCount()); |
171 |
> |
assertEquals(0, p.getStealCount()); |
172 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
173 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
174 |
|
assertFalse(p.hasQueuedSubmissions()); |
175 |
|
assertFalse(p.isShutdown()); |
176 |
|
assertFalse(p.isTerminating()); |
200 |
|
} catch (NullPointerException success) {} |
201 |
|
} |
202 |
|
|
199 |
– |
|
203 |
|
/** |
204 |
|
* getParallelism returns size set in constructor |
205 |
|
*/ |
206 |
|
public void testGetParallelism() { |
207 |
|
ForkJoinPool p = new ForkJoinPool(1); |
208 |
|
try { |
209 |
< |
assertTrue(p.getParallelism() == 1); |
209 |
> |
assertEquals(1, p.getParallelism()); |
210 |
|
} finally { |
211 |
|
joinPool(p); |
212 |
|
} |
218 |
|
public void testGetPoolSize() { |
219 |
|
ForkJoinPool p = new ForkJoinPool(1); |
220 |
|
try { |
221 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
221 |
> |
assertEquals(0, p.getActiveThreadCount()); |
222 |
|
Future<String> future = p.submit(new StringTask()); |
223 |
< |
assertTrue(p.getPoolSize() == 1); |
223 |
> |
assertEquals(1, p.getPoolSize()); |
224 |
|
} finally { |
225 |
|
joinPool(p); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
+ |
* awaitTermination on a non-shutdown pool times out |
231 |
+ |
*/ |
232 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
233 |
+ |
ForkJoinPool p = new ForkJoinPool(1); |
234 |
+ |
assertFalse(p.isTerminated()); |
235 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
236 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
237 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
238 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
239 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
240 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
241 |
+ |
long timeoutNanos = 999999L; |
242 |
+ |
long startTime = System.nanoTime(); |
243 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
244 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
245 |
+ |
assertFalse(p.isTerminated()); |
246 |
+ |
startTime = System.nanoTime(); |
247 |
+ |
long timeoutMillis = timeoutMillis(); |
248 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
249 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
250 |
+ |
assertFalse(p.isTerminated()); |
251 |
+ |
p.shutdown(); |
252 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
253 |
+ |
assertTrue(p.isTerminated()); |
254 |
+ |
} |
255 |
+ |
|
256 |
+ |
/** |
257 |
|
* setUncaughtExceptionHandler changes handler for uncaught exceptions. |
258 |
|
* |
259 |
|
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
260 |
|
* performs its defined action |
261 |
|
*/ |
262 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
263 |
< |
MyHandler eh = new MyHandler(); |
264 |
< |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
263 |
> |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
264 |
> |
final Thread.UncaughtExceptionHandler eh = |
265 |
> |
new Thread.UncaughtExceptionHandler() { |
266 |
> |
public void uncaughtException(Thread t, Throwable e) { |
267 |
> |
uehInvoked.countDown(); |
268 |
> |
}}; |
269 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
270 |
> |
eh, false); |
271 |
|
try { |
272 |
< |
assert(eh == p.getUncaughtExceptionHandler()); |
273 |
< |
p.execute(new FailingTask()); |
274 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
275 |
< |
assertTrue(eh.catches > 0); |
272 |
> |
assertSame(eh, p.getUncaughtExceptionHandler()); |
273 |
> |
try { |
274 |
> |
p.execute(new FibTask(8)); |
275 |
> |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
276 |
> |
} catch (RejectedExecutionException ok) { |
277 |
> |
} |
278 |
|
} finally { |
279 |
< |
p.shutdownNow(); |
279 |
> |
p.shutdownNow(); // failure might have prevented processing task |
280 |
|
joinPool(p); |
281 |
|
} |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
< |
* After invoking a single task, isQuiescent is true, |
286 |
< |
* queues are empty, threads are not active, and |
287 |
< |
* construction parameters continue to hold |
285 |
> |
* After invoking a single task, isQuiescent eventually becomes |
286 |
> |
* true, at which time queues are empty, threads are not active, |
287 |
> |
* the task has completed successfully, and construction |
288 |
> |
* parameters continue to hold |
289 |
|
*/ |
290 |
< |
public void testisQuiescent() throws InterruptedException { |
290 |
> |
public void testIsQuiescent() throws Exception { |
291 |
|
ForkJoinPool p = new ForkJoinPool(2); |
292 |
|
try { |
293 |
< |
p.invoke(new FibTask(20)); |
294 |
< |
assertTrue(p.getFactory() == |
295 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
296 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
293 |
> |
assertTrue(p.isQuiescent()); |
294 |
> |
long startTime = System.nanoTime(); |
295 |
> |
FibTask f = new FibTask(20); |
296 |
> |
p.invoke(f); |
297 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
298 |
> |
p.getFactory()); |
299 |
> |
while (! p.isQuiescent()) { |
300 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
301 |
> |
throw new AssertionFailedError("timed out"); |
302 |
> |
assertFalse(p.getAsyncMode()); |
303 |
> |
assertFalse(p.isShutdown()); |
304 |
> |
assertFalse(p.isTerminating()); |
305 |
> |
assertFalse(p.isTerminated()); |
306 |
> |
Thread.yield(); |
307 |
> |
} |
308 |
> |
|
309 |
|
assertTrue(p.isQuiescent()); |
310 |
|
assertFalse(p.getAsyncMode()); |
311 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
312 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
313 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
311 |
> |
assertEquals(0, p.getActiveThreadCount()); |
312 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
313 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
314 |
|
assertFalse(p.hasQueuedSubmissions()); |
315 |
|
assertFalse(p.isShutdown()); |
316 |
|
assertFalse(p.isTerminating()); |
317 |
|
assertFalse(p.isTerminated()); |
318 |
+ |
assertTrue(f.isDone()); |
319 |
+ |
assertEquals(6765, (int) f.get()); |
320 |
|
} finally { |
321 |
|
joinPool(p); |
322 |
|
} |
329 |
|
ForkJoinPool p = new ForkJoinPool(1); |
330 |
|
try { |
331 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
332 |
< |
int r = f.get(); |
280 |
< |
assertTrue(r == 21); |
332 |
> |
assertEquals(21, (int) f.get()); |
333 |
|
} finally { |
334 |
|
joinPool(p); |
335 |
|
} |
343 |
|
try { |
344 |
|
p.shutdown(); |
345 |
|
assertTrue(p.isShutdown()); |
346 |
< |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
347 |
< |
shouldThrow(); |
348 |
< |
} catch (RejectedExecutionException success) { |
346 |
> |
try { |
347 |
> |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
348 |
> |
shouldThrow(); |
349 |
> |
} catch (RejectedExecutionException success) {} |
350 |
|
} finally { |
351 |
|
joinPool(p); |
352 |
|
} |
360 |
|
try { |
361 |
|
ReentrantLock lock = new ReentrantLock(); |
362 |
|
ManagedLocker locker = new ManagedLocker(lock); |
363 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
363 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
364 |
|
p.execute(f); |
365 |
< |
int r = f.get(); |
313 |
< |
assertTrue(r == 832040); |
365 |
> |
assertEquals(6765, (int) f.get()); |
366 |
|
} finally { |
367 |
|
p.shutdownNow(); // don't wait out shutdown |
368 |
|
} |
372 |
|
* pollSubmission returns unexecuted submitted task, if present |
373 |
|
*/ |
374 |
|
public void testPollSubmission() { |
375 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
376 |
|
SubFJP p = new SubFJP(); |
377 |
|
try { |
378 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
379 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
380 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
378 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
379 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
380 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
381 |
|
ForkJoinTask r = p.pollSubmission(); |
382 |
|
assertTrue(r == a || r == b || r == c); |
383 |
|
assertFalse(r.isDone()); |
384 |
|
} finally { |
385 |
+ |
done.countDown(); |
386 |
|
joinPool(p); |
387 |
|
} |
388 |
|
} |
391 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
392 |
|
*/ |
393 |
|
public void testDrainTasksTo() { |
394 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
395 |
|
SubFJP p = new SubFJP(); |
396 |
|
try { |
397 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
398 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
399 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
397 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
398 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
399 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
400 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
401 |
|
p.drainTasksTo(al); |
402 |
|
assertTrue(al.size() > 0); |
405 |
|
assertFalse(r.isDone()); |
406 |
|
} |
407 |
|
} finally { |
408 |
+ |
done.countDown(); |
409 |
|
joinPool(p); |
410 |
|
} |
411 |
|
} |
412 |
|
|
357 |
– |
|
413 |
|
// FJ Versions of AbstractExecutorService tests |
414 |
|
|
415 |
|
/** |
418 |
|
public void testExecuteRunnable() throws Throwable { |
419 |
|
ExecutorService e = new ForkJoinPool(1); |
420 |
|
try { |
421 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
422 |
< |
assertFalse(task.done); |
423 |
< |
Future<?> future = e.submit(task); |
424 |
< |
future.get(); |
425 |
< |
assertTrue(task.done); |
421 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
422 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
423 |
> |
public void realRun() { |
424 |
> |
done.set(true); |
425 |
> |
}}); |
426 |
> |
assertNull(future.get()); |
427 |
> |
assertNull(future.get(0, MILLISECONDS)); |
428 |
> |
assertTrue(done.get()); |
429 |
> |
assertTrue(future.isDone()); |
430 |
> |
assertFalse(future.isCancelled()); |
431 |
|
} finally { |
432 |
|
joinPool(e); |
433 |
|
} |
434 |
|
} |
435 |
|
|
376 |
– |
|
436 |
|
/** |
437 |
|
* Completed submit(callable) returns result |
438 |
|
*/ |
440 |
|
ExecutorService e = new ForkJoinPool(1); |
441 |
|
try { |
442 |
|
Future<String> future = e.submit(new StringTask()); |
443 |
< |
String result = future.get(); |
444 |
< |
assertSame(TEST_STRING, result); |
443 |
> |
assertSame(TEST_STRING, future.get()); |
444 |
> |
assertTrue(future.isDone()); |
445 |
> |
assertFalse(future.isCancelled()); |
446 |
|
} finally { |
447 |
|
joinPool(e); |
448 |
|
} |
455 |
|
ExecutorService e = new ForkJoinPool(1); |
456 |
|
try { |
457 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
458 |
< |
future.get(); |
458 |
> |
assertNull(future.get()); |
459 |
|
assertTrue(future.isDone()); |
460 |
+ |
assertFalse(future.isCancelled()); |
461 |
|
} finally { |
462 |
|
joinPool(e); |
463 |
|
} |
470 |
|
ExecutorService e = new ForkJoinPool(1); |
471 |
|
try { |
472 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
473 |
< |
String result = future.get(); |
474 |
< |
assertSame(TEST_STRING, result); |
473 |
> |
assertSame(TEST_STRING, future.get()); |
474 |
> |
assertTrue(future.isDone()); |
475 |
> |
assertFalse(future.isCancelled()); |
476 |
|
} finally { |
477 |
|
joinPool(e); |
478 |
|
} |
479 |
|
} |
480 |
|
|
419 |
– |
|
481 |
|
/** |
482 |
< |
* A submitted privileged action to completion |
482 |
> |
* A submitted privileged action runs to completion |
483 |
|
*/ |
484 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
485 |
< |
Policy savedPolicy = null; |
486 |
< |
try { |
487 |
< |
savedPolicy = Policy.getPolicy(); |
488 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
428 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
429 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
430 |
< |
Policy.setPolicy(policy); |
431 |
< |
} catch (AccessControlException ok) { |
432 |
< |
return; |
433 |
< |
} |
434 |
< |
|
435 |
< |
try { |
484 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
485 |
> |
final Callable callable = Executors.callable(new PrivilegedAction() { |
486 |
> |
public Object run() { return TEST_STRING; }}); |
487 |
> |
Runnable r = new CheckedRunnable() { |
488 |
> |
public void realRun() throws Exception { |
489 |
|
ExecutorService e = new ForkJoinPool(1); |
490 |
|
try { |
491 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
492 |
< |
public Object run() { |
440 |
< |
return TEST_STRING; |
441 |
< |
}})); |
442 |
< |
|
443 |
< |
Object result = future.get(); |
444 |
< |
assertSame(TEST_STRING, result); |
491 |
> |
Future future = e.submit(callable); |
492 |
> |
assertSame(TEST_STRING, future.get()); |
493 |
|
} finally { |
494 |
|
joinPool(e); |
495 |
|
} |
496 |
< |
} finally { |
497 |
< |
Policy.setPolicy(savedPolicy); |
498 |
< |
} |
496 |
> |
}}; |
497 |
> |
|
498 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
502 |
< |
* A submitted a privileged exception action runs to completion |
502 |
> |
* A submitted privileged exception action runs to completion |
503 |
|
*/ |
504 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
505 |
< |
Policy savedPolicy = null; |
506 |
< |
try { |
507 |
< |
savedPolicy = Policy.getPolicy(); |
508 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
509 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
462 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
463 |
< |
Policy.setPolicy(policy); |
464 |
< |
} catch (AccessControlException ok) { |
465 |
< |
return; |
466 |
< |
} |
467 |
< |
|
468 |
< |
try { |
504 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
505 |
> |
final Callable callable = |
506 |
> |
Executors.callable(new PrivilegedExceptionAction() { |
507 |
> |
public Object run() { return TEST_STRING; }}); |
508 |
> |
Runnable r = new CheckedRunnable() { |
509 |
> |
public void realRun() throws Exception { |
510 |
|
ExecutorService e = new ForkJoinPool(1); |
511 |
|
try { |
512 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
513 |
< |
public Object run() { |
473 |
< |
return TEST_STRING; |
474 |
< |
}})); |
475 |
< |
|
476 |
< |
Object result = future.get(); |
477 |
< |
assertSame(TEST_STRING, result); |
512 |
> |
Future future = e.submit(callable); |
513 |
> |
assertSame(TEST_STRING, future.get()); |
514 |
|
} finally { |
515 |
|
joinPool(e); |
516 |
|
} |
517 |
< |
} finally { |
518 |
< |
Policy.setPolicy(savedPolicy); |
519 |
< |
} |
517 |
> |
}}; |
518 |
> |
|
519 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
|
* A submitted failed privileged exception action reports exception |
524 |
|
*/ |
525 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
526 |
< |
Policy savedPolicy = null; |
527 |
< |
try { |
528 |
< |
savedPolicy = Policy.getPolicy(); |
529 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
530 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
495 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
496 |
< |
Policy.setPolicy(policy); |
497 |
< |
} catch (AccessControlException ok) { |
498 |
< |
return; |
499 |
< |
} |
500 |
< |
|
501 |
< |
try { |
525 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
526 |
> |
final Callable callable = |
527 |
> |
Executors.callable(new PrivilegedExceptionAction() { |
528 |
> |
public Object run() { throw new IndexOutOfBoundsException(); }}); |
529 |
> |
Runnable r = new CheckedRunnable() { |
530 |
> |
public void realRun() throws Exception { |
531 |
|
ExecutorService e = new ForkJoinPool(1); |
532 |
|
try { |
533 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
534 |
< |
public Object run() throws Exception { |
535 |
< |
throw new IndexOutOfBoundsException(); |
536 |
< |
}})); |
537 |
< |
|
538 |
< |
Object result = future.get(); |
539 |
< |
shouldThrow(); |
511 |
< |
} catch (ExecutionException success) { |
512 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
533 |
> |
Future future = e.submit(callable); |
534 |
> |
try { |
535 |
> |
future.get(); |
536 |
> |
shouldThrow(); |
537 |
> |
} catch (ExecutionException success) { |
538 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
539 |
> |
} |
540 |
|
} finally { |
541 |
|
joinPool(e); |
542 |
|
} |
543 |
< |
} finally { |
544 |
< |
Policy.setPolicy(savedPolicy); |
545 |
< |
} |
543 |
> |
}}; |
544 |
> |
|
545 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
546 |
|
} |
547 |
|
|
548 |
|
/** |
551 |
|
public void testExecuteNullRunnable() { |
552 |
|
ExecutorService e = new ForkJoinPool(1); |
553 |
|
try { |
554 |
< |
TrackedShortRunnable task = null; |
528 |
< |
Future<?> future = e.submit(task); |
554 |
> |
Future<?> future = e.submit((Runnable) null); |
555 |
|
shouldThrow(); |
556 |
|
} catch (NullPointerException success) { |
557 |
|
} finally { |
559 |
|
} |
560 |
|
} |
561 |
|
|
536 |
– |
|
562 |
|
/** |
563 |
|
* submit(null callable) throws NullPointerException |
564 |
|
*/ |
565 |
|
public void testSubmitNullCallable() { |
566 |
|
ExecutorService e = new ForkJoinPool(1); |
567 |
|
try { |
568 |
< |
StringTask t = null; |
544 |
< |
Future<String> future = e.submit(t); |
568 |
> |
Future<String> future = e.submit((Callable) null); |
569 |
|
shouldThrow(); |
570 |
|
} catch (NullPointerException success) { |
571 |
|
} finally { |
573 |
|
} |
574 |
|
} |
575 |
|
|
552 |
– |
|
576 |
|
/** |
577 |
< |
* Blocking on submit(callable) throws InterruptedException if |
555 |
< |
* caller interrupted. |
577 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
578 |
|
*/ |
579 |
|
public void testInterruptedSubmit() throws InterruptedException { |
580 |
< |
final ForkJoinPool p = new ForkJoinPool(1); |
581 |
< |
|
582 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
583 |
< |
public void realRun() throws Throwable { |
584 |
< |
p.submit(new CheckedCallable<Object>() { |
585 |
< |
public Object realCall() throws Throwable { |
586 |
< |
try { |
587 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
588 |
< |
} catch (InterruptedException ok) { |
589 |
< |
} |
590 |
< |
return null; |
591 |
< |
}}).get(); |
592 |
< |
}}); |
593 |
< |
|
594 |
< |
t.start(); |
595 |
< |
Thread.sleep(SHORT_DELAY_MS); |
596 |
< |
t.interrupt(); |
597 |
< |
t.join(); |
598 |
< |
p.shutdownNow(); |
599 |
< |
joinPool(p); |
580 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
581 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
582 |
> |
final ExecutorService p = new ForkJoinPool(1); |
583 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
584 |
> |
public Void realCall() throws InterruptedException { |
585 |
> |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
586 |
> |
return null; |
587 |
> |
}}; |
588 |
> |
try { |
589 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
590 |
> |
public void realRun() throws Exception { |
591 |
> |
Future<Void> future = p.submit(awaiter); |
592 |
> |
submitted.countDown(); |
593 |
> |
future.get(); |
594 |
> |
}}); |
595 |
> |
t.start(); |
596 |
> |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
597 |
> |
t.interrupt(); |
598 |
> |
t.join(); |
599 |
> |
} finally { |
600 |
> |
quittingTime.countDown(); |
601 |
> |
joinPool(p); |
602 |
> |
} |
603 |
|
} |
604 |
|
|
605 |
|
/** |
610 |
|
ForkJoinPool p = new ForkJoinPool(1); |
611 |
|
try { |
612 |
|
p.submit(new Callable() { |
613 |
< |
public Object call() { |
614 |
< |
int i = 5/0; |
590 |
< |
return Boolean.TRUE; |
591 |
< |
}}).get(); |
613 |
> |
public Object call() { throw new ArithmeticException(); }}) |
614 |
> |
.get(); |
615 |
|
shouldThrow(); |
616 |
|
} catch (ExecutionException success) { |
617 |
|
assertTrue(success.getCause() instanceof ArithmeticException); |
799 |
|
} |
800 |
|
} |
801 |
|
|
779 |
– |
|
802 |
|
/** |
803 |
|
* timed invokeAny(null) throws NullPointerException |
804 |
|
*/ |
805 |
|
public void testTimedInvokeAny1() throws Throwable { |
806 |
|
ExecutorService e = new ForkJoinPool(1); |
807 |
|
try { |
808 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
808 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
809 |
|
shouldThrow(); |
810 |
|
} catch (NullPointerException success) { |
811 |
|
} finally { |
836 |
|
ExecutorService e = new ForkJoinPool(1); |
837 |
|
try { |
838 |
|
e.invokeAny(new ArrayList<Callable<String>>(), |
839 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
839 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
840 |
|
shouldThrow(); |
841 |
|
} catch (IllegalArgumentException success) { |
842 |
|
} finally { |
854 |
|
l.add(latchAwaitingStringTask(latch)); |
855 |
|
l.add(null); |
856 |
|
try { |
857 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
857 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
858 |
|
shouldThrow(); |
859 |
|
} catch (NullPointerException success) { |
860 |
|
} finally { |
871 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
872 |
|
l.add(new NPETask()); |
873 |
|
try { |
874 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
874 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
875 |
|
shouldThrow(); |
876 |
|
} catch (ExecutionException success) { |
877 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
889 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
890 |
|
l.add(new StringTask()); |
891 |
|
l.add(new StringTask()); |
892 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
892 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
893 |
|
assertSame(TEST_STRING, result); |
894 |
|
} finally { |
895 |
|
joinPool(e); |
902 |
|
public void testTimedInvokeAll1() throws Throwable { |
903 |
|
ExecutorService e = new ForkJoinPool(1); |
904 |
|
try { |
905 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
905 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
906 |
|
shouldThrow(); |
907 |
|
} catch (NullPointerException success) { |
908 |
|
} finally { |
934 |
|
try { |
935 |
|
List<Future<String>> r |
936 |
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
937 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
937 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
938 |
|
assertTrue(r.isEmpty()); |
939 |
|
} finally { |
940 |
|
joinPool(e); |
950 |
|
l.add(new StringTask()); |
951 |
|
l.add(null); |
952 |
|
try { |
953 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
953 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
954 |
|
shouldThrow(); |
955 |
|
} catch (NullPointerException success) { |
956 |
|
} finally { |
966 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
967 |
|
l.add(new NPETask()); |
968 |
|
List<Future<String>> futures |
969 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
969 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
970 |
|
assertEquals(1, futures.size()); |
971 |
|
try { |
972 |
|
futures.get(0).get(); |
988 |
|
l.add(new StringTask()); |
989 |
|
l.add(new StringTask()); |
990 |
|
List<Future<String>> futures |
991 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
991 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
992 |
|
assertEquals(2, futures.size()); |
993 |
|
for (Future<String> future : futures) |
994 |
|
assertSame(TEST_STRING, future.get()); |