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