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