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.57 by jsr166, Sat Oct 3 16:57:25 2015 UTC vs.
Revision 1.71 by jsr166, Thu Sep 15 01:18:01 2016 UTC

# Line 24 | Line 24 | import java.util.concurrent.Future;
24   import java.util.concurrent.RecursiveTask;
25   import java.util.concurrent.RejectedExecutionException;
26   import java.util.concurrent.atomic.AtomicBoolean;
27 + import java.util.concurrent.atomic.AtomicInteger;
28   import java.util.concurrent.locks.ReentrantLock;
29  
30   import junit.framework.AssertionFailedError;
# Line 57 | Line 58 | public class ForkJoinPoolTest extends JS
58  
59      // Some classes to test extension and factory methods
60  
61 <    static class MyHandler implements Thread.UncaughtExceptionHandler {
61 <        volatile int catches = 0;
62 <        public void uncaughtException(Thread t, Throwable e) {
63 <            ++catches;
64 <        }
65 <    }
61 >    static class MyError extends Error {}
62  
63      // to test handlers
64      static class FailingFJWSubclass extends ForkJoinWorkerThread {
65          public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
66 <        protected void onStart() { super.onStart(); throw new Error(); }
66 >        protected void onStart() { super.onStart(); throw new MyError(); }
67      }
68  
69      static class FailingThreadFactory
70              implements ForkJoinPool.ForkJoinWorkerThreadFactory {
71 <        volatile int calls = 0;
71 >        final AtomicInteger calls = new AtomicInteger(0);
72          public ForkJoinWorkerThread newThread(ForkJoinPool p) {
73 <            if (++calls > 1) return null;
73 >            if (calls.incrementAndGet() > 1) return null;
74              return new FailingFJWSubclass(p);
75          }
76      }
# Line 162 | Line 158 | public class ForkJoinPoolTest extends JS
158       */
159      public void testDefaultInitialState() {
160          ForkJoinPool p = new ForkJoinPool(1);
161 <        try {
161 >        try (PoolCleaner cleaner = cleaner(p)) {
162              assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
163                         p.getFactory());
164              assertFalse(p.getAsyncMode());
# Line 174 | Line 170 | public class ForkJoinPoolTest extends JS
170              assertFalse(p.isShutdown());
171              assertFalse(p.isTerminating());
172              assertFalse(p.isTerminated());
177        } finally {
178            joinPool(p);
173          }
174      }
175  
# Line 204 | Line 198 | public class ForkJoinPoolTest extends JS
198       */
199      public void testGetParallelism() {
200          ForkJoinPool p = new ForkJoinPool(1);
201 <        try {
201 >        try (PoolCleaner cleaner = cleaner(p)) {
202              assertEquals(1, p.getParallelism());
209        } finally {
210            joinPool(p);
203          }
204      }
205  
# Line 215 | Line 207 | public class ForkJoinPoolTest extends JS
207       * getPoolSize returns number of started workers.
208       */
209      public void testGetPoolSize() {
210 <        ForkJoinPool p = new ForkJoinPool(1);
211 <        try {
210 >        final CountDownLatch taskStarted = new CountDownLatch(1);
211 >        final CountDownLatch done = new CountDownLatch(1);
212 >        final ForkJoinPool p = new ForkJoinPool(1);
213 >        try (PoolCleaner cleaner = cleaner(p)) {
214              assertEquals(0, p.getActiveThreadCount());
215 <            Future<String> future = p.submit(new StringTask());
215 >            final Runnable task = new CheckedRunnable() {
216 >                public void realRun() throws InterruptedException {
217 >                    taskStarted.countDown();
218 >                    assertEquals(1, p.getPoolSize());
219 >                    assertEquals(1, p.getActiveThreadCount());
220 >                    done.await();
221 >                }};
222 >            Future<?> future = p.submit(task);
223 >            await(taskStarted);
224              assertEquals(1, p.getPoolSize());
225 <        } finally {
226 <            joinPool(p);
225 >            assertEquals(1, p.getActiveThreadCount());
226 >            done.countDown();
227          }
228 +        assertEquals(0, p.getPoolSize());
229 +        assertEquals(0, p.getActiveThreadCount());
230      }
231  
232      /**
# Line 230 | Line 234 | public class ForkJoinPoolTest extends JS
234       */
235      public void testAwaitTermination_timesOut() throws InterruptedException {
236          ForkJoinPool p = new ForkJoinPool(1);
237 <        assertFalse(p.isTerminated());
238 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
239 <        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
240 <        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
241 <        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
242 <        assertFalse(p.awaitTermination(0L, NANOSECONDS));
243 <        assertFalse(p.awaitTermination(0L, MILLISECONDS));
244 <        long timeoutNanos = 999999L;
245 <        long startTime = System.nanoTime();
246 <        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
247 <        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
248 <        assertFalse(p.isTerminated());
249 <        startTime = System.nanoTime();
250 <        long timeoutMillis = timeoutMillis();
251 <        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
252 <        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
253 <        assertFalse(p.isTerminated());
254 <        p.shutdown();
255 <        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
256 <        assertTrue(p.isTerminated());
237 >        try (PoolCleaner cleaner = cleaner(p)) {
238 >            assertFalse(p.isTerminated());
239 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
240 >            assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
241 >            assertFalse(p.awaitTermination(-1L, NANOSECONDS));
242 >            assertFalse(p.awaitTermination(-1L, MILLISECONDS));
243 >            assertFalse(p.awaitTermination(0L, NANOSECONDS));
244 >            assertFalse(p.awaitTermination(0L, MILLISECONDS));
245 >            long timeoutNanos = 999999L;
246 >            long startTime = System.nanoTime();
247 >            assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
248 >            assertTrue(System.nanoTime() - startTime >= timeoutNanos);
249 >            assertFalse(p.isTerminated());
250 >            startTime = System.nanoTime();
251 >            long timeoutMillis = timeoutMillis();
252 >            assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
253 >            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
254 >            assertFalse(p.isTerminated());
255 >            p.shutdown();
256 >            assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
257 >            assertTrue(p.isTerminated());
258 >        }
259      }
260  
261      /**
# Line 260 | Line 266 | public class ForkJoinPoolTest extends JS
266       */
267      public void testSetUncaughtExceptionHandler() throws InterruptedException {
268          final CountDownLatch uehInvoked = new CountDownLatch(1);
269 <        final Thread.UncaughtExceptionHandler eh =
269 >        final Thread.UncaughtExceptionHandler ueh =
270              new Thread.UncaughtExceptionHandler() {
271                  public void uncaughtException(Thread t, Throwable e) {
272 +                    threadAssertTrue(e instanceof MyError);
273 +                    threadAssertTrue(t instanceof FailingFJWSubclass);
274                      uehInvoked.countDown();
275                  }};
276          ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
277 <                                          eh, false);
278 <        try {
279 <            assertSame(eh, p.getUncaughtExceptionHandler());
277 >                                          ueh, false);
278 >        try (PoolCleaner cleaner = cleaner(p)) {
279 >            assertSame(ueh, p.getUncaughtExceptionHandler());
280              try {
281                  p.execute(new FibTask(8));
282 <                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
283 <            } catch (RejectedExecutionException ok) {
282 >                await(uehInvoked);
283 >            } finally {
284 >                p.shutdownNow(); // failure might have prevented processing task
285              }
277        } finally {
278            p.shutdownNow(); // failure might have prevented processing task
279            joinPool(p);
286          }
287      }
288  
# Line 288 | Line 294 | public class ForkJoinPoolTest extends JS
294       */
295      public void testIsQuiescent() throws Exception {
296          ForkJoinPool p = new ForkJoinPool(2);
297 <        try {
297 >        try (PoolCleaner cleaner = cleaner(p)) {
298              assertTrue(p.isQuiescent());
299              long startTime = System.nanoTime();
300              FibTask f = new FibTask(20);
# Line 307 | Line 313 | public class ForkJoinPoolTest extends JS
313  
314              assertTrue(p.isQuiescent());
315              assertFalse(p.getAsyncMode());
310            assertEquals(0, p.getActiveThreadCount());
316              assertEquals(0, p.getQueuedTaskCount());
317              assertEquals(0, p.getQueuedSubmissionCount());
318              assertFalse(p.hasQueuedSubmissions());
319 +            while (p.getActiveThreadCount() != 0
320 +                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
321 +                Thread.yield();
322              assertFalse(p.isShutdown());
323              assertFalse(p.isTerminating());
324              assertFalse(p.isTerminated());
325              assertTrue(f.isDone());
326              assertEquals(6765, (int) f.get());
327 <        } finally {
320 <            joinPool(p);
327 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
328          }
329      }
330  
# Line 326 | Line 333 | public class ForkJoinPoolTest extends JS
333       */
334      public void testSubmitForkJoinTask() throws Throwable {
335          ForkJoinPool p = new ForkJoinPool(1);
336 <        try {
336 >        try (PoolCleaner cleaner = cleaner(p)) {
337              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
338              assertEquals(21, (int) f.get());
332        } finally {
333            joinPool(p);
339          }
340      }
341  
# Line 339 | Line 344 | public class ForkJoinPoolTest extends JS
344       */
345      public void testSubmitAfterShutdown() {
346          ForkJoinPool p = new ForkJoinPool(1);
347 <        try {
347 >        try (PoolCleaner cleaner = cleaner(p)) {
348              p.shutdown();
349              assertTrue(p.isShutdown());
350              try {
351                  ForkJoinTask<Integer> f = p.submit(new FibTask(8));
352                  shouldThrow();
353              } catch (RejectedExecutionException success) {}
349        } finally {
350            joinPool(p);
354          }
355      }
356  
# Line 373 | Line 376 | public class ForkJoinPoolTest extends JS
376      public void testPollSubmission() {
377          final CountDownLatch done = new CountDownLatch(1);
378          SubFJP p = new SubFJP();
379 <        try {
379 >        try (PoolCleaner cleaner = cleaner(p)) {
380              ForkJoinTask a = p.submit(awaiter(done));
381              ForkJoinTask b = p.submit(awaiter(done));
382              ForkJoinTask c = p.submit(awaiter(done));
383              ForkJoinTask r = p.pollSubmission();
384              assertTrue(r == a || r == b || r == c);
385              assertFalse(r.isDone());
383        } finally {
386              done.countDown();
385            joinPool(p);
387          }
388      }
389  
# Line 392 | Line 393 | public class ForkJoinPoolTest extends JS
393      public void testDrainTasksTo() {
394          final CountDownLatch done = new CountDownLatch(1);
395          SubFJP p = new SubFJP();
396 <        try {
396 >        try (PoolCleaner cleaner = cleaner(p)) {
397              ForkJoinTask a = p.submit(awaiter(done));
398              ForkJoinTask b = p.submit(awaiter(done));
399              ForkJoinTask c = p.submit(awaiter(done));
# Line 403 | Line 404 | public class ForkJoinPoolTest extends JS
404                  assertTrue(r == a || r == b || r == c);
405                  assertFalse(r.isDone());
406              }
406        } finally {
407              done.countDown();
408            joinPool(p);
408          }
409      }
410  
# Line 416 | Line 415 | public class ForkJoinPoolTest extends JS
415       */
416      public void testExecuteRunnable() throws Throwable {
417          ExecutorService e = new ForkJoinPool(1);
418 <        try {
418 >        try (PoolCleaner cleaner = cleaner(e)) {
419              final AtomicBoolean done = new AtomicBoolean(false);
420              Future<?> future = e.submit(new CheckedRunnable() {
421                  public void realRun() {
# Line 427 | Line 426 | public class ForkJoinPoolTest extends JS
426              assertTrue(done.get());
427              assertTrue(future.isDone());
428              assertFalse(future.isCancelled());
430        } finally {
431            joinPool(e);
429          }
430      }
431  
# Line 437 | Line 434 | public class ForkJoinPoolTest extends JS
434       */
435      public void testSubmitCallable() throws Throwable {
436          ExecutorService e = new ForkJoinPool(1);
437 <        try {
437 >        try (PoolCleaner cleaner = cleaner(e)) {
438              Future<String> future = e.submit(new StringTask());
439              assertSame(TEST_STRING, future.get());
440              assertTrue(future.isDone());
441              assertFalse(future.isCancelled());
445        } finally {
446            joinPool(e);
442          }
443      }
444  
# Line 452 | Line 447 | public class ForkJoinPoolTest extends JS
447       */
448      public void testSubmitRunnable() throws Throwable {
449          ExecutorService e = new ForkJoinPool(1);
450 <        try {
450 >        try (PoolCleaner cleaner = cleaner(e)) {
451              Future<?> future = e.submit(new NoOpRunnable());
452              assertNull(future.get());
453              assertTrue(future.isDone());
454              assertFalse(future.isCancelled());
460        } finally {
461            joinPool(e);
455          }
456      }
457  
# Line 467 | Line 460 | public class ForkJoinPoolTest extends JS
460       */
461      public void testSubmitRunnable2() throws Throwable {
462          ExecutorService e = new ForkJoinPool(1);
463 <        try {
463 >        try (PoolCleaner cleaner = cleaner(e)) {
464              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
465              assertSame(TEST_STRING, future.get());
466              assertTrue(future.isDone());
467              assertFalse(future.isCancelled());
475        } finally {
476            joinPool(e);
468          }
469      }
470  
# Line 486 | Line 477 | public class ForkJoinPoolTest extends JS
477          Runnable r = new CheckedRunnable() {
478          public void realRun() throws Exception {
479              ExecutorService e = new ForkJoinPool(1);
480 <            try {
480 >            try (PoolCleaner cleaner = cleaner(e)) {
481                  Future future = e.submit(callable);
482                  assertSame(TEST_STRING, future.get());
492            } finally {
493                joinPool(e);
483              }
484          }};
485  
# Line 507 | Line 496 | public class ForkJoinPoolTest extends JS
496          Runnable r = new CheckedRunnable() {
497          public void realRun() throws Exception {
498              ExecutorService e = new ForkJoinPool(1);
499 <            try {
499 >            try (PoolCleaner cleaner = cleaner(e)) {
500                  Future future = e.submit(callable);
501                  assertSame(TEST_STRING, future.get());
513            } finally {
514                joinPool(e);
502              }
503          }};
504  
# Line 528 | Line 515 | public class ForkJoinPoolTest extends JS
515          Runnable r = new CheckedRunnable() {
516          public void realRun() throws Exception {
517              ExecutorService e = new ForkJoinPool(1);
518 <            try {
518 >            try (PoolCleaner cleaner = cleaner(e)) {
519                  Future future = e.submit(callable);
520                  try {
521                      future.get();
# Line 536 | Line 523 | public class ForkJoinPoolTest extends JS
523                  } catch (ExecutionException success) {
524                      assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
525                  }
539            } finally {
540                joinPool(e);
526              }
527          }};
528  
# Line 549 | Line 534 | public class ForkJoinPoolTest extends JS
534       */
535      public void testExecuteNullRunnable() {
536          ExecutorService e = new ForkJoinPool(1);
537 <        try {
538 <            Future<?> future = e.submit((Runnable) null);
539 <            shouldThrow();
540 <        } catch (NullPointerException success) {
541 <        } finally {
557 <            joinPool(e);
537 >        try (PoolCleaner cleaner = cleaner(e)) {
538 >            try {
539 >                Future<?> future = e.submit((Runnable) null);
540 >                shouldThrow();
541 >            } catch (NullPointerException success) {}
542          }
543      }
544  
# Line 563 | Line 547 | public class ForkJoinPoolTest extends JS
547       */
548      public void testSubmitNullCallable() {
549          ExecutorService e = new ForkJoinPool(1);
550 <        try {
551 <            Future<String> future = e.submit((Callable) null);
552 <            shouldThrow();
553 <        } catch (NullPointerException success) {
554 <        } finally {
571 <            joinPool(e);
550 >        try (PoolCleaner cleaner = cleaner(e)) {
551 >            try {
552 >                Future<String> future = e.submit((Callable) null);
553 >                shouldThrow();
554 >            } catch (NullPointerException success) {}
555          }
556      }
557  
# Line 578 | Line 561 | public class ForkJoinPoolTest extends JS
561      public void testInterruptedSubmit() throws InterruptedException {
562          final CountDownLatch submitted    = new CountDownLatch(1);
563          final CountDownLatch quittingTime = new CountDownLatch(1);
581        final ExecutorService p = new ForkJoinPool(1);
564          final Callable<Void> awaiter = new CheckedCallable<Void>() {
565              public Void realCall() throws InterruptedException {
566 <                assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
566 >                assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS));
567                  return null;
568              }};
569 <        try {
569 >        final ExecutorService p = new ForkJoinPool(1);
570 >        try (PoolCleaner cleaner = cleaner(p, quittingTime)) {
571              Thread t = new Thread(new CheckedInterruptedRunnable() {
572                  public void realRun() throws Exception {
573                      Future<Void> future = p.submit(awaiter);
# Line 592 | Line 575 | public class ForkJoinPoolTest extends JS
575                      future.get();
576                  }});
577              t.start();
578 <            assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
578 >            await(submitted);
579              t.interrupt();
580 <            t.join();
598 <        } finally {
599 <            quittingTime.countDown();
600 <            joinPool(p);
580 >            awaitTermination(t);
581          }
582      }
583  
# Line 607 | Line 587 | public class ForkJoinPoolTest extends JS
587       */
588      public void testSubmitEE() throws Throwable {
589          ForkJoinPool p = new ForkJoinPool(1);
590 <        try {
591 <            p.submit(new Callable() {
592 <                public Object call() { throw new ArithmeticException(); }})
593 <                .get();
594 <            shouldThrow();
595 <        } catch (ExecutionException success) {
596 <            assertTrue(success.getCause() instanceof ArithmeticException);
597 <        } finally {
598 <            joinPool(p);
590 >        try (PoolCleaner cleaner = cleaner(p)) {
591 >            try {
592 >                p.submit(new Callable() {
593 >                        public Object call() { throw new ArithmeticException(); }})
594 >                    .get();
595 >                shouldThrow();
596 >            } catch (ExecutionException success) {
597 >                assertTrue(success.getCause() instanceof ArithmeticException);
598 >            }
599          }
600      }
601  
# Line 624 | Line 604 | public class ForkJoinPoolTest extends JS
604       */
605      public void testInvokeAny1() throws Throwable {
606          ExecutorService e = new ForkJoinPool(1);
607 <        try {
608 <            e.invokeAny(null);
609 <            shouldThrow();
610 <        } catch (NullPointerException success) {
611 <        } finally {
632 <            joinPool(e);
607 >        try (PoolCleaner cleaner = cleaner(e)) {
608 >            try {
609 >                e.invokeAny(null);
610 >                shouldThrow();
611 >            } catch (NullPointerException success) {}
612          }
613      }
614  
# Line 638 | Line 617 | public class ForkJoinPoolTest extends JS
617       */
618      public void testInvokeAny2() throws Throwable {
619          ExecutorService e = new ForkJoinPool(1);
620 <        try {
621 <            e.invokeAny(new ArrayList<Callable<String>>());
622 <            shouldThrow();
623 <        } catch (IllegalArgumentException success) {
624 <        } finally {
646 <            joinPool(e);
620 >        try (PoolCleaner cleaner = cleaner(e)) {
621 >            try {
622 >                e.invokeAny(new ArrayList<Callable<String>>());
623 >                shouldThrow();
624 >            } catch (IllegalArgumentException success) {}
625          }
626      }
627  
# Line 652 | Line 630 | public class ForkJoinPoolTest extends JS
630       */
631      public void testInvokeAny3() throws Throwable {
632          ExecutorService e = new ForkJoinPool(1);
633 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
634 <        l.add(null);
635 <        try {
636 <            e.invokeAny(l);
637 <            shouldThrow();
638 <        } catch (NullPointerException success) {
639 <        } finally {
662 <            joinPool(e);
633 >        try (PoolCleaner cleaner = cleaner(e)) {
634 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
635 >            l.add(null);
636 >            try {
637 >                e.invokeAny(l);
638 >                shouldThrow();
639 >            } catch (NullPointerException success) {}
640          }
641      }
642  
# Line 669 | Line 646 | public class ForkJoinPoolTest extends JS
646      public void testInvokeAny4() throws Throwable {
647          CountDownLatch latch = new CountDownLatch(1);
648          ExecutorService e = new ForkJoinPool(1);
649 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
650 <        l.add(latchAwaitingStringTask(latch));
651 <        l.add(null);
652 <        try {
653 <            e.invokeAny(l);
654 <            shouldThrow();
655 <        } catch (NullPointerException success) {
656 <        } finally {
649 >        try (PoolCleaner cleaner = cleaner(e)) {
650 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
651 >            l.add(latchAwaitingStringTask(latch));
652 >            l.add(null);
653 >            try {
654 >                e.invokeAny(l);
655 >                shouldThrow();
656 >            } catch (NullPointerException success) {}
657              latch.countDown();
681            joinPool(e);
658          }
659      }
660  
# Line 687 | Line 663 | public class ForkJoinPoolTest extends JS
663       */
664      public void testInvokeAny5() throws Throwable {
665          ExecutorService e = new ForkJoinPool(1);
666 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
667 <        l.add(new NPETask());
668 <        try {
669 <            e.invokeAny(l);
670 <            shouldThrow();
671 <        } catch (ExecutionException success) {
672 <            assertTrue(success.getCause() instanceof NullPointerException);
673 <        } finally {
674 <            joinPool(e);
666 >        try (PoolCleaner cleaner = cleaner(e)) {
667 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
668 >            l.add(new NPETask());
669 >            try {
670 >                e.invokeAny(l);
671 >                shouldThrow();
672 >            } catch (ExecutionException success) {
673 >                assertTrue(success.getCause() instanceof NullPointerException);
674 >            }
675          }
676      }
677  
# Line 704 | Line 680 | public class ForkJoinPoolTest extends JS
680       */
681      public void testInvokeAny6() throws Throwable {
682          ExecutorService e = new ForkJoinPool(1);
683 <        try {
683 >        try (PoolCleaner cleaner = cleaner(e)) {
684              List<Callable<String>> l = new ArrayList<Callable<String>>();
685              l.add(new StringTask());
686              l.add(new StringTask());
687              String result = e.invokeAny(l);
688              assertSame(TEST_STRING, result);
713        } finally {
714            joinPool(e);
689          }
690      }
691  
# Line 720 | Line 694 | public class ForkJoinPoolTest extends JS
694       */
695      public void testInvokeAll1() throws Throwable {
696          ExecutorService e = new ForkJoinPool(1);
697 <        try {
698 <            e.invokeAll(null);
699 <            shouldThrow();
700 <        } catch (NullPointerException success) {
701 <        } finally {
728 <            joinPool(e);
697 >        try (PoolCleaner cleaner = cleaner(e)) {
698 >            try {
699 >                e.invokeAll(null);
700 >                shouldThrow();
701 >            } catch (NullPointerException success) {}
702          }
703      }
704  
# Line 734 | Line 707 | public class ForkJoinPoolTest extends JS
707       */
708      public void testInvokeAll2() throws InterruptedException {
709          ExecutorService e = new ForkJoinPool(1);
710 <        try {
710 >        try (PoolCleaner cleaner = cleaner(e)) {
711              List<Future<String>> r
712                  = e.invokeAll(new ArrayList<Callable<String>>());
713              assertTrue(r.isEmpty());
741        } finally {
742            joinPool(e);
714          }
715      }
716  
# Line 748 | Line 719 | public class ForkJoinPoolTest extends JS
719       */
720      public void testInvokeAll3() throws InterruptedException {
721          ExecutorService e = new ForkJoinPool(1);
722 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
723 <        l.add(new StringTask());
724 <        l.add(null);
725 <        try {
726 <            e.invokeAll(l);
727 <            shouldThrow();
728 <        } catch (NullPointerException success) {
729 <        } finally {
759 <            joinPool(e);
722 >        try (PoolCleaner cleaner = cleaner(e)) {
723 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
724 >            l.add(new StringTask());
725 >            l.add(null);
726 >            try {
727 >                e.invokeAll(l);
728 >                shouldThrow();
729 >            } catch (NullPointerException success) {}
730          }
731      }
732  
# Line 766 | Line 736 | public class ForkJoinPoolTest extends JS
736       */
737      public void testInvokeAll4() throws Throwable {
738          ExecutorService e = new ForkJoinPool(1);
739 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
740 <        l.add(new NPETask());
741 <        List<Future<String>> futures = e.invokeAll(l);
742 <        assertEquals(1, futures.size());
743 <        try {
744 <            futures.get(0).get();
745 <            shouldThrow();
746 <        } catch (ExecutionException success) {
747 <            assertTrue(success.getCause() instanceof NullPointerException);
748 <        } finally {
749 <            joinPool(e);
739 >        try (PoolCleaner cleaner = cleaner(e)) {
740 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
741 >            l.add(new NPETask());
742 >            List<Future<String>> futures = e.invokeAll(l);
743 >            assertEquals(1, futures.size());
744 >            try {
745 >                futures.get(0).get();
746 >                shouldThrow();
747 >            } catch (ExecutionException success) {
748 >                assertTrue(success.getCause() instanceof NullPointerException);
749 >            }
750          }
751      }
752  
# Line 785 | Line 755 | public class ForkJoinPoolTest extends JS
755       */
756      public void testInvokeAll5() throws Throwable {
757          ExecutorService e = new ForkJoinPool(1);
758 <        try {
758 >        try (PoolCleaner cleaner = cleaner(e)) {
759              List<Callable<String>> l = new ArrayList<Callable<String>>();
760              l.add(new StringTask());
761              l.add(new StringTask());
# Line 793 | Line 763 | public class ForkJoinPoolTest extends JS
763              assertEquals(2, futures.size());
764              for (Future<String> future : futures)
765                  assertSame(TEST_STRING, future.get());
796        } finally {
797            joinPool(e);
766          }
767      }
768  
# Line 803 | Line 771 | public class ForkJoinPoolTest extends JS
771       */
772      public void testTimedInvokeAny1() throws Throwable {
773          ExecutorService e = new ForkJoinPool(1);
774 <        try {
775 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
776 <            shouldThrow();
777 <        } catch (NullPointerException success) {
778 <        } finally {
811 <            joinPool(e);
774 >        try (PoolCleaner cleaner = cleaner(e)) {
775 >            try {
776 >                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
777 >                shouldThrow();
778 >            } catch (NullPointerException success) {}
779          }
780      }
781  
# Line 817 | Line 784 | public class ForkJoinPoolTest extends JS
784       */
785      public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
786          ExecutorService e = new ForkJoinPool(1);
787 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
788 <        l.add(new StringTask());
789 <        try {
790 <            e.invokeAny(l, MEDIUM_DELAY_MS, null);
791 <            shouldThrow();
792 <        } catch (NullPointerException success) {
793 <        } finally {
827 <            joinPool(e);
787 >        try (PoolCleaner cleaner = cleaner(e)) {
788 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
789 >            l.add(new StringTask());
790 >            try {
791 >                e.invokeAny(l, MEDIUM_DELAY_MS, null);
792 >                shouldThrow();
793 >            } catch (NullPointerException success) {}
794          }
795      }
796  
# Line 833 | Line 799 | public class ForkJoinPoolTest extends JS
799       */
800      public void testTimedInvokeAny2() throws Throwable {
801          ExecutorService e = new ForkJoinPool(1);
802 <        try {
803 <            e.invokeAny(new ArrayList<Callable<String>>(),
804 <                        MEDIUM_DELAY_MS, MILLISECONDS);
805 <            shouldThrow();
806 <        } catch (IllegalArgumentException success) {
807 <        } finally {
842 <            joinPool(e);
802 >        try (PoolCleaner cleaner = cleaner(e)) {
803 >            try {
804 >                e.invokeAny(new ArrayList<Callable<String>>(),
805 >                            MEDIUM_DELAY_MS, MILLISECONDS);
806 >                shouldThrow();
807 >            } catch (IllegalArgumentException success) {}
808          }
809      }
810  
# Line 849 | Line 814 | public class ForkJoinPoolTest extends JS
814      public void testTimedInvokeAny3() throws Throwable {
815          CountDownLatch latch = new CountDownLatch(1);
816          ExecutorService e = new ForkJoinPool(1);
817 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
818 <        l.add(latchAwaitingStringTask(latch));
819 <        l.add(null);
820 <        try {
821 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
822 <            shouldThrow();
823 <        } catch (NullPointerException success) {
824 <        } finally {
817 >        try (PoolCleaner cleaner = cleaner(e)) {
818 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
819 >            l.add(latchAwaitingStringTask(latch));
820 >            l.add(null);
821 >            try {
822 >                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
823 >                shouldThrow();
824 >            } catch (NullPointerException success) {}
825              latch.countDown();
861            joinPool(e);
826          }
827      }
828  
# Line 867 | Line 831 | public class ForkJoinPoolTest extends JS
831       */
832      public void testTimedInvokeAny4() throws Throwable {
833          ExecutorService e = new ForkJoinPool(1);
834 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
835 <        l.add(new NPETask());
836 <        try {
837 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
838 <            shouldThrow();
839 <        } catch (ExecutionException success) {
840 <            assertTrue(success.getCause() instanceof NullPointerException);
841 <        } finally {
842 <            joinPool(e);
834 >        try (PoolCleaner cleaner = cleaner(e)) {
835 >            long startTime = System.nanoTime();
836 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
837 >            l.add(new NPETask());
838 >            try {
839 >                e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
840 >                shouldThrow();
841 >            } catch (ExecutionException success) {
842 >                assertTrue(success.getCause() instanceof NullPointerException);
843 >            }
844 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
845          }
846      }
847  
# Line 884 | Line 850 | public class ForkJoinPoolTest extends JS
850       */
851      public void testTimedInvokeAny5() throws Throwable {
852          ExecutorService e = new ForkJoinPool(1);
853 <        try {
853 >        try (PoolCleaner cleaner = cleaner(e)) {
854 >            long startTime = System.nanoTime();
855              List<Callable<String>> l = new ArrayList<Callable<String>>();
856              l.add(new StringTask());
857              l.add(new StringTask());
858 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
858 >            String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
859              assertSame(TEST_STRING, result);
860 <        } finally {
894 <            joinPool(e);
860 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
861          }
862      }
863  
# Line 900 | Line 866 | public class ForkJoinPoolTest extends JS
866       */
867      public void testTimedInvokeAll1() throws Throwable {
868          ExecutorService e = new ForkJoinPool(1);
869 <        try {
870 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
871 <            shouldThrow();
872 <        } catch (NullPointerException success) {
873 <        } finally {
908 <            joinPool(e);
869 >        try (PoolCleaner cleaner = cleaner(e)) {
870 >            try {
871 >                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
872 >                shouldThrow();
873 >            } catch (NullPointerException success) {}
874          }
875      }
876  
# Line 914 | Line 879 | public class ForkJoinPoolTest extends JS
879       */
880      public void testTimedInvokeAllNullTimeUnit() throws Throwable {
881          ExecutorService e = new ForkJoinPool(1);
882 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
883 <        l.add(new StringTask());
884 <        try {
885 <            e.invokeAll(l, MEDIUM_DELAY_MS, null);
886 <            shouldThrow();
887 <        } catch (NullPointerException success) {
888 <        } finally {
924 <            joinPool(e);
882 >        try (PoolCleaner cleaner = cleaner(e)) {
883 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
884 >            l.add(new StringTask());
885 >            try {
886 >                e.invokeAll(l, MEDIUM_DELAY_MS, null);
887 >                shouldThrow();
888 >            } catch (NullPointerException success) {}
889          }
890      }
891  
# Line 930 | Line 894 | public class ForkJoinPoolTest extends JS
894       */
895      public void testTimedInvokeAll2() throws InterruptedException {
896          ExecutorService e = new ForkJoinPool(1);
897 <        try {
897 >        try (PoolCleaner cleaner = cleaner(e)) {
898              List<Future<String>> r
899                  = e.invokeAll(new ArrayList<Callable<String>>(),
900                                MEDIUM_DELAY_MS, MILLISECONDS);
901              assertTrue(r.isEmpty());
938        } finally {
939            joinPool(e);
902          }
903      }
904  
# Line 945 | Line 907 | public class ForkJoinPoolTest extends JS
907       */
908      public void testTimedInvokeAll3() throws InterruptedException {
909          ExecutorService e = new ForkJoinPool(1);
910 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
911 <        l.add(new StringTask());
912 <        l.add(null);
913 <        try {
914 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
915 <            shouldThrow();
916 <        } catch (NullPointerException success) {
917 <        } finally {
956 <            joinPool(e);
910 >        try (PoolCleaner cleaner = cleaner(e)) {
911 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
912 >            l.add(new StringTask());
913 >            l.add(null);
914 >            try {
915 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
916 >                shouldThrow();
917 >            } catch (NullPointerException success) {}
918          }
919      }
920  
# Line 962 | Line 923 | public class ForkJoinPoolTest extends JS
923       */
924      public void testTimedInvokeAll4() throws Throwable {
925          ExecutorService e = new ForkJoinPool(1);
926 <        List<Callable<String>> l = new ArrayList<Callable<String>>();
927 <        l.add(new NPETask());
928 <        List<Future<String>> futures
929 <            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
930 <        assertEquals(1, futures.size());
931 <        try {
932 <            futures.get(0).get();
933 <            shouldThrow();
934 <        } catch (ExecutionException success) {
935 <            assertTrue(success.getCause() instanceof NullPointerException);
936 <        } finally {
937 <            joinPool(e);
926 >        try (PoolCleaner cleaner = cleaner(e)) {
927 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
928 >            l.add(new NPETask());
929 >            List<Future<String>> futures
930 >                = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
931 >            assertEquals(1, futures.size());
932 >            try {
933 >                futures.get(0).get();
934 >                shouldThrow();
935 >            } catch (ExecutionException success) {
936 >                assertTrue(success.getCause() instanceof NullPointerException);
937 >            }
938          }
939      }
940  
# Line 981 | Line 942 | public class ForkJoinPoolTest extends JS
942       * timed invokeAll(c) returns results of all completed tasks in c
943       */
944      public void testTimedInvokeAll5() throws Throwable {
945 <        try (PoolCloser<ForkJoinPool> poolCloser
946 <             = new PoolCloser(new ForkJoinPool(1))) {
986 <            ForkJoinPool e = poolCloser.pool;
945 >        ForkJoinPool e = new ForkJoinPool(1);
946 >        try (PoolCleaner cleaner = cleaner(e)) {
947              List<Callable<String>> l = new ArrayList<Callable<String>>();
948              l.add(new StringTask());
949              l.add(new StringTask());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines