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.69 |
final CountDownLatch taskStarted = new CountDownLatch(1); |
217 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
218 |
|
|
final ForkJoinPool p = new ForkJoinPool(1); |
219 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(p)) { |
220 |
jsr166 |
1.26 |
assertEquals(0, p.getActiveThreadCount()); |
221 |
jsr166 |
1.69 |
final Runnable task = new CheckedRunnable() { |
222 |
|
|
public void realRun() throws InterruptedException { |
223 |
|
|
taskStarted.countDown(); |
224 |
|
|
assertEquals(1, p.getPoolSize()); |
225 |
|
|
assertEquals(1, p.getActiveThreadCount()); |
226 |
|
|
done.await(); |
227 |
|
|
}}; |
228 |
|
|
Future<?> future = p.submit(task); |
229 |
|
|
await(taskStarted); |
230 |
jsr166 |
1.26 |
assertEquals(1, p.getPoolSize()); |
231 |
jsr166 |
1.69 |
assertEquals(1, p.getActiveThreadCount()); |
232 |
|
|
done.countDown(); |
233 |
dl |
1.1 |
} |
234 |
jsr166 |
1.69 |
assertEquals(0, p.getPoolSize()); |
235 |
|
|
assertEquals(0, p.getActiveThreadCount()); |
236 |
dl |
1.1 |
} |
237 |
|
|
|
238 |
jsr166 |
1.2 |
/** |
239 |
jsr166 |
1.50 |
* awaitTermination on a non-shutdown pool times out |
240 |
|
|
*/ |
241 |
|
|
public void testAwaitTermination_timesOut() throws InterruptedException { |
242 |
|
|
ForkJoinPool p = new ForkJoinPool(1); |
243 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(p)) { |
244 |
|
|
assertFalse(p.isTerminated()); |
245 |
|
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
246 |
|
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
247 |
|
|
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
248 |
|
|
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
249 |
|
|
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
250 |
|
|
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
251 |
|
|
long timeoutNanos = 999999L; |
252 |
|
|
long startTime = System.nanoTime(); |
253 |
|
|
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
254 |
|
|
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
255 |
|
|
assertFalse(p.isTerminated()); |
256 |
|
|
startTime = System.nanoTime(); |
257 |
|
|
long timeoutMillis = timeoutMillis(); |
258 |
|
|
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
259 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
260 |
|
|
assertFalse(p.isTerminated()); |
261 |
|
|
p.shutdown(); |
262 |
|
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
263 |
|
|
assertTrue(p.isTerminated()); |
264 |
|
|
} |
265 |
jsr166 |
1.50 |
} |
266 |
|
|
|
267 |
|
|
/** |
268 |
dl |
1.1 |
* setUncaughtExceptionHandler changes handler for uncaught exceptions. |
269 |
|
|
* |
270 |
|
|
* Additionally tests: Overriding ForkJoinWorkerThread.onStart |
271 |
|
|
* performs its defined action |
272 |
|
|
*/ |
273 |
jsr166 |
1.6 |
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
274 |
jsr166 |
1.29 |
final CountDownLatch uehInvoked = new CountDownLatch(1); |
275 |
jsr166 |
1.65 |
final Thread.UncaughtExceptionHandler ueh = |
276 |
jsr166 |
1.27 |
new Thread.UncaughtExceptionHandler() { |
277 |
|
|
public void uncaughtException(Thread t, Throwable e) { |
278 |
jsr166 |
1.66 |
threadAssertTrue(e instanceof MyError); |
279 |
|
|
threadAssertTrue(t instanceof FailingFJWSubclass); |
280 |
jsr166 |
1.29 |
uehInvoked.countDown(); |
281 |
jsr166 |
1.27 |
}}; |
282 |
jsr166 |
1.26 |
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
283 |
jsr166 |
1.65 |
ueh, false); |
284 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
285 |
|
|
assertSame(ueh, p.getUncaughtExceptionHandler()); |
286 |
dl |
1.44 |
try { |
287 |
jsr166 |
1.66 |
p.execute(new FibTask(8)); |
288 |
|
|
await(uehInvoked); |
289 |
jsr166 |
1.65 |
} finally { |
290 |
|
|
p.shutdownNow(); // failure might have prevented processing task |
291 |
dl |
1.44 |
} |
292 |
dl |
1.1 |
} |
293 |
|
|
} |
294 |
|
|
|
295 |
jsr166 |
1.2 |
/** |
296 |
jsr166 |
1.40 |
* After invoking a single task, isQuiescent eventually becomes |
297 |
|
|
* true, at which time queues are empty, threads are not active, |
298 |
|
|
* the task has completed successfully, and construction |
299 |
|
|
* parameters continue to hold |
300 |
dl |
1.1 |
*/ |
301 |
jsr166 |
1.43 |
public void testIsQuiescent() throws Exception { |
302 |
jsr166 |
1.23 |
ForkJoinPool p = new ForkJoinPool(2); |
303 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(p)) { |
304 |
jsr166 |
1.30 |
assertTrue(p.isQuiescent()); |
305 |
jsr166 |
1.40 |
long startTime = System.nanoTime(); |
306 |
|
|
FibTask f = new FibTask(20); |
307 |
|
|
p.invoke(f); |
308 |
jsr166 |
1.26 |
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
309 |
|
|
p.getFactory()); |
310 |
jsr166 |
1.40 |
while (! p.isQuiescent()) { |
311 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
312 |
|
|
throw new AssertionFailedError("timed out"); |
313 |
|
|
assertFalse(p.getAsyncMode()); |
314 |
|
|
assertFalse(p.isShutdown()); |
315 |
|
|
assertFalse(p.isTerminating()); |
316 |
|
|
assertFalse(p.isTerminated()); |
317 |
|
|
Thread.yield(); |
318 |
|
|
} |
319 |
|
|
|
320 |
dl |
1.1 |
assertTrue(p.isQuiescent()); |
321 |
|
|
assertFalse(p.getAsyncMode()); |
322 |
jsr166 |
1.26 |
assertEquals(0, p.getQueuedTaskCount()); |
323 |
|
|
assertEquals(0, p.getQueuedSubmissionCount()); |
324 |
dl |
1.1 |
assertFalse(p.hasQueuedSubmissions()); |
325 |
jsr166 |
1.70 |
while (p.getActiveThreadCount() != 0 |
326 |
|
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
327 |
|
|
Thread.yield(); |
328 |
dl |
1.1 |
assertFalse(p.isShutdown()); |
329 |
|
|
assertFalse(p.isTerminating()); |
330 |
|
|
assertFalse(p.isTerminated()); |
331 |
jsr166 |
1.40 |
assertTrue(f.isDone()); |
332 |
|
|
assertEquals(6765, (int) f.get()); |
333 |
jsr166 |
1.70 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
334 |
dl |
1.1 |
} |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
/** |
338 |
|
|
* Completed submit(ForkJoinTask) returns result |
339 |
|
|
*/ |
340 |
jsr166 |
1.6 |
public void testSubmitForkJoinTask() throws Throwable { |
341 |
jsr166 |
1.23 |
ForkJoinPool p = new ForkJoinPool(1); |
342 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(p)) { |
343 |
dl |
1.1 |
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
344 |
jsr166 |
1.26 |
assertEquals(21, (int) f.get()); |
345 |
dl |
1.1 |
} |
346 |
|
|
} |
347 |
|
|
|
348 |
|
|
/** |
349 |
|
|
* A task submitted after shutdown is rejected |
350 |
|
|
*/ |
351 |
|
|
public void testSubmitAfterShutdown() { |
352 |
jsr166 |
1.23 |
ForkJoinPool p = new ForkJoinPool(1); |
353 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(p)) { |
354 |
dl |
1.1 |
p.shutdown(); |
355 |
|
|
assertTrue(p.isShutdown()); |
356 |
jsr166 |
1.26 |
try { |
357 |
|
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
358 |
|
|
shouldThrow(); |
359 |
|
|
} catch (RejectedExecutionException success) {} |
360 |
dl |
1.1 |
} |
361 |
|
|
} |
362 |
|
|
|
363 |
|
|
/** |
364 |
|
|
* Pool maintains parallelism when using ManagedBlocker |
365 |
|
|
*/ |
366 |
jsr166 |
1.6 |
public void testBlockingForkJoinTask() throws Throwable { |
367 |
jsr166 |
1.23 |
ForkJoinPool p = new ForkJoinPool(4); |
368 |
dl |
1.1 |
try { |
369 |
|
|
ReentrantLock lock = new ReentrantLock(); |
370 |
|
|
ManagedLocker locker = new ManagedLocker(lock); |
371 |
jsr166 |
1.32 |
ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock); |
372 |
dl |
1.1 |
p.execute(f); |
373 |
jsr166 |
1.32 |
assertEquals(6765, (int) f.get()); |
374 |
dl |
1.1 |
} finally { |
375 |
dl |
1.7 |
p.shutdownNow(); // don't wait out shutdown |
376 |
dl |
1.1 |
} |
377 |
|
|
} |
378 |
|
|
|
379 |
|
|
/** |
380 |
|
|
* pollSubmission returns unexecuted submitted task, if present |
381 |
|
|
*/ |
382 |
|
|
public void testPollSubmission() { |
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 |
ForkJoinTask r = p.pollSubmission(); |
390 |
|
|
assertTrue(r == a || r == b || r == c); |
391 |
|
|
assertFalse(r.isDone()); |
392 |
jsr166 |
1.36 |
done.countDown(); |
393 |
dl |
1.1 |
} |
394 |
|
|
} |
395 |
|
|
|
396 |
|
|
/** |
397 |
|
|
* drainTasksTo transfers unexecuted submitted tasks, if present |
398 |
|
|
*/ |
399 |
|
|
public void testDrainTasksTo() { |
400 |
jsr166 |
1.36 |
final CountDownLatch done = new CountDownLatch(1); |
401 |
jsr166 |
1.23 |
SubFJP p = new SubFJP(); |
402 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(p)) { |
403 |
jsr166 |
1.36 |
ForkJoinTask a = p.submit(awaiter(done)); |
404 |
|
|
ForkJoinTask b = p.submit(awaiter(done)); |
405 |
|
|
ForkJoinTask c = p.submit(awaiter(done)); |
406 |
dl |
1.1 |
ArrayList<ForkJoinTask> al = new ArrayList(); |
407 |
|
|
p.drainTasksTo(al); |
408 |
|
|
assertTrue(al.size() > 0); |
409 |
|
|
for (ForkJoinTask r : al) { |
410 |
|
|
assertTrue(r == a || r == b || r == c); |
411 |
|
|
assertFalse(r.isDone()); |
412 |
|
|
} |
413 |
jsr166 |
1.36 |
done.countDown(); |
414 |
dl |
1.1 |
} |
415 |
|
|
} |
416 |
|
|
|
417 |
|
|
// FJ Versions of AbstractExecutorService tests |
418 |
|
|
|
419 |
|
|
/** |
420 |
|
|
* execute(runnable) runs it to completion |
421 |
|
|
*/ |
422 |
jsr166 |
1.6 |
public void testExecuteRunnable() throws Throwable { |
423 |
|
|
ExecutorService e = new ForkJoinPool(1); |
424 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
425 |
jsr166 |
1.42 |
final AtomicBoolean done = new AtomicBoolean(false); |
426 |
jsr166 |
1.52 |
Future<?> future = e.submit(new CheckedRunnable() { |
427 |
jsr166 |
1.42 |
public void realRun() { |
428 |
|
|
done.set(true); |
429 |
jsr166 |
1.52 |
}}); |
430 |
jsr166 |
1.34 |
assertNull(future.get()); |
431 |
jsr166 |
1.42 |
assertNull(future.get(0, MILLISECONDS)); |
432 |
|
|
assertTrue(done.get()); |
433 |
jsr166 |
1.36 |
assertTrue(future.isDone()); |
434 |
jsr166 |
1.34 |
assertFalse(future.isCancelled()); |
435 |
jsr166 |
1.23 |
} |
436 |
dl |
1.1 |
} |
437 |
|
|
|
438 |
|
|
/** |
439 |
|
|
* Completed submit(callable) returns result |
440 |
|
|
*/ |
441 |
jsr166 |
1.6 |
public void testSubmitCallable() throws Throwable { |
442 |
|
|
ExecutorService e = new ForkJoinPool(1); |
443 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
444 |
jsr166 |
1.23 |
Future<String> future = e.submit(new StringTask()); |
445 |
jsr166 |
1.34 |
assertSame(TEST_STRING, future.get()); |
446 |
|
|
assertTrue(future.isDone()); |
447 |
|
|
assertFalse(future.isCancelled()); |
448 |
jsr166 |
1.23 |
} |
449 |
dl |
1.1 |
} |
450 |
|
|
|
451 |
|
|
/** |
452 |
|
|
* Completed submit(runnable) returns successfully |
453 |
|
|
*/ |
454 |
jsr166 |
1.6 |
public void testSubmitRunnable() throws Throwable { |
455 |
|
|
ExecutorService e = new ForkJoinPool(1); |
456 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
457 |
jsr166 |
1.23 |
Future<?> future = e.submit(new NoOpRunnable()); |
458 |
jsr166 |
1.34 |
assertNull(future.get()); |
459 |
jsr166 |
1.23 |
assertTrue(future.isDone()); |
460 |
jsr166 |
1.34 |
assertFalse(future.isCancelled()); |
461 |
jsr166 |
1.23 |
} |
462 |
dl |
1.1 |
} |
463 |
|
|
|
464 |
|
|
/** |
465 |
|
|
* Completed submit(runnable, result) returns result |
466 |
|
|
*/ |
467 |
jsr166 |
1.6 |
public void testSubmitRunnable2() throws Throwable { |
468 |
|
|
ExecutorService e = new ForkJoinPool(1); |
469 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
470 |
jsr166 |
1.23 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
471 |
jsr166 |
1.34 |
assertSame(TEST_STRING, future.get()); |
472 |
|
|
assertTrue(future.isDone()); |
473 |
|
|
assertFalse(future.isCancelled()); |
474 |
jsr166 |
1.23 |
} |
475 |
dl |
1.1 |
} |
476 |
|
|
|
477 |
|
|
/** |
478 |
jsr166 |
1.33 |
* A submitted privileged action runs to completion |
479 |
dl |
1.1 |
*/ |
480 |
dl |
1.35 |
public void testSubmitPrivilegedAction() throws Exception { |
481 |
jsr166 |
1.48 |
final Callable callable = Executors.callable(new PrivilegedAction() { |
482 |
|
|
public Object run() { return TEST_STRING; }}); |
483 |
dl |
1.35 |
Runnable r = new CheckedRunnable() { |
484 |
jsr166 |
1.48 |
public void realRun() throws Exception { |
485 |
|
|
ExecutorService e = new ForkJoinPool(1); |
486 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
487 |
jsr166 |
1.48 |
Future future = e.submit(callable); |
488 |
dl |
1.35 |
assertSame(TEST_STRING, future.get()); |
489 |
jsr166 |
1.48 |
} |
490 |
|
|
}}; |
491 |
dl |
1.35 |
|
492 |
jsr166 |
1.48 |
runWithPermissions(r, new RuntimePermission("modifyThread")); |
493 |
dl |
1.1 |
} |
494 |
|
|
|
495 |
|
|
/** |
496 |
jsr166 |
1.33 |
* A submitted privileged exception action runs to completion |
497 |
dl |
1.1 |
*/ |
498 |
dl |
1.35 |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
499 |
jsr166 |
1.48 |
final Callable callable = |
500 |
|
|
Executors.callable(new PrivilegedExceptionAction() { |
501 |
|
|
public Object run() { return TEST_STRING; }}); |
502 |
dl |
1.35 |
Runnable r = new CheckedRunnable() { |
503 |
jsr166 |
1.48 |
public void realRun() throws Exception { |
504 |
|
|
ExecutorService e = new ForkJoinPool(1); |
505 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
506 |
jsr166 |
1.48 |
Future future = e.submit(callable); |
507 |
dl |
1.35 |
assertSame(TEST_STRING, future.get()); |
508 |
jsr166 |
1.48 |
} |
509 |
|
|
}}; |
510 |
dl |
1.35 |
|
511 |
|
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
512 |
dl |
1.1 |
} |
513 |
|
|
|
514 |
|
|
/** |
515 |
|
|
* A submitted failed privileged exception action reports exception |
516 |
|
|
*/ |
517 |
dl |
1.35 |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
518 |
jsr166 |
1.48 |
final Callable callable = |
519 |
|
|
Executors.callable(new PrivilegedExceptionAction() { |
520 |
|
|
public Object run() { throw new IndexOutOfBoundsException(); }}); |
521 |
dl |
1.35 |
Runnable r = new CheckedRunnable() { |
522 |
jsr166 |
1.48 |
public void realRun() throws Exception { |
523 |
|
|
ExecutorService e = new ForkJoinPool(1); |
524 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
525 |
jsr166 |
1.48 |
Future future = e.submit(callable); |
526 |
dl |
1.35 |
try { |
527 |
|
|
future.get(); |
528 |
|
|
shouldThrow(); |
529 |
|
|
} catch (ExecutionException success) { |
530 |
|
|
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
531 |
jsr166 |
1.48 |
} |
532 |
|
|
} |
533 |
|
|
}}; |
534 |
dl |
1.35 |
|
535 |
|
|
runWithPermissions(r, new RuntimePermission("modifyThread")); |
536 |
dl |
1.1 |
} |
537 |
|
|
|
538 |
|
|
/** |
539 |
jsr166 |
1.6 |
* execute(null runnable) throws NullPointerException |
540 |
dl |
1.1 |
*/ |
541 |
|
|
public void testExecuteNullRunnable() { |
542 |
jsr166 |
1.23 |
ExecutorService e = new ForkJoinPool(1); |
543 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
544 |
|
|
try { |
545 |
|
|
Future<?> future = e.submit((Runnable) null); |
546 |
|
|
shouldThrow(); |
547 |
|
|
} catch (NullPointerException success) {} |
548 |
jsr166 |
1.23 |
} |
549 |
dl |
1.1 |
} |
550 |
|
|
|
551 |
|
|
/** |
552 |
jsr166 |
1.6 |
* submit(null callable) throws NullPointerException |
553 |
dl |
1.1 |
*/ |
554 |
|
|
public void testSubmitNullCallable() { |
555 |
jsr166 |
1.23 |
ExecutorService e = new ForkJoinPool(1); |
556 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
557 |
|
|
try { |
558 |
|
|
Future<String> future = e.submit((Callable) null); |
559 |
|
|
shouldThrow(); |
560 |
|
|
} catch (NullPointerException success) {} |
561 |
jsr166 |
1.23 |
} |
562 |
dl |
1.1 |
} |
563 |
|
|
|
564 |
|
|
/** |
565 |
jsr166 |
1.27 |
* submit(callable).get() throws InterruptedException if interrupted |
566 |
dl |
1.1 |
*/ |
567 |
jsr166 |
1.6 |
public void testInterruptedSubmit() throws InterruptedException { |
568 |
jsr166 |
1.27 |
final CountDownLatch submitted = new CountDownLatch(1); |
569 |
|
|
final CountDownLatch quittingTime = new CountDownLatch(1); |
570 |
|
|
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
571 |
|
|
public Void realCall() throws InterruptedException { |
572 |
jsr166 |
1.64 |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
573 |
jsr166 |
1.27 |
return null; |
574 |
|
|
}}; |
575 |
jsr166 |
1.64 |
final ExecutorService p = new ForkJoinPool(1); |
576 |
|
|
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
577 |
jsr166 |
1.27 |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
578 |
|
|
public void realRun() throws Exception { |
579 |
|
|
Future<Void> future = p.submit(awaiter); |
580 |
|
|
submitted.countDown(); |
581 |
|
|
future.get(); |
582 |
|
|
}}); |
583 |
|
|
t.start(); |
584 |
jsr166 |
1.64 |
await(submitted); |
585 |
jsr166 |
1.27 |
t.interrupt(); |
586 |
jsr166 |
1.64 |
awaitTermination(t); |
587 |
jsr166 |
1.27 |
} |
588 |
dl |
1.1 |
} |
589 |
|
|
|
590 |
|
|
/** |
591 |
jsr166 |
1.4 |
* get of submit(callable) throws ExecutionException if callable |
592 |
|
|
* throws exception |
593 |
dl |
1.1 |
*/ |
594 |
jsr166 |
1.6 |
public void testSubmitEE() throws Throwable { |
595 |
dl |
1.1 |
ForkJoinPool p = new ForkJoinPool(1); |
596 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(p)) { |
597 |
|
|
try { |
598 |
|
|
p.submit(new Callable() { |
599 |
|
|
public Object call() { throw new ArithmeticException(); }}) |
600 |
|
|
.get(); |
601 |
|
|
shouldThrow(); |
602 |
|
|
} catch (ExecutionException success) { |
603 |
|
|
assertTrue(success.getCause() instanceof ArithmeticException); |
604 |
|
|
} |
605 |
jsr166 |
1.12 |
} |
606 |
dl |
1.1 |
} |
607 |
|
|
|
608 |
|
|
/** |
609 |
jsr166 |
1.6 |
* invokeAny(null) throws NullPointerException |
610 |
dl |
1.1 |
*/ |
611 |
jsr166 |
1.6 |
public void testInvokeAny1() throws Throwable { |
612 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
613 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
614 |
|
|
try { |
615 |
|
|
e.invokeAny(null); |
616 |
|
|
shouldThrow(); |
617 |
|
|
} catch (NullPointerException success) {} |
618 |
dl |
1.1 |
} |
619 |
|
|
} |
620 |
|
|
|
621 |
|
|
/** |
622 |
jsr166 |
1.6 |
* invokeAny(empty collection) throws IllegalArgumentException |
623 |
dl |
1.1 |
*/ |
624 |
jsr166 |
1.6 |
public void testInvokeAny2() throws Throwable { |
625 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
626 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
627 |
|
|
try { |
628 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
629 |
|
|
shouldThrow(); |
630 |
|
|
} catch (IllegalArgumentException success) {} |
631 |
dl |
1.1 |
} |
632 |
|
|
} |
633 |
|
|
|
634 |
|
|
/** |
635 |
jsr166 |
1.9 |
* invokeAny(c) throws NullPointerException if c has a single null element |
636 |
|
|
*/ |
637 |
|
|
public void testInvokeAny3() throws Throwable { |
638 |
|
|
ExecutorService e = new ForkJoinPool(1); |
639 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
640 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
641 |
|
|
l.add(null); |
642 |
|
|
try { |
643 |
|
|
e.invokeAny(l); |
644 |
|
|
shouldThrow(); |
645 |
|
|
} catch (NullPointerException success) {} |
646 |
jsr166 |
1.9 |
} |
647 |
|
|
} |
648 |
|
|
|
649 |
|
|
/** |
650 |
jsr166 |
1.6 |
* invokeAny(c) throws NullPointerException if c has null elements |
651 |
dl |
1.1 |
*/ |
652 |
jsr166 |
1.9 |
public void testInvokeAny4() throws Throwable { |
653 |
jsr166 |
1.16 |
CountDownLatch latch = new CountDownLatch(1); |
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(latchAwaitingStringTask(latch)); |
658 |
|
|
l.add(null); |
659 |
|
|
try { |
660 |
|
|
e.invokeAny(l); |
661 |
|
|
shouldThrow(); |
662 |
|
|
} catch (NullPointerException success) {} |
663 |
jsr166 |
1.16 |
latch.countDown(); |
664 |
dl |
1.1 |
} |
665 |
|
|
} |
666 |
|
|
|
667 |
|
|
/** |
668 |
|
|
* invokeAny(c) throws ExecutionException if no task in c completes |
669 |
|
|
*/ |
670 |
jsr166 |
1.9 |
public void testInvokeAny5() throws Throwable { |
671 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
672 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
673 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
674 |
|
|
l.add(new NPETask()); |
675 |
|
|
try { |
676 |
|
|
e.invokeAny(l); |
677 |
|
|
shouldThrow(); |
678 |
|
|
} catch (ExecutionException success) { |
679 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
680 |
|
|
} |
681 |
dl |
1.1 |
} |
682 |
|
|
} |
683 |
|
|
|
684 |
|
|
/** |
685 |
|
|
* invokeAny(c) returns result of some task in c if at least one completes |
686 |
|
|
*/ |
687 |
jsr166 |
1.9 |
public void testInvokeAny6() throws Throwable { |
688 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
689 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
690 |
jsr166 |
1.16 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
691 |
dl |
1.1 |
l.add(new StringTask()); |
692 |
|
|
l.add(new StringTask()); |
693 |
|
|
String result = e.invokeAny(l); |
694 |
|
|
assertSame(TEST_STRING, result); |
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 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
704 |
|
|
try { |
705 |
|
|
e.invokeAll(null); |
706 |
|
|
shouldThrow(); |
707 |
|
|
} catch (NullPointerException success) {} |
708 |
dl |
1.1 |
} |
709 |
|
|
} |
710 |
|
|
|
711 |
|
|
/** |
712 |
|
|
* invokeAll(empty collection) returns empty collection |
713 |
|
|
*/ |
714 |
jsr166 |
1.6 |
public void testInvokeAll2() throws InterruptedException { |
715 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
716 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
717 |
jsr166 |
1.6 |
List<Future<String>> r |
718 |
|
|
= e.invokeAll(new ArrayList<Callable<String>>()); |
719 |
dl |
1.1 |
assertTrue(r.isEmpty()); |
720 |
|
|
} |
721 |
|
|
} |
722 |
|
|
|
723 |
|
|
/** |
724 |
jsr166 |
1.6 |
* invokeAll(c) throws NullPointerException if c has null elements |
725 |
dl |
1.1 |
*/ |
726 |
jsr166 |
1.6 |
public void testInvokeAll3() throws InterruptedException { |
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 StringTask()); |
731 |
|
|
l.add(null); |
732 |
|
|
try { |
733 |
|
|
e.invokeAll(l); |
734 |
|
|
shouldThrow(); |
735 |
|
|
} catch (NullPointerException success) {} |
736 |
dl |
1.1 |
} |
737 |
|
|
} |
738 |
|
|
|
739 |
|
|
/** |
740 |
jsr166 |
1.6 |
* get of returned element of invokeAll(c) throws |
741 |
|
|
* ExecutionException on failed task |
742 |
dl |
1.1 |
*/ |
743 |
jsr166 |
1.6 |
public void testInvokeAll4() throws Throwable { |
744 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
745 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
746 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
747 |
|
|
l.add(new NPETask()); |
748 |
|
|
List<Future<String>> futures = e.invokeAll(l); |
749 |
|
|
assertEquals(1, futures.size()); |
750 |
|
|
try { |
751 |
|
|
futures.get(0).get(); |
752 |
|
|
shouldThrow(); |
753 |
|
|
} catch (ExecutionException success) { |
754 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
755 |
|
|
} |
756 |
dl |
1.1 |
} |
757 |
|
|
} |
758 |
|
|
|
759 |
|
|
/** |
760 |
|
|
* invokeAll(c) returns results of all completed tasks in c |
761 |
|
|
*/ |
762 |
jsr166 |
1.6 |
public void testInvokeAll5() throws Throwable { |
763 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
764 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
765 |
jsr166 |
1.16 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
766 |
dl |
1.1 |
l.add(new StringTask()); |
767 |
|
|
l.add(new StringTask()); |
768 |
jsr166 |
1.16 |
List<Future<String>> futures = e.invokeAll(l); |
769 |
|
|
assertEquals(2, futures.size()); |
770 |
|
|
for (Future<String> future : futures) |
771 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
772 |
dl |
1.1 |
} |
773 |
|
|
} |
774 |
|
|
|
775 |
|
|
/** |
776 |
jsr166 |
1.6 |
* timed invokeAny(null) throws NullPointerException |
777 |
dl |
1.1 |
*/ |
778 |
jsr166 |
1.6 |
public void testTimedInvokeAny1() throws Throwable { |
779 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
780 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
781 |
|
|
try { |
782 |
|
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
783 |
|
|
shouldThrow(); |
784 |
|
|
} catch (NullPointerException success) {} |
785 |
dl |
1.1 |
} |
786 |
|
|
} |
787 |
|
|
|
788 |
|
|
/** |
789 |
jsr166 |
1.6 |
* timed invokeAny(null time unit) throws NullPointerException |
790 |
dl |
1.1 |
*/ |
791 |
jsr166 |
1.6 |
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
792 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
793 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
794 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
795 |
|
|
l.add(new StringTask()); |
796 |
|
|
try { |
797 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
798 |
|
|
shouldThrow(); |
799 |
|
|
} catch (NullPointerException success) {} |
800 |
dl |
1.1 |
} |
801 |
|
|
} |
802 |
|
|
|
803 |
|
|
/** |
804 |
jsr166 |
1.6 |
* timed invokeAny(empty collection) throws IllegalArgumentException |
805 |
dl |
1.1 |
*/ |
806 |
jsr166 |
1.6 |
public void testTimedInvokeAny2() throws Throwable { |
807 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
808 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
809 |
|
|
try { |
810 |
|
|
e.invokeAny(new ArrayList<Callable<String>>(), |
811 |
|
|
MEDIUM_DELAY_MS, MILLISECONDS); |
812 |
|
|
shouldThrow(); |
813 |
|
|
} catch (IllegalArgumentException success) {} |
814 |
dl |
1.1 |
} |
815 |
|
|
} |
816 |
|
|
|
817 |
|
|
/** |
818 |
jsr166 |
1.6 |
* timed invokeAny(c) throws NullPointerException if c has null elements |
819 |
dl |
1.1 |
*/ |
820 |
jsr166 |
1.6 |
public void testTimedInvokeAny3() throws Throwable { |
821 |
jsr166 |
1.16 |
CountDownLatch latch = new CountDownLatch(1); |
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(latchAwaitingStringTask(latch)); |
826 |
|
|
l.add(null); |
827 |
|
|
try { |
828 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
829 |
|
|
shouldThrow(); |
830 |
|
|
} catch (NullPointerException success) {} |
831 |
jsr166 |
1.15 |
latch.countDown(); |
832 |
dl |
1.1 |
} |
833 |
|
|
} |
834 |
|
|
|
835 |
|
|
/** |
836 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
837 |
|
|
*/ |
838 |
jsr166 |
1.6 |
public void testTimedInvokeAny4() throws Throwable { |
839 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
840 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
841 |
jsr166 |
1.67 |
long startTime = System.nanoTime(); |
842 |
jsr166 |
1.62 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
843 |
|
|
l.add(new NPETask()); |
844 |
|
|
try { |
845 |
jsr166 |
1.67 |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
846 |
jsr166 |
1.62 |
shouldThrow(); |
847 |
|
|
} catch (ExecutionException success) { |
848 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
849 |
|
|
} |
850 |
jsr166 |
1.67 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
851 |
dl |
1.1 |
} |
852 |
|
|
} |
853 |
|
|
|
854 |
|
|
/** |
855 |
|
|
* timed invokeAny(c) returns result of some task in c |
856 |
|
|
*/ |
857 |
jsr166 |
1.6 |
public void testTimedInvokeAny5() throws Throwable { |
858 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
859 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
860 |
jsr166 |
1.68 |
long startTime = System.nanoTime(); |
861 |
jsr166 |
1.16 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
862 |
dl |
1.1 |
l.add(new StringTask()); |
863 |
|
|
l.add(new StringTask()); |
864 |
jsr166 |
1.68 |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
865 |
dl |
1.1 |
assertSame(TEST_STRING, result); |
866 |
jsr166 |
1.68 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
867 |
dl |
1.1 |
} |
868 |
|
|
} |
869 |
|
|
|
870 |
|
|
/** |
871 |
jsr166 |
1.6 |
* timed invokeAll(null) throws NullPointerException |
872 |
dl |
1.1 |
*/ |
873 |
jsr166 |
1.6 |
public void testTimedInvokeAll1() throws Throwable { |
874 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
875 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
876 |
|
|
try { |
877 |
|
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
878 |
|
|
shouldThrow(); |
879 |
|
|
} catch (NullPointerException success) {} |
880 |
dl |
1.1 |
} |
881 |
|
|
} |
882 |
|
|
|
883 |
|
|
/** |
884 |
jsr166 |
1.6 |
* timed invokeAll(null time unit) throws NullPointerException |
885 |
dl |
1.1 |
*/ |
886 |
jsr166 |
1.6 |
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
887 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
888 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
889 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
890 |
|
|
l.add(new StringTask()); |
891 |
|
|
try { |
892 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
893 |
|
|
shouldThrow(); |
894 |
|
|
} catch (NullPointerException success) {} |
895 |
dl |
1.1 |
} |
896 |
|
|
} |
897 |
|
|
|
898 |
|
|
/** |
899 |
|
|
* timed invokeAll(empty collection) returns empty collection |
900 |
|
|
*/ |
901 |
jsr166 |
1.6 |
public void testTimedInvokeAll2() throws InterruptedException { |
902 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
903 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
904 |
jsr166 |
1.6 |
List<Future<String>> r |
905 |
|
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
906 |
jsr166 |
1.27 |
MEDIUM_DELAY_MS, MILLISECONDS); |
907 |
dl |
1.1 |
assertTrue(r.isEmpty()); |
908 |
|
|
} |
909 |
|
|
} |
910 |
|
|
|
911 |
|
|
/** |
912 |
jsr166 |
1.6 |
* timed invokeAll(c) throws NullPointerException if c has null elements |
913 |
dl |
1.1 |
*/ |
914 |
jsr166 |
1.6 |
public void testTimedInvokeAll3() throws InterruptedException { |
915 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
916 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
917 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
918 |
|
|
l.add(new StringTask()); |
919 |
|
|
l.add(null); |
920 |
|
|
try { |
921 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
922 |
|
|
shouldThrow(); |
923 |
|
|
} catch (NullPointerException success) {} |
924 |
dl |
1.1 |
} |
925 |
|
|
} |
926 |
|
|
|
927 |
|
|
/** |
928 |
|
|
* get of returned element of invokeAll(c) throws exception on failed task |
929 |
|
|
*/ |
930 |
jsr166 |
1.6 |
public void testTimedInvokeAll4() throws Throwable { |
931 |
dl |
1.1 |
ExecutorService e = new ForkJoinPool(1); |
932 |
jsr166 |
1.62 |
try (PoolCleaner cleaner = cleaner(e)) { |
933 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
934 |
|
|
l.add(new NPETask()); |
935 |
|
|
List<Future<String>> futures |
936 |
jsr166 |
1.63 |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
937 |
jsr166 |
1.62 |
assertEquals(1, futures.size()); |
938 |
|
|
try { |
939 |
|
|
futures.get(0).get(); |
940 |
|
|
shouldThrow(); |
941 |
|
|
} catch (ExecutionException success) { |
942 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
943 |
|
|
} |
944 |
dl |
1.1 |
} |
945 |
|
|
} |
946 |
|
|
|
947 |
|
|
/** |
948 |
|
|
* timed invokeAll(c) returns results of all completed tasks in c |
949 |
|
|
*/ |
950 |
jsr166 |
1.6 |
public void testTimedInvokeAll5() throws Throwable { |
951 |
jsr166 |
1.60 |
ForkJoinPool e = new ForkJoinPool(1); |
952 |
jsr166 |
1.61 |
try (PoolCleaner cleaner = cleaner(e)) { |
953 |
jsr166 |
1.16 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
954 |
dl |
1.1 |
l.add(new StringTask()); |
955 |
|
|
l.add(new StringTask()); |
956 |
jsr166 |
1.16 |
List<Future<String>> futures |
957 |
jsr166 |
1.56 |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
958 |
jsr166 |
1.16 |
assertEquals(2, futures.size()); |
959 |
|
|
for (Future<String> future : futures) |
960 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
961 |
dl |
1.1 |
} |
962 |
|
|
} |
963 |
|
|
|
964 |
|
|
} |