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