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