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