4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
*/ |
6 |
|
|
7 |
– |
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
9 |
> |
import java.util.concurrent.Executor; |
10 |
> |
import java.util.concurrent.Executors; |
11 |
> |
import java.util.concurrent.ExecutorService; |
12 |
> |
import java.util.concurrent.AbstractExecutorService; |
13 |
> |
import java.util.concurrent.CountDownLatch; |
14 |
> |
import java.util.concurrent.Callable; |
15 |
> |
import java.util.concurrent.Future; |
16 |
> |
import java.util.concurrent.ExecutionException; |
17 |
> |
import java.util.concurrent.CancellationException; |
18 |
> |
import java.util.concurrent.RejectedExecutionException; |
19 |
> |
import java.util.concurrent.ForkJoinPool; |
20 |
> |
import java.util.concurrent.ForkJoinTask; |
21 |
> |
import java.util.concurrent.ForkJoinWorkerThread; |
22 |
> |
import java.util.concurrent.RecursiveAction; |
23 |
> |
import java.util.concurrent.RecursiveTask; |
24 |
> |
import java.util.concurrent.TimeUnit; |
25 |
|
import java.util.concurrent.locks.*; |
26 |
|
import java.security.*; |
27 |
|
|
28 |
|
public class ForkJoinPoolTest extends JSR166TestCase { |
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
+ |
|
33 |
|
public static Test suite() { |
34 |
|
return new TestSuite(ForkJoinPoolTest.class); |
35 |
|
} |
53 |
|
// Some classes to test extension and factory methods |
54 |
|
|
55 |
|
static class MyHandler implements Thread.UncaughtExceptionHandler { |
56 |
< |
int catches = 0; |
56 |
> |
volatile int catches = 0; |
57 |
|
public void uncaughtException(Thread t, Throwable e) { |
58 |
|
++catches; |
59 |
|
} |
62 |
|
// to test handlers |
63 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
64 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
65 |
< |
protected void onStart() { throw new Error(); } |
65 |
> |
protected void onStart() { super.onStart(); throw new Error(); } |
66 |
|
} |
67 |
|
|
68 |
< |
static class FailingThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
69 |
< |
int calls = 0; |
68 |
> |
static class FailingThreadFactory |
69 |
> |
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
70 |
> |
volatile int calls = 0; |
71 |
|
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
72 |
|
if (++calls > 1) return null; |
73 |
|
return new FailingFJWSubclass(p); |
151 |
|
} |
152 |
|
|
153 |
|
/** |
154 |
< |
* Succesfully constructed pool reports default factory, |
154 |
> |
* Successfully constructed pool reports default factory, |
155 |
|
* parallelism and async mode policies, no active threads or |
156 |
|
* tasks, and quiescent running state. |
157 |
|
*/ |
158 |
|
public void testDefaultInitialState() { |
159 |
< |
ForkJoinPool p = null; |
159 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
160 |
|
try { |
161 |
< |
p = new ForkJoinPool(1); |
162 |
< |
assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
161 |
> |
assertTrue(p.getFactory() == |
162 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
163 |
|
assertTrue(p.isQuiescent()); |
148 |
– |
assertTrue(p.getMaintainsParallelism()); |
164 |
|
assertFalse(p.getAsyncMode()); |
165 |
|
assertTrue(p.getActiveThreadCount() == 0); |
166 |
|
assertTrue(p.getStealCount() == 0); |
182 |
|
try { |
183 |
|
new ForkJoinPool(-1); |
184 |
|
shouldThrow(); |
185 |
< |
} |
171 |
< |
catch (IllegalArgumentException success) {} |
185 |
> |
} catch (IllegalArgumentException success) {} |
186 |
|
} |
187 |
|
|
188 |
|
/** |
190 |
|
*/ |
191 |
|
public void testConstructor2() { |
192 |
|
try { |
193 |
< |
new ForkJoinPool(1, null); |
193 |
> |
new ForkJoinPool(1, null, null, false); |
194 |
|
shouldThrow(); |
195 |
< |
} |
182 |
< |
catch (NullPointerException success) {} |
195 |
> |
} catch (NullPointerException success) {} |
196 |
|
} |
197 |
|
|
198 |
|
|
200 |
|
* getParallelism returns size set in constructor |
201 |
|
*/ |
202 |
|
public void testGetParallelism() { |
203 |
< |
ForkJoinPool p = null; |
191 |
< |
try { |
192 |
< |
p = new ForkJoinPool(1); |
193 |
< |
assertTrue(p.getParallelism() == 1); |
194 |
< |
} finally { |
195 |
< |
joinPool(p); |
196 |
< |
} |
197 |
< |
} |
198 |
< |
|
199 |
< |
/** |
200 |
< |
* setParallelism changes reported parallelism level. |
201 |
< |
*/ |
202 |
< |
public void testSetParallelism() { |
203 |
< |
ForkJoinPool p = null; |
204 |
< |
try { |
205 |
< |
p = new ForkJoinPool(1); |
206 |
< |
assertTrue(p.getParallelism() == 1); |
207 |
< |
p.setParallelism(2); |
208 |
< |
assertTrue(p.getParallelism() == 2); |
209 |
< |
} finally { |
210 |
< |
joinPool(p); |
211 |
< |
} |
212 |
< |
} |
213 |
< |
|
214 |
< |
/** |
215 |
< |
* setParallelism with argument <= 0 throws exception |
216 |
< |
*/ |
217 |
< |
public void testSetParallelism2() { |
218 |
< |
ForkJoinPool p = null; |
203 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
204 |
|
try { |
220 |
– |
p = new ForkJoinPool(1); |
205 |
|
assertTrue(p.getParallelism() == 1); |
222 |
– |
p.setParallelism(-2); |
223 |
– |
shouldThrow(); |
224 |
– |
} catch (IllegalArgumentException success) { |
206 |
|
} finally { |
207 |
|
joinPool(p); |
208 |
|
} |
212 |
|
* getPoolSize returns number of started workers. |
213 |
|
*/ |
214 |
|
public void testGetPoolSize() { |
215 |
< |
ForkJoinPool p = null; |
215 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
216 |
|
try { |
217 |
< |
p = new ForkJoinPool(1); |
237 |
< |
assertTrue(p.getPoolSize() == 0); |
217 |
> |
assertTrue(p.getActiveThreadCount() == 0); |
218 |
|
Future<String> future = p.submit(new StringTask()); |
219 |
|
assertTrue(p.getPoolSize() == 1); |
240 |
– |
|
241 |
– |
} finally { |
242 |
– |
joinPool(p); |
243 |
– |
} |
244 |
– |
} |
245 |
– |
|
246 |
– |
/** |
247 |
– |
* setMaximumPoolSize changes size reported by getMaximumPoolSize. |
248 |
– |
*/ |
249 |
– |
public void testSetMaximumPoolSize() { |
250 |
– |
ForkJoinPool p = null; |
251 |
– |
try { |
252 |
– |
p = new ForkJoinPool(1); |
253 |
– |
p.setMaximumPoolSize(2); |
254 |
– |
assertTrue(p.getMaximumPoolSize() == 2); |
255 |
– |
} finally { |
256 |
– |
joinPool(p); |
257 |
– |
} |
258 |
– |
} |
259 |
– |
|
260 |
– |
/** |
261 |
– |
* setMaximumPoolSize with argument <= 0 throws exception |
262 |
– |
*/ |
263 |
– |
public void testSetMaximumPoolSize2() { |
264 |
– |
ForkJoinPool p = null; |
265 |
– |
try { |
266 |
– |
p = new ForkJoinPool(1); |
267 |
– |
p.setMaximumPoolSize(-2); |
268 |
– |
shouldThrow(); |
269 |
– |
} catch (IllegalArgumentException success) { |
270 |
– |
} finally { |
271 |
– |
joinPool(p); |
272 |
– |
} |
273 |
– |
} |
274 |
– |
|
275 |
– |
/** |
276 |
– |
* setMaintainsParallelism changes policy reported by |
277 |
– |
* getMaintainsParallelism. |
278 |
– |
*/ |
279 |
– |
public void testSetMaintainsParallelism() { |
280 |
– |
ForkJoinPool p = null; |
281 |
– |
try { |
282 |
– |
p = new ForkJoinPool(1); |
283 |
– |
p.setMaintainsParallelism(false); |
284 |
– |
assertFalse(p.getMaintainsParallelism()); |
285 |
– |
} finally { |
286 |
– |
joinPool(p); |
287 |
– |
} |
288 |
– |
} |
289 |
– |
|
290 |
– |
/** |
291 |
– |
* setAsyncMode changes policy reported by |
292 |
– |
* getAsyncMode. |
293 |
– |
*/ |
294 |
– |
public void testSetAsyncMode() { |
295 |
– |
ForkJoinPool p = null; |
296 |
– |
try { |
297 |
– |
p = new ForkJoinPool(1); |
298 |
– |
p.setAsyncMode(true); |
299 |
– |
assertTrue(p.getAsyncMode()); |
220 |
|
} finally { |
221 |
|
joinPool(p); |
222 |
|
} |
228 |
|
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
229 |
|
* performs its defined action |
230 |
|
*/ |
231 |
< |
public void testSetUncaughtExceptionHandler() { |
232 |
< |
ForkJoinPool p = null; |
231 |
> |
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
232 |
> |
MyHandler eh = new MyHandler(); |
233 |
> |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false); |
234 |
|
try { |
235 |
< |
p = new ForkJoinPool(1, new FailingThreadFactory()); |
315 |
< |
MyHandler eh = new MyHandler(); |
316 |
< |
p.setUncaughtExceptionHandler(eh); |
317 |
< |
assertEquals(eh, p.getUncaughtExceptionHandler()); |
235 |
> |
assert(eh == p.getUncaughtExceptionHandler()); |
236 |
|
p.execute(new FailingTask()); |
237 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
238 |
|
assertTrue(eh.catches > 0); |
321 |
– |
} catch (InterruptedException e) { |
322 |
– |
unexpectedException(); |
323 |
– |
} finally { |
324 |
– |
joinPool(p); |
325 |
– |
} |
326 |
– |
} |
327 |
– |
|
328 |
– |
/** |
329 |
– |
* setUncaughtExceptionHandler of null removes handler |
330 |
– |
*/ |
331 |
– |
public void testSetUncaughtExceptionHandler2() { |
332 |
– |
ForkJoinPool p = null; |
333 |
– |
try { |
334 |
– |
p = new ForkJoinPool(1); |
335 |
– |
p.setUncaughtExceptionHandler(null); |
336 |
– |
assertNull(p.getUncaughtExceptionHandler()); |
239 |
|
} finally { |
240 |
+ |
p.shutdownNow(); |
241 |
|
joinPool(p); |
242 |
|
} |
243 |
|
} |
244 |
|
|
342 |
– |
|
245 |
|
/** |
246 |
|
* After invoking a single task, isQuiescent is true, |
247 |
|
* queues are empty, threads are not active, and |
248 |
|
* construction parameters continue to hold |
249 |
|
*/ |
250 |
< |
public void testisQuiescent() { |
251 |
< |
ForkJoinPool p = null; |
250 |
> |
public void testisQuiescent() throws InterruptedException { |
251 |
> |
ForkJoinPool p = new ForkJoinPool(2); |
252 |
|
try { |
351 |
– |
p = new ForkJoinPool(2); |
253 |
|
p.invoke(new FibTask(20)); |
254 |
< |
assertTrue(p.getFactory() == ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
254 |
> |
assertTrue(p.getFactory() == |
255 |
> |
ForkJoinPool.defaultForkJoinWorkerThreadFactory); |
256 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
257 |
|
assertTrue(p.isQuiescent()); |
356 |
– |
assertTrue(p.getMaintainsParallelism()); |
258 |
|
assertFalse(p.getAsyncMode()); |
259 |
|
assertTrue(p.getActiveThreadCount() == 0); |
260 |
|
assertTrue(p.getQueuedTaskCount() == 0); |
263 |
|
assertFalse(p.isShutdown()); |
264 |
|
assertFalse(p.isTerminating()); |
265 |
|
assertFalse(p.isTerminated()); |
365 |
– |
} catch (InterruptedException e) { |
366 |
– |
unexpectedException(); |
266 |
|
} finally { |
267 |
|
joinPool(p); |
268 |
|
} |
271 |
|
/** |
272 |
|
* Completed submit(ForkJoinTask) returns result |
273 |
|
*/ |
274 |
< |
public void testSubmitForkJoinTask() { |
275 |
< |
ForkJoinPool p = null; |
274 |
> |
public void testSubmitForkJoinTask() throws Throwable { |
275 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
276 |
|
try { |
378 |
– |
p = new ForkJoinPool(1); |
277 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
278 |
|
int r = f.get(); |
279 |
|
assertTrue(r == 21); |
382 |
– |
} catch (ExecutionException ex) { |
383 |
– |
unexpectedException(); |
384 |
– |
} catch (InterruptedException ex) { |
385 |
– |
unexpectedException(); |
280 |
|
} finally { |
281 |
|
joinPool(p); |
282 |
|
} |
286 |
|
* A task submitted after shutdown is rejected |
287 |
|
*/ |
288 |
|
public void testSubmitAfterShutdown() { |
289 |
< |
ForkJoinPool p = null; |
289 |
> |
ForkJoinPool p = new ForkJoinPool(1); |
290 |
|
try { |
397 |
– |
p = new ForkJoinPool(1); |
291 |
|
p.shutdown(); |
292 |
|
assertTrue(p.isShutdown()); |
293 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
301 |
|
/** |
302 |
|
* Pool maintains parallelism when using ManagedBlocker |
303 |
|
*/ |
304 |
< |
public void testBlockingForkJoinTask() { |
305 |
< |
ForkJoinPool p = null; |
304 |
> |
public void testBlockingForkJoinTask() throws Throwable { |
305 |
> |
ForkJoinPool p = new ForkJoinPool(4); |
306 |
|
try { |
414 |
– |
p = new ForkJoinPool(4); |
307 |
|
ReentrantLock lock = new ReentrantLock(); |
308 |
|
ManagedLocker locker = new ManagedLocker(lock); |
309 |
|
ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock); |
310 |
|
p.execute(f); |
419 |
– |
assertTrue(p.getPoolSize() >= 4); |
311 |
|
int r = f.get(); |
312 |
< |
assertTrue(r == 832040); |
422 |
< |
} catch (ExecutionException ex) { |
423 |
< |
unexpectedException(); |
424 |
< |
} catch (InterruptedException ex) { |
425 |
< |
unexpectedException(); |
312 |
> |
assertTrue(r == 832040); |
313 |
|
} finally { |
314 |
< |
joinPool(p); |
314 |
> |
p.shutdownNow(); // don't wait out shutdown |
315 |
|
} |
316 |
|
} |
317 |
|
|
319 |
|
* pollSubmission returns unexecuted submitted task, if present |
320 |
|
*/ |
321 |
|
public void testPollSubmission() { |
322 |
< |
SubFJP p = null; |
322 |
> |
SubFJP p = new SubFJP(); |
323 |
|
try { |
437 |
– |
p = new SubFJP(); |
324 |
|
ForkJoinTask a = p.submit(new MediumRunnable()); |
325 |
|
ForkJoinTask b = p.submit(new MediumRunnable()); |
326 |
|
ForkJoinTask c = p.submit(new MediumRunnable()); |
336 |
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
337 |
|
*/ |
338 |
|
public void testDrainTasksTo() { |
339 |
< |
SubFJP p = null; |
339 |
> |
SubFJP p = new SubFJP(); |
340 |
|
try { |
455 |
– |
p = new SubFJP(); |
341 |
|
ForkJoinTask a = p.submit(new MediumRunnable()); |
342 |
|
ForkJoinTask b = p.submit(new MediumRunnable()); |
343 |
|
ForkJoinTask c = p.submit(new MediumRunnable()); |
359 |
|
/** |
360 |
|
* execute(runnable) runs it to completion |
361 |
|
*/ |
362 |
< |
public void testExecuteRunnable() { |
362 |
> |
public void testExecuteRunnable() throws Throwable { |
363 |
> |
ExecutorService e = new ForkJoinPool(1); |
364 |
|
try { |
479 |
– |
ExecutorService e = new ForkJoinPool(1); |
365 |
|
TrackedShortRunnable task = new TrackedShortRunnable(); |
366 |
|
assertFalse(task.done); |
367 |
|
Future<?> future = e.submit(task); |
368 |
|
future.get(); |
369 |
|
assertTrue(task.done); |
370 |
< |
} |
371 |
< |
catch (ExecutionException ex) { |
487 |
< |
unexpectedException(); |
488 |
< |
} |
489 |
< |
catch (InterruptedException ex) { |
490 |
< |
unexpectedException(); |
370 |
> |
} finally { |
371 |
> |
joinPool(e); |
372 |
|
} |
373 |
|
} |
374 |
|
|
376 |
|
/** |
377 |
|
* Completed submit(callable) returns result |
378 |
|
*/ |
379 |
< |
public void testSubmitCallable() { |
379 |
> |
public void testSubmitCallable() throws Throwable { |
380 |
> |
ExecutorService e = new ForkJoinPool(1); |
381 |
|
try { |
500 |
– |
ExecutorService e = new ForkJoinPool(1); |
382 |
|
Future<String> future = e.submit(new StringTask()); |
383 |
|
String result = future.get(); |
384 |
|
assertSame(TEST_STRING, result); |
385 |
< |
} |
386 |
< |
catch (ExecutionException ex) { |
506 |
< |
unexpectedException(); |
507 |
< |
} |
508 |
< |
catch (InterruptedException ex) { |
509 |
< |
unexpectedException(); |
385 |
> |
} finally { |
386 |
> |
joinPool(e); |
387 |
|
} |
388 |
|
} |
389 |
|
|
390 |
|
/** |
391 |
|
* Completed submit(runnable) returns successfully |
392 |
|
*/ |
393 |
< |
public void testSubmitRunnable() { |
393 |
> |
public void testSubmitRunnable() throws Throwable { |
394 |
> |
ExecutorService e = new ForkJoinPool(1); |
395 |
|
try { |
518 |
– |
ExecutorService e = new ForkJoinPool(1); |
396 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
397 |
|
future.get(); |
398 |
|
assertTrue(future.isDone()); |
399 |
< |
} |
400 |
< |
catch (ExecutionException ex) { |
524 |
< |
unexpectedException(); |
525 |
< |
} |
526 |
< |
catch (InterruptedException ex) { |
527 |
< |
unexpectedException(); |
399 |
> |
} finally { |
400 |
> |
joinPool(e); |
401 |
|
} |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
|
* Completed submit(runnable, result) returns result |
406 |
|
*/ |
407 |
< |
public void testSubmitRunnable2() { |
407 |
> |
public void testSubmitRunnable2() throws Throwable { |
408 |
> |
ExecutorService e = new ForkJoinPool(1); |
409 |
|
try { |
536 |
– |
ExecutorService e = new ForkJoinPool(1); |
410 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
411 |
|
String result = future.get(); |
412 |
|
assertSame(TEST_STRING, result); |
413 |
< |
} |
414 |
< |
catch (ExecutionException ex) { |
542 |
< |
unexpectedException(); |
543 |
< |
} |
544 |
< |
catch (InterruptedException ex) { |
545 |
< |
unexpectedException(); |
413 |
> |
} finally { |
414 |
> |
joinPool(e); |
415 |
|
} |
416 |
|
} |
417 |
|
|
419 |
|
/** |
420 |
|
* A submitted privileged action to completion |
421 |
|
*/ |
422 |
< |
public void testSubmitPrivilegedAction() { |
422 |
> |
public void testSubmitPrivilegedAction() throws Throwable { |
423 |
|
Policy savedPolicy = null; |
424 |
|
try { |
425 |
|
savedPolicy = Policy.getPolicy(); |
430 |
|
} catch (AccessControlException ok) { |
431 |
|
return; |
432 |
|
} |
433 |
+ |
|
434 |
|
try { |
435 |
|
ExecutorService e = new ForkJoinPool(1); |
436 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
436 |
> |
try { |
437 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
438 |
|
public Object run() { |
439 |
|
return TEST_STRING; |
440 |
|
}})); |
441 |
|
|
442 |
< |
Object result = future.get(); |
443 |
< |
assertSame(TEST_STRING, result); |
444 |
< |
} |
445 |
< |
catch (ExecutionException ex) { |
575 |
< |
unexpectedException(); |
576 |
< |
} |
577 |
< |
catch (InterruptedException ex) { |
578 |
< |
unexpectedException(); |
579 |
< |
} |
580 |
< |
finally { |
581 |
< |
try { |
582 |
< |
Policy.setPolicy(savedPolicy); |
583 |
< |
} catch (AccessControlException ok) { |
584 |
< |
return; |
442 |
> |
Object result = future.get(); |
443 |
> |
assertSame(TEST_STRING, result); |
444 |
> |
} finally { |
445 |
> |
joinPool(e); |
446 |
|
} |
447 |
+ |
} finally { |
448 |
+ |
Policy.setPolicy(savedPolicy); |
449 |
|
} |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
|
* A submitted a privileged exception action runs to completion |
454 |
|
*/ |
455 |
< |
public void testSubmitPrivilegedExceptionAction() { |
455 |
> |
public void testSubmitPrivilegedExceptionAction() throws Throwable { |
456 |
|
Policy savedPolicy = null; |
457 |
|
try { |
458 |
|
savedPolicy = Policy.getPolicy(); |
466 |
|
|
467 |
|
try { |
468 |
|
ExecutorService e = new ForkJoinPool(1); |
469 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
469 |
> |
try { |
470 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
471 |
|
public Object run() { |
472 |
|
return TEST_STRING; |
473 |
|
}})); |
474 |
|
|
475 |
< |
Object result = future.get(); |
476 |
< |
assertSame(TEST_STRING, result); |
477 |
< |
} |
478 |
< |
catch (ExecutionException ex) { |
479 |
< |
unexpectedException(); |
480 |
< |
} |
617 |
< |
catch (InterruptedException ex) { |
618 |
< |
unexpectedException(); |
619 |
< |
} |
620 |
< |
finally { |
475 |
> |
Object result = future.get(); |
476 |
> |
assertSame(TEST_STRING, result); |
477 |
> |
} finally { |
478 |
> |
joinPool(e); |
479 |
> |
} |
480 |
> |
} finally { |
481 |
|
Policy.setPolicy(savedPolicy); |
482 |
|
} |
483 |
|
} |
485 |
|
/** |
486 |
|
* A submitted failed privileged exception action reports exception |
487 |
|
*/ |
488 |
< |
public void testSubmitFailedPrivilegedExceptionAction() { |
488 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Throwable { |
489 |
|
Policy savedPolicy = null; |
490 |
|
try { |
491 |
|
savedPolicy = Policy.getPolicy(); |
497 |
|
return; |
498 |
|
} |
499 |
|
|
640 |
– |
|
500 |
|
try { |
501 |
|
ExecutorService e = new ForkJoinPool(1); |
502 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
502 |
> |
try { |
503 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
504 |
|
public Object run() throws Exception { |
505 |
|
throw new IndexOutOfBoundsException(); |
506 |
|
}})); |
507 |
|
|
508 |
< |
Object result = future.get(); |
509 |
< |
shouldThrow(); |
510 |
< |
} |
511 |
< |
catch (ExecutionException success) { |
512 |
< |
} catch (CancellationException success) { |
513 |
< |
} catch (InterruptedException ex) { |
514 |
< |
unexpectedException(); |
515 |
< |
} |
656 |
< |
finally { |
508 |
> |
Object result = future.get(); |
509 |
> |
shouldThrow(); |
510 |
> |
} catch (ExecutionException success) { |
511 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
512 |
> |
} finally { |
513 |
> |
joinPool(e); |
514 |
> |
} |
515 |
> |
} finally { |
516 |
|
Policy.setPolicy(savedPolicy); |
517 |
|
} |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
< |
* execute(null runnable) throws NPE |
521 |
> |
* execute(null runnable) throws NullPointerException |
522 |
|
*/ |
523 |
|
public void testExecuteNullRunnable() { |
524 |
+ |
ExecutorService e = new ForkJoinPool(1); |
525 |
|
try { |
666 |
– |
ExecutorService e = new ForkJoinPool(1); |
526 |
|
TrackedShortRunnable task = null; |
527 |
|
Future<?> future = e.submit(task); |
528 |
|
shouldThrow(); |
529 |
< |
} |
530 |
< |
catch (NullPointerException success) { |
531 |
< |
} |
673 |
< |
catch (Exception ex) { |
674 |
< |
unexpectedException(); |
529 |
> |
} catch (NullPointerException success) { |
530 |
> |
} finally { |
531 |
> |
joinPool(e); |
532 |
|
} |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
|
/** |
537 |
< |
* submit(null callable) throws NPE |
537 |
> |
* submit(null callable) throws NullPointerException |
538 |
|
*/ |
539 |
|
public void testSubmitNullCallable() { |
540 |
+ |
ExecutorService e = new ForkJoinPool(1); |
541 |
|
try { |
684 |
– |
ExecutorService e = new ForkJoinPool(1); |
542 |
|
StringTask t = null; |
543 |
|
Future<String> future = e.submit(t); |
544 |
|
shouldThrow(); |
545 |
< |
} |
546 |
< |
catch (NullPointerException success) { |
547 |
< |
} |
691 |
< |
catch (Exception ex) { |
692 |
< |
unexpectedException(); |
545 |
> |
} catch (NullPointerException success) { |
546 |
> |
} finally { |
547 |
> |
joinPool(e); |
548 |
|
} |
549 |
|
} |
550 |
|
|
553 |
|
* Blocking on submit(callable) throws InterruptedException if |
554 |
|
* caller interrupted. |
555 |
|
*/ |
556 |
< |
public void testInterruptedSubmit() { |
556 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
557 |
|
final ForkJoinPool p = new ForkJoinPool(1); |
558 |
< |
Thread t = new Thread(new Runnable() { |
559 |
< |
public void run() { |
560 |
< |
try { |
561 |
< |
p.submit(new Callable<Object>() { |
562 |
< |
public Object call() { |
563 |
< |
try { |
564 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
565 |
< |
shouldThrow(); |
566 |
< |
} catch (InterruptedException e) { |
567 |
< |
} |
568 |
< |
return null; |
569 |
< |
} |
570 |
< |
}).get(); |
571 |
< |
} catch (InterruptedException success) { |
572 |
< |
} catch (Exception e) { |
573 |
< |
unexpectedException(); |
574 |
< |
} |
575 |
< |
|
721 |
< |
} |
722 |
< |
}); |
723 |
< |
try { |
724 |
< |
t.start(); |
725 |
< |
Thread.sleep(SHORT_DELAY_MS); |
726 |
< |
t.interrupt(); |
727 |
< |
} catch (Exception e) { |
728 |
< |
unexpectedException(); |
729 |
< |
} |
558 |
> |
|
559 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
560 |
> |
public void realRun() throws Throwable { |
561 |
> |
p.submit(new CheckedCallable<Object>() { |
562 |
> |
public Object realCall() throws Throwable { |
563 |
> |
try { |
564 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
565 |
> |
} catch (InterruptedException ok) { |
566 |
> |
} |
567 |
> |
return null; |
568 |
> |
}}).get(); |
569 |
> |
}}); |
570 |
> |
|
571 |
> |
t.start(); |
572 |
> |
Thread.sleep(SHORT_DELAY_MS); |
573 |
> |
t.interrupt(); |
574 |
> |
t.join(); |
575 |
> |
p.shutdownNow(); |
576 |
|
joinPool(p); |
577 |
|
} |
578 |
|
|
580 |
|
* get of submit(callable) throws ExecutionException if callable |
581 |
|
* throws exception |
582 |
|
*/ |
583 |
< |
public void testSubmitEE() { |
583 |
> |
public void testSubmitEE() throws Throwable { |
584 |
|
ForkJoinPool p = new ForkJoinPool(1); |
739 |
– |
|
585 |
|
try { |
586 |
< |
Callable c = new Callable() { |
587 |
< |
public Object call() { |
588 |
< |
int i = 5/0; |
589 |
< |
return Boolean.TRUE; |
590 |
< |
} |
746 |
< |
}; |
747 |
< |
|
748 |
< |
for (int i = 0; i < 5; i++) { |
749 |
< |
p.submit(c).get(); |
750 |
< |
} |
751 |
< |
|
586 |
> |
p.submit(new Callable() { |
587 |
> |
public Object call() { |
588 |
> |
int i = 5/0; |
589 |
> |
return Boolean.TRUE; |
590 |
> |
}}).get(); |
591 |
|
shouldThrow(); |
592 |
|
} catch (ExecutionException success) { |
593 |
< |
} catch (CancellationException success) { |
594 |
< |
} catch (Exception e) { |
595 |
< |
unexpectedException(); |
593 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
594 |
> |
} finally { |
595 |
> |
joinPool(p); |
596 |
|
} |
758 |
– |
joinPool(p); |
597 |
|
} |
598 |
|
|
599 |
|
/** |
600 |
< |
* invokeAny(null) throws NPE |
600 |
> |
* invokeAny(null) throws NullPointerException |
601 |
|
*/ |
602 |
< |
public void testInvokeAny1() { |
602 |
> |
public void testInvokeAny1() throws Throwable { |
603 |
|
ExecutorService e = new ForkJoinPool(1); |
604 |
|
try { |
605 |
|
e.invokeAny(null); |
606 |
+ |
shouldThrow(); |
607 |
|
} catch (NullPointerException success) { |
769 |
– |
} catch (Exception ex) { |
770 |
– |
unexpectedException(); |
608 |
|
} finally { |
609 |
|
joinPool(e); |
610 |
|
} |
611 |
|
} |
612 |
|
|
613 |
|
/** |
614 |
< |
* invokeAny(empty collection) throws IAE |
614 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
615 |
|
*/ |
616 |
< |
public void testInvokeAny2() { |
616 |
> |
public void testInvokeAny2() throws Throwable { |
617 |
|
ExecutorService e = new ForkJoinPool(1); |
618 |
|
try { |
619 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
620 |
+ |
shouldThrow(); |
621 |
|
} catch (IllegalArgumentException success) { |
784 |
– |
} catch (Exception ex) { |
785 |
– |
unexpectedException(); |
622 |
|
} finally { |
623 |
|
joinPool(e); |
624 |
|
} |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
< |
* invokeAny(c) throws NPE if c has null elements |
628 |
> |
* invokeAny(c) throws NullPointerException if c has a single null element |
629 |
|
*/ |
630 |
< |
public void testInvokeAny3() { |
630 |
> |
public void testInvokeAny3() throws Throwable { |
631 |
|
ExecutorService e = new ForkJoinPool(1); |
632 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
633 |
+ |
l.add(null); |
634 |
|
try { |
797 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
798 |
– |
l.add(new StringTask()); |
799 |
– |
l.add(null); |
635 |
|
e.invokeAny(l); |
636 |
+ |
shouldThrow(); |
637 |
+ |
} catch (NullPointerException success) { |
638 |
+ |
} finally { |
639 |
+ |
joinPool(e); |
640 |
+ |
} |
641 |
+ |
} |
642 |
+ |
|
643 |
+ |
/** |
644 |
+ |
* invokeAny(c) throws NullPointerException if c has null elements |
645 |
+ |
*/ |
646 |
+ |
public void testInvokeAny4() throws Throwable { |
647 |
+ |
CountDownLatch latch = new CountDownLatch(1); |
648 |
+ |
ExecutorService e = new ForkJoinPool(1); |
649 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
650 |
+ |
l.add(latchAwaitingStringTask(latch)); |
651 |
+ |
l.add(null); |
652 |
+ |
try { |
653 |
+ |
e.invokeAny(l); |
654 |
+ |
shouldThrow(); |
655 |
|
} catch (NullPointerException success) { |
802 |
– |
} catch (Exception ex) { |
803 |
– |
ex.printStackTrace(); |
804 |
– |
unexpectedException(); |
656 |
|
} finally { |
657 |
+ |
latch.countDown(); |
658 |
|
joinPool(e); |
659 |
|
} |
660 |
|
} |
662 |
|
/** |
663 |
|
* invokeAny(c) throws ExecutionException if no task in c completes |
664 |
|
*/ |
665 |
< |
public void testInvokeAny4() { |
665 |
> |
public void testInvokeAny5() throws Throwable { |
666 |
|
ExecutorService e = new ForkJoinPool(1); |
667 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
668 |
+ |
l.add(new NPETask()); |
669 |
|
try { |
816 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
817 |
– |
l.add(new NPETask()); |
670 |
|
e.invokeAny(l); |
671 |
+ |
shouldThrow(); |
672 |
|
} catch (ExecutionException success) { |
673 |
< |
} catch (CancellationException success) { |
821 |
< |
} catch (Exception ex) { |
822 |
< |
unexpectedException(); |
673 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
674 |
|
} finally { |
675 |
|
joinPool(e); |
676 |
|
} |
679 |
|
/** |
680 |
|
* invokeAny(c) returns result of some task in c if at least one completes |
681 |
|
*/ |
682 |
< |
public void testInvokeAny5() { |
682 |
> |
public void testInvokeAny6() throws Throwable { |
683 |
|
ExecutorService e = new ForkJoinPool(1); |
684 |
|
try { |
685 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
685 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
686 |
|
l.add(new StringTask()); |
687 |
|
l.add(new StringTask()); |
688 |
|
String result = e.invokeAny(l); |
689 |
|
assertSame(TEST_STRING, result); |
839 |
– |
} catch (ExecutionException success) { |
840 |
– |
} catch (CancellationException success) { |
841 |
– |
} catch (Exception ex) { |
842 |
– |
unexpectedException(); |
690 |
|
} finally { |
691 |
|
joinPool(e); |
692 |
|
} |
693 |
|
} |
694 |
|
|
695 |
|
/** |
696 |
< |
* invokeAll(null) throws NPE |
696 |
> |
* invokeAll(null) throws NullPointerException |
697 |
|
*/ |
698 |
< |
public void testInvokeAll1() { |
698 |
> |
public void testInvokeAll1() throws Throwable { |
699 |
|
ExecutorService e = new ForkJoinPool(1); |
700 |
|
try { |
701 |
|
e.invokeAll(null); |
702 |
+ |
shouldThrow(); |
703 |
|
} catch (NullPointerException success) { |
856 |
– |
} catch (Exception ex) { |
857 |
– |
unexpectedException(); |
704 |
|
} finally { |
705 |
|
joinPool(e); |
706 |
|
} |
709 |
|
/** |
710 |
|
* invokeAll(empty collection) returns empty collection |
711 |
|
*/ |
712 |
< |
public void testInvokeAll2() { |
712 |
> |
public void testInvokeAll2() throws InterruptedException { |
713 |
|
ExecutorService e = new ForkJoinPool(1); |
714 |
|
try { |
715 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
715 |
> |
List<Future<String>> r |
716 |
> |
= e.invokeAll(new ArrayList<Callable<String>>()); |
717 |
|
assertTrue(r.isEmpty()); |
871 |
– |
} catch (Exception ex) { |
872 |
– |
unexpectedException(); |
718 |
|
} finally { |
719 |
|
joinPool(e); |
720 |
|
} |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
< |
* invokeAll(c) throws NPE if c has null elements |
724 |
> |
* invokeAll(c) throws NullPointerException if c has null elements |
725 |
|
*/ |
726 |
< |
public void testInvokeAll3() { |
726 |
> |
public void testInvokeAll3() throws InterruptedException { |
727 |
|
ExecutorService e = new ForkJoinPool(1); |
728 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
729 |
+ |
l.add(new StringTask()); |
730 |
+ |
l.add(null); |
731 |
|
try { |
884 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
885 |
– |
l.add(new StringTask()); |
886 |
– |
l.add(null); |
732 |
|
e.invokeAll(l); |
733 |
+ |
shouldThrow(); |
734 |
|
} catch (NullPointerException success) { |
889 |
– |
} catch (Exception ex) { |
890 |
– |
unexpectedException(); |
735 |
|
} finally { |
736 |
|
joinPool(e); |
737 |
|
} |
738 |
|
} |
739 |
|
|
740 |
|
/** |
741 |
< |
* get of returned element of invokeAll(c) throws exception on failed task |
741 |
> |
* get of returned element of invokeAll(c) throws |
742 |
> |
* ExecutionException on failed task |
743 |
|
*/ |
744 |
< |
public void testInvokeAll4() { |
744 |
> |
public void testInvokeAll4() throws Throwable { |
745 |
|
ExecutorService e = new ForkJoinPool(1); |
746 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
747 |
+ |
l.add(new NPETask()); |
748 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
749 |
+ |
assertEquals(1, futures.size()); |
750 |
|
try { |
751 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
752 |
< |
l.add(new NPETask()); |
904 |
< |
List<Future<String>> result = e.invokeAll(l); |
905 |
< |
assertEquals(1, result.size()); |
906 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
907 |
< |
it.next().get(); |
751 |
> |
futures.get(0).get(); |
752 |
> |
shouldThrow(); |
753 |
|
} catch (ExecutionException success) { |
754 |
< |
} catch (CancellationException success) { |
910 |
< |
} catch (Exception ex) { |
911 |
< |
ex.printStackTrace(); |
912 |
< |
unexpectedException(); |
754 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
755 |
|
} finally { |
756 |
|
joinPool(e); |
757 |
|
} |
760 |
|
/** |
761 |
|
* invokeAll(c) returns results of all completed tasks in c |
762 |
|
*/ |
763 |
< |
public void testInvokeAll5() { |
763 |
> |
public void testInvokeAll5() throws Throwable { |
764 |
|
ExecutorService e = new ForkJoinPool(1); |
765 |
|
try { |
766 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
766 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
767 |
|
l.add(new StringTask()); |
768 |
|
l.add(new StringTask()); |
769 |
< |
List<Future<String>> result = e.invokeAll(l); |
770 |
< |
assertEquals(2, result.size()); |
771 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
772 |
< |
assertSame(TEST_STRING, it.next().get()); |
931 |
< |
} catch (ExecutionException success) { |
932 |
< |
} catch (CancellationException success) { |
933 |
< |
} catch (Exception ex) { |
934 |
< |
ex.printStackTrace(); |
935 |
< |
unexpectedException(); |
769 |
> |
List<Future<String>> futures = e.invokeAll(l); |
770 |
> |
assertEquals(2, futures.size()); |
771 |
> |
for (Future<String> future : futures) |
772 |
> |
assertSame(TEST_STRING, future.get()); |
773 |
|
} finally { |
774 |
|
joinPool(e); |
775 |
|
} |
777 |
|
|
778 |
|
|
779 |
|
/** |
780 |
< |
* timed invokeAny(null) throws NPE |
780 |
> |
* timed invokeAny(null) throws NullPointerException |
781 |
|
*/ |
782 |
< |
public void testTimedInvokeAny1() { |
782 |
> |
public void testTimedInvokeAny1() throws Throwable { |
783 |
|
ExecutorService e = new ForkJoinPool(1); |
784 |
|
try { |
785 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
786 |
+ |
shouldThrow(); |
787 |
|
} catch (NullPointerException success) { |
950 |
– |
} catch (Exception ex) { |
951 |
– |
ex.printStackTrace(); |
952 |
– |
unexpectedException(); |
788 |
|
} finally { |
789 |
|
joinPool(e); |
790 |
|
} |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
< |
* timed invokeAny(null time unit) throws NPE |
794 |
> |
* timed invokeAny(null time unit) throws NullPointerException |
795 |
|
*/ |
796 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
796 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
797 |
|
ExecutorService e = new ForkJoinPool(1); |
798 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
799 |
+ |
l.add(new StringTask()); |
800 |
|
try { |
964 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
965 |
– |
l.add(new StringTask()); |
801 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
802 |
+ |
shouldThrow(); |
803 |
|
} catch (NullPointerException success) { |
968 |
– |
} catch (Exception ex) { |
969 |
– |
ex.printStackTrace(); |
970 |
– |
unexpectedException(); |
804 |
|
} finally { |
805 |
|
joinPool(e); |
806 |
|
} |
807 |
|
} |
808 |
|
|
809 |
|
/** |
810 |
< |
* timed invokeAny(empty collection) throws IAE |
810 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
811 |
|
*/ |
812 |
< |
public void testTimedInvokeAny2() { |
812 |
> |
public void testTimedInvokeAny2() throws Throwable { |
813 |
|
ExecutorService e = new ForkJoinPool(1); |
814 |
|
try { |
815 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
815 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
816 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
817 |
> |
shouldThrow(); |
818 |
|
} catch (IllegalArgumentException success) { |
984 |
– |
} catch (Exception ex) { |
985 |
– |
ex.printStackTrace(); |
986 |
– |
unexpectedException(); |
819 |
|
} finally { |
820 |
|
joinPool(e); |
821 |
|
} |
822 |
|
} |
823 |
|
|
824 |
|
/** |
825 |
< |
* timed invokeAny(c) throws NPE if c has null elements |
825 |
> |
* timed invokeAny(c) throws NullPointerException if c has null elements |
826 |
|
*/ |
827 |
< |
public void testTimedInvokeAny3() { |
827 |
> |
public void testTimedInvokeAny3() throws Throwable { |
828 |
> |
CountDownLatch latch = new CountDownLatch(1); |
829 |
|
ExecutorService e = new ForkJoinPool(1); |
830 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
831 |
+ |
l.add(latchAwaitingStringTask(latch)); |
832 |
+ |
l.add(null); |
833 |
|
try { |
998 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
– |
l.add(new StringTask()); |
1000 |
– |
l.add(null); |
834 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
835 |
+ |
shouldThrow(); |
836 |
|
} catch (NullPointerException success) { |
1003 |
– |
} catch (Exception ex) { |
1004 |
– |
ex.printStackTrace(); |
1005 |
– |
unexpectedException(); |
837 |
|
} finally { |
838 |
+ |
latch.countDown(); |
839 |
|
joinPool(e); |
840 |
|
} |
841 |
|
} |
843 |
|
/** |
844 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
845 |
|
*/ |
846 |
< |
public void testTimedInvokeAny4() { |
846 |
> |
public void testTimedInvokeAny4() throws Throwable { |
847 |
|
ExecutorService e = new ForkJoinPool(1); |
848 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
849 |
+ |
l.add(new NPETask()); |
850 |
|
try { |
1017 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1018 |
– |
l.add(new NPETask()); |
851 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
852 |
+ |
shouldThrow(); |
853 |
|
} catch (ExecutionException success) { |
854 |
< |
} catch (CancellationException success) { |
1022 |
< |
} catch (Exception ex) { |
1023 |
< |
ex.printStackTrace(); |
1024 |
< |
unexpectedException(); |
854 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
855 |
|
} finally { |
856 |
|
joinPool(e); |
857 |
|
} |
860 |
|
/** |
861 |
|
* timed invokeAny(c) returns result of some task in c |
862 |
|
*/ |
863 |
< |
public void testTimedInvokeAny5() { |
863 |
> |
public void testTimedInvokeAny5() throws Throwable { |
864 |
|
ExecutorService e = new ForkJoinPool(1); |
865 |
|
try { |
866 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
866 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
867 |
|
l.add(new StringTask()); |
868 |
|
l.add(new StringTask()); |
869 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
870 |
|
assertSame(TEST_STRING, result); |
1041 |
– |
} catch (ExecutionException success) { |
1042 |
– |
} catch (CancellationException success) { |
1043 |
– |
} catch (Exception ex) { |
1044 |
– |
ex.printStackTrace(); |
1045 |
– |
unexpectedException(); |
871 |
|
} finally { |
872 |
|
joinPool(e); |
873 |
|
} |
874 |
|
} |
875 |
|
|
876 |
|
/** |
877 |
< |
* timed invokeAll(null) throws NPE |
877 |
> |
* timed invokeAll(null) throws NullPointerException |
878 |
|
*/ |
879 |
< |
public void testTimedInvokeAll1() { |
879 |
> |
public void testTimedInvokeAll1() throws Throwable { |
880 |
|
ExecutorService e = new ForkJoinPool(1); |
881 |
|
try { |
882 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
883 |
+ |
shouldThrow(); |
884 |
|
} catch (NullPointerException success) { |
1059 |
– |
} catch (Exception ex) { |
1060 |
– |
ex.printStackTrace(); |
1061 |
– |
unexpectedException(); |
885 |
|
} finally { |
886 |
|
joinPool(e); |
887 |
|
} |
888 |
|
} |
889 |
|
|
890 |
|
/** |
891 |
< |
* timed invokeAll(null time unit) throws NPE |
891 |
> |
* timed invokeAll(null time unit) throws NullPointerException |
892 |
|
*/ |
893 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
893 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
894 |
|
ExecutorService e = new ForkJoinPool(1); |
895 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
896 |
+ |
l.add(new StringTask()); |
897 |
|
try { |
1073 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1074 |
– |
l.add(new StringTask()); |
898 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
899 |
+ |
shouldThrow(); |
900 |
|
} catch (NullPointerException success) { |
1077 |
– |
} catch (Exception ex) { |
1078 |
– |
ex.printStackTrace(); |
1079 |
– |
unexpectedException(); |
901 |
|
} finally { |
902 |
|
joinPool(e); |
903 |
|
} |
906 |
|
/** |
907 |
|
* timed invokeAll(empty collection) returns empty collection |
908 |
|
*/ |
909 |
< |
public void testTimedInvokeAll2() { |
909 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
910 |
|
ExecutorService e = new ForkJoinPool(1); |
911 |
|
try { |
912 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
912 |
> |
List<Future<String>> r |
913 |
> |
= e.invokeAll(new ArrayList<Callable<String>>(), |
914 |
> |
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
915 |
|
assertTrue(r.isEmpty()); |
1093 |
– |
} catch (Exception ex) { |
1094 |
– |
ex.printStackTrace(); |
1095 |
– |
unexpectedException(); |
916 |
|
} finally { |
917 |
|
joinPool(e); |
918 |
|
} |
919 |
|
} |
920 |
|
|
921 |
|
/** |
922 |
< |
* timed invokeAll(c) throws NPE if c has null elements |
922 |
> |
* timed invokeAll(c) throws NullPointerException if c has null elements |
923 |
|
*/ |
924 |
< |
public void testTimedInvokeAll3() { |
924 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
925 |
|
ExecutorService e = new ForkJoinPool(1); |
926 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
927 |
+ |
l.add(new StringTask()); |
928 |
+ |
l.add(null); |
929 |
|
try { |
1107 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1108 |
– |
l.add(new StringTask()); |
1109 |
– |
l.add(null); |
930 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
931 |
+ |
shouldThrow(); |
932 |
|
} catch (NullPointerException success) { |
1112 |
– |
} catch (Exception ex) { |
1113 |
– |
ex.printStackTrace(); |
1114 |
– |
unexpectedException(); |
933 |
|
} finally { |
934 |
|
joinPool(e); |
935 |
|
} |
938 |
|
/** |
939 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
940 |
|
*/ |
941 |
< |
public void testTimedInvokeAll4() { |
941 |
> |
public void testTimedInvokeAll4() throws Throwable { |
942 |
|
ExecutorService e = new ForkJoinPool(1); |
943 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
944 |
+ |
l.add(new NPETask()); |
945 |
+ |
List<Future<String>> futures |
946 |
+ |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
947 |
+ |
assertEquals(1, futures.size()); |
948 |
|
try { |
949 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
950 |
< |
l.add(new NPETask()); |
1128 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1129 |
< |
assertEquals(1, result.size()); |
1130 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1131 |
< |
it.next().get(); |
949 |
> |
futures.get(0).get(); |
950 |
> |
shouldThrow(); |
951 |
|
} catch (ExecutionException success) { |
952 |
< |
} catch (CancellationException success) { |
1134 |
< |
} catch (Exception ex) { |
1135 |
< |
ex.printStackTrace(); |
1136 |
< |
unexpectedException(); |
952 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
953 |
|
} finally { |
954 |
|
joinPool(e); |
955 |
|
} |
958 |
|
/** |
959 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
960 |
|
*/ |
961 |
< |
public void testTimedInvokeAll5() { |
961 |
> |
public void testTimedInvokeAll5() throws Throwable { |
962 |
|
ExecutorService e = new ForkJoinPool(1); |
963 |
|
try { |
964 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
964 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
965 |
|
l.add(new StringTask()); |
966 |
|
l.add(new StringTask()); |
967 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
968 |
< |
assertEquals(2, result.size()); |
969 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
970 |
< |
assertSame(TEST_STRING, it.next().get()); |
971 |
< |
} catch (ExecutionException success) { |
1156 |
< |
} catch (CancellationException success) { |
1157 |
< |
} catch (Exception ex) { |
1158 |
< |
ex.printStackTrace(); |
1159 |
< |
unexpectedException(); |
967 |
> |
List<Future<String>> futures |
968 |
> |
= e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
969 |
> |
assertEquals(2, futures.size()); |
970 |
> |
for (Future<String> future : futures) |
971 |
> |
assertSame(TEST_STRING, future.get()); |
972 |
|
} finally { |
973 |
|
joinPool(e); |
974 |
|
} |