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