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