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