35394
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation.
|
|
7 |
*
|
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
12 |
* accompanied this code).
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License version
|
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
17 |
*
|
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
19 |
* or visit www.oracle.com if you need additional information or have any
|
|
20 |
* questions.
|
|
21 |
*/
|
|
22 |
|
|
23 |
/*
|
|
24 |
* This file is available under and governed by the GNU General Public
|
|
25 |
* License version 2 only, as published by the Free Software Foundation.
|
|
26 |
* However, the following notice accompanied the original version of this
|
|
27 |
* file:
|
|
28 |
*
|
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166
|
|
30 |
* Expert Group and released to the public domain, as explained at
|
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes,
|
|
33 |
* Pat Fisher, Mike Judd.
|
|
34 |
*/
|
|
35 |
|
|
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
|
37 |
|
|
38 |
import java.security.PrivilegedAction;
|
|
39 |
import java.security.PrivilegedExceptionAction;
|
|
40 |
import java.util.ArrayList;
|
|
41 |
import java.util.Collections;
|
|
42 |
import java.util.List;
|
|
43 |
import java.util.concurrent.AbstractExecutorService;
|
|
44 |
import java.util.concurrent.ArrayBlockingQueue;
|
|
45 |
import java.util.concurrent.Callable;
|
|
46 |
import java.util.concurrent.CancellationException;
|
|
47 |
import java.util.concurrent.CountDownLatch;
|
|
48 |
import java.util.concurrent.ExecutionException;
|
|
49 |
import java.util.concurrent.Executors;
|
|
50 |
import java.util.concurrent.ExecutorService;
|
|
51 |
import java.util.concurrent.Future;
|
|
52 |
import java.util.concurrent.ThreadPoolExecutor;
|
|
53 |
import java.util.concurrent.TimeUnit;
|
|
54 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
55 |
|
|
56 |
import junit.framework.Test;
|
|
57 |
import junit.framework.TestSuite;
|
|
58 |
|
|
59 |
public class AbstractExecutorServiceTest extends JSR166TestCase {
|
|
60 |
public static void main(String[] args) {
|
|
61 |
main(suite(), args);
|
|
62 |
}
|
|
63 |
public static Test suite() {
|
|
64 |
return new TestSuite(AbstractExecutorServiceTest.class);
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* A no-frills implementation of AbstractExecutorService, designed
|
|
69 |
* to test the submit methods only.
|
|
70 |
*/
|
|
71 |
static class DirectExecutorService extends AbstractExecutorService {
|
|
72 |
public void execute(Runnable r) { r.run(); }
|
|
73 |
public void shutdown() { shutdown = true; }
|
|
74 |
public List<Runnable> shutdownNow() {
|
|
75 |
shutdown = true;
|
|
76 |
return Collections.EMPTY_LIST;
|
|
77 |
}
|
|
78 |
public boolean isShutdown() { return shutdown; }
|
|
79 |
public boolean isTerminated() { return isShutdown(); }
|
|
80 |
public boolean awaitTermination(long timeout, TimeUnit unit) {
|
|
81 |
return isShutdown();
|
|
82 |
}
|
|
83 |
private volatile boolean shutdown = false;
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* execute(runnable) runs it to completion
|
|
88 |
*/
|
|
89 |
public void testExecuteRunnable() throws Exception {
|
|
90 |
ExecutorService e = new DirectExecutorService();
|
|
91 |
final AtomicBoolean done = new AtomicBoolean(false);
|
|
92 |
Future<?> future = e.submit(new CheckedRunnable() {
|
|
93 |
public void realRun() {
|
|
94 |
done.set(true);
|
|
95 |
}});
|
|
96 |
assertNull(future.get());
|
|
97 |
assertNull(future.get(0, MILLISECONDS));
|
|
98 |
assertTrue(done.get());
|
|
99 |
assertTrue(future.isDone());
|
|
100 |
assertFalse(future.isCancelled());
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* Completed submit(callable) returns result
|
|
105 |
*/
|
|
106 |
public void testSubmitCallable() throws Exception {
|
|
107 |
ExecutorService e = new DirectExecutorService();
|
|
108 |
Future<String> future = e.submit(new StringTask());
|
|
109 |
String result = future.get();
|
|
110 |
assertSame(TEST_STRING, result);
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Completed submit(runnable) returns successfully
|
|
115 |
*/
|
|
116 |
public void testSubmitRunnable() throws Exception {
|
|
117 |
ExecutorService e = new DirectExecutorService();
|
|
118 |
Future<?> future = e.submit(new NoOpRunnable());
|
|
119 |
future.get();
|
|
120 |
assertTrue(future.isDone());
|
|
121 |
}
|
|
122 |
|
|
123 |
/**
|
|
124 |
* Completed submit(runnable, result) returns result
|
|
125 |
*/
|
|
126 |
public void testSubmitRunnable2() throws Exception {
|
|
127 |
ExecutorService e = new DirectExecutorService();
|
|
128 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
|
|
129 |
String result = future.get();
|
|
130 |
assertSame(TEST_STRING, result);
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* A submitted privileged action runs to completion
|
|
135 |
*/
|
|
136 |
public void testSubmitPrivilegedAction() throws Exception {
|
|
137 |
Runnable r = new CheckedRunnable() {
|
|
138 |
public void realRun() throws Exception {
|
|
139 |
ExecutorService e = new DirectExecutorService();
|
|
140 |
Future future = e.submit(Executors.callable(new PrivilegedAction() {
|
|
141 |
public Object run() {
|
|
142 |
return TEST_STRING;
|
|
143 |
}}));
|
|
144 |
|
|
145 |
assertSame(TEST_STRING, future.get());
|
|
146 |
}};
|
|
147 |
|
|
148 |
runWithPermissions(r,
|
|
149 |
new RuntimePermission("getClassLoader"),
|
|
150 |
new RuntimePermission("setContextClassLoader"),
|
|
151 |
new RuntimePermission("modifyThread"));
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* A submitted privileged exception action runs to completion
|
|
156 |
*/
|
|
157 |
public void testSubmitPrivilegedExceptionAction() throws Exception {
|
|
158 |
Runnable r = new CheckedRunnable() {
|
|
159 |
public void realRun() throws Exception {
|
|
160 |
ExecutorService e = new DirectExecutorService();
|
|
161 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
|
|
162 |
public Object run() {
|
|
163 |
return TEST_STRING;
|
|
164 |
}}));
|
|
165 |
|
|
166 |
assertSame(TEST_STRING, future.get());
|
|
167 |
}};
|
|
168 |
|
|
169 |
runWithPermissions(r);
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* A submitted failed privileged exception action reports exception
|
|
174 |
*/
|
|
175 |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
|
|
176 |
Runnable r = new CheckedRunnable() {
|
|
177 |
public void realRun() throws Exception {
|
|
178 |
ExecutorService e = new DirectExecutorService();
|
|
179 |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
|
|
180 |
public Object run() throws Exception {
|
|
181 |
throw new IndexOutOfBoundsException();
|
|
182 |
}}));
|
|
183 |
|
|
184 |
try {
|
|
185 |
future.get();
|
|
186 |
shouldThrow();
|
|
187 |
} catch (ExecutionException success) {
|
|
188 |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
|
|
189 |
}}};
|
|
190 |
|
|
191 |
runWithPermissions(r);
|
|
192 |
}
|
|
193 |
|
|
194 |
/**
|
|
195 |
* execute(null runnable) throws NPE
|
|
196 |
*/
|
|
197 |
public void testExecuteNullRunnable() {
|
|
198 |
ExecutorService e = new DirectExecutorService();
|
|
199 |
try {
|
|
200 |
e.submit((Runnable) null);
|
|
201 |
shouldThrow();
|
|
202 |
} catch (NullPointerException success) {}
|
|
203 |
}
|
|
204 |
|
|
205 |
/**
|
|
206 |
* submit(null callable) throws NPE
|
|
207 |
*/
|
|
208 |
public void testSubmitNullCallable() {
|
|
209 |
ExecutorService e = new DirectExecutorService();
|
|
210 |
try {
|
|
211 |
e.submit((Callable) null);
|
|
212 |
shouldThrow();
|
|
213 |
} catch (NullPointerException success) {}
|
|
214 |
}
|
|
215 |
|
|
216 |
/**
|
|
217 |
* submit(callable).get() throws InterruptedException if interrupted
|
|
218 |
*/
|
|
219 |
public void testInterruptedSubmit() throws InterruptedException {
|
|
220 |
final CountDownLatch submitted = new CountDownLatch(1);
|
|
221 |
final CountDownLatch quittingTime = new CountDownLatch(1);
|
|
222 |
final Callable<Void> awaiter = new CheckedCallable<Void>() {
|
|
223 |
public Void realCall() throws InterruptedException {
|
|
224 |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
|
|
225 |
return null;
|
|
226 |
}};
|
|
227 |
final ExecutorService p
|
|
228 |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
|
|
229 |
new ArrayBlockingQueue<Runnable>(10));
|
|
230 |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
|
|
231 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
|
|
232 |
public void realRun() throws Exception {
|
|
233 |
Future<Void> future = p.submit(awaiter);
|
|
234 |
submitted.countDown();
|
|
235 |
future.get();
|
|
236 |
}});
|
|
237 |
|
|
238 |
await(submitted);
|
|
239 |
t.interrupt();
|
|
240 |
awaitTermination(t);
|
|
241 |
}
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* get of submit(callable) throws ExecutionException if callable
|
|
246 |
* throws exception
|
|
247 |
*/
|
|
248 |
public void testSubmitEE() throws InterruptedException {
|
|
249 |
final ThreadPoolExecutor p =
|
|
250 |
new ThreadPoolExecutor(1, 1,
|
|
251 |
60, TimeUnit.SECONDS,
|
|
252 |
new ArrayBlockingQueue<Runnable>(10));
|
|
253 |
try (PoolCleaner cleaner = cleaner(p)) {
|
|
254 |
Callable c = new Callable() {
|
|
255 |
public Object call() { throw new ArithmeticException(); }};
|
|
256 |
try {
|
|
257 |
p.submit(c).get();
|
|
258 |
shouldThrow();
|
|
259 |
} catch (ExecutionException success) {
|
|
260 |
assertTrue(success.getCause() instanceof ArithmeticException);
|
|
261 |
}
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* invokeAny(null) throws NPE
|
|
267 |
*/
|
|
268 |
public void testInvokeAny1() throws Exception {
|
|
269 |
final ExecutorService e = new DirectExecutorService();
|
|
270 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
271 |
try {
|
|
272 |
e.invokeAny(null);
|
|
273 |
shouldThrow();
|
|
274 |
} catch (NullPointerException success) {}
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
/**
|
|
279 |
* invokeAny(empty collection) throws IAE
|
|
280 |
*/
|
|
281 |
public void testInvokeAny2() throws Exception {
|
|
282 |
final ExecutorService e = new DirectExecutorService();
|
|
283 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
284 |
try {
|
|
285 |
e.invokeAny(new ArrayList<Callable<String>>());
|
|
286 |
shouldThrow();
|
|
287 |
} catch (IllegalArgumentException success) {}
|
|
288 |
}
|
|
289 |
}
|
|
290 |
|
|
291 |
/**
|
|
292 |
* invokeAny(c) throws NPE if c has null elements
|
|
293 |
*/
|
|
294 |
public void testInvokeAny3() throws Exception {
|
|
295 |
final ExecutorService e = new DirectExecutorService();
|
|
296 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
297 |
List<Callable<Long>> l = new ArrayList<Callable<Long>>();
|
|
298 |
l.add(new Callable<Long>() {
|
|
299 |
public Long call() { throw new ArithmeticException(); }});
|
|
300 |
l.add(null);
|
|
301 |
try {
|
|
302 |
e.invokeAny(l);
|
|
303 |
shouldThrow();
|
|
304 |
} catch (NullPointerException success) {}
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
/**
|
|
309 |
* invokeAny(c) throws ExecutionException if no task in c completes
|
|
310 |
*/
|
|
311 |
public void testInvokeAny4() throws InterruptedException {
|
|
312 |
final ExecutorService e = new DirectExecutorService();
|
|
313 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
314 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
315 |
l.add(new NPETask());
|
|
316 |
try {
|
|
317 |
e.invokeAny(l);
|
|
318 |
shouldThrow();
|
|
319 |
} catch (ExecutionException success) {
|
|
320 |
assertTrue(success.getCause() instanceof NullPointerException);
|
|
321 |
}
|
|
322 |
}
|
|
323 |
}
|
|
324 |
|
|
325 |
/**
|
|
326 |
* invokeAny(c) returns result of some task in c if at least one completes
|
|
327 |
*/
|
|
328 |
public void testInvokeAny5() throws Exception {
|
|
329 |
final ExecutorService e = new DirectExecutorService();
|
|
330 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
331 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
332 |
l.add(new StringTask());
|
|
333 |
l.add(new StringTask());
|
|
334 |
String result = e.invokeAny(l);
|
|
335 |
assertSame(TEST_STRING, result);
|
|
336 |
}
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* invokeAll(null) throws NPE
|
|
341 |
*/
|
|
342 |
public void testInvokeAll1() throws InterruptedException {
|
|
343 |
final ExecutorService e = new DirectExecutorService();
|
|
344 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
345 |
try {
|
|
346 |
e.invokeAll(null);
|
|
347 |
shouldThrow();
|
|
348 |
} catch (NullPointerException success) {}
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
/**
|
|
353 |
* invokeAll(empty collection) returns empty collection
|
|
354 |
*/
|
|
355 |
public void testInvokeAll2() throws InterruptedException {
|
|
356 |
final ExecutorService e = new DirectExecutorService();
|
|
357 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
358 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
|
|
359 |
assertTrue(r.isEmpty());
|
|
360 |
}
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* invokeAll(c) throws NPE if c has null elements
|
|
365 |
*/
|
|
366 |
public void testInvokeAll3() throws InterruptedException {
|
|
367 |
final ExecutorService e = new DirectExecutorService();
|
|
368 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
369 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
370 |
l.add(new StringTask());
|
|
371 |
l.add(null);
|
|
372 |
try {
|
|
373 |
e.invokeAll(l);
|
|
374 |
shouldThrow();
|
|
375 |
} catch (NullPointerException success) {}
|
|
376 |
}
|
|
377 |
}
|
|
378 |
|
|
379 |
/**
|
|
380 |
* get of returned element of invokeAll(c) throws exception on failed task
|
|
381 |
*/
|
|
382 |
public void testInvokeAll4() throws Exception {
|
|
383 |
final ExecutorService e = new DirectExecutorService();
|
|
384 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
385 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
386 |
l.add(new NPETask());
|
|
387 |
List<Future<String>> futures = e.invokeAll(l);
|
|
388 |
assertEquals(1, futures.size());
|
|
389 |
try {
|
|
390 |
futures.get(0).get();
|
|
391 |
shouldThrow();
|
|
392 |
} catch (ExecutionException success) {
|
|
393 |
assertTrue(success.getCause() instanceof NullPointerException);
|
|
394 |
}
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
|
398 |
/**
|
|
399 |
* invokeAll(c) returns results of all completed tasks in c
|
|
400 |
*/
|
|
401 |
public void testInvokeAll5() throws Exception {
|
|
402 |
final ExecutorService e = new DirectExecutorService();
|
|
403 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
404 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
405 |
l.add(new StringTask());
|
|
406 |
l.add(new StringTask());
|
|
407 |
List<Future<String>> futures = e.invokeAll(l);
|
|
408 |
assertEquals(2, futures.size());
|
|
409 |
for (Future<String> future : futures)
|
|
410 |
assertSame(TEST_STRING, future.get());
|
|
411 |
}
|
|
412 |
}
|
|
413 |
|
|
414 |
/**
|
|
415 |
* timed invokeAny(null) throws NPE
|
|
416 |
*/
|
|
417 |
public void testTimedInvokeAny1() throws Exception {
|
|
418 |
final ExecutorService e = new DirectExecutorService();
|
|
419 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
420 |
try {
|
|
421 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
|
|
422 |
shouldThrow();
|
|
423 |
} catch (NullPointerException success) {}
|
|
424 |
}
|
|
425 |
}
|
|
426 |
|
|
427 |
/**
|
|
428 |
* timed invokeAny(null time unit) throws NPE
|
|
429 |
*/
|
|
430 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception {
|
|
431 |
final ExecutorService e = new DirectExecutorService();
|
|
432 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
433 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
434 |
l.add(new StringTask());
|
|
435 |
try {
|
|
436 |
e.invokeAny(l, MEDIUM_DELAY_MS, null);
|
|
437 |
shouldThrow();
|
|
438 |
} catch (NullPointerException success) {}
|
|
439 |
}
|
|
440 |
}
|
|
441 |
|
|
442 |
/**
|
|
443 |
* timed invokeAny(empty collection) throws IAE
|
|
444 |
*/
|
|
445 |
public void testTimedInvokeAny2() throws Exception {
|
|
446 |
final ExecutorService e = new DirectExecutorService();
|
|
447 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
448 |
try {
|
|
449 |
e.invokeAny(new ArrayList<Callable<String>>(),
|
|
450 |
MEDIUM_DELAY_MS, MILLISECONDS);
|
|
451 |
shouldThrow();
|
|
452 |
} catch (IllegalArgumentException success) {}
|
|
453 |
}
|
|
454 |
}
|
|
455 |
|
|
456 |
/**
|
|
457 |
* timed invokeAny(c) throws NPE if c has null elements
|
|
458 |
*/
|
|
459 |
public void testTimedInvokeAny3() throws Exception {
|
|
460 |
final ExecutorService e = new DirectExecutorService();
|
|
461 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
462 |
List<Callable<Long>> l = new ArrayList<Callable<Long>>();
|
|
463 |
l.add(new Callable<Long>() {
|
|
464 |
public Long call() { throw new ArithmeticException(); }});
|
|
465 |
l.add(null);
|
|
466 |
try {
|
|
467 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
|
|
468 |
shouldThrow();
|
|
469 |
} catch (NullPointerException success) {}
|
|
470 |
}
|
|
471 |
}
|
|
472 |
|
|
473 |
/**
|
|
474 |
* timed invokeAny(c) throws ExecutionException if no task completes
|
|
475 |
*/
|
|
476 |
public void testTimedInvokeAny4() throws Exception {
|
|
477 |
final ExecutorService e = new DirectExecutorService();
|
|
478 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
479 |
long startTime = System.nanoTime();
|
|
480 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
481 |
l.add(new NPETask());
|
|
482 |
try {
|
|
483 |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
|
|
484 |
shouldThrow();
|
|
485 |
} catch (ExecutionException success) {
|
|
486 |
assertTrue(success.getCause() instanceof NullPointerException);
|
|
487 |
}
|
|
488 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
|
|
489 |
}
|
|
490 |
}
|
|
491 |
|
|
492 |
/**
|
|
493 |
* timed invokeAny(c) returns result of some task in c
|
|
494 |
*/
|
|
495 |
public void testTimedInvokeAny5() throws Exception {
|
|
496 |
final ExecutorService e = new DirectExecutorService();
|
|
497 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
498 |
long startTime = System.nanoTime();
|
|
499 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
500 |
l.add(new StringTask());
|
|
501 |
l.add(new StringTask());
|
|
502 |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
|
|
503 |
assertSame(TEST_STRING, result);
|
|
504 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
|
|
505 |
}
|
|
506 |
}
|
|
507 |
|
|
508 |
/**
|
|
509 |
* timed invokeAll(null) throws NPE
|
|
510 |
*/
|
|
511 |
public void testTimedInvokeAll1() throws InterruptedException {
|
|
512 |
final ExecutorService e = new DirectExecutorService();
|
|
513 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
514 |
try {
|
|
515 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
|
|
516 |
shouldThrow();
|
|
517 |
} catch (NullPointerException success) {}
|
|
518 |
}
|
|
519 |
}
|
|
520 |
|
|
521 |
/**
|
|
522 |
* timed invokeAll(null time unit) throws NPE
|
|
523 |
*/
|
|
524 |
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
|
|
525 |
final ExecutorService e = new DirectExecutorService();
|
|
526 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
527 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
528 |
l.add(new StringTask());
|
|
529 |
try {
|
|
530 |
e.invokeAll(l, MEDIUM_DELAY_MS, null);
|
|
531 |
shouldThrow();
|
|
532 |
} catch (NullPointerException success) {}
|
|
533 |
}
|
|
534 |
}
|
|
535 |
|
|
536 |
/**
|
|
537 |
* timed invokeAll(empty collection) returns empty collection
|
|
538 |
*/
|
|
539 |
public void testTimedInvokeAll2() throws InterruptedException {
|
|
540 |
final ExecutorService e = new DirectExecutorService();
|
|
541 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
542 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
|
|
543 |
assertTrue(r.isEmpty());
|
|
544 |
}
|
|
545 |
}
|
|
546 |
|
|
547 |
/**
|
|
548 |
* timed invokeAll(c) throws NPE if c has null elements
|
|
549 |
*/
|
|
550 |
public void testTimedInvokeAll3() throws InterruptedException {
|
|
551 |
final ExecutorService e = new DirectExecutorService();
|
|
552 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
553 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
554 |
l.add(new StringTask());
|
|
555 |
l.add(null);
|
|
556 |
try {
|
|
557 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
|
|
558 |
shouldThrow();
|
|
559 |
} catch (NullPointerException success) {}
|
|
560 |
}
|
|
561 |
}
|
|
562 |
|
|
563 |
/**
|
|
564 |
* get of returned element of invokeAll(c) throws exception on failed task
|
|
565 |
*/
|
|
566 |
public void testTimedInvokeAll4() throws Exception {
|
|
567 |
final ExecutorService e = new DirectExecutorService();
|
|
568 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
569 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
570 |
l.add(new NPETask());
|
|
571 |
List<Future<String>> futures =
|
|
572 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
|
|
573 |
assertEquals(1, futures.size());
|
|
574 |
try {
|
|
575 |
futures.get(0).get();
|
|
576 |
shouldThrow();
|
|
577 |
} catch (ExecutionException success) {
|
|
578 |
assertTrue(success.getCause() instanceof NullPointerException);
|
|
579 |
}
|
|
580 |
}
|
|
581 |
}
|
|
582 |
|
|
583 |
/**
|
|
584 |
* timed invokeAll(c) returns results of all completed tasks in c
|
|
585 |
*/
|
|
586 |
public void testTimedInvokeAll5() throws Exception {
|
|
587 |
final ExecutorService e = new DirectExecutorService();
|
|
588 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
589 |
List<Callable<String>> l = new ArrayList<Callable<String>>();
|
|
590 |
l.add(new StringTask());
|
|
591 |
l.add(new StringTask());
|
|
592 |
List<Future<String>> futures =
|
|
593 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
|
|
594 |
assertEquals(2, futures.size());
|
|
595 |
for (Future<String> future : futures)
|
|
596 |
assertSame(TEST_STRING, future.get());
|
|
597 |
}
|
|
598 |
}
|
|
599 |
|
|
600 |
/**
|
|
601 |
* timed invokeAll cancels tasks not completed by timeout
|
|
602 |
*/
|
|
603 |
public void testTimedInvokeAll6() throws Exception {
|
|
604 |
final ExecutorService e = new DirectExecutorService();
|
|
605 |
try (PoolCleaner cleaner = cleaner(e)) {
|
|
606 |
for (long timeout = timeoutMillis();;) {
|
|
607 |
List<Callable<String>> tasks = new ArrayList<>();
|
|
608 |
tasks.add(new StringTask("0"));
|
|
609 |
tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout),
|
|
610 |
TEST_STRING));
|
|
611 |
tasks.add(new StringTask("2"));
|
|
612 |
long startTime = System.nanoTime();
|
|
613 |
List<Future<String>> futures =
|
|
614 |
e.invokeAll(tasks, timeout, MILLISECONDS);
|
|
615 |
assertEquals(tasks.size(), futures.size());
|
|
616 |
assertTrue(millisElapsedSince(startTime) >= timeout);
|
|
617 |
for (Future future : futures)
|
|
618 |
assertTrue(future.isDone());
|
|
619 |
try {
|
|
620 |
assertEquals("0", futures.get(0).get());
|
|
621 |
assertEquals(TEST_STRING, futures.get(1).get());
|
|
622 |
} catch (CancellationException retryWithLongerTimeout) {
|
|
623 |
// unusual delay before starting second task
|
|
624 |
timeout *= 2;
|
|
625 |
if (timeout >= LONG_DELAY_MS / 2)
|
|
626 |
fail("expected exactly one task to be cancelled");
|
|
627 |
continue;
|
|
628 |
}
|
|
629 |
assertTrue(futures.get(2).isCancelled());
|
|
630 |
break;
|
|
631 |
}
|
|
632 |
}
|
|
633 |
}
|
|
634 |
|
|
635 |
}
|