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