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.56 by jsr166, Fri Oct 2 21:52:36 2015 UTC vs.
Revision 1.75 by jsr166, Mon May 29 22:44:27 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines