ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
(Generate patch)

Comparing jsr166/src/test/tck/AbstractExecutorServiceTest.java (file contents):
Revision 1.31 by jsr166, Sat May 28 22:33:35 2011 UTC vs.
Revision 1.47 by jsr166, Sun Jul 16 18:05:47 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.math.BigInteger;
11 < import java.security.*;
10 >
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 > 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);
# Line 45 | Line 62 | public class AbstractExecutorServiceTest
62       */
63      public void testExecuteRunnable() throws Exception {
64          ExecutorService e = new DirectExecutorService();
65 <        TrackedShortRunnable task = new TrackedShortRunnable();
66 <        assertFalse(task.done);
67 <        Future<?> future = e.submit(task);
68 <        future.get();
69 <        assertTrue(task.done);
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  
77      /**
# Line 144 | Line 166 | public class AbstractExecutorServiceTest
166      }
167  
168      /**
169 <     * execute(null runnable) throws NPE
148 <     */
149 <    public void testExecuteNullRunnable() {
150 <        try {
151 <            ExecutorService e = new DirectExecutorService();
152 <            e.submit((Runnable) null);
153 <            shouldThrow();
154 <        } catch (NullPointerException success) {}
155 <    }
156 <
157 <    /**
158 <     * submit(null callable) throws NPE
169 >     * Submitting null tasks throws NullPointerException.
170       */
171 <    public void testSubmitNullCallable() {
172 <        try {
173 <            ExecutorService e = new DirectExecutorService();
174 <            e.submit((Callable) null);
175 <            shouldThrow();
176 <        } catch (NullPointerException success) {}
171 >    @SuppressWarnings("FutureReturnValueIgnored")
172 >    public void testNullTaskSubmission() throws Exception {
173 >        final ExecutorService e = new DirectExecutorService();
174 >        try (PoolCleaner cleaner = cleaner(e)) {
175 >            try {
176 >                e.execute((Runnable) null);
177 >                shouldThrow();
178 >            } catch (NullPointerException success) {}
179 >            try {
180 >                e.submit((Runnable) null);
181 >                shouldThrow();
182 >            } catch (NullPointerException success) {}
183 >            try {
184 >                e.submit((Callable) null);
185 >                shouldThrow();
186 >            } catch (NullPointerException success) {}
187 >        }
188      }
189  
190      /**
# Line 171 | Line 193 | public class AbstractExecutorServiceTest
193      public void testInterruptedSubmit() throws InterruptedException {
194          final CountDownLatch submitted    = new CountDownLatch(1);
195          final CountDownLatch quittingTime = new CountDownLatch(1);
174        final ExecutorService p
175            = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS,
176                                     new ArrayBlockingQueue<Runnable>(10));
196          final Callable<Void> awaiter = new CheckedCallable<Void>() {
197              public Void realCall() throws InterruptedException {
198 <                quittingTime.await();
198 >                assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
199                  return null;
200              }};
201 <        try {
202 <            Thread t = new Thread(new CheckedInterruptedRunnable() {
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 <            t.start();
212 <            submitted.await();
211 >
212 >            await(submitted);
213              t.interrupt();
214 <            t.join();
193 <        } finally {
194 <            quittingTime.countDown();
195 <            joinPool(p);
214 >            awaitTermination(t);
215          }
216      }
217  
# Line 201 | Line 220 | public class AbstractExecutorServiceTest
220       * throws exception
221       */
222      public void testSubmitEE() throws InterruptedException {
223 <        ThreadPoolExecutor p =
223 >        final ThreadPoolExecutor p =
224              new ThreadPoolExecutor(1, 1,
225                                     60, TimeUnit.SECONDS,
226                                     new ArrayBlockingQueue<Runnable>(10));
227 <
228 <        Callable c = new Callable() {
229 <            public Object call() { return 5/0; }};
230 <
231 <        try {
232 <            p.submit(c).get();
233 <            shouldThrow();
234 <        } catch (ExecutionException success) {
235 <            assertTrue(success.getCause() instanceof ArithmeticException);
227 >        try (PoolCleaner cleaner = cleaner(p)) {
228 >            Callable c = new Callable() {
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 >            }
236          }
218        joinPool(p);
237      }
238  
239      /**
240       * invokeAny(null) throws NPE
241       */
242      public void testInvokeAny1() throws Exception {
243 <        ExecutorService e = new DirectExecutorService();
244 <        try {
245 <            e.invokeAny(null);
246 <            shouldThrow();
247 <        } catch (NullPointerException success) {
248 <        } finally {
231 <            joinPool(e);
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
253 >     * invokeAny(empty collection) throws IllegalArgumentException
254       */
255      public void testInvokeAny2() throws Exception {
256 <        ExecutorService e = new DirectExecutorService();
257 <        try {
258 <            e.invokeAny(new ArrayList<Callable<String>>());
259 <            shouldThrow();
260 <        } catch (IllegalArgumentException success) {
261 <        } finally {
262 <            joinPool(e);
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  
# Line 250 | Line 268 | public class AbstractExecutorServiceTest
268       * invokeAny(c) throws NPE if c has null elements
269       */
270      public void testInvokeAny3() throws Exception {
271 <        ExecutorService e = new DirectExecutorService();
272 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
273 <        l.add(new Callable<Integer>() {
274 <                  public Integer call() { return 5/0; }});
275 <        l.add(null);
276 <        try {
277 <            e.invokeAny(l);
278 <            shouldThrow();
279 <        } catch (NullPointerException success) {
280 <        } finally {
263 <            joinPool(e);
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 >            try {
278 >                e.invokeAny(l);
279 >                shouldThrow();
280 >            } catch (NullPointerException success) {}
281          }
282      }
283  
# Line 268 | Line 285 | public class AbstractExecutorServiceTest
285       * invokeAny(c) throws ExecutionException if no task in c completes
286       */
287      public void testInvokeAny4() throws InterruptedException {
288 <        ExecutorService e = new DirectExecutorService();
289 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
290 <        l.add(new NPETask());
291 <        try {
292 <            e.invokeAny(l);
293 <            shouldThrow();
294 <        } catch (ExecutionException success) {
295 <            assertTrue(success.getCause() instanceof NullPointerException);
296 <        } finally {
297 <            joinPool(e);
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 >            try {
293 >                e.invokeAny(l);
294 >                shouldThrow();
295 >            } catch (ExecutionException success) {
296 >                assertTrue(success.getCause() instanceof NullPointerException);
297 >            }
298          }
299      }
300  
# Line 285 | Line 302 | public class AbstractExecutorServiceTest
302       * invokeAny(c) returns result of some task in c if at least one completes
303       */
304      public void testInvokeAny5() throws Exception {
305 <        ExecutorService e = new DirectExecutorService();
306 <        try {
307 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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);
295        } finally {
296            joinPool(e);
312          }
313      }
314  
# Line 301 | Line 316 | public class AbstractExecutorServiceTest
316       * invokeAll(null) throws NPE
317       */
318      public void testInvokeAll1() throws InterruptedException {
319 <        ExecutorService e = new DirectExecutorService();
320 <        try {
321 <            e.invokeAll(null);
322 <            shouldThrow();
323 <        } catch (NullPointerException success) {
324 <        } finally {
310 <            joinPool(e);
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() throws InterruptedException {
332 <        ExecutorService e = new DirectExecutorService();
333 <        try {
334 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
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());
322        } finally {
323            joinPool(e);
338          }
339      }
340  
# Line 328 | Line 342 | public class AbstractExecutorServiceTest
342       * invokeAll(c) throws NPE if c has null elements
343       */
344      public void testInvokeAll3() throws InterruptedException {
345 <        ExecutorService e = new DirectExecutorService();
346 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
347 <        l.add(new StringTask());
348 <        l.add(null);
349 <        try {
350 <            e.invokeAll(l);
351 <            shouldThrow();
352 <        } catch (NullPointerException success) {
353 <        } finally {
340 <            joinPool(e);
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 >            try {
351 >                e.invokeAll(l);
352 >                shouldThrow();
353 >            } catch (NullPointerException success) {}
354          }
355      }
356  
# Line 345 | Line 358 | public class AbstractExecutorServiceTest
358       * get of returned element of invokeAll(c) throws exception on failed task
359       */
360      public void testInvokeAll4() throws Exception {
361 <        ExecutorService e = new DirectExecutorService();
362 <        try {
363 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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>> futures = e.invokeAll(l);
366              assertEquals(1, futures.size());
# Line 357 | Line 370 | public class AbstractExecutorServiceTest
370              } catch (ExecutionException success) {
371                  assertTrue(success.getCause() instanceof NullPointerException);
372              }
360        } finally {
361            joinPool(e);
373          }
374      }
375  
# Line 366 | Line 377 | public class AbstractExecutorServiceTest
377       * invokeAll(c) returns results of all completed tasks in c
378       */
379      public void testInvokeAll5() throws Exception {
380 <        ExecutorService e = new DirectExecutorService();
381 <        try {
382 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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>> futures = e.invokeAll(l);
386              assertEquals(2, futures.size());
387              for (Future<String> future : futures)
388                  assertSame(TEST_STRING, future.get());
378        } finally {
379            joinPool(e);
389          }
390      }
391  
# Line 384 | Line 393 | public class AbstractExecutorServiceTest
393       * timed invokeAny(null) throws NPE
394       */
395      public void testTimedInvokeAny1() throws Exception {
396 <        ExecutorService e = new DirectExecutorService();
397 <        try {
398 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
399 <            shouldThrow();
400 <        } catch (NullPointerException success) {
401 <        } finally {
393 <            joinPool(e);
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 time unit) throws NPE
406 >     * timed invokeAny(null time unit) throws NullPointerException
407       */
408      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
409 <        ExecutorService e = new DirectExecutorService();
410 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
411 <        l.add(new StringTask());
412 <        try {
413 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
414 <            shouldThrow();
415 <        } catch (NullPointerException success) {
416 <        } finally {
409 <            joinPool(e);
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 >            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() throws Exception {
424 <        ExecutorService e = new DirectExecutorService();
425 <        try {
426 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
427 <            shouldThrow();
428 <        } catch (IllegalArgumentException success) {
429 <        } finally {
430 <            joinPool(e);
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  
# Line 428 | Line 436 | public class AbstractExecutorServiceTest
436       * timed invokeAny(c) throws NPE if c has null elements
437       */
438      public void testTimedInvokeAny3() throws Exception {
439 <        ExecutorService e = new DirectExecutorService();
440 <        List<Callable<Integer>> l = new ArrayList<Callable<Integer>>();
441 <        l.add(new Callable<Integer>() {
442 <                  public Integer call() { return 5/0; }});
443 <        l.add(null);
444 <        try {
445 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
446 <            shouldThrow();
447 <        } catch (NullPointerException success) {
448 <        } finally {
441 <            joinPool(e);
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 >            try {
446 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
447 >                shouldThrow();
448 >            } catch (NullPointerException success) {}
449          }
450      }
451  
# Line 446 | Line 453 | public class AbstractExecutorServiceTest
453       * timed invokeAny(c) throws ExecutionException if no task completes
454       */
455      public void testTimedInvokeAny4() throws Exception {
456 <        ExecutorService e = new DirectExecutorService();
457 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
458 <        l.add(new NPETask());
459 <        try {
460 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
461 <            shouldThrow();
462 <        } catch (ExecutionException success) {
463 <            assertTrue(success.getCause() instanceof NullPointerException);
464 <        } finally {
465 <            joinPool(e);
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 >            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  
# Line 463 | Line 472 | public class AbstractExecutorServiceTest
472       * timed invokeAny(c) returns result of some task in c
473       */
474      public void testTimedInvokeAny5() throws Exception {
475 <        ExecutorService e = new DirectExecutorService();
476 <        try {
477 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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, MILLISECONDS);
481 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
482              assertSame(TEST_STRING, result);
483 <        } finally {
474 <            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() throws InterruptedException {
491 <        ExecutorService e = new DirectExecutorService();
492 <        try {
493 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
494 <            shouldThrow();
495 <        } catch (NullPointerException success) {
496 <        } finally {
488 <            joinPool(e);
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  
# Line 493 | Line 501 | public class AbstractExecutorServiceTest
501       * timed invokeAll(null time unit) throws NPE
502       */
503      public void testTimedInvokeAllNullTimeUnit() throws InterruptedException {
504 <        ExecutorService e = new DirectExecutorService();
505 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
506 <        l.add(new StringTask());
507 <        try {
508 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
509 <            shouldThrow();
510 <        } catch (NullPointerException success) {
511 <        } finally {
504 <            joinPool(e);
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 >            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() throws InterruptedException {
519 <        ExecutorService e = new DirectExecutorService();
520 <        try {
521 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
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());
516        } finally {
517            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() throws InterruptedException {
533 <        ExecutorService e = new DirectExecutorService();
534 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
535 <        l.add(new StringTask());
536 <        l.add(null);
537 <        try {
538 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
539 <            shouldThrow();
540 <        } catch (NullPointerException success) {
541 <        } finally {
534 <            joinPool(e);
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 >            try {
539 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
540 >                shouldThrow();
541 >            } catch (NullPointerException success) {}
542          }
543      }
544  
# Line 539 | Line 546 | public class AbstractExecutorServiceTest
546       * get of returned element of invokeAll(c) throws exception on failed task
547       */
548      public void testTimedInvokeAll4() throws Exception {
549 <        ExecutorService e = new DirectExecutorService();
550 <        try {
551 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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>> futures =
554 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
554 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
555              assertEquals(1, futures.size());
556              try {
557                  futures.get(0).get();
# Line 552 | Line 559 | public class AbstractExecutorServiceTest
559              } catch (ExecutionException success) {
560                  assertTrue(success.getCause() instanceof NullPointerException);
561              }
555        } finally {
556            joinPool(e);
562          }
563      }
564  
# Line 561 | Line 566 | public class AbstractExecutorServiceTest
566       * timed invokeAll(c) returns results of all completed tasks in c
567       */
568      public void testTimedInvokeAll5() throws Exception {
569 <        ExecutorService e = new DirectExecutorService();
570 <        try {
571 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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, MEDIUM_DELAY_MS, MILLISECONDS);
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());
574        } finally {
575            joinPool(e);
579          }
580      }
581  
582      /**
583       * timed invokeAll cancels tasks not completed by timeout
584       */
585 <    public void testTimedInvokeAll6() throws InterruptedException {
586 <        ExecutorService e = new DirectExecutorService();
587 <        try {
588 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
589 <            l.add(new StringTask());
590 <            l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING));
591 <            l.add(new StringTask());
592 <            List<Future<String>> futures =
593 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
594 <            assertEquals(3, futures.size());
595 <            Iterator<Future<String>> it = futures.iterator();
596 <            Future<String> f1 = it.next();
597 <            Future<String> f2 = it.next();
598 <            Future<String> f3 = it.next();
599 <            assertTrue(f1.isDone());
600 <            assertFalse(f1.isCancelled());
601 <            assertTrue(f2.isDone());
602 <            assertFalse(f2.isCancelled());
603 <            assertTrue(f3.isDone());
604 <            assertTrue(f3.isCancelled());
605 <        } finally {
606 <            joinPool(e);
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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines