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

Comparing jsr166/src/test/tck/ForkJoinPoolTest.java (file contents):
Revision 1.52 by jsr166, Wed Sep 25 07:39:17 2013 UTC vs.
Revision 1.68 by jsr166, Thu Oct 8 03:08:37 2015 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
9 >
10 > import java.security.PrivilegedAction;
11 > import java.security.PrivilegedExceptionAction;
12   import java.util.ArrayList;
13   import java.util.Collection;
14   import java.util.List;
11 import java.util.concurrent.Executors;
12 import java.util.concurrent.ExecutorService;
13 import java.util.concurrent.AbstractExecutorService;
14 import java.util.concurrent.CountDownLatch;
15   import java.util.concurrent.Callable;
16 < import java.util.concurrent.Future;
16 > import java.util.concurrent.CountDownLatch;
17   import java.util.concurrent.ExecutionException;
18 < import java.util.concurrent.CancellationException;
19 < import java.util.concurrent.RejectedExecutionException;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20   import java.util.concurrent.ForkJoinPool;
21   import java.util.concurrent.ForkJoinTask;
22   import java.util.concurrent.ForkJoinWorkerThread;
23 + import java.util.concurrent.Future;
24   import java.util.concurrent.RecursiveTask;
25 < import java.util.concurrent.TimeUnit;
25 > import java.util.concurrent.RejectedExecutionException;
26   import java.util.concurrent.atomic.AtomicBoolean;
27   import java.util.concurrent.locks.ReentrantLock;
28 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
29 < import static java.util.concurrent.TimeUnit.NANOSECONDS;
30 < import java.security.AccessControlException;
31 < import java.security.Policy;
31 < import java.security.PrivilegedAction;
32 < import java.security.PrivilegedExceptionAction;
28 >
29 > import junit.framework.AssertionFailedError;
30 > import junit.framework.Test;
31 > import junit.framework.TestSuite;
32  
33   public class ForkJoinPoolTest extends JSR166TestCase {
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run(suite());
35 >        main(suite(), args);
36      }
37  
38      public static Test suite() {
# Line 65 | Line 64 | public class ForkJoinPoolTest extends JS
64          }
65      }
66  
67 +    static class MyError extends Error {}
68 +
69      // to test handlers
70      static class FailingFJWSubclass extends ForkJoinWorkerThread {
71          public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
72 <        protected void onStart() { super.onStart(); throw new Error(); }
72 >        protected void onStart() { super.onStart(); throw new MyError(); }
73      }
74  
75      static class FailingThreadFactory
# Line 163 | Line 164 | public class ForkJoinPoolTest extends JS
164       */
165      public void testDefaultInitialState() {
166          ForkJoinPool p = new ForkJoinPool(1);
167 <        try {
167 >        try (PoolCleaner cleaner = cleaner(p)) {
168              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
169                         p.getFactory());
170              assertFalse(p.getAsyncMode());
# Line 175 | Line 176 | public class ForkJoinPoolTest extends JS
176              assertFalse(p.isShutdown());
177              assertFalse(p.isTerminating());
178              assertFalse(p.isTerminated());
178        } finally {
179            joinPool(p);
179          }
180      }
181  
# Line 205 | Line 204 | public class ForkJoinPoolTest extends JS
204       */
205      public void testGetParallelism() {
206          ForkJoinPool p = new ForkJoinPool(1);
207 <        try {
207 >        try (PoolCleaner cleaner = cleaner(p)) {
208              assertEquals(1, p.getParallelism());
210        } finally {
211            joinPool(p);
209          }
210      }
211  
# Line 217 | Line 214 | public class ForkJoinPoolTest extends JS
214       */
215      public void testGetPoolSize() {
216          ForkJoinPool p = new ForkJoinPool(1);
217 <        try {
217 >        try (PoolCleaner cleaner = cleaner(p)) {
218              assertEquals(0, p.getActiveThreadCount());
219              Future<String> future = p.submit(new StringTask());
220              assertEquals(1, p.getPoolSize());
224        } finally {
225            joinPool(p);
221          }
222      }
223  
# Line 231 | Line 226 | public class ForkJoinPoolTest extends JS
226       */
227      public void testAwaitTermination_timesOut() throws InterruptedException {
228          ForkJoinPool p = new ForkJoinPool(1);
229 <        assertFalse(p.isTerminated());
230 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
231 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
232 <        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
233 <        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
234 <        assertFalse(p.awaitTermination(0L, NANOSECONDS));
235 <        assertFalse(p.awaitTermination(0L, MILLISECONDS));
236 <        long timeoutNanos = 999999L;
237 <        long startTime = System.nanoTime();
238 <        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
239 <        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
240 <        assertFalse(p.isTerminated());
241 <        startTime = System.nanoTime();
242 <        long timeoutMillis = timeoutMillis();
243 <        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
244 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
245 <        assertFalse(p.isTerminated());
246 <        p.shutdown();
247 <        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
248 <        assertTrue(p.isTerminated());
229 >        try (PoolCleaner cleaner = cleaner(p)) {
230 >            assertFalse(p.isTerminated());
231 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
232 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
233 >            assertFalse(p.awaitTermination(-1L, NANOSECONDS));
234 >            assertFalse(p.awaitTermination(-1L, MILLISECONDS));
235 >            assertFalse(p.awaitTermination(0L, NANOSECONDS));
236 >            assertFalse(p.awaitTermination(0L, MILLISECONDS));
237 >            long timeoutNanos = 999999L;
238 >            long startTime = System.nanoTime();
239 >            assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
240 >            assertTrue(System.nanoTime() - startTime >= timeoutNanos);
241 >            assertFalse(p.isTerminated());
242 >            startTime = System.nanoTime();
243 >            long timeoutMillis = timeoutMillis();
244 >            assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
245 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
246 >            assertFalse(p.isTerminated());
247 >            p.shutdown();
248 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
249 >            assertTrue(p.isTerminated());
250 >        }
251      }
252  
253      /**
# Line 261 | Line 258 | public class ForkJoinPoolTest extends JS
258       */
259      public void testSetUncaughtExceptionHandler() throws InterruptedException {
260          final CountDownLatch uehInvoked = new CountDownLatch(1);
261 <        final Thread.UncaughtExceptionHandler eh =
261 >        final Thread.UncaughtExceptionHandler ueh =
262              new Thread.UncaughtExceptionHandler() {
263                  public void uncaughtException(Thread t, Throwable e) {
264 +                    threadAssertTrue(e instanceof MyError);
265 +                    threadAssertTrue(t instanceof FailingFJWSubclass);
266                      uehInvoked.countDown();
267                  }};
268          ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
269 <                                          eh, false);
270 <        try {
271 <            assertSame(eh, p.getUncaughtExceptionHandler());
269 >                                          ueh, false);
270 >        try (PoolCleaner cleaner = cleaner(p)) {
271 >            assertSame(ueh, p.getUncaughtExceptionHandler());
272              try {
273                  p.execute(new FibTask(8));
274 <                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
275 <            } catch (RejectedExecutionException ok) {
274 >                await(uehInvoked);
275 >            } finally {
276 >                p.shutdownNow(); // failure might have prevented processing task
277              }
278        } finally {
279            p.shutdownNow(); // failure might have prevented processing task
280            joinPool(p);
278          }
279      }
280  
# Line 289 | Line 286 | public class ForkJoinPoolTest extends JS
286       */
287      public void testIsQuiescent() throws Exception {
288          ForkJoinPool p = new ForkJoinPool(2);
289 <        try {
289 >        try (PoolCleaner cleaner = cleaner(p)) {
290              assertTrue(p.isQuiescent());
291              long startTime = System.nanoTime();
292              FibTask f = new FibTask(20);
# Line 317 | Line 314 | public class ForkJoinPoolTest extends JS
314              assertFalse(p.isTerminated());
315              assertTrue(f.isDone());
316              assertEquals(6765, (int) f.get());
320        } finally {
321            joinPool(p);
317          }
318      }
319  
# Line 327 | Line 322 | public class ForkJoinPoolTest extends JS
322       */
323      public void testSubmitForkJoinTask() throws Throwable {
324          ForkJoinPool p = new ForkJoinPool(1);
325 <        try {
325 >        try (PoolCleaner cleaner = cleaner(p)) {
326              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
327              assertEquals(21, (int) f.get());
333        } finally {
334            joinPool(p);
328          }
329      }
330  
# Line 340 | Line 333 | public class ForkJoinPoolTest extends JS
333       */
334      public void testSubmitAfterShutdown() {
335          ForkJoinPool p = new ForkJoinPool(1);
336 <        try {
336 >        try (PoolCleaner cleaner = cleaner(p)) {
337              p.shutdown();
338              assertTrue(p.isShutdown());
339              try {
340                  ForkJoinTask<Integer> f = p.submit(new FibTask(8));
341                  shouldThrow();
342              } catch (RejectedExecutionException success) {}
350        } finally {
351            joinPool(p);
343          }
344      }
345  
# Line 374 | Line 365 | public class ForkJoinPoolTest extends JS
365      public void testPollSubmission() {
366          final CountDownLatch done = new CountDownLatch(1);
367          SubFJP p = new SubFJP();
368 <        try {
368 >        try (PoolCleaner cleaner = cleaner(p)) {
369              ForkJoinTask a = p.submit(awaiter(done));
370              ForkJoinTask b = p.submit(awaiter(done));
371              ForkJoinTask c = p.submit(awaiter(done));
372              ForkJoinTask r = p.pollSubmission();
373              assertTrue(r == a || r == b || r == c);
374              assertFalse(r.isDone());
384        } finally {
375              done.countDown();
386            joinPool(p);
376          }
377      }
378  
# Line 393 | Line 382 | public class ForkJoinPoolTest extends JS
382      public void testDrainTasksTo() {
383          final CountDownLatch done = new CountDownLatch(1);
384          SubFJP p = new SubFJP();
385 <        try {
385 >        try (PoolCleaner cleaner = cleaner(p)) {
386              ForkJoinTask a = p.submit(awaiter(done));
387              ForkJoinTask b = p.submit(awaiter(done));
388              ForkJoinTask c = p.submit(awaiter(done));
# Line 404 | Line 393 | public class ForkJoinPoolTest extends JS
393                  assertTrue(r == a || r == b || r == c);
394                  assertFalse(r.isDone());
395              }
407        } finally {
396              done.countDown();
409            joinPool(p);
397          }
398      }
399  
# Line 417 | Line 404 | public class ForkJoinPoolTest extends JS
404       */
405      public void testExecuteRunnable() throws Throwable {
406          ExecutorService e = new ForkJoinPool(1);
407 <        try {
407 >        try (PoolCleaner cleaner = cleaner(e)) {
408              final AtomicBoolean done = new AtomicBoolean(false);
409              Future<?> future = e.submit(new CheckedRunnable() {
410                  public void realRun() {
# Line 428 | Line 415 | public class ForkJoinPoolTest extends JS
415              assertTrue(done.get());
416              assertTrue(future.isDone());
417              assertFalse(future.isCancelled());
431        } finally {
432            joinPool(e);
418          }
419      }
420  
# Line 438 | Line 423 | public class ForkJoinPoolTest extends JS
423       */
424      public void testSubmitCallable() throws Throwable {
425          ExecutorService e = new ForkJoinPool(1);
426 <        try {
426 >        try (PoolCleaner cleaner = cleaner(e)) {
427              Future<String> future = e.submit(new StringTask());
428              assertSame(TEST_STRING, future.get());
429              assertTrue(future.isDone());
430              assertFalse(future.isCancelled());
446        } finally {
447            joinPool(e);
431          }
432      }
433  
# Line 453 | Line 436 | public class ForkJoinPoolTest extends JS
436       */
437      public void testSubmitRunnable() throws Throwable {
438          ExecutorService e = new ForkJoinPool(1);
439 <        try {
439 >        try (PoolCleaner cleaner = cleaner(e)) {
440              Future<?> future = e.submit(new NoOpRunnable());
441              assertNull(future.get());
442              assertTrue(future.isDone());
443              assertFalse(future.isCancelled());
461        } finally {
462            joinPool(e);
444          }
445      }
446  
# Line 468 | Line 449 | public class ForkJoinPoolTest extends JS
449       */
450      public void testSubmitRunnable2() throws Throwable {
451          ExecutorService e = new ForkJoinPool(1);
452 <        try {
452 >        try (PoolCleaner cleaner = cleaner(e)) {
453              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
454              assertSame(TEST_STRING, future.get());
455              assertTrue(future.isDone());
456              assertFalse(future.isCancelled());
476        } finally {
477            joinPool(e);
457          }
458      }
459  
# Line 487 | Line 466 | public class ForkJoinPoolTest extends JS
466          Runnable r = new CheckedRunnable() {
467          public void realRun() throws Exception {
468              ExecutorService e = new ForkJoinPool(1);
469 <            try {
469 >            try (PoolCleaner cleaner = cleaner(e)) {
470                  Future future = e.submit(callable);
471                  assertSame(TEST_STRING, future.get());
493            } finally {
494                joinPool(e);
472              }
473          }};
474  
# Line 508 | Line 485 | public class ForkJoinPoolTest extends JS
485          Runnable r = new CheckedRunnable() {
486          public void realRun() throws Exception {
487              ExecutorService e = new ForkJoinPool(1);
488 <            try {
488 >            try (PoolCleaner cleaner = cleaner(e)) {
489                  Future future = e.submit(callable);
490                  assertSame(TEST_STRING, future.get());
514            } finally {
515                joinPool(e);
491              }
492          }};
493  
# Line 529 | Line 504 | public class ForkJoinPoolTest extends JS
504          Runnable r = new CheckedRunnable() {
505          public void realRun() throws Exception {
506              ExecutorService e = new ForkJoinPool(1);
507 <            try {
507 >            try (PoolCleaner cleaner = cleaner(e)) {
508                  Future future = e.submit(callable);
509                  try {
510                      future.get();
# Line 537 | Line 512 | public class ForkJoinPoolTest extends JS
512                  } catch (ExecutionException success) {
513                      assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
514                  }
540            } finally {
541                joinPool(e);
515              }
516          }};
517  
# Line 550 | Line 523 | public class ForkJoinPoolTest extends JS
523       */
524      public void testExecuteNullRunnable() {
525          ExecutorService e = new ForkJoinPool(1);
526 <        try {
527 <            Future<?> future = e.submit((Runnable) null);
528 <            shouldThrow();
529 <        } catch (NullPointerException success) {
530 <        } finally {
558 <            joinPool(e);
526 >        try (PoolCleaner cleaner = cleaner(e)) {
527 >            try {
528 >                Future<?> future = e.submit((Runnable) null);
529 >                shouldThrow();
530 >            } catch (NullPointerException success) {}
531          }
532      }
533  
# Line 564 | Line 536 | public class ForkJoinPoolTest extends JS
536       */
537      public void testSubmitNullCallable() {
538          ExecutorService e = new ForkJoinPool(1);
539 <        try {
540 <            Future<String> future = e.submit((Callable) null);
541 <            shouldThrow();
542 <        } catch (NullPointerException success) {
543 <        } finally {
572 <            joinPool(e);
539 >        try (PoolCleaner cleaner = cleaner(e)) {
540 >            try {
541 >                Future<String> future = e.submit((Callable) null);
542 >                shouldThrow();
543 >            } catch (NullPointerException success) {}
544          }
545      }
546  
# Line 579 | Line 550 | public class ForkJoinPoolTest extends JS
550      public void testInterruptedSubmit() throws InterruptedException {
551          final CountDownLatch submitted    = new CountDownLatch(1);
552          final CountDownLatch quittingTime = new CountDownLatch(1);
582        final ExecutorService p = new ForkJoinPool(1);
553          final Callable<Void> awaiter = new CheckedCallable<Void>() {
554              public Void realCall() throws InterruptedException {
555 <                assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
555 >                assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
556                  return null;
557              }};
558 <        try {
558 >        final ExecutorService p = new ForkJoinPool(1);
559 >        try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
560              Thread t = new Thread(new CheckedInterruptedRunnable() {
561                  public void realRun() throws Exception {
562                      Future<Void> future = p.submit(awaiter);
# Line 593 | Line 564 | public class ForkJoinPoolTest extends JS
564                      future.get();
565                  }});
566              t.start();
567 <            assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
567 >            await(submitted);
568              t.interrupt();
569 <            t.join();
599 <        } finally {
600 <            quittingTime.countDown();
601 <            joinPool(p);
569 >            awaitTermination(t);
570          }
571      }
572  
# Line 608 | Line 576 | public class ForkJoinPoolTest extends JS
576       */
577      public void testSubmitEE() throws Throwable {
578          ForkJoinPool p = new ForkJoinPool(1);
579 <        try {
580 <            p.submit(new Callable() {
581 <                public Object call() { throw new ArithmeticException(); }})
582 <                .get();
583 <            shouldThrow();
584 <        } catch (ExecutionException success) {
585 <            assertTrue(success.getCause() instanceof ArithmeticException);
586 <        } finally {
587 <            joinPool(p);
579 >        try (PoolCleaner cleaner = cleaner(p)) {
580 >            try {
581 >                p.submit(new Callable() {
582 >                        public Object call() { throw new ArithmeticException(); }})
583 >                    .get();
584 >                shouldThrow();
585 >            } catch (ExecutionException success) {
586 >                assertTrue(success.getCause() instanceof ArithmeticException);
587 >            }
588          }
589      }
590  
# Line 625 | Line 593 | public class ForkJoinPoolTest extends JS
593       */
594      public void testInvokeAny1() throws Throwable {
595          ExecutorService e = new ForkJoinPool(1);
596 <        try {
597 <            e.invokeAny(null);
598 <            shouldThrow();
599 <        } catch (NullPointerException success) {
600 <        } finally {
633 <            joinPool(e);
596 >        try (PoolCleaner cleaner = cleaner(e)) {
597 >            try {
598 >                e.invokeAny(null);
599 >                shouldThrow();
600 >            } catch (NullPointerException success) {}
601          }
602      }
603  
# Line 639 | Line 606 | public class ForkJoinPoolTest extends JS
606       */
607      public void testInvokeAny2() throws Throwable {
608          ExecutorService e = new ForkJoinPool(1);
609 <        try {
610 <            e.invokeAny(new ArrayList<Callable<String>>());
611 <            shouldThrow();
612 <        } catch (IllegalArgumentException success) {
613 <        } finally {
647 <            joinPool(e);
609 >        try (PoolCleaner cleaner = cleaner(e)) {
610 >            try {
611 >                e.invokeAny(new ArrayList<Callable<String>>());
612 >                shouldThrow();
613 >            } catch (IllegalArgumentException success) {}
614          }
615      }
616  
# Line 653 | Line 619 | public class ForkJoinPoolTest extends JS
619       */
620      public void testInvokeAny3() throws Throwable {
621          ExecutorService e = new ForkJoinPool(1);
622 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
623 <        l.add(null);
624 <        try {
625 <            e.invokeAny(l);
626 <            shouldThrow();
627 <        } catch (NullPointerException success) {
628 <        } finally {
663 <            joinPool(e);
622 >        try (PoolCleaner cleaner = cleaner(e)) {
623 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
624 >            l.add(null);
625 >            try {
626 >                e.invokeAny(l);
627 >                shouldThrow();
628 >            } catch (NullPointerException success) {}
629          }
630      }
631  
# Line 670 | Line 635 | public class ForkJoinPoolTest extends JS
635      public void testInvokeAny4() throws Throwable {
636          CountDownLatch latch = new CountDownLatch(1);
637          ExecutorService e = new ForkJoinPool(1);
638 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
639 <        l.add(latchAwaitingStringTask(latch));
640 <        l.add(null);
641 <        try {
642 <            e.invokeAny(l);
643 <            shouldThrow();
644 <        } catch (NullPointerException success) {
645 <        } finally {
638 >        try (PoolCleaner cleaner = cleaner(e)) {
639 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
640 >            l.add(latchAwaitingStringTask(latch));
641 >            l.add(null);
642 >            try {
643 >                e.invokeAny(l);
644 >                shouldThrow();
645 >            } catch (NullPointerException success) {}
646              latch.countDown();
682            joinPool(e);
647          }
648      }
649  
# Line 688 | Line 652 | public class ForkJoinPoolTest extends JS
652       */
653      public void testInvokeAny5() throws Throwable {
654          ExecutorService e = new ForkJoinPool(1);
655 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
656 <        l.add(new NPETask());
657 <        try {
658 <            e.invokeAny(l);
659 <            shouldThrow();
660 <        } catch (ExecutionException success) {
661 <            assertTrue(success.getCause() instanceof NullPointerException);
662 <        } finally {
663 <            joinPool(e);
655 >        try (PoolCleaner cleaner = cleaner(e)) {
656 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
657 >            l.add(new NPETask());
658 >            try {
659 >                e.invokeAny(l);
660 >                shouldThrow();
661 >            } catch (ExecutionException success) {
662 >                assertTrue(success.getCause() instanceof NullPointerException);
663 >            }
664          }
665      }
666  
# Line 705 | Line 669 | public class ForkJoinPoolTest extends JS
669       */
670      public void testInvokeAny6() throws Throwable {
671          ExecutorService e = new ForkJoinPool(1);
672 <        try {
672 >        try (PoolCleaner cleaner = cleaner(e)) {
673              List<Callable<String>> l = new ArrayList<Callable<String>>();
674              l.add(new StringTask());
675              l.add(new StringTask());
676              String result = e.invokeAny(l);
677              assertSame(TEST_STRING, result);
714        } finally {
715            joinPool(e);
678          }
679      }
680  
# Line 721 | Line 683 | public class ForkJoinPoolTest extends JS
683       */
684      public void testInvokeAll1() throws Throwable {
685          ExecutorService e = new ForkJoinPool(1);
686 <        try {
687 <            e.invokeAll(null);
688 <            shouldThrow();
689 <        } catch (NullPointerException success) {
690 <        } finally {
729 <            joinPool(e);
686 >        try (PoolCleaner cleaner = cleaner(e)) {
687 >            try {
688 >                e.invokeAll(null);
689 >                shouldThrow();
690 >            } catch (NullPointerException success) {}
691          }
692      }
693  
# Line 735 | Line 696 | public class ForkJoinPoolTest extends JS
696       */
697      public void testInvokeAll2() throws InterruptedException {
698          ExecutorService e = new ForkJoinPool(1);
699 <        try {
699 >        try (PoolCleaner cleaner = cleaner(e)) {
700              List<Future<String>> r
701                  = e.invokeAll(new ArrayList<Callable<String>>());
702              assertTrue(r.isEmpty());
742        } finally {
743            joinPool(e);
703          }
704      }
705  
# Line 749 | Line 708 | public class ForkJoinPoolTest extends JS
708       */
709      public void testInvokeAll3() throws InterruptedException {
710          ExecutorService e = new ForkJoinPool(1);
711 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
712 <        l.add(new StringTask());
713 <        l.add(null);
714 <        try {
715 <            e.invokeAll(l);
716 <            shouldThrow();
717 <        } catch (NullPointerException success) {
718 <        } finally {
760 <            joinPool(e);
711 >        try (PoolCleaner cleaner = cleaner(e)) {
712 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
713 >            l.add(new StringTask());
714 >            l.add(null);
715 >            try {
716 >                e.invokeAll(l);
717 >                shouldThrow();
718 >            } catch (NullPointerException success) {}
719          }
720      }
721  
# Line 767 | Line 725 | public class ForkJoinPoolTest extends JS
725       */
726      public void testInvokeAll4() throws Throwable {
727          ExecutorService e = new ForkJoinPool(1);
728 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
729 <        l.add(new NPETask());
730 <        List<Future<String>> futures = e.invokeAll(l);
731 <        assertEquals(1, futures.size());
732 <        try {
733 <            futures.get(0).get();
734 <            shouldThrow();
735 <        } catch (ExecutionException success) {
736 <            assertTrue(success.getCause() instanceof NullPointerException);
737 <        } finally {
738 <            joinPool(e);
728 >        try (PoolCleaner cleaner = cleaner(e)) {
729 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
730 >            l.add(new NPETask());
731 >            List<Future<String>> futures = e.invokeAll(l);
732 >            assertEquals(1, futures.size());
733 >            try {
734 >                futures.get(0).get();
735 >                shouldThrow();
736 >            } catch (ExecutionException success) {
737 >                assertTrue(success.getCause() instanceof NullPointerException);
738 >            }
739          }
740      }
741  
# Line 786 | Line 744 | public class ForkJoinPoolTest extends JS
744       */
745      public void testInvokeAll5() throws Throwable {
746          ExecutorService e = new ForkJoinPool(1);
747 <        try {
747 >        try (PoolCleaner cleaner = cleaner(e)) {
748              List<Callable<String>> l = new ArrayList<Callable<String>>();
749              l.add(new StringTask());
750              l.add(new StringTask());
# Line 794 | Line 752 | public class ForkJoinPoolTest extends JS
752              assertEquals(2, futures.size());
753              for (Future<String> future : futures)
754                  assertSame(TEST_STRING, future.get());
797        } finally {
798            joinPool(e);
755          }
756      }
757  
# Line 804 | Line 760 | public class ForkJoinPoolTest extends JS
760       */
761      public void testTimedInvokeAny1() throws Throwable {
762          ExecutorService e = new ForkJoinPool(1);
763 <        try {
764 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
765 <            shouldThrow();
766 <        } catch (NullPointerException success) {
767 <        } finally {
812 <            joinPool(e);
763 >        try (PoolCleaner cleaner = cleaner(e)) {
764 >            try {
765 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
766 >                shouldThrow();
767 >            } catch (NullPointerException success) {}
768          }
769      }
770  
# Line 818 | Line 773 | public class ForkJoinPoolTest extends JS
773       */
774      public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
775          ExecutorService e = new ForkJoinPool(1);
776 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
777 <        l.add(new StringTask());
778 <        try {
779 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
780 <            shouldThrow();
781 <        } catch (NullPointerException success) {
782 <        } finally {
828 <            joinPool(e);
776 >        try (PoolCleaner cleaner = cleaner(e)) {
777 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
778 >            l.add(new StringTask());
779 >            try {
780 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
781 >                shouldThrow();
782 >            } catch (NullPointerException success) {}
783          }
784      }
785  
# Line 834 | Line 788 | public class ForkJoinPoolTest extends JS
788       */
789      public void testTimedInvokeAny2() throws Throwable {
790          ExecutorService e = new ForkJoinPool(1);
791 <        try {
792 <            e.invokeAny(new ArrayList<Callable<String>>(),
793 <                        MEDIUM_DELAY_MS, MILLISECONDS);
794 <            shouldThrow();
795 <        } catch (IllegalArgumentException success) {
796 <        } finally {
843 <            joinPool(e);
791 >        try (PoolCleaner cleaner = cleaner(e)) {
792 >            try {
793 >                e.invokeAny(new ArrayList<Callable<String>>(),
794 >                            MEDIUM_DELAY_MS, MILLISECONDS);
795 >                shouldThrow();
796 >            } catch (IllegalArgumentException success) {}
797          }
798      }
799  
# Line 850 | Line 803 | public class ForkJoinPoolTest extends JS
803      public void testTimedInvokeAny3() throws Throwable {
804          CountDownLatch latch = new CountDownLatch(1);
805          ExecutorService e = new ForkJoinPool(1);
806 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
807 <        l.add(latchAwaitingStringTask(latch));
808 <        l.add(null);
809 <        try {
810 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
811 <            shouldThrow();
812 <        } catch (NullPointerException success) {
813 <        } finally {
806 >        try (PoolCleaner cleaner = cleaner(e)) {
807 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
808 >            l.add(latchAwaitingStringTask(latch));
809 >            l.add(null);
810 >            try {
811 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
812 >                shouldThrow();
813 >            } catch (NullPointerException success) {}
814              latch.countDown();
862            joinPool(e);
815          }
816      }
817  
# Line 868 | Line 820 | public class ForkJoinPoolTest extends JS
820       */
821      public void testTimedInvokeAny4() throws Throwable {
822          ExecutorService e = new ForkJoinPool(1);
823 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
824 <        l.add(new NPETask());
825 <        try {
826 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
827 <            shouldThrow();
828 <        } catch (ExecutionException success) {
829 <            assertTrue(success.getCause() instanceof NullPointerException);
830 <        } finally {
831 <            joinPool(e);
823 >        try (PoolCleaner cleaner = cleaner(e)) {
824 >            long startTime = System.nanoTime();
825 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
826 >            l.add(new NPETask());
827 >            try {
828 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
829 >                shouldThrow();
830 >            } catch (ExecutionException success) {
831 >                assertTrue(success.getCause() instanceof NullPointerException);
832 >            }
833 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
834          }
835      }
836  
# Line 885 | Line 839 | public class ForkJoinPoolTest extends JS
839       */
840      public void testTimedInvokeAny5() throws Throwable {
841          ExecutorService e = new ForkJoinPool(1);
842 <        try {
842 >        try (PoolCleaner cleaner = cleaner(e)) {
843 >            long startTime = System.nanoTime();
844              List<Callable<String>> l = new ArrayList<Callable<String>>();
845              l.add(new StringTask());
846              l.add(new StringTask());
847 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
847 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
848              assertSame(TEST_STRING, result);
849 <        } finally {
895 <            joinPool(e);
849 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
850          }
851      }
852  
# Line 901 | Line 855 | public class ForkJoinPoolTest extends JS
855       */
856      public void testTimedInvokeAll1() throws Throwable {
857          ExecutorService e = new ForkJoinPool(1);
858 <        try {
859 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
860 <            shouldThrow();
861 <        } catch (NullPointerException success) {
862 <        } finally {
909 <            joinPool(e);
858 >        try (PoolCleaner cleaner = cleaner(e)) {
859 >            try {
860 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
861 >                shouldThrow();
862 >            } catch (NullPointerException success) {}
863          }
864      }
865  
# Line 915 | Line 868 | public class ForkJoinPoolTest extends JS
868       */
869      public void testTimedInvokeAllNullTimeUnit() throws Throwable {
870          ExecutorService e = new ForkJoinPool(1);
871 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
872 <        l.add(new StringTask());
873 <        try {
874 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
875 <            shouldThrow();
876 <        } catch (NullPointerException success) {
877 <        } finally {
925 <            joinPool(e);
871 >        try (PoolCleaner cleaner = cleaner(e)) {
872 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
873 >            l.add(new StringTask());
874 >            try {
875 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
876 >                shouldThrow();
877 >            } catch (NullPointerException success) {}
878          }
879      }
880  
# Line 931 | Line 883 | public class ForkJoinPoolTest extends JS
883       */
884      public void testTimedInvokeAll2() throws InterruptedException {
885          ExecutorService e = new ForkJoinPool(1);
886 <        try {
886 >        try (PoolCleaner cleaner = cleaner(e)) {
887              List<Future<String>> r
888                  = e.invokeAll(new ArrayList<Callable<String>>(),
889                                MEDIUM_DELAY_MS, MILLISECONDS);
890              assertTrue(r.isEmpty());
939        } finally {
940            joinPool(e);
891          }
892      }
893  
# Line 946 | Line 896 | public class ForkJoinPoolTest extends JS
896       */
897      public void testTimedInvokeAll3() throws InterruptedException {
898          ExecutorService e = new ForkJoinPool(1);
899 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
900 <        l.add(new StringTask());
901 <        l.add(null);
902 <        try {
903 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
904 <            shouldThrow();
905 <        } catch (NullPointerException success) {
906 <        } finally {
957 <            joinPool(e);
899 >        try (PoolCleaner cleaner = cleaner(e)) {
900 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
901 >            l.add(new StringTask());
902 >            l.add(null);
903 >            try {
904 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
905 >                shouldThrow();
906 >            } catch (NullPointerException success) {}
907          }
908      }
909  
# Line 963 | Line 912 | public class ForkJoinPoolTest extends JS
912       */
913      public void testTimedInvokeAll4() throws Throwable {
914          ExecutorService e = new ForkJoinPool(1);
915 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
916 <        l.add(new NPETask());
917 <        List<Future<String>> futures
918 <            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
919 <        assertEquals(1, futures.size());
920 <        try {
921 <            futures.get(0).get();
922 <            shouldThrow();
923 <        } catch (ExecutionException success) {
924 <            assertTrue(success.getCause() instanceof NullPointerException);
925 <        } finally {
926 <            joinPool(e);
915 >        try (PoolCleaner cleaner = cleaner(e)) {
916 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
917 >            l.add(new NPETask());
918 >            List<Future<String>> futures
919 >                = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
920 >            assertEquals(1, futures.size());
921 >            try {
922 >                futures.get(0).get();
923 >                shouldThrow();
924 >            } catch (ExecutionException success) {
925 >                assertTrue(success.getCause() instanceof NullPointerException);
926 >            }
927          }
928      }
929  
# Line 982 | Line 931 | public class ForkJoinPoolTest extends JS
931       * timed invokeAll(c) returns results of all completed tasks in c
932       */
933      public void testTimedInvokeAll5() throws Throwable {
934 <        ExecutorService e = new ForkJoinPool(1);
935 <        try {
934 >        ForkJoinPool e = new ForkJoinPool(1);
935 >        try (PoolCleaner cleaner = cleaner(e)) {
936              List<Callable<String>> l = new ArrayList<Callable<String>>();
937              l.add(new StringTask());
938              l.add(new StringTask());
939              List<Future<String>> futures
940 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
940 >                = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
941              assertEquals(2, futures.size());
942              for (Future<String> future : futures)
943                  assertSame(TEST_STRING, future.get());
995        } finally {
996            joinPool(e);
944          }
945      }
946  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines