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