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