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.*; |
9 |
< |
import java.util.concurrent.Executors; |
10 |
< |
import java.util.concurrent.ExecutorService; |
11 |
< |
import java.util.concurrent.AbstractExecutorService; |
12 |
< |
import java.util.concurrent.CountDownLatch; |
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; |
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) { |
35 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
|
38 |
|
public static Test suite() { |
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. |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
+ |
static class MyError extends Error {} |
68 |
+ |
|
69 |
|
// to test handlers |
70 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
71 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
72 |
< |
protected void onStart() { super.onStart(); throw new Error(); } |
72 |
> |
protected void onStart() { super.onStart(); throw new MyError(); } |
73 |
|
} |
74 |
|
|
75 |
|
static class FailingThreadFactory |
109 |
|
static final class FibTask extends RecursiveTask<Integer> { |
110 |
|
final int number; |
111 |
|
FibTask(int n) { number = n; } |
112 |
< |
public Integer compute() { |
112 |
> |
protected Integer compute() { |
113 |
|
int n = number; |
114 |
|
if (n <= 1) |
115 |
|
return n; |
137 |
|
this.locker = locker; |
138 |
|
this.lock = lock; |
139 |
|
} |
140 |
< |
public Integer compute() { |
140 |
> |
protected Integer compute() { |
141 |
|
int n; |
142 |
|
LockingFibTask f1 = null; |
143 |
|
LockingFibTask f2 = null; |
164 |
|
*/ |
165 |
|
public void testDefaultInitialState() { |
166 |
|
ForkJoinPool p = new ForkJoinPool(1); |
167 |
< |
try { |
168 |
< |
assertTrue(p.getFactory() == |
169 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
164 |
< |
assertTrue(p.isQuiescent()); |
167 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
168 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
169 |
> |
p.getFactory()); |
170 |
|
assertFalse(p.getAsyncMode()); |
171 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
172 |
< |
assertTrue(p.getStealCount() == 0); |
173 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
174 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
171 |
> |
assertEquals(0, p.getActiveThreadCount()); |
172 |
> |
assertEquals(0, p.getStealCount()); |
173 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
174 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
175 |
|
assertFalse(p.hasQueuedSubmissions()); |
176 |
|
assertFalse(p.isShutdown()); |
177 |
|
assertFalse(p.isTerminating()); |
178 |
|
assertFalse(p.isTerminated()); |
174 |
– |
} finally { |
175 |
– |
joinPool(p); |
179 |
|
} |
180 |
|
} |
181 |
|
|
199 |
|
} catch (NullPointerException success) {} |
200 |
|
} |
201 |
|
|
199 |
– |
|
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); |
207 |
< |
} finally { |
208 |
< |
joinPool(p); |
207 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
208 |
> |
assertEquals(1, p.getParallelism()); |
209 |
|
} |
210 |
|
} |
211 |
|
|
214 |
|
*/ |
215 |
|
public void testGetPoolSize() { |
216 |
|
ForkJoinPool p = new ForkJoinPool(1); |
217 |
< |
try { |
218 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
217 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
218 |
> |
assertEquals(0, p.getActiveThreadCount()); |
219 |
|
Future<String> future = p.submit(new StringTask()); |
220 |
< |
assertTrue(p.getPoolSize() == 1); |
221 |
< |
} finally { |
222 |
< |
joinPool(p); |
220 |
> |
assertEquals(1, p.getPoolSize()); |
221 |
> |
} |
222 |
> |
} |
223 |
> |
|
224 |
> |
/** |
225 |
> |
* awaitTermination on a non-shutdown pool times out |
226 |
> |
*/ |
227 |
> |
public void testAwaitTermination_timesOut() throws InterruptedException { |
228 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
229 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
230 |
> |
assertFalse(p.isTerminated()); |
231 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
232 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
233 |
> |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
234 |
> |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
235 |
> |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
236 |
> |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
237 |
> |
long timeoutNanos = 999999L; |
238 |
> |
long startTime = System.nanoTime(); |
239 |
> |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
240 |
> |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
241 |
> |
assertFalse(p.isTerminated()); |
242 |
> |
startTime = System.nanoTime(); |
243 |
> |
long timeoutMillis = timeoutMillis(); |
244 |
> |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
245 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
246 |
> |
assertFalse(p.isTerminated()); |
247 |
> |
p.shutdown(); |
248 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
249 |
> |
assertTrue(p.isTerminated()); |
250 |
|
} |
251 |
|
} |
252 |
|
|
257 |
|
* performs its defined action |
258 |
|
*/ |
259 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
260 |
< |
MyHandler eh = new MyHandler(); |
261 |
< |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
262 |
< |
try { |
263 |
< |
assert(eh == p.getUncaughtExceptionHandler()); |
264 |
< |
p.execute(new FailingTask()); |
265 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
266 |
< |
assertTrue(eh.catches > 0); |
267 |
< |
} finally { |
268 |
< |
p.shutdownNow(); |
269 |
< |
joinPool(p); |
260 |
> |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
261 |
> |
final Thread.UncaughtExceptionHandler ueh = |
262 |
> |
new Thread.UncaughtExceptionHandler() { |
263 |
> |
public void uncaughtException(Thread t, Throwable e) { |
264 |
> |
threadAssertTrue(e instanceof MyError); |
265 |
> |
threadAssertTrue(t instanceof FailingFJWSubclass); |
266 |
> |
uehInvoked.countDown(); |
267 |
> |
}}; |
268 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
269 |
> |
ueh, false); |
270 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
271 |
> |
assertSame(ueh, p.getUncaughtExceptionHandler()); |
272 |
> |
try { |
273 |
> |
p.execute(new FibTask(8)); |
274 |
> |
await(uehInvoked); |
275 |
> |
} finally { |
276 |
> |
p.shutdownNow(); // failure might have prevented processing task |
277 |
> |
} |
278 |
|
} |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
< |
* After invoking a single task, isQuiescent is true, |
283 |
< |
* queues are empty, threads are not active, and |
284 |
< |
* construction parameters continue to hold |
282 |
> |
* After invoking a single task, isQuiescent eventually becomes |
283 |
> |
* true, at which time queues are empty, threads are not active, |
284 |
> |
* the task has completed successfully, and construction |
285 |
> |
* parameters continue to hold |
286 |
|
*/ |
287 |
< |
public void testisQuiescent() throws InterruptedException { |
287 |
> |
public void testIsQuiescent() throws Exception { |
288 |
|
ForkJoinPool p = new ForkJoinPool(2); |
289 |
< |
try { |
290 |
< |
p.invoke(new FibTask(20)); |
291 |
< |
assertTrue(p.getFactory() == |
292 |
< |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
293 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
289 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
290 |
> |
assertTrue(p.isQuiescent()); |
291 |
> |
long startTime = System.nanoTime(); |
292 |
> |
FibTask f = new FibTask(20); |
293 |
> |
p.invoke(f); |
294 |
> |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
295 |
> |
p.getFactory()); |
296 |
> |
while (! p.isQuiescent()) { |
297 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
298 |
> |
throw new AssertionFailedError("timed out"); |
299 |
> |
assertFalse(p.getAsyncMode()); |
300 |
> |
assertFalse(p.isShutdown()); |
301 |
> |
assertFalse(p.isTerminating()); |
302 |
> |
assertFalse(p.isTerminated()); |
303 |
> |
Thread.yield(); |
304 |
> |
} |
305 |
> |
|
306 |
|
assertTrue(p.isQuiescent()); |
307 |
|
assertFalse(p.getAsyncMode()); |
308 |
< |
assertTrue(p.getActiveThreadCount() == 0); |
309 |
< |
assertTrue(p.getQueuedTaskCount() == 0); |
310 |
< |
assertTrue(p.getQueuedSubmissionCount() == 0); |
308 |
> |
assertEquals(0, p.getActiveThreadCount()); |
309 |
> |
assertEquals(0, p.getQueuedTaskCount()); |
310 |
> |
assertEquals(0, p.getQueuedSubmissionCount()); |
311 |
|
assertFalse(p.hasQueuedSubmissions()); |
312 |
|
assertFalse(p.isShutdown()); |
313 |
|
assertFalse(p.isTerminating()); |
314 |
|
assertFalse(p.isTerminated()); |
315 |
< |
} finally { |
316 |
< |
joinPool(p); |
315 |
> |
assertTrue(f.isDone()); |
316 |
> |
assertEquals(6765, (int) f.get()); |
317 |
|
} |
318 |
|
} |
319 |
|
|
322 |
|
*/ |
323 |
|
public void testSubmitForkJoinTask() throws Throwable { |
324 |
|
ForkJoinPool p = new ForkJoinPool(1); |
325 |
< |
try { |
325 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
326 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
327 |
< |
int r = f.get(); |
280 |
< |
assertTrue(r == 21); |
281 |
< |
} finally { |
282 |
< |
joinPool(p); |
327 |
> |
assertEquals(21, (int) f.get()); |
328 |
|
} |
329 |
|
} |
330 |
|
|
333 |
|
*/ |
334 |
|
public void testSubmitAfterShutdown() { |
335 |
|
ForkJoinPool p = new ForkJoinPool(1); |
336 |
< |
try { |
336 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
|
p.shutdown(); |
338 |
|
assertTrue(p.isShutdown()); |
339 |
< |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
340 |
< |
shouldThrow(); |
341 |
< |
} catch (RejectedExecutionException success) { |
342 |
< |
} finally { |
298 |
< |
joinPool(p); |
339 |
> |
try { |
340 |
> |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
341 |
> |
shouldThrow(); |
342 |
> |
} catch (RejectedExecutionException success) {} |
343 |
|
} |
344 |
|
} |
345 |
|
|
351 |
|
try { |
352 |
|
ReentrantLock lock = new ReentrantLock(); |
353 |
|
ManagedLocker locker = new ManagedLocker(lock); |
354 |
< |
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
354 |
> |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
355 |
|
p.execute(f); |
356 |
< |
int r = f.get(); |
313 |
< |
assertTrue(r == 832040); |
356 |
> |
assertEquals(6765, (int) f.get()); |
357 |
|
} finally { |
358 |
|
p.shutdownNow(); // don't wait out shutdown |
359 |
|
} |
363 |
|
* pollSubmission returns unexecuted submitted task, if present |
364 |
|
*/ |
365 |
|
public void testPollSubmission() { |
366 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
367 |
|
SubFJP p = new SubFJP(); |
368 |
< |
try { |
369 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
370 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
371 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
368 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
369 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
370 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
371 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
372 |
|
ForkJoinTask r = p.pollSubmission(); |
373 |
|
assertTrue(r == a || r == b || r == c); |
374 |
|
assertFalse(r.isDone()); |
375 |
< |
} finally { |
332 |
< |
joinPool(p); |
375 |
> |
done.countDown(); |
376 |
|
} |
377 |
|
} |
378 |
|
|
380 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
381 |
|
*/ |
382 |
|
public void testDrainTasksTo() { |
383 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
384 |
|
SubFJP p = new SubFJP(); |
385 |
< |
try { |
386 |
< |
ForkJoinTask a = p.submit(new MediumRunnable()); |
387 |
< |
ForkJoinTask b = p.submit(new MediumRunnable()); |
388 |
< |
ForkJoinTask c = p.submit(new MediumRunnable()); |
385 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
386 |
> |
ForkJoinTask a = p.submit(awaiter(done)); |
387 |
> |
ForkJoinTask b = p.submit(awaiter(done)); |
388 |
> |
ForkJoinTask c = p.submit(awaiter(done)); |
389 |
|
ArrayList<ForkJoinTask> al = new ArrayList(); |
390 |
|
p.drainTasksTo(al); |
391 |
|
assertTrue(al.size() > 0); |
393 |
|
assertTrue(r == a || r == b || r == c); |
394 |
|
assertFalse(r.isDone()); |
395 |
|
} |
396 |
< |
} finally { |
353 |
< |
joinPool(p); |
396 |
> |
done.countDown(); |
397 |
|
} |
398 |
|
} |
399 |
|
|
357 |
– |
|
400 |
|
// FJ Versions of AbstractExecutorService tests |
401 |
|
|
402 |
|
/** |
404 |
|
*/ |
405 |
|
public void testExecuteRunnable() throws Throwable { |
406 |
|
ExecutorService e = new ForkJoinPool(1); |
407 |
< |
try { |
408 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
409 |
< |
assertFalse(task.done); |
410 |
< |
Future<?> future = e.submit(task); |
411 |
< |
future.get(); |
412 |
< |
assertTrue(task.done); |
413 |
< |
} finally { |
414 |
< |
joinPool(e); |
407 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
408 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
409 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
410 |
> |
public void realRun() { |
411 |
> |
done.set(true); |
412 |
> |
}}); |
413 |
> |
assertNull(future.get()); |
414 |
> |
assertNull(future.get(0, MILLISECONDS)); |
415 |
> |
assertTrue(done.get()); |
416 |
> |
assertTrue(future.isDone()); |
417 |
> |
assertFalse(future.isCancelled()); |
418 |
|
} |
419 |
|
} |
420 |
|
|
376 |
– |
|
421 |
|
/** |
422 |
|
* Completed submit(callable) returns result |
423 |
|
*/ |
424 |
|
public void testSubmitCallable() throws Throwable { |
425 |
|
ExecutorService e = new ForkJoinPool(1); |
426 |
< |
try { |
426 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
427 |
|
Future<String> future = e.submit(new StringTask()); |
428 |
< |
String result = future.get(); |
429 |
< |
assertSame(TEST_STRING, result); |
430 |
< |
} finally { |
387 |
< |
joinPool(e); |
428 |
> |
assertSame(TEST_STRING, future.get()); |
429 |
> |
assertTrue(future.isDone()); |
430 |
> |
assertFalse(future.isCancelled()); |
431 |
|
} |
432 |
|
} |
433 |
|
|
436 |
|
*/ |
437 |
|
public void testSubmitRunnable() throws Throwable { |
438 |
|
ExecutorService e = new ForkJoinPool(1); |
439 |
< |
try { |
439 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
440 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
441 |
< |
future.get(); |
441 |
> |
assertNull(future.get()); |
442 |
|
assertTrue(future.isDone()); |
443 |
< |
} finally { |
401 |
< |
joinPool(e); |
443 |
> |
assertFalse(future.isCancelled()); |
444 |
|
} |
445 |
|
} |
446 |
|
|
449 |
|
*/ |
450 |
|
public void testSubmitRunnable2() throws Throwable { |
451 |
|
ExecutorService e = new ForkJoinPool(1); |
452 |
< |
try { |
452 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
453 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
454 |
< |
String result = future.get(); |
455 |
< |
assertSame(TEST_STRING, result); |
456 |
< |
} finally { |
415 |
< |
joinPool(e); |
454 |
> |
assertSame(TEST_STRING, future.get()); |
455 |
> |
assertTrue(future.isDone()); |
456 |
> |
assertFalse(future.isCancelled()); |
457 |
|
} |
458 |
|
} |
459 |
|
|
419 |
– |
|
460 |
|
/** |
461 |
< |
* A submitted privileged action to completion |
461 |
> |
* A submitted privileged action runs to completion |
462 |
|
*/ |
463 |
< |
public void testSubmitPrivilegedAction() throws Throwable { |
464 |
< |
Policy savedPolicy = null; |
465 |
< |
try { |
466 |
< |
savedPolicy = Policy.getPolicy(); |
467 |
< |
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 { |
463 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
464 |
> |
final Callable callable = Executors.callable(new PrivilegedAction() { |
465 |
> |
public Object run() { return TEST_STRING; }}); |
466 |
> |
Runnable r = new CheckedRunnable() { |
467 |
> |
public void realRun() throws Exception { |
468 |
|
ExecutorService e = new ForkJoinPool(1); |
469 |
< |
try { |
470 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
471 |
< |
public Object run() { |
440 |
< |
return TEST_STRING; |
441 |
< |
}})); |
442 |
< |
|
443 |
< |
Object result = future.get(); |
444 |
< |
assertSame(TEST_STRING, result); |
445 |
< |
} finally { |
446 |
< |
joinPool(e); |
469 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
470 |
> |
Future future = e.submit(callable); |
471 |
> |
assertSame(TEST_STRING, future.get()); |
472 |
|
} |
473 |
< |
} finally { |
474 |
< |
Policy.setPolicy(savedPolicy); |
475 |
< |
} |
473 |
> |
}}; |
474 |
> |
|
475 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* A submitted a privileged exception action runs to completion |
479 |
> |
* A submitted privileged exception action runs to completion |
480 |
|
*/ |
481 |
< |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
482 |
< |
Policy savedPolicy = null; |
483 |
< |
try { |
484 |
< |
savedPolicy = Policy.getPolicy(); |
485 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
486 |
< |
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 { |
481 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
482 |
> |
final Callable callable = |
483 |
> |
Executors.callable(new PrivilegedExceptionAction() { |
484 |
> |
public Object run() { return TEST_STRING; }}); |
485 |
> |
Runnable r = new CheckedRunnable() { |
486 |
> |
public void realRun() throws Exception { |
487 |
|
ExecutorService e = new ForkJoinPool(1); |
488 |
< |
try { |
489 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
490 |
< |
public Object run() { |
473 |
< |
return TEST_STRING; |
474 |
< |
}})); |
475 |
< |
|
476 |
< |
Object result = future.get(); |
477 |
< |
assertSame(TEST_STRING, result); |
478 |
< |
} finally { |
479 |
< |
joinPool(e); |
488 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
489 |
> |
Future future = e.submit(callable); |
490 |
> |
assertSame(TEST_STRING, future.get()); |
491 |
|
} |
492 |
< |
} finally { |
493 |
< |
Policy.setPolicy(savedPolicy); |
494 |
< |
} |
492 |
> |
}}; |
493 |
> |
|
494 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
498 |
|
* A submitted failed privileged exception action reports exception |
499 |
|
*/ |
500 |
< |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
501 |
< |
Policy savedPolicy = null; |
502 |
< |
try { |
503 |
< |
savedPolicy = Policy.getPolicy(); |
504 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
505 |
< |
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 { |
500 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
501 |
> |
final Callable callable = |
502 |
> |
Executors.callable(new PrivilegedExceptionAction() { |
503 |
> |
public Object run() { throw new IndexOutOfBoundsException(); }}); |
504 |
> |
Runnable r = new CheckedRunnable() { |
505 |
> |
public void realRun() throws Exception { |
506 |
|
ExecutorService e = new ForkJoinPool(1); |
507 |
< |
try { |
508 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
509 |
< |
public Object run() throws Exception { |
510 |
< |
throw new IndexOutOfBoundsException(); |
511 |
< |
}})); |
512 |
< |
|
513 |
< |
Object result = future.get(); |
514 |
< |
shouldThrow(); |
511 |
< |
} catch (ExecutionException success) { |
512 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
513 |
< |
} finally { |
514 |
< |
joinPool(e); |
507 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
508 |
> |
Future future = e.submit(callable); |
509 |
> |
try { |
510 |
> |
future.get(); |
511 |
> |
shouldThrow(); |
512 |
> |
} catch (ExecutionException success) { |
513 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
514 |
> |
} |
515 |
|
} |
516 |
< |
} finally { |
517 |
< |
Policy.setPolicy(savedPolicy); |
518 |
< |
} |
516 |
> |
}}; |
517 |
> |
|
518 |
> |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
519 |
|
} |
520 |
|
|
521 |
|
/** |
523 |
|
*/ |
524 |
|
public void testExecuteNullRunnable() { |
525 |
|
ExecutorService e = new ForkJoinPool(1); |
526 |
< |
try { |
527 |
< |
TrackedShortRunnable task = null; |
528 |
< |
Future<?> future = e.submit(task); |
529 |
< |
shouldThrow(); |
530 |
< |
} catch (NullPointerException success) { |
531 |
< |
} finally { |
532 |
< |
joinPool(e); |
526 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
527 |
> |
try { |
528 |
> |
Future<?> future = e.submit((Runnable) null); |
529 |
> |
shouldThrow(); |
530 |
> |
} catch (NullPointerException success) {} |
531 |
|
} |
532 |
|
} |
533 |
|
|
536 |
– |
|
534 |
|
/** |
535 |
|
* submit(null callable) throws NullPointerException |
536 |
|
*/ |
537 |
|
public void testSubmitNullCallable() { |
538 |
|
ExecutorService e = new ForkJoinPool(1); |
539 |
< |
try { |
540 |
< |
StringTask t = null; |
541 |
< |
Future<String> future = e.submit(t); |
542 |
< |
shouldThrow(); |
543 |
< |
} catch (NullPointerException success) { |
547 |
< |
} finally { |
548 |
< |
joinPool(e); |
539 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
540 |
> |
try { |
541 |
> |
Future<String> future = e.submit((Callable) null); |
542 |
> |
shouldThrow(); |
543 |
> |
} catch (NullPointerException success) {} |
544 |
|
} |
545 |
|
} |
546 |
|
|
552 |
– |
|
547 |
|
/** |
548 |
< |
* Blocking on submit(callable) throws InterruptedException if |
555 |
< |
* caller interrupted. |
548 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
549 |
|
*/ |
550 |
|
public void testInterruptedSubmit() throws InterruptedException { |
551 |
< |
final ForkJoinPool p = new ForkJoinPool(1); |
552 |
< |
|
553 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
554 |
< |
public void realRun() throws Throwable { |
555 |
< |
p.submit(new CheckedCallable<Object>() { |
556 |
< |
public Object realCall() throws Throwable { |
557 |
< |
try { |
558 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
559 |
< |
} catch (InterruptedException ok) { |
560 |
< |
} |
561 |
< |
return null; |
562 |
< |
}}).get(); |
563 |
< |
}}); |
564 |
< |
|
565 |
< |
t.start(); |
566 |
< |
Thread.sleep(SHORT_DELAY_MS); |
567 |
< |
t.interrupt(); |
568 |
< |
t.join(); |
569 |
< |
p.shutdownNow(); |
570 |
< |
joinPool(p); |
551 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
552 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
553 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
554 |
> |
public Void realCall() throws InterruptedException { |
555 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
556 |
> |
return null; |
557 |
> |
}}; |
558 |
> |
final ExecutorService p = new ForkJoinPool(1); |
559 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
560 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
561 |
> |
public void realRun() throws Exception { |
562 |
> |
Future<Void> future = p.submit(awaiter); |
563 |
> |
submitted.countDown(); |
564 |
> |
future.get(); |
565 |
> |
}}); |
566 |
> |
t.start(); |
567 |
> |
await(submitted); |
568 |
> |
t.interrupt(); |
569 |
> |
awaitTermination(t); |
570 |
> |
} |
571 |
|
} |
572 |
|
|
573 |
|
/** |
576 |
|
*/ |
577 |
|
public void testSubmitEE() throws Throwable { |
578 |
|
ForkJoinPool p = new ForkJoinPool(1); |
579 |
< |
try { |
580 |
< |
p.submit(new Callable() { |
581 |
< |
public Object call() { |
582 |
< |
int i = 5/0; |
583 |
< |
return Boolean.TRUE; |
584 |
< |
}}).get(); |
585 |
< |
shouldThrow(); |
586 |
< |
} catch (ExecutionException success) { |
587 |
< |
assertTrue(success.getCause() instanceof ArithmeticException); |
595 |
< |
} finally { |
596 |
< |
joinPool(p); |
579 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
580 |
> |
try { |
581 |
> |
p.submit(new Callable() { |
582 |
> |
public Object call() { throw new ArithmeticException(); }}) |
583 |
> |
.get(); |
584 |
> |
shouldThrow(); |
585 |
> |
} catch (ExecutionException success) { |
586 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
587 |
> |
} |
588 |
|
} |
589 |
|
} |
590 |
|
|
593 |
|
*/ |
594 |
|
public void testInvokeAny1() throws Throwable { |
595 |
|
ExecutorService e = new ForkJoinPool(1); |
596 |
< |
try { |
597 |
< |
e.invokeAny(null); |
598 |
< |
shouldThrow(); |
599 |
< |
} catch (NullPointerException success) { |
600 |
< |
} finally { |
610 |
< |
joinPool(e); |
596 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
597 |
> |
try { |
598 |
> |
e.invokeAny(null); |
599 |
> |
shouldThrow(); |
600 |
> |
} catch (NullPointerException success) {} |
601 |
|
} |
602 |
|
} |
603 |
|
|
606 |
|
*/ |
607 |
|
public void testInvokeAny2() throws Throwable { |
608 |
|
ExecutorService e = new ForkJoinPool(1); |
609 |
< |
try { |
610 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
611 |
< |
shouldThrow(); |
612 |
< |
} catch (IllegalArgumentException success) { |
613 |
< |
} finally { |
624 |
< |
joinPool(e); |
609 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
610 |
> |
try { |
611 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
612 |
> |
shouldThrow(); |
613 |
> |
} catch (IllegalArgumentException success) {} |
614 |
|
} |
615 |
|
} |
616 |
|
|
619 |
|
*/ |
620 |
|
public void testInvokeAny3() throws Throwable { |
621 |
|
ExecutorService e = new ForkJoinPool(1); |
622 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
623 |
< |
l.add(null); |
624 |
< |
try { |
625 |
< |
e.invokeAny(l); |
626 |
< |
shouldThrow(); |
627 |
< |
} catch (NullPointerException success) { |
628 |
< |
} finally { |
640 |
< |
joinPool(e); |
622 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
623 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
624 |
> |
l.add(null); |
625 |
> |
try { |
626 |
> |
e.invokeAny(l); |
627 |
> |
shouldThrow(); |
628 |
> |
} catch (NullPointerException success) {} |
629 |
|
} |
630 |
|
} |
631 |
|
|
635 |
|
public void testInvokeAny4() throws Throwable { |
636 |
|
CountDownLatch latch = new CountDownLatch(1); |
637 |
|
ExecutorService e = new ForkJoinPool(1); |
638 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
639 |
< |
l.add(latchAwaitingStringTask(latch)); |
640 |
< |
l.add(null); |
641 |
< |
try { |
642 |
< |
e.invokeAny(l); |
643 |
< |
shouldThrow(); |
644 |
< |
} catch (NullPointerException success) { |
645 |
< |
} finally { |
638 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
639 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
640 |
> |
l.add(latchAwaitingStringTask(latch)); |
641 |
> |
l.add(null); |
642 |
> |
try { |
643 |
> |
e.invokeAny(l); |
644 |
> |
shouldThrow(); |
645 |
> |
} catch (NullPointerException success) {} |
646 |
|
latch.countDown(); |
659 |
– |
joinPool(e); |
647 |
|
} |
648 |
|
} |
649 |
|
|
652 |
|
*/ |
653 |
|
public void testInvokeAny5() throws Throwable { |
654 |
|
ExecutorService e = new ForkJoinPool(1); |
655 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
656 |
< |
l.add(new NPETask()); |
657 |
< |
try { |
658 |
< |
e.invokeAny(l); |
659 |
< |
shouldThrow(); |
660 |
< |
} catch (ExecutionException success) { |
661 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
662 |
< |
} finally { |
663 |
< |
joinPool(e); |
655 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
656 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
657 |
> |
l.add(new NPETask()); |
658 |
> |
try { |
659 |
> |
e.invokeAny(l); |
660 |
> |
shouldThrow(); |
661 |
> |
} catch (ExecutionException success) { |
662 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
663 |
> |
} |
664 |
|
} |
665 |
|
} |
666 |
|
|
669 |
|
*/ |
670 |
|
public void testInvokeAny6() throws Throwable { |
671 |
|
ExecutorService e = new ForkJoinPool(1); |
672 |
< |
try { |
672 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
673 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
674 |
|
l.add(new StringTask()); |
675 |
|
l.add(new StringTask()); |
676 |
|
String result = e.invokeAny(l); |
677 |
|
assertSame(TEST_STRING, result); |
691 |
– |
} finally { |
692 |
– |
joinPool(e); |
678 |
|
} |
679 |
|
} |
680 |
|
|
683 |
|
*/ |
684 |
|
public void testInvokeAll1() throws Throwable { |
685 |
|
ExecutorService e = new ForkJoinPool(1); |
686 |
< |
try { |
687 |
< |
e.invokeAll(null); |
688 |
< |
shouldThrow(); |
689 |
< |
} catch (NullPointerException success) { |
690 |
< |
} finally { |
706 |
< |
joinPool(e); |
686 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
687 |
> |
try { |
688 |
> |
e.invokeAll(null); |
689 |
> |
shouldThrow(); |
690 |
> |
} catch (NullPointerException success) {} |
691 |
|
} |
692 |
|
} |
693 |
|
|
696 |
|
*/ |
697 |
|
public void testInvokeAll2() throws InterruptedException { |
698 |
|
ExecutorService e = new ForkJoinPool(1); |
699 |
< |
try { |
699 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
700 |
|
List<Future<String>> r |
701 |
|
= e.invokeAll(new ArrayList<Callable<String>>()); |
702 |
|
assertTrue(r.isEmpty()); |
719 |
– |
} finally { |
720 |
– |
joinPool(e); |
703 |
|
} |
704 |
|
} |
705 |
|
|
708 |
|
*/ |
709 |
|
public void testInvokeAll3() throws InterruptedException { |
710 |
|
ExecutorService e = new ForkJoinPool(1); |
711 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
712 |
< |
l.add(new StringTask()); |
713 |
< |
l.add(null); |
714 |
< |
try { |
715 |
< |
e.invokeAll(l); |
716 |
< |
shouldThrow(); |
717 |
< |
} catch (NullPointerException success) { |
718 |
< |
} finally { |
737 |
< |
joinPool(e); |
711 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
712 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
713 |
> |
l.add(new StringTask()); |
714 |
> |
l.add(null); |
715 |
> |
try { |
716 |
> |
e.invokeAll(l); |
717 |
> |
shouldThrow(); |
718 |
> |
} catch (NullPointerException success) {} |
719 |
|
} |
720 |
|
} |
721 |
|
|
725 |
|
*/ |
726 |
|
public void testInvokeAll4() throws Throwable { |
727 |
|
ExecutorService e = new ForkJoinPool(1); |
728 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
729 |
< |
l.add(new NPETask()); |
730 |
< |
List<Future<String>> futures = e.invokeAll(l); |
731 |
< |
assertEquals(1, futures.size()); |
732 |
< |
try { |
733 |
< |
futures.get(0).get(); |
734 |
< |
shouldThrow(); |
735 |
< |
} catch (ExecutionException success) { |
736 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
737 |
< |
} finally { |
738 |
< |
joinPool(e); |
728 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
729 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
730 |
> |
l.add(new NPETask()); |
731 |
> |
List<Future<String>> futures = e.invokeAll(l); |
732 |
> |
assertEquals(1, futures.size()); |
733 |
> |
try { |
734 |
> |
futures.get(0).get(); |
735 |
> |
shouldThrow(); |
736 |
> |
} catch (ExecutionException success) { |
737 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
738 |
> |
} |
739 |
|
} |
740 |
|
} |
741 |
|
|
744 |
|
*/ |
745 |
|
public void testInvokeAll5() throws Throwable { |
746 |
|
ExecutorService e = new ForkJoinPool(1); |
747 |
< |
try { |
747 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
748 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
749 |
|
l.add(new StringTask()); |
750 |
|
l.add(new StringTask()); |
752 |
|
assertEquals(2, futures.size()); |
753 |
|
for (Future<String> future : futures) |
754 |
|
assertSame(TEST_STRING, future.get()); |
774 |
– |
} finally { |
775 |
– |
joinPool(e); |
755 |
|
} |
756 |
|
} |
757 |
|
|
779 |
– |
|
758 |
|
/** |
759 |
|
* timed invokeAny(null) throws NullPointerException |
760 |
|
*/ |
761 |
|
public void testTimedInvokeAny1() throws Throwable { |
762 |
|
ExecutorService e = new ForkJoinPool(1); |
763 |
< |
try { |
764 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
765 |
< |
shouldThrow(); |
766 |
< |
} catch (NullPointerException success) { |
767 |
< |
} finally { |
790 |
< |
joinPool(e); |
763 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
764 |
> |
try { |
765 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
766 |
> |
shouldThrow(); |
767 |
> |
} catch (NullPointerException success) {} |
768 |
|
} |
769 |
|
} |
770 |
|
|
773 |
|
*/ |
774 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
775 |
|
ExecutorService e = new ForkJoinPool(1); |
776 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
777 |
< |
l.add(new StringTask()); |
778 |
< |
try { |
779 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
780 |
< |
shouldThrow(); |
781 |
< |
} catch (NullPointerException success) { |
782 |
< |
} finally { |
806 |
< |
joinPool(e); |
776 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
777 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
778 |
> |
l.add(new StringTask()); |
779 |
> |
try { |
780 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
781 |
> |
shouldThrow(); |
782 |
> |
} catch (NullPointerException success) {} |
783 |
|
} |
784 |
|
} |
785 |
|
|
788 |
|
*/ |
789 |
|
public void testTimedInvokeAny2() throws Throwable { |
790 |
|
ExecutorService e = new ForkJoinPool(1); |
791 |
< |
try { |
792 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), |
793 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
794 |
< |
shouldThrow(); |
795 |
< |
} catch (IllegalArgumentException success) { |
796 |
< |
} finally { |
821 |
< |
joinPool(e); |
791 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
792 |
> |
try { |
793 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
794 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
795 |
> |
shouldThrow(); |
796 |
> |
} catch (IllegalArgumentException success) {} |
797 |
|
} |
798 |
|
} |
799 |
|
|
803 |
|
public void testTimedInvokeAny3() throws Throwable { |
804 |
|
CountDownLatch latch = new CountDownLatch(1); |
805 |
|
ExecutorService e = new ForkJoinPool(1); |
806 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
807 |
< |
l.add(latchAwaitingStringTask(latch)); |
808 |
< |
l.add(null); |
809 |
< |
try { |
810 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
811 |
< |
shouldThrow(); |
812 |
< |
} catch (NullPointerException success) { |
813 |
< |
} finally { |
806 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
807 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
808 |
> |
l.add(latchAwaitingStringTask(latch)); |
809 |
> |
l.add(null); |
810 |
> |
try { |
811 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
812 |
> |
shouldThrow(); |
813 |
> |
} catch (NullPointerException success) {} |
814 |
|
latch.countDown(); |
840 |
– |
joinPool(e); |
815 |
|
} |
816 |
|
} |
817 |
|
|
820 |
|
*/ |
821 |
|
public void testTimedInvokeAny4() throws Throwable { |
822 |
|
ExecutorService e = new ForkJoinPool(1); |
823 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
824 |
< |
l.add(new NPETask()); |
825 |
< |
try { |
826 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
827 |
< |
shouldThrow(); |
828 |
< |
} catch (ExecutionException success) { |
829 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
830 |
< |
} finally { |
831 |
< |
joinPool(e); |
823 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
824 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
825 |
> |
l.add(new NPETask()); |
826 |
> |
try { |
827 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
828 |
> |
shouldThrow(); |
829 |
> |
} catch (ExecutionException success) { |
830 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
831 |
> |
} |
832 |
|
} |
833 |
|
} |
834 |
|
|
837 |
|
*/ |
838 |
|
public void testTimedInvokeAny5() throws Throwable { |
839 |
|
ExecutorService e = new ForkJoinPool(1); |
840 |
< |
try { |
840 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
841 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
842 |
|
l.add(new StringTask()); |
843 |
|
l.add(new StringTask()); |
844 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
844 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
845 |
|
assertSame(TEST_STRING, result); |
872 |
– |
} finally { |
873 |
– |
joinPool(e); |
846 |
|
} |
847 |
|
} |
848 |
|
|
851 |
|
*/ |
852 |
|
public void testTimedInvokeAll1() throws Throwable { |
853 |
|
ExecutorService e = new ForkJoinPool(1); |
854 |
< |
try { |
855 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
856 |
< |
shouldThrow(); |
857 |
< |
} catch (NullPointerException success) { |
858 |
< |
} finally { |
887 |
< |
joinPool(e); |
854 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
855 |
> |
try { |
856 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
857 |
> |
shouldThrow(); |
858 |
> |
} catch (NullPointerException success) {} |
859 |
|
} |
860 |
|
} |
861 |
|
|
864 |
|
*/ |
865 |
|
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
866 |
|
ExecutorService e = new ForkJoinPool(1); |
867 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
868 |
< |
l.add(new StringTask()); |
869 |
< |
try { |
870 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
871 |
< |
shouldThrow(); |
872 |
< |
} catch (NullPointerException success) { |
873 |
< |
} finally { |
903 |
< |
joinPool(e); |
867 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
868 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
869 |
> |
l.add(new StringTask()); |
870 |
> |
try { |
871 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
872 |
> |
shouldThrow(); |
873 |
> |
} catch (NullPointerException success) {} |
874 |
|
} |
875 |
|
} |
876 |
|
|
879 |
|
*/ |
880 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
881 |
|
ExecutorService e = new ForkJoinPool(1); |
882 |
< |
try { |
882 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
883 |
|
List<Future<String>> r |
884 |
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
885 |
< |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
885 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
886 |
|
assertTrue(r.isEmpty()); |
917 |
– |
} finally { |
918 |
– |
joinPool(e); |
887 |
|
} |
888 |
|
} |
889 |
|
|
892 |
|
*/ |
893 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
894 |
|
ExecutorService e = new ForkJoinPool(1); |
895 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
896 |
< |
l.add(new StringTask()); |
897 |
< |
l.add(null); |
898 |
< |
try { |
899 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
900 |
< |
shouldThrow(); |
901 |
< |
} catch (NullPointerException success) { |
902 |
< |
} finally { |
935 |
< |
joinPool(e); |
895 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
896 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
897 |
> |
l.add(new StringTask()); |
898 |
> |
l.add(null); |
899 |
> |
try { |
900 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
901 |
> |
shouldThrow(); |
902 |
> |
} catch (NullPointerException success) {} |
903 |
|
} |
904 |
|
} |
905 |
|
|
908 |
|
*/ |
909 |
|
public void testTimedInvokeAll4() throws Throwable { |
910 |
|
ExecutorService e = new ForkJoinPool(1); |
911 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
912 |
< |
l.add(new NPETask()); |
913 |
< |
List<Future<String>> futures |
914 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
915 |
< |
assertEquals(1, futures.size()); |
916 |
< |
try { |
917 |
< |
futures.get(0).get(); |
918 |
< |
shouldThrow(); |
919 |
< |
} catch (ExecutionException success) { |
920 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
921 |
< |
} finally { |
922 |
< |
joinPool(e); |
911 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
912 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
913 |
> |
l.add(new NPETask()); |
914 |
> |
List<Future<String>> futures |
915 |
> |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
916 |
> |
assertEquals(1, futures.size()); |
917 |
> |
try { |
918 |
> |
futures.get(0).get(); |
919 |
> |
shouldThrow(); |
920 |
> |
} catch (ExecutionException success) { |
921 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
922 |
> |
} |
923 |
|
} |
924 |
|
} |
925 |
|
|
927 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
928 |
|
*/ |
929 |
|
public void testTimedInvokeAll5() throws Throwable { |
930 |
< |
ExecutorService e = new ForkJoinPool(1); |
931 |
< |
try { |
930 |
> |
ForkJoinPool e = new ForkJoinPool(1); |
931 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
932 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
933 |
|
l.add(new StringTask()); |
934 |
|
l.add(new StringTask()); |
935 |
|
List<Future<String>> futures |
936 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
936 |
> |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
937 |
|
assertEquals(2, futures.size()); |
938 |
|
for (Future<String> future : futures) |
939 |
|
assertSame(TEST_STRING, future.get()); |
973 |
– |
} finally { |
974 |
– |
joinPool(e); |
940 |
|
} |
941 |
|
} |
942 |
|
|