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