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