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