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