1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
13 |
< |
import java.util.concurrent.*; |
14 |
< |
import java.math.BigInteger; |
15 |
< |
import java.security.*; |
11 |
> |
import java.security.PrivilegedAction; |
12 |
> |
import java.security.PrivilegedExceptionAction; |
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.Collections; |
15 |
> |
import java.util.List; |
16 |
> |
import java.util.concurrent.AbstractExecutorService; |
17 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
18 |
> |
import java.util.concurrent.Callable; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
20 |
> |
import java.util.concurrent.ExecutionException; |
21 |
> |
import java.util.concurrent.Executors; |
22 |
> |
import java.util.concurrent.ExecutorService; |
23 |
> |
import java.util.concurrent.Future; |
24 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
25 |
> |
import java.util.concurrent.TimeUnit; |
26 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
27 |
|
|
28 |
< |
public class AbstractExecutorServiceTest extends JSR166TestCase{ |
28 |
> |
import junit.framework.Test; |
29 |
> |
import junit.framework.TestSuite; |
30 |
> |
|
31 |
> |
public class AbstractExecutorServiceTest extends JSR166TestCase { |
32 |
|
public static void main(String[] args) { |
33 |
< |
junit.textui.TestRunner.run (suite()); |
33 |
> |
junit.textui.TestRunner.run(suite()); |
34 |
|
} |
35 |
|
public static Test suite() { |
36 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
37 |
|
} |
38 |
|
|
39 |
< |
/** |
39 |
> |
/** |
40 |
|
* A no-frills implementation of AbstractExecutorService, designed |
41 |
|
* to test the submit methods only. |
42 |
|
*/ |
43 |
|
static class DirectExecutorService extends AbstractExecutorService { |
44 |
|
public void execute(Runnable r) { r.run(); } |
45 |
|
public void shutdown() { shutdown = true; } |
46 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
46 |
> |
public List<Runnable> shutdownNow() { |
47 |
> |
shutdown = true; |
48 |
> |
return Collections.EMPTY_LIST; |
49 |
> |
} |
50 |
|
public boolean isShutdown() { return shutdown; } |
51 |
|
public boolean isTerminated() { return isShutdown(); } |
52 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
52 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
53 |
> |
return isShutdown(); |
54 |
> |
} |
55 |
|
private volatile boolean shutdown = false; |
56 |
|
} |
57 |
|
|
58 |
|
/** |
59 |
< |
* execute of runnable runs it to completion |
59 |
> |
* execute(runnable) runs it to completion |
60 |
|
*/ |
61 |
< |
public void testExecuteRunnable() { |
62 |
< |
try { |
63 |
< |
ExecutorService e = new DirectExecutorService(); |
64 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
65 |
< |
assertFalse(task.done); |
66 |
< |
Future<?> future = e.submit(task); |
67 |
< |
future.get(); |
68 |
< |
assertTrue(task.done); |
69 |
< |
} |
70 |
< |
catch (ExecutionException ex) { |
71 |
< |
unexpectedException(); |
72 |
< |
} |
52 |
< |
catch (InterruptedException ex) { |
53 |
< |
unexpectedException(); |
54 |
< |
} |
61 |
> |
public void testExecuteRunnable() throws Exception { |
62 |
> |
ExecutorService e = new DirectExecutorService(); |
63 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
64 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
65 |
> |
public void realRun() { |
66 |
> |
done.set(true); |
67 |
> |
}}); |
68 |
> |
assertNull(future.get()); |
69 |
> |
assertNull(future.get(0, MILLISECONDS)); |
70 |
> |
assertTrue(done.get()); |
71 |
> |
assertTrue(future.isDone()); |
72 |
> |
assertFalse(future.isCancelled()); |
73 |
|
} |
74 |
|
|
57 |
– |
|
75 |
|
/** |
76 |
< |
* completed submit of callable returns result |
76 |
> |
* Completed submit(callable) returns result |
77 |
|
*/ |
78 |
< |
public void testSubmitCallable() { |
79 |
< |
try { |
80 |
< |
ExecutorService e = new DirectExecutorService(); |
81 |
< |
Future<String> future = e.submit(new StringTask()); |
82 |
< |
String result = future.get(); |
66 |
< |
assertSame(TEST_STRING, result); |
67 |
< |
} |
68 |
< |
catch (ExecutionException ex) { |
69 |
< |
unexpectedException(); |
70 |
< |
} |
71 |
< |
catch (InterruptedException ex) { |
72 |
< |
unexpectedException(); |
73 |
< |
} |
78 |
> |
public void testSubmitCallable() throws Exception { |
79 |
> |
ExecutorService e = new DirectExecutorService(); |
80 |
> |
Future<String> future = e.submit(new StringTask()); |
81 |
> |
String result = future.get(); |
82 |
> |
assertSame(TEST_STRING, result); |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
< |
* completed submit of runnable returns successfully |
86 |
> |
* Completed submit(runnable) returns successfully |
87 |
|
*/ |
88 |
< |
public void testSubmitRunnable() { |
89 |
< |
try { |
90 |
< |
ExecutorService e = new DirectExecutorService(); |
91 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
92 |
< |
future.get(); |
84 |
< |
assertTrue(future.isDone()); |
85 |
< |
} |
86 |
< |
catch (ExecutionException ex) { |
87 |
< |
unexpectedException(); |
88 |
< |
} |
89 |
< |
catch (InterruptedException ex) { |
90 |
< |
unexpectedException(); |
91 |
< |
} |
88 |
> |
public void testSubmitRunnable() throws Exception { |
89 |
> |
ExecutorService e = new DirectExecutorService(); |
90 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
91 |
> |
future.get(); |
92 |
> |
assertTrue(future.isDone()); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* completed submit of (runnable, result) returns result |
96 |
> |
* Completed submit(runnable, result) returns result |
97 |
|
*/ |
98 |
< |
public void testSubmitRunnable2() { |
99 |
< |
try { |
100 |
< |
ExecutorService e = new DirectExecutorService(); |
101 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
102 |
< |
String result = future.get(); |
102 |
< |
assertSame(TEST_STRING, result); |
103 |
< |
} |
104 |
< |
catch (ExecutionException ex) { |
105 |
< |
unexpectedException(); |
106 |
< |
} |
107 |
< |
catch (InterruptedException ex) { |
108 |
< |
unexpectedException(); |
109 |
< |
} |
98 |
> |
public void testSubmitRunnable2() throws Exception { |
99 |
> |
ExecutorService e = new DirectExecutorService(); |
100 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
101 |
> |
String result = future.get(); |
102 |
> |
assertSame(TEST_STRING, result); |
103 |
|
} |
104 |
|
|
112 |
– |
|
105 |
|
/** |
106 |
< |
* submit of a privileged action runs it to completion |
106 |
> |
* A submitted privileged action runs to completion |
107 |
|
*/ |
108 |
< |
public void testSubmitPrivilegedAction() { |
109 |
< |
Policy savedPolicy = Policy.getPolicy(); |
110 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
111 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
112 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
121 |
< |
Policy.setPolicy(policy); |
122 |
< |
try { |
123 |
< |
ExecutorService e = new DirectExecutorService(); |
124 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
108 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
109 |
> |
Runnable r = new CheckedRunnable() { |
110 |
> |
public void realRun() throws Exception { |
111 |
> |
ExecutorService e = new DirectExecutorService(); |
112 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
113 |
|
public Object run() { |
114 |
|
return TEST_STRING; |
115 |
|
}})); |
116 |
|
|
117 |
< |
Object result = future.get(); |
118 |
< |
assertSame(TEST_STRING, result); |
119 |
< |
} |
120 |
< |
catch (ExecutionException ex) { |
121 |
< |
unexpectedException(); |
122 |
< |
} |
123 |
< |
catch (InterruptedException ex) { |
136 |
< |
unexpectedException(); |
137 |
< |
} |
138 |
< |
finally { |
139 |
< |
Policy.setPolicy(savedPolicy); |
140 |
< |
} |
117 |
> |
assertSame(TEST_STRING, future.get()); |
118 |
> |
}}; |
119 |
> |
|
120 |
> |
runWithPermissions(r, |
121 |
> |
new RuntimePermission("getClassLoader"), |
122 |
> |
new RuntimePermission("setContextClassLoader"), |
123 |
> |
new RuntimePermission("modifyThread")); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* submit of a privileged exception action runs it to completion |
127 |
> |
* A submitted privileged exception action runs to completion |
128 |
|
*/ |
129 |
< |
public void testSubmitPrivilegedExceptionAction() { |
130 |
< |
Policy savedPolicy = Policy.getPolicy(); |
131 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
132 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
133 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
151 |
< |
Policy.setPolicy(policy); |
152 |
< |
try { |
153 |
< |
ExecutorService e = new DirectExecutorService(); |
154 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
129 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
130 |
> |
Runnable r = new CheckedRunnable() { |
131 |
> |
public void realRun() throws Exception { |
132 |
> |
ExecutorService e = new DirectExecutorService(); |
133 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
134 |
|
public Object run() { |
135 |
|
return TEST_STRING; |
136 |
|
}})); |
137 |
|
|
138 |
< |
Object result = future.get(); |
139 |
< |
assertSame(TEST_STRING, result); |
140 |
< |
} |
141 |
< |
catch (ExecutionException ex) { |
163 |
< |
unexpectedException(); |
164 |
< |
} |
165 |
< |
catch (InterruptedException ex) { |
166 |
< |
unexpectedException(); |
167 |
< |
} |
168 |
< |
finally { |
169 |
< |
Policy.setPolicy(savedPolicy); |
170 |
< |
} |
138 |
> |
assertSame(TEST_STRING, future.get()); |
139 |
> |
}}; |
140 |
> |
|
141 |
> |
runWithPermissions(r); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
< |
* submit of a failed privileged exception action reports exception |
145 |
> |
* A submitted failed privileged exception action reports exception |
146 |
|
*/ |
147 |
< |
public void testSubmitFailedPrivilegedExceptionAction() { |
148 |
< |
Policy savedPolicy = Policy.getPolicy(); |
149 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
150 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
151 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
181 |
< |
Policy.setPolicy(policy); |
182 |
< |
try { |
183 |
< |
ExecutorService e = new DirectExecutorService(); |
184 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
147 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
148 |
> |
Runnable r = new CheckedRunnable() { |
149 |
> |
public void realRun() throws Exception { |
150 |
> |
ExecutorService e = new DirectExecutorService(); |
151 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
152 |
|
public Object run() throws Exception { |
153 |
|
throw new IndexOutOfBoundsException(); |
154 |
|
}})); |
155 |
|
|
156 |
< |
Object result = future.get(); |
157 |
< |
shouldThrow(); |
158 |
< |
} |
159 |
< |
catch (ExecutionException success) { |
160 |
< |
} |
161 |
< |
catch (InterruptedException ex) { |
162 |
< |
unexpectedException(); |
163 |
< |
} |
197 |
< |
finally { |
198 |
< |
Policy.setPolicy(savedPolicy); |
199 |
< |
} |
156 |
> |
try { |
157 |
> |
future.get(); |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (ExecutionException success) { |
160 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
161 |
> |
}}}; |
162 |
> |
|
163 |
> |
runWithPermissions(r); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
167 |
< |
* execute with a null runnable throws NPE |
167 |
> |
* execute(null runnable) throws NPE |
168 |
|
*/ |
169 |
|
public void testExecuteNullRunnable() { |
170 |
|
try { |
171 |
|
ExecutorService e = new DirectExecutorService(); |
172 |
< |
TrackedShortRunnable task = null; |
209 |
< |
Future<?> future = e.submit(task); |
172 |
> |
e.submit((Runnable) null); |
173 |
|
shouldThrow(); |
174 |
< |
} |
212 |
< |
catch (NullPointerException success) { |
213 |
< |
} |
214 |
< |
catch (Exception ex) { |
215 |
< |
unexpectedException(); |
216 |
< |
} |
174 |
> |
} catch (NullPointerException success) {} |
175 |
|
} |
176 |
|
|
219 |
– |
|
177 |
|
/** |
178 |
< |
* submit of a null callable throws NPE |
178 |
> |
* submit(null callable) throws NPE |
179 |
|
*/ |
180 |
|
public void testSubmitNullCallable() { |
181 |
|
try { |
182 |
|
ExecutorService e = new DirectExecutorService(); |
183 |
< |
StringTask t = null; |
227 |
< |
Future<String> future = e.submit(t); |
183 |
> |
e.submit((Callable) null); |
184 |
|
shouldThrow(); |
185 |
< |
} |
230 |
< |
catch (NullPointerException success) { |
231 |
< |
} |
232 |
< |
catch (Exception ex) { |
233 |
< |
unexpectedException(); |
234 |
< |
} |
185 |
> |
} catch (NullPointerException success) {} |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
< |
* submit of Runnable throws RejectedExecutionException if |
239 |
< |
* saturated. |
189 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
190 |
|
*/ |
191 |
< |
public void testExecute1() { |
192 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
193 |
< |
try { |
194 |
< |
|
195 |
< |
for(int i = 0; i < 5; ++i){ |
196 |
< |
p.submit(new MediumRunnable()); |
197 |
< |
} |
198 |
< |
shouldThrow(); |
199 |
< |
} catch(RejectedExecutionException success){} |
200 |
< |
joinPool(p); |
201 |
< |
} |
202 |
< |
|
203 |
< |
/** |
204 |
< |
* Completed submit of Callable throws RejectedExecutionException |
205 |
< |
* if saturated. |
206 |
< |
*/ |
207 |
< |
public void testExecute2() { |
208 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
259 |
< |
try { |
260 |
< |
for(int i = 0; i < 5; ++i) { |
261 |
< |
p.submit(new SmallCallable()); |
262 |
< |
} |
263 |
< |
shouldThrow(); |
264 |
< |
} catch(RejectedExecutionException e){} |
265 |
< |
joinPool(p); |
266 |
< |
} |
267 |
< |
|
268 |
< |
|
269 |
< |
/** |
270 |
< |
* blocking on submit of Callable throws InterruptedException if |
271 |
< |
* caller interrupted. |
272 |
< |
*/ |
273 |
< |
public void testInterruptedSubmit() { |
274 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
275 |
< |
Thread t = new Thread(new Runnable() { |
276 |
< |
public void run() { |
277 |
< |
try { |
278 |
< |
p.submit(new Callable<Object>() { |
279 |
< |
public Object call() { |
280 |
< |
try { |
281 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
282 |
< |
shouldThrow(); |
283 |
< |
} catch(InterruptedException e){ |
284 |
< |
} |
285 |
< |
return null; |
286 |
< |
} |
287 |
< |
}).get(); |
288 |
< |
} catch(InterruptedException success){ |
289 |
< |
} catch(Exception e) { |
290 |
< |
unexpectedException(); |
291 |
< |
} |
292 |
< |
|
293 |
< |
} |
294 |
< |
}); |
295 |
< |
try { |
296 |
< |
t.start(); |
297 |
< |
Thread.sleep(SHORT_DELAY_MS); |
298 |
< |
t.interrupt(); |
299 |
< |
} catch(Exception e){ |
300 |
< |
unexpectedException(); |
301 |
< |
} |
302 |
< |
joinPool(p); |
303 |
< |
} |
304 |
< |
|
305 |
< |
/** |
306 |
< |
* get of submit of Callable throws Exception if callable |
307 |
< |
* interrupted |
308 |
< |
*/ |
309 |
< |
public void testSubmitIE() { |
310 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
311 |
< |
|
312 |
< |
final Callable c = new Callable() { |
313 |
< |
public Object call() { |
314 |
< |
try { |
315 |
< |
p.submit(new SmallCallable()).get(); |
316 |
< |
shouldThrow(); |
317 |
< |
} catch(InterruptedException e){} |
318 |
< |
catch(RejectedExecutionException e2){} |
319 |
< |
catch(ExecutionException e3){} |
320 |
< |
return Boolean.TRUE; |
321 |
< |
} |
322 |
< |
}; |
323 |
< |
|
324 |
< |
|
325 |
< |
|
326 |
< |
Thread t = new Thread(new Runnable() { |
327 |
< |
public void run() { |
328 |
< |
try { |
329 |
< |
c.call(); |
330 |
< |
} catch(Exception e){} |
331 |
< |
} |
332 |
< |
}); |
333 |
< |
try { |
191 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
192 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
193 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
194 |
> |
final ExecutorService p |
195 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
196 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
197 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
198 |
> |
public Void realCall() throws InterruptedException { |
199 |
> |
quittingTime.await(); |
200 |
> |
return null; |
201 |
> |
}}; |
202 |
> |
try { |
203 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
204 |
> |
public void realRun() throws Exception { |
205 |
> |
Future<Void> future = p.submit(awaiter); |
206 |
> |
submitted.countDown(); |
207 |
> |
future.get(); |
208 |
> |
}}); |
209 |
|
t.start(); |
210 |
< |
Thread.sleep(SHORT_DELAY_MS); |
210 |
> |
submitted.await(); |
211 |
|
t.interrupt(); |
212 |
|
t.join(); |
213 |
< |
} catch(InterruptedException e){ |
214 |
< |
unexpectedException(); |
213 |
> |
} finally { |
214 |
> |
quittingTime.countDown(); |
215 |
> |
joinPool(p); |
216 |
|
} |
341 |
– |
|
342 |
– |
joinPool(p); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* completed submit of Callable throws ExecutionException if |
221 |
< |
* callable throws exception |
220 |
> |
* get of submit(callable) throws ExecutionException if callable |
221 |
> |
* throws exception |
222 |
|
*/ |
223 |
< |
public void testSubmitEE() { |
224 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
223 |
> |
public void testSubmitEE() throws InterruptedException { |
224 |
> |
ThreadPoolExecutor p = |
225 |
> |
new ThreadPoolExecutor(1, 1, |
226 |
> |
60, TimeUnit.SECONDS, |
227 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
228 |
|
|
229 |
< |
try { |
230 |
< |
Callable c = new Callable() { |
354 |
< |
public Object call() { |
355 |
< |
int i = 5/0; |
356 |
< |
return Boolean.TRUE; |
357 |
< |
} |
358 |
< |
}; |
359 |
< |
|
360 |
< |
for(int i =0; i < 5; i++){ |
361 |
< |
p.submit(c).get(); |
362 |
< |
} |
229 |
> |
Callable c = new Callable() { |
230 |
> |
public Object call() { throw new ArithmeticException(); }}; |
231 |
|
|
232 |
+ |
try { |
233 |
+ |
p.submit(c).get(); |
234 |
|
shouldThrow(); |
235 |
< |
} |
236 |
< |
catch(ExecutionException success){ |
367 |
< |
} catch(Exception e) { |
368 |
< |
unexpectedException(); |
235 |
> |
} catch (ExecutionException success) { |
236 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
237 |
|
} |
238 |
|
joinPool(p); |
239 |
|
} |
241 |
|
/** |
242 |
|
* invokeAny(null) throws NPE |
243 |
|
*/ |
244 |
< |
public void testInvokeAny1() { |
244 |
> |
public void testInvokeAny1() throws Exception { |
245 |
|
ExecutorService e = new DirectExecutorService(); |
246 |
|
try { |
247 |
|
e.invokeAny(null); |
248 |
+ |
shouldThrow(); |
249 |
|
} catch (NullPointerException success) { |
381 |
– |
} catch(Exception ex) { |
382 |
– |
unexpectedException(); |
250 |
|
} finally { |
251 |
|
joinPool(e); |
252 |
|
} |
255 |
|
/** |
256 |
|
* invokeAny(empty collection) throws IAE |
257 |
|
*/ |
258 |
< |
public void testInvokeAny2() { |
258 |
> |
public void testInvokeAny2() throws Exception { |
259 |
|
ExecutorService e = new DirectExecutorService(); |
260 |
|
try { |
261 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
262 |
+ |
shouldThrow(); |
263 |
|
} catch (IllegalArgumentException success) { |
396 |
– |
} catch(Exception ex) { |
397 |
– |
unexpectedException(); |
264 |
|
} finally { |
265 |
|
joinPool(e); |
266 |
|
} |
269 |
|
/** |
270 |
|
* invokeAny(c) throws NPE if c has null elements |
271 |
|
*/ |
272 |
< |
public void testInvokeAny3() { |
272 |
> |
public void testInvokeAny3() throws Exception { |
273 |
|
ExecutorService e = new DirectExecutorService(); |
274 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
275 |
+ |
l.add(new Callable<Long>() { |
276 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
277 |
+ |
l.add(null); |
278 |
|
try { |
409 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
410 |
– |
l.add(new StringTask()); |
411 |
– |
l.add(null); |
279 |
|
e.invokeAny(l); |
280 |
+ |
shouldThrow(); |
281 |
|
} catch (NullPointerException success) { |
414 |
– |
} catch(Exception ex) { |
415 |
– |
ex.printStackTrace(); |
416 |
– |
unexpectedException(); |
282 |
|
} finally { |
283 |
|
joinPool(e); |
284 |
|
} |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* invokeAny(c) throws ExecutionException if no task completes |
288 |
> |
* invokeAny(c) throws ExecutionException if no task in c completes |
289 |
|
*/ |
290 |
< |
public void testInvokeAny4() { |
290 |
> |
public void testInvokeAny4() throws InterruptedException { |
291 |
|
ExecutorService e = new DirectExecutorService(); |
292 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
293 |
+ |
l.add(new NPETask()); |
294 |
|
try { |
428 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
429 |
– |
l.add(new NPETask()); |
295 |
|
e.invokeAny(l); |
296 |
< |
} catch(ExecutionException success) { |
297 |
< |
} catch(Exception ex) { |
298 |
< |
unexpectedException(); |
296 |
> |
shouldThrow(); |
297 |
> |
} catch (ExecutionException success) { |
298 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
299 |
|
} finally { |
300 |
|
joinPool(e); |
301 |
|
} |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
< |
* invokeAny(c) returns result of some task |
305 |
> |
* invokeAny(c) returns result of some task in c if at least one completes |
306 |
|
*/ |
307 |
< |
public void testInvokeAny5() { |
307 |
> |
public void testInvokeAny5() throws Exception { |
308 |
|
ExecutorService e = new DirectExecutorService(); |
309 |
|
try { |
310 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
310 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
311 |
|
l.add(new StringTask()); |
312 |
|
l.add(new StringTask()); |
313 |
|
String result = e.invokeAny(l); |
314 |
|
assertSame(TEST_STRING, result); |
450 |
– |
} catch (ExecutionException success) { |
451 |
– |
} catch(Exception ex) { |
452 |
– |
unexpectedException(); |
315 |
|
} finally { |
316 |
|
joinPool(e); |
317 |
|
} |
320 |
|
/** |
321 |
|
* invokeAll(null) throws NPE |
322 |
|
*/ |
323 |
< |
public void testInvokeAll1() { |
323 |
> |
public void testInvokeAll1() throws InterruptedException { |
324 |
|
ExecutorService e = new DirectExecutorService(); |
325 |
|
try { |
326 |
|
e.invokeAll(null); |
327 |
+ |
shouldThrow(); |
328 |
|
} catch (NullPointerException success) { |
466 |
– |
} catch(Exception ex) { |
467 |
– |
unexpectedException(); |
329 |
|
} finally { |
330 |
|
joinPool(e); |
331 |
|
} |
334 |
|
/** |
335 |
|
* invokeAll(empty collection) returns empty collection |
336 |
|
*/ |
337 |
< |
public void testInvokeAll2() { |
337 |
> |
public void testInvokeAll2() throws InterruptedException { |
338 |
|
ExecutorService e = new DirectExecutorService(); |
339 |
|
try { |
340 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
341 |
|
assertTrue(r.isEmpty()); |
481 |
– |
} catch(Exception ex) { |
482 |
– |
unexpectedException(); |
342 |
|
} finally { |
343 |
|
joinPool(e); |
344 |
|
} |
347 |
|
/** |
348 |
|
* invokeAll(c) throws NPE if c has null elements |
349 |
|
*/ |
350 |
< |
public void testInvokeAll3() { |
350 |
> |
public void testInvokeAll3() throws InterruptedException { |
351 |
|
ExecutorService e = new DirectExecutorService(); |
352 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
353 |
+ |
l.add(new StringTask()); |
354 |
+ |
l.add(null); |
355 |
|
try { |
494 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
495 |
– |
l.add(new StringTask()); |
496 |
– |
l.add(null); |
356 |
|
e.invokeAll(l); |
357 |
+ |
shouldThrow(); |
358 |
|
} catch (NullPointerException success) { |
499 |
– |
} catch(Exception ex) { |
500 |
– |
unexpectedException(); |
359 |
|
} finally { |
360 |
|
joinPool(e); |
361 |
|
} |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
< |
* get of element of invokeAll(c) throws exception on failed task |
365 |
> |
* get of returned element of invokeAll(c) throws exception on failed task |
366 |
|
*/ |
367 |
< |
public void testInvokeAll4() { |
367 |
> |
public void testInvokeAll4() throws Exception { |
368 |
|
ExecutorService e = new DirectExecutorService(); |
369 |
|
try { |
370 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
370 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
371 |
|
l.add(new NPETask()); |
372 |
< |
List<Future<String>> result = e.invokeAll(l); |
373 |
< |
assertEquals(1, result.size()); |
374 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
375 |
< |
it.next().get(); |
376 |
< |
} catch(ExecutionException success) { |
377 |
< |
} catch(Exception ex) { |
378 |
< |
unexpectedException(); |
372 |
> |
List<Future<String>> futures = e.invokeAll(l); |
373 |
> |
assertEquals(1, futures.size()); |
374 |
> |
try { |
375 |
> |
futures.get(0).get(); |
376 |
> |
shouldThrow(); |
377 |
> |
} catch (ExecutionException success) { |
378 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
379 |
> |
} |
380 |
|
} finally { |
381 |
|
joinPool(e); |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
< |
* invokeAll(c) returns results of all completed tasks |
386 |
> |
* invokeAll(c) returns results of all completed tasks in c |
387 |
|
*/ |
388 |
< |
public void testInvokeAll5() { |
388 |
> |
public void testInvokeAll5() throws Exception { |
389 |
|
ExecutorService e = new DirectExecutorService(); |
390 |
|
try { |
391 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
391 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
392 |
|
l.add(new StringTask()); |
393 |
|
l.add(new StringTask()); |
394 |
< |
List<Future<String>> result = e.invokeAll(l); |
395 |
< |
assertEquals(2, result.size()); |
396 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
397 |
< |
assertSame(TEST_STRING, it.next().get()); |
539 |
< |
} catch (ExecutionException success) { |
540 |
< |
} catch(Exception ex) { |
541 |
< |
unexpectedException(); |
394 |
> |
List<Future<String>> futures = e.invokeAll(l); |
395 |
> |
assertEquals(2, futures.size()); |
396 |
> |
for (Future<String> future : futures) |
397 |
> |
assertSame(TEST_STRING, future.get()); |
398 |
|
} finally { |
399 |
|
joinPool(e); |
400 |
|
} |
401 |
|
} |
402 |
|
|
547 |
– |
|
403 |
|
/** |
404 |
|
* timed invokeAny(null) throws NPE |
405 |
|
*/ |
406 |
< |
public void testTimedInvokeAny1() { |
406 |
> |
public void testTimedInvokeAny1() throws Exception { |
407 |
|
ExecutorService e = new DirectExecutorService(); |
408 |
|
try { |
409 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
409 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
410 |
> |
shouldThrow(); |
411 |
|
} catch (NullPointerException success) { |
556 |
– |
} catch(Exception ex) { |
557 |
– |
unexpectedException(); |
412 |
|
} finally { |
413 |
|
joinPool(e); |
414 |
|
} |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* timed invokeAny(,,null) throws NPE |
418 |
> |
* timed invokeAny(null time unit) throws NPE |
419 |
|
*/ |
420 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
420 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
421 |
|
ExecutorService e = new DirectExecutorService(); |
422 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
423 |
+ |
l.add(new StringTask()); |
424 |
|
try { |
569 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
570 |
– |
l.add(new StringTask()); |
425 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
426 |
+ |
shouldThrow(); |
427 |
|
} catch (NullPointerException success) { |
573 |
– |
} catch(Exception ex) { |
574 |
– |
unexpectedException(); |
428 |
|
} finally { |
429 |
|
joinPool(e); |
430 |
|
} |
433 |
|
/** |
434 |
|
* timed invokeAny(empty collection) throws IAE |
435 |
|
*/ |
436 |
< |
public void testTimedInvokeAny2() { |
436 |
> |
public void testTimedInvokeAny2() throws Exception { |
437 |
|
ExecutorService e = new DirectExecutorService(); |
438 |
|
try { |
439 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
439 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
440 |
> |
shouldThrow(); |
441 |
|
} catch (IllegalArgumentException success) { |
588 |
– |
} catch(Exception ex) { |
589 |
– |
unexpectedException(); |
442 |
|
} finally { |
443 |
|
joinPool(e); |
444 |
|
} |
447 |
|
/** |
448 |
|
* timed invokeAny(c) throws NPE if c has null elements |
449 |
|
*/ |
450 |
< |
public void testTimedInvokeAny3() { |
450 |
> |
public void testTimedInvokeAny3() throws Exception { |
451 |
|
ExecutorService e = new DirectExecutorService(); |
452 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
453 |
+ |
l.add(new Callable<Long>() { |
454 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
455 |
+ |
l.add(null); |
456 |
|
try { |
457 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
458 |
< |
l.add(new StringTask()); |
603 |
< |
l.add(null); |
604 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
457 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
458 |
> |
shouldThrow(); |
459 |
|
} catch (NullPointerException success) { |
606 |
– |
} catch(Exception ex) { |
607 |
– |
ex.printStackTrace(); |
608 |
– |
unexpectedException(); |
460 |
|
} finally { |
461 |
|
joinPool(e); |
462 |
|
} |
465 |
|
/** |
466 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
467 |
|
*/ |
468 |
< |
public void testTimedInvokeAny4() { |
468 |
> |
public void testTimedInvokeAny4() throws Exception { |
469 |
|
ExecutorService e = new DirectExecutorService(); |
470 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
471 |
+ |
l.add(new NPETask()); |
472 |
|
try { |
473 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
474 |
< |
l.add(new NPETask()); |
475 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
476 |
< |
} catch(ExecutionException success) { |
624 |
< |
} catch(Exception ex) { |
625 |
< |
unexpectedException(); |
473 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
474 |
> |
shouldThrow(); |
475 |
> |
} catch (ExecutionException success) { |
476 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
477 |
|
} finally { |
478 |
|
joinPool(e); |
479 |
|
} |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
< |
* timed invokeAny(c) returns result of some task |
483 |
> |
* timed invokeAny(c) returns result of some task in c |
484 |
|
*/ |
485 |
< |
public void testTimedInvokeAny5() { |
485 |
> |
public void testTimedInvokeAny5() throws Exception { |
486 |
|
ExecutorService e = new DirectExecutorService(); |
487 |
|
try { |
488 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
488 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
489 |
|
l.add(new StringTask()); |
490 |
|
l.add(new StringTask()); |
491 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
491 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
492 |
|
assertSame(TEST_STRING, result); |
642 |
– |
} catch (ExecutionException success) { |
643 |
– |
} catch(Exception ex) { |
644 |
– |
unexpectedException(); |
493 |
|
} finally { |
494 |
|
joinPool(e); |
495 |
|
} |
498 |
|
/** |
499 |
|
* timed invokeAll(null) throws NPE |
500 |
|
*/ |
501 |
< |
public void testTimedInvokeAll1() { |
501 |
> |
public void testTimedInvokeAll1() throws InterruptedException { |
502 |
|
ExecutorService e = new DirectExecutorService(); |
503 |
|
try { |
504 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
504 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
505 |
> |
shouldThrow(); |
506 |
|
} catch (NullPointerException success) { |
658 |
– |
} catch(Exception ex) { |
659 |
– |
unexpectedException(); |
507 |
|
} finally { |
508 |
|
joinPool(e); |
509 |
|
} |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
< |
* timed invokeAll(,,null) throws NPE |
513 |
> |
* timed invokeAll(null time unit) throws NPE |
514 |
|
*/ |
515 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
515 |
> |
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
516 |
|
ExecutorService e = new DirectExecutorService(); |
517 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
518 |
+ |
l.add(new StringTask()); |
519 |
|
try { |
671 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
672 |
– |
l.add(new StringTask()); |
520 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
521 |
+ |
shouldThrow(); |
522 |
|
} catch (NullPointerException success) { |
675 |
– |
} catch(Exception ex) { |
676 |
– |
unexpectedException(); |
523 |
|
} finally { |
524 |
|
joinPool(e); |
525 |
|
} |
528 |
|
/** |
529 |
|
* timed invokeAll(empty collection) returns empty collection |
530 |
|
*/ |
531 |
< |
public void testTimedInvokeAll2() { |
531 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
532 |
|
ExecutorService e = new DirectExecutorService(); |
533 |
|
try { |
534 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
534 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
535 |
|
assertTrue(r.isEmpty()); |
690 |
– |
} catch(Exception ex) { |
691 |
– |
unexpectedException(); |
536 |
|
} finally { |
537 |
|
joinPool(e); |
538 |
|
} |
541 |
|
/** |
542 |
|
* timed invokeAll(c) throws NPE if c has null elements |
543 |
|
*/ |
544 |
< |
public void testTimedInvokeAll3() { |
544 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
545 |
|
ExecutorService e = new DirectExecutorService(); |
546 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
547 |
+ |
l.add(new StringTask()); |
548 |
+ |
l.add(null); |
549 |
|
try { |
550 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
551 |
< |
l.add(new StringTask()); |
705 |
< |
l.add(null); |
706 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
550 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
551 |
> |
shouldThrow(); |
552 |
|
} catch (NullPointerException success) { |
708 |
– |
} catch(Exception ex) { |
709 |
– |
unexpectedException(); |
553 |
|
} finally { |
554 |
|
joinPool(e); |
555 |
|
} |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* get of element of invokeAll(c) throws exception on failed task |
559 |
> |
* get of returned element of invokeAll(c) throws exception on failed task |
560 |
|
*/ |
561 |
< |
public void testTimedInvokeAll4() { |
561 |
> |
public void testTimedInvokeAll4() throws Exception { |
562 |
|
ExecutorService e = new DirectExecutorService(); |
563 |
|
try { |
564 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
564 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
565 |
|
l.add(new NPETask()); |
566 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
567 |
< |
assertEquals(1, result.size()); |
568 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
569 |
< |
it.next().get(); |
570 |
< |
} catch(ExecutionException success) { |
571 |
< |
} catch(Exception ex) { |
572 |
< |
unexpectedException(); |
566 |
> |
List<Future<String>> futures = |
567 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
568 |
> |
assertEquals(1, futures.size()); |
569 |
> |
try { |
570 |
> |
futures.get(0).get(); |
571 |
> |
shouldThrow(); |
572 |
> |
} catch (ExecutionException success) { |
573 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
574 |
> |
} |
575 |
|
} finally { |
576 |
|
joinPool(e); |
577 |
|
} |
578 |
|
} |
579 |
|
|
580 |
|
/** |
581 |
< |
* timed invokeAll(c) returns results of all completed tasks |
581 |
> |
* timed invokeAll(c) returns results of all completed tasks in c |
582 |
|
*/ |
583 |
< |
public void testTimedInvokeAll5() { |
583 |
> |
public void testTimedInvokeAll5() throws Exception { |
584 |
|
ExecutorService e = new DirectExecutorService(); |
585 |
|
try { |
586 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
586 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
587 |
|
l.add(new StringTask()); |
588 |
|
l.add(new StringTask()); |
589 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
590 |
< |
assertEquals(2, result.size()); |
591 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
592 |
< |
assertSame(TEST_STRING, it.next().get()); |
593 |
< |
} catch (ExecutionException success) { |
749 |
< |
} catch(Exception ex) { |
750 |
< |
unexpectedException(); |
589 |
> |
List<Future<String>> futures = |
590 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
591 |
> |
assertEquals(2, futures.size()); |
592 |
> |
for (Future<String> future : futures) |
593 |
> |
assertSame(TEST_STRING, future.get()); |
594 |
|
} finally { |
595 |
|
joinPool(e); |
596 |
|
} |
597 |
|
} |
598 |
|
|
599 |
|
/** |
600 |
< |
* timed invokeAll(c) cancels tasks not completed by timeout |
600 |
> |
* timed invokeAll cancels tasks not completed by timeout |
601 |
|
*/ |
602 |
< |
public void testTimedInvokeAll6() { |
602 |
> |
public void testTimedInvokeAll6() throws InterruptedException { |
603 |
|
ExecutorService e = new DirectExecutorService(); |
604 |
|
try { |
605 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
605 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
606 |
> |
l.add(new StringTask()); |
607 |
> |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
608 |
|
l.add(new StringTask()); |
609 |
< |
l.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
610 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
611 |
< |
assertEquals(2, result.size()); |
612 |
< |
Iterator<Future<String>> it = result.iterator(); |
613 |
< |
Future<String> f1 = it.next(); |
614 |
< |
Future<String> f2 = it.next(); |
615 |
< |
assertTrue(f1.isDone()); |
616 |
< |
assertFalse(f1.isCancelled()); |
772 |
< |
assertTrue(f2.isDone()); |
773 |
< |
// assertTrue(f2.isCancelled()); |
774 |
< |
} catch(Exception ex) { |
775 |
< |
unexpectedException(); |
609 |
> |
List<Future<String>> futures = |
610 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
611 |
> |
assertEquals(l.size(), futures.size()); |
612 |
> |
for (Future future : futures) |
613 |
> |
assertTrue(future.isDone()); |
614 |
> |
assertFalse(futures.get(0).isCancelled()); |
615 |
> |
assertFalse(futures.get(1).isCancelled()); |
616 |
> |
assertTrue(futures.get(2).isCancelled()); |
617 |
|
} finally { |
618 |
|
joinPool(e); |
619 |
|
} |