author | dl |
Wed, 09 Aug 2017 17:40:38 -0700 | |
changeset 46146 | b3e220a04d3f |
parent 45937 | 646816090183 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
*/ |
|
33 |
||
34 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
35 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
|
36 |
||
37 |
import java.security.PrivilegedAction; |
|
38 |
import java.security.PrivilegedExceptionAction; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Collection; |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
41 |
import java.util.Collections; |
35394 | 42 |
import java.util.List; |
43 |
import java.util.concurrent.Callable; |
|
44 |
import java.util.concurrent.CountDownLatch; |
|
45 |
import java.util.concurrent.ExecutionException; |
|
46 |
import java.util.concurrent.Executors; |
|
47 |
import java.util.concurrent.ExecutorService; |
|
48 |
import java.util.concurrent.ForkJoinPool; |
|
49 |
import java.util.concurrent.ForkJoinTask; |
|
50 |
import java.util.concurrent.ForkJoinWorkerThread; |
|
51 |
import java.util.concurrent.Future; |
|
52 |
import java.util.concurrent.RecursiveTask; |
|
53 |
import java.util.concurrent.RejectedExecutionException; |
|
54 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
55 |
import java.util.concurrent.atomic.AtomicInteger; |
35394 | 56 |
import java.util.concurrent.locks.ReentrantLock; |
57 |
||
58 |
import junit.framework.AssertionFailedError; |
|
59 |
import junit.framework.Test; |
|
60 |
import junit.framework.TestSuite; |
|
61 |
||
62 |
public class ForkJoinPoolTest extends JSR166TestCase { |
|
63 |
public static void main(String[] args) { |
|
64 |
main(suite(), args); |
|
65 |
} |
|
66 |
||
67 |
public static Test suite() { |
|
68 |
return new TestSuite(ForkJoinPoolTest.class); |
|
69 |
} |
|
70 |
||
71 |
/* |
|
72 |
* Testing coverage notes: |
|
73 |
* |
|
74 |
* 1. shutdown and related methods are tested via super.joinPool. |
|
75 |
* |
|
76 |
* 2. newTaskFor and adapters are tested in submit/invoke tests |
|
77 |
* |
|
78 |
* 3. We cannot portably test monitoring methods such as |
|
79 |
* getStealCount() since they rely ultimately on random task |
|
80 |
* stealing that may cause tasks not to be stolen/propagated |
|
81 |
* across threads, especially on uniprocessors. |
|
82 |
* |
|
83 |
* 4. There are no independently testable ForkJoinWorkerThread |
|
84 |
* methods, but they are covered here and in task tests. |
|
85 |
*/ |
|
86 |
||
87 |
// Some classes to test extension and factory methods |
|
88 |
||
89 |
static class MyError extends Error {} |
|
90 |
||
91 |
// to test handlers |
|
92 |
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
|
93 |
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
|
94 |
protected void onStart() { super.onStart(); throw new MyError(); } |
|
95 |
} |
|
96 |
||
97 |
static class FailingThreadFactory |
|
98 |
implements ForkJoinPool.ForkJoinWorkerThreadFactory { |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
99 |
final AtomicInteger calls = new AtomicInteger(0); |
35394 | 100 |
public ForkJoinWorkerThread newThread(ForkJoinPool p) { |
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
35394
diff
changeset
|
101 |
if (calls.incrementAndGet() > 1) return null; |
35394 | 102 |
return new FailingFJWSubclass(p); |
103 |
} |
|
104 |
} |
|
105 |
||
106 |
static class SubFJP extends ForkJoinPool { // to expose protected |
|
107 |
SubFJP() { super(1); } |
|
108 |
public int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
|
109 |
return super.drainTasksTo(c); |
|
110 |
} |
|
111 |
public ForkJoinTask<?> pollSubmission() { |
|
112 |
return super.pollSubmission(); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
static class ManagedLocker implements ForkJoinPool.ManagedBlocker { |
|
117 |
final ReentrantLock lock; |
|
118 |
boolean hasLock = false; |
|
119 |
ManagedLocker(ReentrantLock lock) { this.lock = lock; } |
|
120 |
public boolean block() { |
|
121 |
if (!hasLock) |
|
122 |
lock.lock(); |
|
123 |
return true; |
|
124 |
} |
|
125 |
public boolean isReleasable() { |
|
126 |
return hasLock || (hasLock = lock.tryLock()); |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
// A simple recursive task for testing |
|
131 |
static final class FibTask extends RecursiveTask<Integer> { |
|
132 |
final int number; |
|
133 |
FibTask(int n) { number = n; } |
|
134 |
protected Integer compute() { |
|
135 |
int n = number; |
|
136 |
if (n <= 1) |
|
137 |
return n; |
|
138 |
FibTask f1 = new FibTask(n - 1); |
|
139 |
f1.fork(); |
|
140 |
return (new FibTask(n - 2)).compute() + f1.join(); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
// A failing task for testing |
|
145 |
static final class FailingTask extends ForkJoinTask<Void> { |
|
146 |
public final Void getRawResult() { return null; } |
|
147 |
protected final void setRawResult(Void mustBeNull) { } |
|
148 |
protected final boolean exec() { throw new Error(); } |
|
149 |
FailingTask() {} |
|
150 |
} |
|
151 |
||
152 |
// Fib needlessly using locking to test ManagedBlockers |
|
153 |
static final class LockingFibTask extends RecursiveTask<Integer> { |
|
154 |
final int number; |
|
155 |
final ManagedLocker locker; |
|
156 |
final ReentrantLock lock; |
|
157 |
LockingFibTask(int n, ManagedLocker locker, ReentrantLock lock) { |
|
158 |
number = n; |
|
159 |
this.locker = locker; |
|
160 |
this.lock = lock; |
|
161 |
} |
|
162 |
protected Integer compute() { |
|
163 |
int n; |
|
164 |
LockingFibTask f1 = null; |
|
165 |
LockingFibTask f2 = null; |
|
166 |
locker.block(); |
|
167 |
n = number; |
|
168 |
if (n > 1) { |
|
169 |
f1 = new LockingFibTask(n - 1, locker, lock); |
|
170 |
f2 = new LockingFibTask(n - 2, locker, lock); |
|
171 |
} |
|
172 |
lock.unlock(); |
|
173 |
if (n <= 1) |
|
174 |
return n; |
|
175 |
else { |
|
176 |
f1.fork(); |
|
177 |
return f2.compute() + f1.join(); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* Successfully constructed pool reports default factory, |
|
184 |
* parallelism and async mode policies, no active threads or |
|
185 |
* tasks, and quiescent running state. |
|
186 |
*/ |
|
187 |
public void testDefaultInitialState() { |
|
188 |
ForkJoinPool p = new ForkJoinPool(1); |
|
189 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
190 |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
|
191 |
p.getFactory()); |
|
192 |
assertFalse(p.getAsyncMode()); |
|
193 |
assertEquals(0, p.getActiveThreadCount()); |
|
194 |
assertEquals(0, p.getStealCount()); |
|
195 |
assertEquals(0, p.getQueuedTaskCount()); |
|
196 |
assertEquals(0, p.getQueuedSubmissionCount()); |
|
197 |
assertFalse(p.hasQueuedSubmissions()); |
|
198 |
assertFalse(p.isShutdown()); |
|
199 |
assertFalse(p.isTerminating()); |
|
200 |
assertFalse(p.isTerminated()); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Constructor throws if size argument is less than zero |
|
206 |
*/ |
|
207 |
public void testConstructor1() { |
|
208 |
try { |
|
209 |
new ForkJoinPool(-1); |
|
210 |
shouldThrow(); |
|
211 |
} catch (IllegalArgumentException success) {} |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Constructor throws if factory argument is null |
|
216 |
*/ |
|
217 |
public void testConstructor2() { |
|
218 |
try { |
|
219 |
new ForkJoinPool(1, null, null, false); |
|
220 |
shouldThrow(); |
|
221 |
} catch (NullPointerException success) {} |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* getParallelism returns size set in constructor |
|
226 |
*/ |
|
227 |
public void testGetParallelism() { |
|
228 |
ForkJoinPool p = new ForkJoinPool(1); |
|
229 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
230 |
assertEquals(1, p.getParallelism()); |
|
231 |
} |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* getPoolSize returns number of started workers. |
|
236 |
*/ |
|
237 |
public void testGetPoolSize() { |
|
238 |
final CountDownLatch taskStarted = new CountDownLatch(1); |
|
239 |
final CountDownLatch done = new CountDownLatch(1); |
|
240 |
final ForkJoinPool p = new ForkJoinPool(1); |
|
241 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
242 |
assertEquals(0, p.getActiveThreadCount()); |
|
243 |
final Runnable task = new CheckedRunnable() { |
|
244 |
public void realRun() throws InterruptedException { |
|
245 |
taskStarted.countDown(); |
|
246 |
assertEquals(1, p.getPoolSize()); |
|
247 |
assertEquals(1, p.getActiveThreadCount()); |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
248 |
await(done); |
35394 | 249 |
}}; |
250 |
Future<?> future = p.submit(task); |
|
251 |
await(taskStarted); |
|
252 |
assertEquals(1, p.getPoolSize()); |
|
253 |
assertEquals(1, p.getActiveThreadCount()); |
|
254 |
done.countDown(); |
|
255 |
} |
|
256 |
assertEquals(0, p.getPoolSize()); |
|
257 |
assertEquals(0, p.getActiveThreadCount()); |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* awaitTermination on a non-shutdown pool times out |
|
262 |
*/ |
|
263 |
public void testAwaitTermination_timesOut() throws InterruptedException { |
|
264 |
ForkJoinPool p = new ForkJoinPool(1); |
|
265 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
266 |
assertFalse(p.isTerminated()); |
|
267 |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
|
268 |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
|
269 |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
|
270 |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
271 |
assertFalse(p.awaitTermination(randomExpiredTimeout(), |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
272 |
randomTimeUnit())); |
35394 | 273 |
long timeoutNanos = 999999L; |
274 |
long startTime = System.nanoTime(); |
|
275 |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
|
276 |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
|
277 |
assertFalse(p.isTerminated()); |
|
278 |
startTime = System.nanoTime(); |
|
279 |
long timeoutMillis = timeoutMillis(); |
|
280 |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
|
281 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
|
282 |
assertFalse(p.isTerminated()); |
|
283 |
p.shutdown(); |
|
284 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
|
285 |
assertTrue(p.isTerminated()); |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
/** |
|
290 |
* setUncaughtExceptionHandler changes handler for uncaught exceptions. |
|
291 |
* |
|
292 |
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
|
293 |
* performs its defined action |
|
294 |
*/ |
|
295 |
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
|
296 |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
|
297 |
final Thread.UncaughtExceptionHandler ueh = |
|
298 |
new Thread.UncaughtExceptionHandler() { |
|
299 |
public void uncaughtException(Thread t, Throwable e) { |
|
300 |
threadAssertTrue(e instanceof MyError); |
|
301 |
threadAssertTrue(t instanceof FailingFJWSubclass); |
|
302 |
uehInvoked.countDown(); |
|
303 |
}}; |
|
304 |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
|
305 |
ueh, false); |
|
306 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
307 |
assertSame(ueh, p.getUncaughtExceptionHandler()); |
|
308 |
try { |
|
309 |
p.execute(new FibTask(8)); |
|
310 |
await(uehInvoked); |
|
311 |
} finally { |
|
312 |
p.shutdownNow(); // failure might have prevented processing task |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
/** |
|
318 |
* After invoking a single task, isQuiescent eventually becomes |
|
319 |
* true, at which time queues are empty, threads are not active, |
|
320 |
* the task has completed successfully, and construction |
|
321 |
* parameters continue to hold |
|
322 |
*/ |
|
323 |
public void testIsQuiescent() throws Exception { |
|
324 |
ForkJoinPool p = new ForkJoinPool(2); |
|
325 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
326 |
assertTrue(p.isQuiescent()); |
|
327 |
long startTime = System.nanoTime(); |
|
328 |
FibTask f = new FibTask(20); |
|
329 |
p.invoke(f); |
|
330 |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
|
331 |
p.getFactory()); |
|
332 |
while (! p.isQuiescent()) { |
|
333 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
|
334 |
throw new AssertionFailedError("timed out"); |
|
335 |
assertFalse(p.getAsyncMode()); |
|
336 |
assertFalse(p.isShutdown()); |
|
337 |
assertFalse(p.isTerminating()); |
|
338 |
assertFalse(p.isTerminated()); |
|
339 |
Thread.yield(); |
|
340 |
} |
|
341 |
||
342 |
assertTrue(p.isQuiescent()); |
|
343 |
assertFalse(p.getAsyncMode()); |
|
344 |
assertEquals(0, p.getQueuedTaskCount()); |
|
345 |
assertEquals(0, p.getQueuedSubmissionCount()); |
|
346 |
assertFalse(p.hasQueuedSubmissions()); |
|
347 |
while (p.getActiveThreadCount() != 0 |
|
348 |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
|
349 |
Thread.yield(); |
|
350 |
assertFalse(p.isShutdown()); |
|
351 |
assertFalse(p.isTerminating()); |
|
352 |
assertFalse(p.isTerminated()); |
|
353 |
assertTrue(f.isDone()); |
|
354 |
assertEquals(6765, (int) f.get()); |
|
355 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
356 |
} |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* Completed submit(ForkJoinTask) returns result |
|
361 |
*/ |
|
362 |
public void testSubmitForkJoinTask() throws Throwable { |
|
363 |
ForkJoinPool p = new ForkJoinPool(1); |
|
364 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
365 |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
|
366 |
assertEquals(21, (int) f.get()); |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
/** |
|
371 |
* A task submitted after shutdown is rejected |
|
372 |
*/ |
|
373 |
public void testSubmitAfterShutdown() { |
|
374 |
ForkJoinPool p = new ForkJoinPool(1); |
|
375 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
376 |
p.shutdown(); |
|
377 |
assertTrue(p.isShutdown()); |
|
378 |
try { |
|
379 |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
|
380 |
shouldThrow(); |
|
381 |
} catch (RejectedExecutionException success) {} |
|
382 |
} |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Pool maintains parallelism when using ManagedBlocker |
|
387 |
*/ |
|
388 |
public void testBlockingForkJoinTask() throws Throwable { |
|
389 |
ForkJoinPool p = new ForkJoinPool(4); |
|
390 |
try { |
|
391 |
ReentrantLock lock = new ReentrantLock(); |
|
392 |
ManagedLocker locker = new ManagedLocker(lock); |
|
393 |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
|
394 |
p.execute(f); |
|
395 |
assertEquals(6765, (int) f.get()); |
|
396 |
} finally { |
|
397 |
p.shutdownNow(); // don't wait out shutdown |
|
398 |
} |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* pollSubmission returns unexecuted submitted task, if present |
|
403 |
*/ |
|
404 |
public void testPollSubmission() { |
|
405 |
final CountDownLatch done = new CountDownLatch(1); |
|
406 |
SubFJP p = new SubFJP(); |
|
407 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
408 |
ForkJoinTask a = p.submit(awaiter(done)); |
|
409 |
ForkJoinTask b = p.submit(awaiter(done)); |
|
410 |
ForkJoinTask c = p.submit(awaiter(done)); |
|
411 |
ForkJoinTask r = p.pollSubmission(); |
|
412 |
assertTrue(r == a || r == b || r == c); |
|
413 |
assertFalse(r.isDone()); |
|
414 |
done.countDown(); |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
/** |
|
419 |
* drainTasksTo transfers unexecuted submitted tasks, if present |
|
420 |
*/ |
|
421 |
public void testDrainTasksTo() { |
|
422 |
final CountDownLatch done = new CountDownLatch(1); |
|
423 |
SubFJP p = new SubFJP(); |
|
424 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
425 |
ForkJoinTask a = p.submit(awaiter(done)); |
|
426 |
ForkJoinTask b = p.submit(awaiter(done)); |
|
427 |
ForkJoinTask c = p.submit(awaiter(done)); |
|
428 |
ArrayList<ForkJoinTask> al = new ArrayList(); |
|
429 |
p.drainTasksTo(al); |
|
430 |
assertTrue(al.size() > 0); |
|
431 |
for (ForkJoinTask r : al) { |
|
432 |
assertTrue(r == a || r == b || r == c); |
|
433 |
assertFalse(r.isDone()); |
|
434 |
} |
|
435 |
done.countDown(); |
|
436 |
} |
|
437 |
} |
|
438 |
||
439 |
// FJ Versions of AbstractExecutorService tests |
|
440 |
||
441 |
/** |
|
442 |
* execute(runnable) runs it to completion |
|
443 |
*/ |
|
444 |
public void testExecuteRunnable() throws Throwable { |
|
445 |
ExecutorService e = new ForkJoinPool(1); |
|
446 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
447 |
final AtomicBoolean done = new AtomicBoolean(false); |
|
448 |
Future<?> future = e.submit(new CheckedRunnable() { |
|
449 |
public void realRun() { |
|
450 |
done.set(true); |
|
451 |
}}); |
|
452 |
assertNull(future.get()); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
453 |
assertNull(future.get(randomExpiredTimeout(), randomTimeUnit())); |
35394 | 454 |
assertTrue(done.get()); |
455 |
assertTrue(future.isDone()); |
|
456 |
assertFalse(future.isCancelled()); |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
/** |
|
461 |
* Completed submit(callable) returns result |
|
462 |
*/ |
|
463 |
public void testSubmitCallable() throws Throwable { |
|
464 |
ExecutorService e = new ForkJoinPool(1); |
|
465 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
466 |
Future<String> future = e.submit(new StringTask()); |
|
467 |
assertSame(TEST_STRING, future.get()); |
|
468 |
assertTrue(future.isDone()); |
|
469 |
assertFalse(future.isCancelled()); |
|
470 |
} |
|
471 |
} |
|
472 |
||
473 |
/** |
|
474 |
* Completed submit(runnable) returns successfully |
|
475 |
*/ |
|
476 |
public void testSubmitRunnable() throws Throwable { |
|
477 |
ExecutorService e = new ForkJoinPool(1); |
|
478 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
479 |
Future<?> future = e.submit(new NoOpRunnable()); |
|
480 |
assertNull(future.get()); |
|
481 |
assertTrue(future.isDone()); |
|
482 |
assertFalse(future.isCancelled()); |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
/** |
|
487 |
* Completed submit(runnable, result) returns result |
|
488 |
*/ |
|
489 |
public void testSubmitRunnable2() throws Throwable { |
|
490 |
ExecutorService e = new ForkJoinPool(1); |
|
491 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
492 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
|
493 |
assertSame(TEST_STRING, future.get()); |
|
494 |
assertTrue(future.isDone()); |
|
495 |
assertFalse(future.isCancelled()); |
|
496 |
} |
|
497 |
} |
|
498 |
||
499 |
/** |
|
500 |
* A submitted privileged action runs to completion |
|
501 |
*/ |
|
502 |
public void testSubmitPrivilegedAction() throws Exception { |
|
503 |
final Callable callable = Executors.callable(new PrivilegedAction() { |
|
504 |
public Object run() { return TEST_STRING; }}); |
|
505 |
Runnable r = new CheckedRunnable() { |
|
506 |
public void realRun() throws Exception { |
|
507 |
ExecutorService e = new ForkJoinPool(1); |
|
508 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
509 |
Future future = e.submit(callable); |
|
510 |
assertSame(TEST_STRING, future.get()); |
|
511 |
} |
|
512 |
}}; |
|
513 |
||
514 |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
|
515 |
} |
|
516 |
||
517 |
/** |
|
518 |
* A submitted privileged exception action runs to completion |
|
519 |
*/ |
|
520 |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
|
521 |
final Callable callable = |
|
522 |
Executors.callable(new PrivilegedExceptionAction() { |
|
523 |
public Object run() { return TEST_STRING; }}); |
|
524 |
Runnable r = new CheckedRunnable() { |
|
525 |
public void realRun() throws Exception { |
|
526 |
ExecutorService e = new ForkJoinPool(1); |
|
527 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
528 |
Future future = e.submit(callable); |
|
529 |
assertSame(TEST_STRING, future.get()); |
|
530 |
} |
|
531 |
}}; |
|
532 |
||
533 |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* A submitted failed privileged exception action reports exception |
|
538 |
*/ |
|
539 |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
|
540 |
final Callable callable = |
|
541 |
Executors.callable(new PrivilegedExceptionAction() { |
|
542 |
public Object run() { throw new IndexOutOfBoundsException(); }}); |
|
543 |
Runnable r = new CheckedRunnable() { |
|
544 |
public void realRun() throws Exception { |
|
545 |
ExecutorService e = new ForkJoinPool(1); |
|
546 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
547 |
Future future = e.submit(callable); |
|
548 |
try { |
|
549 |
future.get(); |
|
550 |
shouldThrow(); |
|
551 |
} catch (ExecutionException success) { |
|
552 |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
|
553 |
} |
|
554 |
} |
|
555 |
}}; |
|
556 |
||
557 |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
|
558 |
} |
|
559 |
||
560 |
/** |
|
561 |
* execute(null runnable) throws NullPointerException |
|
562 |
*/ |
|
563 |
public void testExecuteNullRunnable() { |
|
564 |
ExecutorService e = new ForkJoinPool(1); |
|
565 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
566 |
try { |
|
567 |
Future<?> future = e.submit((Runnable) null); |
|
568 |
shouldThrow(); |
|
569 |
} catch (NullPointerException success) {} |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
/** |
|
574 |
* submit(null callable) throws NullPointerException |
|
575 |
*/ |
|
576 |
public void testSubmitNullCallable() { |
|
577 |
ExecutorService e = new ForkJoinPool(1); |
|
578 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
579 |
try { |
|
580 |
Future<String> future = e.submit((Callable) null); |
|
581 |
shouldThrow(); |
|
582 |
} catch (NullPointerException success) {} |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
/** |
|
587 |
* submit(callable).get() throws InterruptedException if interrupted |
|
588 |
*/ |
|
589 |
public void testInterruptedSubmit() throws InterruptedException { |
|
590 |
final CountDownLatch submitted = new CountDownLatch(1); |
|
591 |
final CountDownLatch quittingTime = new CountDownLatch(1); |
|
592 |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
|
593 |
public Void realCall() throws InterruptedException { |
|
594 |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
|
595 |
return null; |
|
596 |
}}; |
|
597 |
final ExecutorService p = new ForkJoinPool(1); |
|
598 |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
|
599 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
|
600 |
public void realRun() throws Exception { |
|
601 |
Future<Void> future = p.submit(awaiter); |
|
602 |
submitted.countDown(); |
|
603 |
future.get(); |
|
604 |
}}); |
|
605 |
t.start(); |
|
606 |
await(submitted); |
|
607 |
t.interrupt(); |
|
608 |
awaitTermination(t); |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
/** |
|
613 |
* get of submit(callable) throws ExecutionException if callable |
|
614 |
* throws exception |
|
615 |
*/ |
|
616 |
public void testSubmitEE() throws Throwable { |
|
617 |
ForkJoinPool p = new ForkJoinPool(1); |
|
618 |
try (PoolCleaner cleaner = cleaner(p)) { |
|
619 |
try { |
|
620 |
p.submit(new Callable() { |
|
621 |
public Object call() { throw new ArithmeticException(); }}) |
|
622 |
.get(); |
|
623 |
shouldThrow(); |
|
624 |
} catch (ExecutionException success) { |
|
625 |
assertTrue(success.getCause() instanceof ArithmeticException); |
|
626 |
} |
|
627 |
} |
|
628 |
} |
|
629 |
||
630 |
/** |
|
631 |
* invokeAny(null) throws NullPointerException |
|
632 |
*/ |
|
633 |
public void testInvokeAny1() throws Throwable { |
|
634 |
ExecutorService e = new ForkJoinPool(1); |
|
635 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
636 |
try { |
|
637 |
e.invokeAny(null); |
|
638 |
shouldThrow(); |
|
639 |
} catch (NullPointerException success) {} |
|
640 |
} |
|
641 |
} |
|
642 |
||
643 |
/** |
|
644 |
* invokeAny(empty collection) throws IllegalArgumentException |
|
645 |
*/ |
|
646 |
public void testInvokeAny2() throws Throwable { |
|
647 |
ExecutorService e = new ForkJoinPool(1); |
|
648 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
649 |
try { |
|
650 |
e.invokeAny(new ArrayList<Callable<String>>()); |
|
651 |
shouldThrow(); |
|
652 |
} catch (IllegalArgumentException success) {} |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
/** |
|
657 |
* invokeAny(c) throws NullPointerException if c has a single null element |
|
658 |
*/ |
|
659 |
public void testInvokeAny3() throws Throwable { |
|
660 |
ExecutorService e = new ForkJoinPool(1); |
|
661 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
662 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 663 |
l.add(null); |
664 |
try { |
|
665 |
e.invokeAny(l); |
|
666 |
shouldThrow(); |
|
667 |
} catch (NullPointerException success) {} |
|
668 |
} |
|
669 |
} |
|
670 |
||
671 |
/** |
|
672 |
* invokeAny(c) throws NullPointerException if c has null elements |
|
673 |
*/ |
|
674 |
public void testInvokeAny4() throws Throwable { |
|
675 |
CountDownLatch latch = new CountDownLatch(1); |
|
676 |
ExecutorService e = new ForkJoinPool(1); |
|
677 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
678 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 679 |
l.add(latchAwaitingStringTask(latch)); |
680 |
l.add(null); |
|
681 |
try { |
|
682 |
e.invokeAny(l); |
|
683 |
shouldThrow(); |
|
684 |
} catch (NullPointerException success) {} |
|
685 |
latch.countDown(); |
|
686 |
} |
|
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* invokeAny(c) throws ExecutionException if no task in c completes |
|
691 |
*/ |
|
692 |
public void testInvokeAny5() throws Throwable { |
|
693 |
ExecutorService e = new ForkJoinPool(1); |
|
694 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
695 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 696 |
l.add(new NPETask()); |
697 |
try { |
|
698 |
e.invokeAny(l); |
|
699 |
shouldThrow(); |
|
700 |
} catch (ExecutionException success) { |
|
701 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
702 |
} |
|
703 |
} |
|
704 |
} |
|
705 |
||
706 |
/** |
|
707 |
* invokeAny(c) returns result of some task in c if at least one completes |
|
708 |
*/ |
|
709 |
public void testInvokeAny6() throws Throwable { |
|
710 |
ExecutorService e = new ForkJoinPool(1); |
|
711 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
712 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 713 |
l.add(new StringTask()); |
714 |
l.add(new StringTask()); |
|
715 |
String result = e.invokeAny(l); |
|
716 |
assertSame(TEST_STRING, result); |
|
717 |
} |
|
718 |
} |
|
719 |
||
720 |
/** |
|
721 |
* invokeAll(null) throws NullPointerException |
|
722 |
*/ |
|
723 |
public void testInvokeAll1() throws Throwable { |
|
724 |
ExecutorService e = new ForkJoinPool(1); |
|
725 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
726 |
try { |
|
727 |
e.invokeAll(null); |
|
728 |
shouldThrow(); |
|
729 |
} catch (NullPointerException success) {} |
|
730 |
} |
|
731 |
} |
|
732 |
||
733 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
734 |
* invokeAll(empty collection) returns empty list |
35394 | 735 |
*/ |
736 |
public void testInvokeAll2() throws InterruptedException { |
|
737 |
ExecutorService e = new ForkJoinPool(1); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
738 |
final Collection<Callable<String>> emptyCollection |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
739 |
= Collections.emptyList(); |
35394 | 740 |
try (PoolCleaner cleaner = cleaner(e)) { |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
741 |
List<Future<String>> r = e.invokeAll(emptyCollection); |
35394 | 742 |
assertTrue(r.isEmpty()); |
743 |
} |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* invokeAll(c) throws NullPointerException if c has null elements |
|
748 |
*/ |
|
749 |
public void testInvokeAll3() throws InterruptedException { |
|
750 |
ExecutorService e = new ForkJoinPool(1); |
|
751 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
752 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 753 |
l.add(new StringTask()); |
754 |
l.add(null); |
|
755 |
try { |
|
756 |
e.invokeAll(l); |
|
757 |
shouldThrow(); |
|
758 |
} catch (NullPointerException success) {} |
|
759 |
} |
|
760 |
} |
|
761 |
||
762 |
/** |
|
763 |
* get of returned element of invokeAll(c) throws |
|
764 |
* ExecutionException on failed task |
|
765 |
*/ |
|
766 |
public void testInvokeAll4() throws Throwable { |
|
767 |
ExecutorService e = new ForkJoinPool(1); |
|
768 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
769 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 770 |
l.add(new NPETask()); |
771 |
List<Future<String>> futures = e.invokeAll(l); |
|
772 |
assertEquals(1, futures.size()); |
|
773 |
try { |
|
774 |
futures.get(0).get(); |
|
775 |
shouldThrow(); |
|
776 |
} catch (ExecutionException success) { |
|
777 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
778 |
} |
|
779 |
} |
|
780 |
} |
|
781 |
||
782 |
/** |
|
783 |
* invokeAll(c) returns results of all completed tasks in c |
|
784 |
*/ |
|
785 |
public void testInvokeAll5() throws Throwable { |
|
786 |
ExecutorService e = new ForkJoinPool(1); |
|
787 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
788 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 789 |
l.add(new StringTask()); |
790 |
l.add(new StringTask()); |
|
791 |
List<Future<String>> futures = e.invokeAll(l); |
|
792 |
assertEquals(2, futures.size()); |
|
793 |
for (Future<String> future : futures) |
|
794 |
assertSame(TEST_STRING, future.get()); |
|
795 |
} |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* timed invokeAny(null) throws NullPointerException |
|
800 |
*/ |
|
801 |
public void testTimedInvokeAny1() throws Throwable { |
|
802 |
ExecutorService e = new ForkJoinPool(1); |
|
803 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
804 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
805 |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
35394 | 806 |
shouldThrow(); |
807 |
} catch (NullPointerException success) {} |
|
808 |
} |
|
809 |
} |
|
810 |
||
811 |
/** |
|
812 |
* timed invokeAny(null time unit) throws NullPointerException |
|
813 |
*/ |
|
814 |
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
|
815 |
ExecutorService e = new ForkJoinPool(1); |
|
816 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
817 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 818 |
l.add(new StringTask()); |
819 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
820 |
e.invokeAny(l, randomTimeout(), null); |
35394 | 821 |
shouldThrow(); |
822 |
} catch (NullPointerException success) {} |
|
823 |
} |
|
824 |
} |
|
825 |
||
826 |
/** |
|
827 |
* timed invokeAny(empty collection) throws IllegalArgumentException |
|
828 |
*/ |
|
829 |
public void testTimedInvokeAny2() throws Throwable { |
|
830 |
ExecutorService e = new ForkJoinPool(1); |
|
831 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
832 |
try { |
|
833 |
e.invokeAny(new ArrayList<Callable<String>>(), |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
834 |
randomTimeout(), randomTimeUnit()); |
35394 | 835 |
shouldThrow(); |
836 |
} catch (IllegalArgumentException success) {} |
|
837 |
} |
|
838 |
} |
|
839 |
||
840 |
/** |
|
841 |
* timed invokeAny(c) throws NullPointerException if c has null elements |
|
842 |
*/ |
|
843 |
public void testTimedInvokeAny3() throws Throwable { |
|
844 |
CountDownLatch latch = new CountDownLatch(1); |
|
845 |
ExecutorService e = new ForkJoinPool(1); |
|
846 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
847 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 848 |
l.add(latchAwaitingStringTask(latch)); |
849 |
l.add(null); |
|
850 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
851 |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
35394 | 852 |
shouldThrow(); |
853 |
} catch (NullPointerException success) {} |
|
854 |
latch.countDown(); |
|
855 |
} |
|
856 |
} |
|
857 |
||
858 |
/** |
|
859 |
* timed invokeAny(c) throws ExecutionException if no task completes |
|
860 |
*/ |
|
861 |
public void testTimedInvokeAny4() throws Throwable { |
|
862 |
ExecutorService e = new ForkJoinPool(1); |
|
863 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
864 |
long startTime = System.nanoTime(); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
865 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 866 |
l.add(new NPETask()); |
867 |
try { |
|
868 |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
|
869 |
shouldThrow(); |
|
870 |
} catch (ExecutionException success) { |
|
871 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
872 |
} |
|
873 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
874 |
} |
|
875 |
} |
|
876 |
||
877 |
/** |
|
878 |
* timed invokeAny(c) returns result of some task in c |
|
879 |
*/ |
|
880 |
public void testTimedInvokeAny5() throws Throwable { |
|
881 |
ExecutorService e = new ForkJoinPool(1); |
|
882 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
883 |
long startTime = System.nanoTime(); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
884 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 885 |
l.add(new StringTask()); |
886 |
l.add(new StringTask()); |
|
887 |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
|
888 |
assertSame(TEST_STRING, result); |
|
889 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
890 |
} |
|
891 |
} |
|
892 |
||
893 |
/** |
|
894 |
* timed invokeAll(null) throws NullPointerException |
|
895 |
*/ |
|
896 |
public void testTimedInvokeAll1() throws Throwable { |
|
897 |
ExecutorService e = new ForkJoinPool(1); |
|
898 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
899 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
900 |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
35394 | 901 |
shouldThrow(); |
902 |
} catch (NullPointerException success) {} |
|
903 |
} |
|
904 |
} |
|
905 |
||
906 |
/** |
|
907 |
* timed invokeAll(null time unit) throws NullPointerException |
|
908 |
*/ |
|
909 |
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
|
910 |
ExecutorService e = new ForkJoinPool(1); |
|
911 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
912 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 913 |
l.add(new StringTask()); |
914 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
915 |
e.invokeAll(l, randomTimeout(), null); |
35394 | 916 |
shouldThrow(); |
917 |
} catch (NullPointerException success) {} |
|
918 |
} |
|
919 |
} |
|
920 |
||
921 |
/** |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
922 |
* timed invokeAll(empty collection) returns empty list |
35394 | 923 |
*/ |
924 |
public void testTimedInvokeAll2() throws InterruptedException { |
|
925 |
ExecutorService e = new ForkJoinPool(1); |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
926 |
final Collection<Callable<String>> emptyCollection |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
927 |
= Collections.emptyList(); |
35394 | 928 |
try (PoolCleaner cleaner = cleaner(e)) { |
929 |
List<Future<String>> r |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
930 |
= e.invokeAll(emptyCollection, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
931 |
randomTimeout(), randomTimeUnit()); |
35394 | 932 |
assertTrue(r.isEmpty()); |
933 |
} |
|
934 |
} |
|
935 |
||
936 |
/** |
|
937 |
* timed invokeAll(c) throws NullPointerException if c has null elements |
|
938 |
*/ |
|
939 |
public void testTimedInvokeAll3() throws InterruptedException { |
|
940 |
ExecutorService e = new ForkJoinPool(1); |
|
941 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
942 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 943 |
l.add(new StringTask()); |
944 |
l.add(null); |
|
945 |
try { |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
946 |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
35394 | 947 |
shouldThrow(); |
948 |
} catch (NullPointerException success) {} |
|
949 |
} |
|
950 |
} |
|
951 |
||
952 |
/** |
|
953 |
* get of returned element of invokeAll(c) throws exception on failed task |
|
954 |
*/ |
|
955 |
public void testTimedInvokeAll4() throws Throwable { |
|
956 |
ExecutorService e = new ForkJoinPool(1); |
|
957 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
958 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 959 |
l.add(new NPETask()); |
960 |
List<Future<String>> futures |
|
961 |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
|
962 |
assertEquals(1, futures.size()); |
|
963 |
try { |
|
964 |
futures.get(0).get(); |
|
965 |
shouldThrow(); |
|
966 |
} catch (ExecutionException success) { |
|
967 |
assertTrue(success.getCause() instanceof NullPointerException); |
|
968 |
} |
|
969 |
} |
|
970 |
} |
|
971 |
||
972 |
/** |
|
973 |
* timed invokeAll(c) returns results of all completed tasks in c |
|
974 |
*/ |
|
975 |
public void testTimedInvokeAll5() throws Throwable { |
|
976 |
ForkJoinPool e = new ForkJoinPool(1); |
|
977 |
try (PoolCleaner cleaner = cleaner(e)) { |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
41131
diff
changeset
|
978 |
List<Callable<String>> l = new ArrayList<>(); |
35394 | 979 |
l.add(new StringTask()); |
980 |
l.add(new StringTask()); |
|
981 |
List<Future<String>> futures |
|
982 |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
|
983 |
assertEquals(2, futures.size()); |
|
984 |
for (Future<String> future : futures) |
|
985 |
assertSame(TEST_STRING, future.get()); |
|
986 |
} |
|
987 |
} |
|
988 |
||
989 |
} |