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