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