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) { |
164 |
< |
unexpectedException(); |
165 |
< |
} |
198 |
< |
finally { |
199 |
< |
Policy.setPolicy(savedPolicy); |
200 |
< |
} |
158 |
> |
try { |
159 |
> |
future.get(); |
160 |
> |
shouldThrow(); |
161 |
> |
} catch (ExecutionException success) { |
162 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
163 |
> |
}}}; |
164 |
> |
|
165 |
> |
runWithPermissions(r); |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
< |
* execute with a null runnable throws NPE |
169 |
> |
* execute(null runnable) throws NPE |
170 |
|
*/ |
171 |
|
public void testExecuteNullRunnable() { |
172 |
+ |
ExecutorService e = new DirectExecutorService(); |
173 |
|
try { |
174 |
< |
ExecutorService e = new DirectExecutorService(); |
209 |
< |
TrackedShortRunnable task = null; |
210 |
< |
Future<?> future = e.submit(task); |
174 |
> |
e.submit((Runnable) null); |
175 |
|
shouldThrow(); |
176 |
< |
} |
213 |
< |
catch (NullPointerException success) { |
214 |
< |
} |
215 |
< |
catch (Exception ex) { |
216 |
< |
unexpectedException(); |
217 |
< |
} |
176 |
> |
} catch (NullPointerException success) {} |
177 |
|
} |
178 |
|
|
220 |
– |
|
179 |
|
/** |
180 |
< |
* submit of a null callable throws NPE |
180 |
> |
* submit(null callable) throws NPE |
181 |
|
*/ |
182 |
|
public void testSubmitNullCallable() { |
183 |
+ |
ExecutorService e = new DirectExecutorService(); |
184 |
|
try { |
185 |
< |
ExecutorService e = new DirectExecutorService(); |
227 |
< |
StringTask t = null; |
228 |
< |
Future<String> future = e.submit(t); |
229 |
< |
shouldThrow(); |
230 |
< |
} |
231 |
< |
catch (NullPointerException success) { |
232 |
< |
} |
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); |
252 |
< |
} |
253 |
< |
|
254 |
< |
/** |
255 |
< |
* Completed submit of Callable throws RejectedExecutionException |
256 |
< |
* if saturated. |
257 |
< |
*/ |
258 |
< |
public void testExecute2() { |
259 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
260 |
< |
try { |
261 |
< |
for(int i = 0; i < 5; ++i) { |
262 |
< |
p.submit(new SmallCallable()); |
263 |
< |
} |
185 |
> |
e.submit((Callable) null); |
186 |
|
shouldThrow(); |
187 |
< |
} catch(RejectedExecutionException e){} |
266 |
< |
joinPool(p); |
187 |
> |
} catch (NullPointerException success) {} |
188 |
|
} |
189 |
|
|
269 |
– |
|
190 |
|
/** |
191 |
< |
* blocking on submit of Callable throws InterruptedException if |
272 |
< |
* caller interrupted. |
191 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
192 |
|
*/ |
193 |
< |
public void testInterruptedSubmit() { |
194 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
195 |
< |
Thread t = new Thread(new Runnable() { |
196 |
< |
public void run() { |
197 |
< |
try { |
198 |
< |
p.submit(new Callable<Object>() { |
199 |
< |
public Object call() { |
200 |
< |
try { |
201 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
202 |
< |
shouldThrow(); |
203 |
< |
} catch(InterruptedException e){ |
204 |
< |
} |
205 |
< |
return null; |
206 |
< |
} |
207 |
< |
}).get(); |
208 |
< |
} catch(InterruptedException success){ |
209 |
< |
} catch(Exception e) { |
210 |
< |
unexpectedException(); |
292 |
< |
} |
193 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
194 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
195 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
196 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
197 |
> |
public Void realCall() throws InterruptedException { |
198 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
199 |
> |
return null; |
200 |
> |
}}; |
201 |
> |
final ExecutorService p |
202 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
203 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
204 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
205 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
206 |
> |
public void realRun() throws Exception { |
207 |
> |
Future<Void> future = p.submit(awaiter); |
208 |
> |
submitted.countDown(); |
209 |
> |
future.get(); |
210 |
> |
}}); |
211 |
|
|
212 |
< |
} |
295 |
< |
}); |
296 |
< |
try { |
297 |
< |
t.start(); |
298 |
< |
Thread.sleep(SHORT_DELAY_MS); |
212 |
> |
await(submitted); |
213 |
|
t.interrupt(); |
214 |
< |
} catch(Exception e){ |
301 |
< |
unexpectedException(); |
214 |
> |
awaitTermination(t); |
215 |
|
} |
303 |
– |
joinPool(p); |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* get of submit of Callable throws Exception if callable |
220 |
< |
* interrupted |
219 |
> |
* get of submit(callable) throws ExecutionException if callable |
220 |
> |
* throws exception |
221 |
|
*/ |
222 |
< |
public void testSubmitIE() { |
223 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
224 |
< |
|
225 |
< |
final Callable c = new Callable() { |
226 |
< |
public Object call() { |
227 |
< |
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 { |
222 |
> |
public void testSubmitEE() throws InterruptedException { |
223 |
> |
final ThreadPoolExecutor p = |
224 |
> |
new ThreadPoolExecutor(1, 1, |
225 |
> |
60, TimeUnit.SECONDS, |
226 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
227 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
228 |
|
Callable c = new Callable() { |
229 |
< |
public Object call() { |
230 |
< |
int i = 5/0; |
357 |
< |
return Boolean.TRUE; |
358 |
< |
} |
359 |
< |
}; |
360 |
< |
|
361 |
< |
for(int i =0; i < 5; i++){ |
229 |
> |
public Object call() { throw new ArithmeticException(); }}; |
230 |
> |
try { |
231 |
|
p.submit(c).get(); |
232 |
+ |
shouldThrow(); |
233 |
+ |
} catch (ExecutionException success) { |
234 |
+ |
assertTrue(success.getCause() instanceof ArithmeticException); |
235 |
|
} |
364 |
– |
|
365 |
– |
shouldThrow(); |
366 |
– |
} |
367 |
– |
catch(ExecutionException success){ |
368 |
– |
} catch(Exception e) { |
369 |
– |
unexpectedException(); |
236 |
|
} |
371 |
– |
joinPool(p); |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
|
* invokeAny(null) throws NPE |
241 |
|
*/ |
242 |
< |
public void testInvokeAny1() { |
243 |
< |
ExecutorService e = new DirectExecutorService(); |
244 |
< |
try { |
245 |
< |
e.invokeAny(null); |
246 |
< |
} catch (NullPointerException success) { |
247 |
< |
} catch(Exception ex) { |
248 |
< |
unexpectedException(); |
384 |
< |
} finally { |
385 |
< |
joinPool(e); |
242 |
> |
public void testInvokeAny1() throws Exception { |
243 |
> |
final ExecutorService e = new DirectExecutorService(); |
244 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
245 |
> |
try { |
246 |
> |
e.invokeAny(null); |
247 |
> |
shouldThrow(); |
248 |
> |
} catch (NullPointerException success) {} |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
< |
* invokeAny(empty collection) throws IAE |
254 |
< |
*/ |
255 |
< |
public void testInvokeAny2() { |
256 |
< |
ExecutorService e = new DirectExecutorService(); |
257 |
< |
try { |
258 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
259 |
< |
} catch (IllegalArgumentException success) { |
260 |
< |
} catch(Exception ex) { |
261 |
< |
unexpectedException(); |
262 |
< |
} finally { |
263 |
< |
joinPool(e); |
253 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
254 |
> |
*/ |
255 |
> |
public void testInvokeAny2() throws Exception { |
256 |
> |
final ExecutorService e = new DirectExecutorService(); |
257 |
> |
final Collection<Callable<String>> emptyCollection |
258 |
> |
= Collections.emptyList(); |
259 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
260 |
> |
try { |
261 |
> |
e.invokeAny(emptyCollection); |
262 |
> |
shouldThrow(); |
263 |
> |
} catch (IllegalArgumentException success) {} |
264 |
|
} |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
|
* invokeAny(c) throws NPE if c has null elements |
269 |
|
*/ |
270 |
< |
public void testInvokeAny3() { |
271 |
< |
ExecutorService e = new DirectExecutorService(); |
272 |
< |
try { |
273 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
274 |
< |
l.add(new StringTask()); |
270 |
> |
public void testInvokeAny3() throws Exception { |
271 |
> |
final ExecutorService e = new DirectExecutorService(); |
272 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
273 |
> |
List<Callable<Long>> l = new ArrayList<>(); |
274 |
> |
l.add(new Callable<Long>() { |
275 |
> |
public Long call() { throw new ArithmeticException(); }}); |
276 |
|
l.add(null); |
277 |
< |
e.invokeAny(l); |
278 |
< |
} catch (NullPointerException success) { |
279 |
< |
} catch(Exception ex) { |
280 |
< |
ex.printStackTrace(); |
417 |
< |
unexpectedException(); |
418 |
< |
} finally { |
419 |
< |
joinPool(e); |
277 |
> |
try { |
278 |
> |
e.invokeAny(l); |
279 |
> |
shouldThrow(); |
280 |
> |
} catch (NullPointerException success) {} |
281 |
|
} |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
< |
* invokeAny(c) throws ExecutionException if no task completes |
285 |
> |
* invokeAny(c) throws ExecutionException if no task in c completes |
286 |
|
*/ |
287 |
< |
public void testInvokeAny4() { |
288 |
< |
ExecutorService e = new DirectExecutorService(); |
289 |
< |
try { |
290 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
287 |
> |
public void testInvokeAny4() throws InterruptedException { |
288 |
> |
final ExecutorService e = new DirectExecutorService(); |
289 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
290 |
> |
List<Callable<String>> l = new ArrayList<>(); |
291 |
|
l.add(new NPETask()); |
292 |
< |
e.invokeAny(l); |
293 |
< |
} catch(ExecutionException success) { |
294 |
< |
} catch(Exception ex) { |
295 |
< |
unexpectedException(); |
296 |
< |
} finally { |
297 |
< |
joinPool(e); |
292 |
> |
try { |
293 |
> |
e.invokeAny(l); |
294 |
> |
shouldThrow(); |
295 |
> |
} catch (ExecutionException success) { |
296 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
297 |
> |
} |
298 |
|
} |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
< |
* invokeAny(c) returns result of some task |
302 |
> |
* invokeAny(c) returns result of some task in c if at least one completes |
303 |
|
*/ |
304 |
< |
public void testInvokeAny5() { |
305 |
< |
ExecutorService e = new DirectExecutorService(); |
306 |
< |
try { |
307 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
304 |
> |
public void testInvokeAny5() throws Exception { |
305 |
> |
final ExecutorService e = new DirectExecutorService(); |
306 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
307 |
> |
List<Callable<String>> l = new ArrayList<>(); |
308 |
|
l.add(new StringTask()); |
309 |
|
l.add(new StringTask()); |
310 |
|
String result = e.invokeAny(l); |
311 |
|
assertSame(TEST_STRING, result); |
451 |
– |
} catch (ExecutionException success) { |
452 |
– |
} catch(Exception ex) { |
453 |
– |
unexpectedException(); |
454 |
– |
} finally { |
455 |
– |
joinPool(e); |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
/** |
316 |
|
* invokeAll(null) throws NPE |
317 |
|
*/ |
318 |
< |
public void testInvokeAll1() { |
319 |
< |
ExecutorService e = new DirectExecutorService(); |
320 |
< |
try { |
321 |
< |
e.invokeAll(null); |
322 |
< |
} catch (NullPointerException success) { |
323 |
< |
} catch(Exception ex) { |
324 |
< |
unexpectedException(); |
469 |
< |
} finally { |
470 |
< |
joinPool(e); |
318 |
> |
public void testInvokeAll1() throws InterruptedException { |
319 |
> |
final ExecutorService e = new DirectExecutorService(); |
320 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
321 |
> |
try { |
322 |
> |
e.invokeAll(null); |
323 |
> |
shouldThrow(); |
324 |
> |
} catch (NullPointerException success) {} |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* invokeAll(empty collection) returns empty collection |
329 |
> |
* invokeAll(empty collection) returns empty list |
330 |
|
*/ |
331 |
< |
public void testInvokeAll2() { |
332 |
< |
ExecutorService e = new DirectExecutorService(); |
333 |
< |
try { |
334 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
331 |
> |
public void testInvokeAll2() throws InterruptedException { |
332 |
> |
final ExecutorService e = new DirectExecutorService(); |
333 |
> |
final Collection<Callable<String>> emptyCollection |
334 |
> |
= Collections.emptyList(); |
335 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
336 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
337 |
|
assertTrue(r.isEmpty()); |
482 |
– |
} catch(Exception ex) { |
483 |
– |
unexpectedException(); |
484 |
– |
} finally { |
485 |
– |
joinPool(e); |
338 |
|
} |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
|
* invokeAll(c) throws NPE if c has null elements |
343 |
|
*/ |
344 |
< |
public void testInvokeAll3() { |
345 |
< |
ExecutorService e = new DirectExecutorService(); |
346 |
< |
try { |
347 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
344 |
> |
public void testInvokeAll3() throws InterruptedException { |
345 |
> |
final ExecutorService e = new DirectExecutorService(); |
346 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
347 |
> |
List<Callable<String>> l = new ArrayList<>(); |
348 |
|
l.add(new StringTask()); |
349 |
|
l.add(null); |
350 |
< |
e.invokeAll(l); |
351 |
< |
} catch (NullPointerException success) { |
352 |
< |
} catch(Exception ex) { |
353 |
< |
unexpectedException(); |
502 |
< |
} finally { |
503 |
< |
joinPool(e); |
350 |
> |
try { |
351 |
> |
e.invokeAll(l); |
352 |
> |
shouldThrow(); |
353 |
> |
} catch (NullPointerException success) {} |
354 |
|
} |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
< |
* get of element of invokeAll(c) throws exception on failed task |
358 |
> |
* get of returned element of invokeAll(c) throws exception on failed task |
359 |
|
*/ |
360 |
< |
public void testInvokeAll4() { |
361 |
< |
ExecutorService e = new DirectExecutorService(); |
362 |
< |
try { |
363 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
360 |
> |
public void testInvokeAll4() throws Exception { |
361 |
> |
final ExecutorService e = new DirectExecutorService(); |
362 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
363 |
> |
List<Callable<String>> l = new ArrayList<>(); |
364 |
|
l.add(new NPETask()); |
365 |
< |
List<Future<String>> result = e.invokeAll(l); |
366 |
< |
assertEquals(1, result.size()); |
367 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
368 |
< |
it.next().get(); |
369 |
< |
} catch(ExecutionException success) { |
370 |
< |
} catch(Exception ex) { |
371 |
< |
unexpectedException(); |
372 |
< |
} finally { |
523 |
< |
joinPool(e); |
365 |
> |
List<Future<String>> futures = e.invokeAll(l); |
366 |
> |
assertEquals(1, futures.size()); |
367 |
> |
try { |
368 |
> |
futures.get(0).get(); |
369 |
> |
shouldThrow(); |
370 |
> |
} catch (ExecutionException success) { |
371 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
372 |
> |
} |
373 |
|
} |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
< |
* invokeAll(c) returns results of all completed tasks |
377 |
> |
* invokeAll(c) returns results of all completed tasks in c |
378 |
|
*/ |
379 |
< |
public void testInvokeAll5() { |
380 |
< |
ExecutorService e = new DirectExecutorService(); |
381 |
< |
try { |
382 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
379 |
> |
public void testInvokeAll5() throws Exception { |
380 |
> |
final ExecutorService e = new DirectExecutorService(); |
381 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
382 |
> |
List<Callable<String>> l = new ArrayList<>(); |
383 |
|
l.add(new StringTask()); |
384 |
|
l.add(new StringTask()); |
385 |
< |
List<Future<String>> result = e.invokeAll(l); |
386 |
< |
assertEquals(2, result.size()); |
387 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
388 |
< |
assertSame(TEST_STRING, it.next().get()); |
540 |
< |
} catch (ExecutionException success) { |
541 |
< |
} catch(Exception ex) { |
542 |
< |
unexpectedException(); |
543 |
< |
} finally { |
544 |
< |
joinPool(e); |
385 |
> |
List<Future<String>> futures = e.invokeAll(l); |
386 |
> |
assertEquals(2, futures.size()); |
387 |
> |
for (Future<String> future : futures) |
388 |
> |
assertSame(TEST_STRING, future.get()); |
389 |
|
} |
390 |
|
} |
391 |
|
|
548 |
– |
|
392 |
|
/** |
393 |
|
* timed invokeAny(null) throws NPE |
394 |
|
*/ |
395 |
< |
public void testTimedInvokeAny1() { |
396 |
< |
ExecutorService e = new DirectExecutorService(); |
397 |
< |
try { |
398 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
399 |
< |
} catch (NullPointerException success) { |
400 |
< |
} catch(Exception ex) { |
401 |
< |
unexpectedException(); |
559 |
< |
} finally { |
560 |
< |
joinPool(e); |
395 |
> |
public void testTimedInvokeAny1() throws Exception { |
396 |
> |
final ExecutorService e = new DirectExecutorService(); |
397 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
398 |
> |
try { |
399 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
400 |
> |
shouldThrow(); |
401 |
> |
} catch (NullPointerException success) {} |
402 |
|
} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* timed invokeAny(,,null) throws NPE |
406 |
> |
* timed invokeAny(null time unit) throws NullPointerException |
407 |
|
*/ |
408 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
409 |
< |
ExecutorService e = new DirectExecutorService(); |
410 |
< |
try { |
411 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
408 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
409 |
> |
final ExecutorService e = new DirectExecutorService(); |
410 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
411 |
> |
List<Callable<String>> l = new ArrayList<>(); |
412 |
|
l.add(new StringTask()); |
413 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
414 |
< |
} catch (NullPointerException success) { |
415 |
< |
} catch(Exception ex) { |
416 |
< |
unexpectedException(); |
576 |
< |
} finally { |
577 |
< |
joinPool(e); |
413 |
> |
try { |
414 |
> |
e.invokeAny(l, randomTimeout(), null); |
415 |
> |
shouldThrow(); |
416 |
> |
} catch (NullPointerException success) {} |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* timed invokeAny(empty collection) throws IAE |
421 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
422 |
|
*/ |
423 |
< |
public void testTimedInvokeAny2() { |
424 |
< |
ExecutorService e = new DirectExecutorService(); |
425 |
< |
try { |
426 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
427 |
< |
} catch (IllegalArgumentException success) { |
428 |
< |
} catch(Exception ex) { |
429 |
< |
unexpectedException(); |
430 |
< |
} finally { |
431 |
< |
joinPool(e); |
423 |
> |
public void testTimedInvokeAny2() throws Exception { |
424 |
> |
final ExecutorService e = new DirectExecutorService(); |
425 |
> |
final Collection<Callable<String>> emptyCollection |
426 |
> |
= Collections.emptyList(); |
427 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
428 |
> |
try { |
429 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
430 |
> |
shouldThrow(); |
431 |
> |
} catch (IllegalArgumentException success) {} |
432 |
|
} |
433 |
|
} |
434 |
|
|
435 |
|
/** |
436 |
|
* timed invokeAny(c) throws NPE if c has null elements |
437 |
|
*/ |
438 |
< |
public void testTimedInvokeAny3() { |
439 |
< |
ExecutorService e = new DirectExecutorService(); |
440 |
< |
try { |
441 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
442 |
< |
l.add(new StringTask()); |
438 |
> |
public void testTimedInvokeAny3() throws Exception { |
439 |
> |
final ExecutorService e = new DirectExecutorService(); |
440 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
441 |
> |
List<Callable<Long>> l = new ArrayList<>(); |
442 |
> |
l.add(new Callable<Long>() { |
443 |
> |
public Long call() { throw new ArithmeticException(); }}); |
444 |
|
l.add(null); |
445 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
446 |
< |
} catch (NullPointerException success) { |
447 |
< |
} catch(Exception ex) { |
448 |
< |
ex.printStackTrace(); |
609 |
< |
unexpectedException(); |
610 |
< |
} finally { |
611 |
< |
joinPool(e); |
445 |
> |
try { |
446 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
447 |
> |
shouldThrow(); |
448 |
> |
} catch (NullPointerException success) {} |
449 |
|
} |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
454 |
|
*/ |
455 |
< |
public void testTimedInvokeAny4() { |
456 |
< |
ExecutorService e = new DirectExecutorService(); |
457 |
< |
try { |
458 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
455 |
> |
public void testTimedInvokeAny4() throws Exception { |
456 |
> |
final ExecutorService e = new DirectExecutorService(); |
457 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
458 |
> |
long startTime = System.nanoTime(); |
459 |
> |
List<Callable<String>> l = new ArrayList<>(); |
460 |
|
l.add(new NPETask()); |
461 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
462 |
< |
} catch(ExecutionException success) { |
463 |
< |
} catch(Exception ex) { |
464 |
< |
unexpectedException(); |
465 |
< |
} finally { |
466 |
< |
joinPool(e); |
461 |
> |
try { |
462 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
463 |
> |
shouldThrow(); |
464 |
> |
} catch (ExecutionException success) { |
465 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
466 |
> |
} |
467 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
|
/** |
472 |
< |
* timed invokeAny(c) returns result of some task |
472 |
> |
* timed invokeAny(c) returns result of some task in c |
473 |
|
*/ |
474 |
< |
public void testTimedInvokeAny5() { |
475 |
< |
ExecutorService e = new DirectExecutorService(); |
476 |
< |
try { |
477 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
474 |
> |
public void testTimedInvokeAny5() throws Exception { |
475 |
> |
final ExecutorService e = new DirectExecutorService(); |
476 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
477 |
> |
long startTime = System.nanoTime(); |
478 |
> |
List<Callable<String>> l = new ArrayList<>(); |
479 |
|
l.add(new StringTask()); |
480 |
|
l.add(new StringTask()); |
481 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
481 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
482 |
|
assertSame(TEST_STRING, result); |
483 |
< |
} catch (ExecutionException success) { |
644 |
< |
} catch(Exception ex) { |
645 |
< |
unexpectedException(); |
646 |
< |
} finally { |
647 |
< |
joinPool(e); |
483 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
484 |
|
} |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
< |
* timed invokeAll(null) throws NPE |
488 |
> |
* timed invokeAll(null) throws NullPointerException |
489 |
|
*/ |
490 |
< |
public void testTimedInvokeAll1() { |
491 |
< |
ExecutorService e = new DirectExecutorService(); |
492 |
< |
try { |
493 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
494 |
< |
} catch (NullPointerException success) { |
495 |
< |
} catch(Exception ex) { |
496 |
< |
unexpectedException(); |
661 |
< |
} finally { |
662 |
< |
joinPool(e); |
490 |
> |
public void testTimedInvokeAll1() throws InterruptedException { |
491 |
> |
final ExecutorService e = new DirectExecutorService(); |
492 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
493 |
> |
try { |
494 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
495 |
> |
shouldThrow(); |
496 |
> |
} catch (NullPointerException success) {} |
497 |
|
} |
498 |
|
} |
499 |
|
|
500 |
|
/** |
501 |
< |
* timed invokeAll(,,null) throws NPE |
501 |
> |
* timed invokeAll(null time unit) throws NPE |
502 |
|
*/ |
503 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
504 |
< |
ExecutorService e = new DirectExecutorService(); |
505 |
< |
try { |
506 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
503 |
> |
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
504 |
> |
final ExecutorService e = new DirectExecutorService(); |
505 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
506 |
> |
List<Callable<String>> l = new ArrayList<>(); |
507 |
|
l.add(new StringTask()); |
508 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
509 |
< |
} catch (NullPointerException success) { |
510 |
< |
} catch(Exception ex) { |
511 |
< |
unexpectedException(); |
678 |
< |
} finally { |
679 |
< |
joinPool(e); |
508 |
> |
try { |
509 |
> |
e.invokeAll(l, randomTimeout(), null); |
510 |
> |
shouldThrow(); |
511 |
> |
} catch (NullPointerException success) {} |
512 |
|
} |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
< |
* timed invokeAll(empty collection) returns empty collection |
516 |
> |
* timed invokeAll(empty collection) returns empty list |
517 |
|
*/ |
518 |
< |
public void testTimedInvokeAll2() { |
519 |
< |
ExecutorService e = new DirectExecutorService(); |
520 |
< |
try { |
521 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
518 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
519 |
> |
final ExecutorService e = new DirectExecutorService(); |
520 |
> |
final Collection<Callable<String>> emptyCollection |
521 |
> |
= Collections.emptyList(); |
522 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
523 |
> |
List<Future<String>> r = |
524 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
525 |
|
assertTrue(r.isEmpty()); |
691 |
– |
} catch(Exception ex) { |
692 |
– |
unexpectedException(); |
693 |
– |
} finally { |
694 |
– |
joinPool(e); |
526 |
|
} |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* timed invokeAll(c) throws NPE if c has null elements |
530 |
> |
* timed invokeAll(c) throws NullPointerException if c has null elements |
531 |
|
*/ |
532 |
< |
public void testTimedInvokeAll3() { |
533 |
< |
ExecutorService e = new DirectExecutorService(); |
534 |
< |
try { |
535 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
532 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
533 |
> |
final ExecutorService e = new DirectExecutorService(); |
534 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
535 |
> |
List<Callable<String>> l = new ArrayList<>(); |
536 |
|
l.add(new StringTask()); |
537 |
|
l.add(null); |
538 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
539 |
< |
} catch (NullPointerException success) { |
540 |
< |
} catch(Exception ex) { |
541 |
< |
unexpectedException(); |
711 |
< |
} finally { |
712 |
< |
joinPool(e); |
538 |
> |
try { |
539 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
540 |
> |
shouldThrow(); |
541 |
> |
} catch (NullPointerException success) {} |
542 |
|
} |
543 |
|
} |
544 |
|
|
545 |
|
/** |
546 |
< |
* get of element of invokeAll(c) throws exception on failed task |
546 |
> |
* get of returned element of invokeAll(c) throws exception on failed task |
547 |
|
*/ |
548 |
< |
public void testTimedInvokeAll4() { |
549 |
< |
ExecutorService e = new DirectExecutorService(); |
550 |
< |
try { |
551 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
548 |
> |
public void testTimedInvokeAll4() throws Exception { |
549 |
> |
final ExecutorService e = new DirectExecutorService(); |
550 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
551 |
> |
List<Callable<String>> l = new ArrayList<>(); |
552 |
|
l.add(new NPETask()); |
553 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
554 |
< |
assertEquals(1, result.size()); |
555 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
556 |
< |
it.next().get(); |
557 |
< |
} catch(ExecutionException success) { |
558 |
< |
} catch(Exception ex) { |
559 |
< |
unexpectedException(); |
560 |
< |
} finally { |
561 |
< |
joinPool(e); |
553 |
> |
List<Future<String>> futures = |
554 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
555 |
> |
assertEquals(1, futures.size()); |
556 |
> |
try { |
557 |
> |
futures.get(0).get(); |
558 |
> |
shouldThrow(); |
559 |
> |
} catch (ExecutionException success) { |
560 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
561 |
> |
} |
562 |
|
} |
563 |
|
} |
564 |
|
|
565 |
|
/** |
566 |
< |
* timed invokeAll(c) returns results of all completed tasks |
566 |
> |
* timed invokeAll(c) returns results of all completed tasks in c |
567 |
|
*/ |
568 |
< |
public void testTimedInvokeAll5() { |
569 |
< |
ExecutorService e = new DirectExecutorService(); |
570 |
< |
try { |
571 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
572 |
< |
l.add(new StringTask()); |
573 |
< |
l.add(new StringTask()); |
574 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
575 |
< |
assertEquals(2, result.size()); |
576 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
577 |
< |
assertSame(TEST_STRING, it.next().get()); |
578 |
< |
} catch (ExecutionException success) { |
750 |
< |
} catch(Exception ex) { |
751 |
< |
unexpectedException(); |
752 |
< |
} finally { |
753 |
< |
joinPool(e); |
568 |
> |
public void testTimedInvokeAll5() throws Exception { |
569 |
> |
final ExecutorService e = new DirectExecutorService(); |
570 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
571 |
> |
List<Callable<String>> l = new ArrayList<>(); |
572 |
> |
l.add(new StringTask()); |
573 |
> |
l.add(new StringTask()); |
574 |
> |
List<Future<String>> futures = |
575 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
576 |
> |
assertEquals(2, futures.size()); |
577 |
> |
for (Future<String> future : futures) |
578 |
> |
assertSame(TEST_STRING, future.get()); |
579 |
|
} |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
< |
* timed invokeAll(c) cancels tasks not completed by timeout |
584 |
< |
*/ |
585 |
< |
public void testTimedInvokeAll6() { |
586 |
< |
ExecutorService e = new DirectExecutorService(); |
587 |
< |
try { |
588 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
589 |
< |
l.add(new StringTask()); |
590 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
591 |
< |
l.add(new StringTask()); |
592 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
593 |
< |
assertEquals(3, result.size()); |
594 |
< |
Iterator<Future<String>> it = result.iterator(); |
595 |
< |
Future<String> f1 = it.next(); |
596 |
< |
Future<String> f2 = it.next(); |
597 |
< |
Future<String> f3 = it.next(); |
598 |
< |
assertTrue(f1.isDone()); |
599 |
< |
assertFalse(f1.isCancelled()); |
600 |
< |
assertTrue(f2.isDone()); |
601 |
< |
assertTrue(f3.isDone()); |
602 |
< |
assertTrue(f3.isCancelled()); |
603 |
< |
} catch(Exception ex) { |
604 |
< |
unexpectedException(); |
605 |
< |
} finally { |
606 |
< |
joinPool(e); |
583 |
> |
* timed invokeAll cancels tasks not completed by timeout |
584 |
> |
*/ |
585 |
> |
public void testTimedInvokeAll6() throws Exception { |
586 |
> |
final ExecutorService e = new DirectExecutorService(); |
587 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
588 |
> |
for (long timeout = timeoutMillis();;) { |
589 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
590 |
> |
tasks.add(new StringTask("0")); |
591 |
> |
tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout), |
592 |
> |
TEST_STRING)); |
593 |
> |
tasks.add(new StringTask("2")); |
594 |
> |
long startTime = System.nanoTime(); |
595 |
> |
List<Future<String>> futures = |
596 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
597 |
> |
assertEquals(tasks.size(), futures.size()); |
598 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
599 |
> |
for (Future future : futures) |
600 |
> |
assertTrue(future.isDone()); |
601 |
> |
try { |
602 |
> |
assertEquals("0", futures.get(0).get()); |
603 |
> |
assertEquals(TEST_STRING, futures.get(1).get()); |
604 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
605 |
> |
// unusual delay before starting second task |
606 |
> |
timeout *= 2; |
607 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
608 |
> |
fail("expected exactly one task to be cancelled"); |
609 |
> |
continue; |
610 |
> |
} |
611 |
> |
assertTrue(futures.get(2).isCancelled()); |
612 |
> |
break; |
613 |
> |
} |
614 |
|
} |
615 |
|
} |
616 |
|
|