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.63 by jsr166, Sun Oct 4 08:27:41 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 211 | 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);
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 +            assertEquals(1, p.getActiveThreadCount());
227 +            done.countDown();
228          }
229 +        assertEquals(0, p.getPoolSize());
230 +        assertEquals(0, p.getActiveThreadCount());
231      }
232  
233      /**
# Line 230 | Line 241 | public class ForkJoinPoolTest extends JS
241              assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
242              assertFalse(p.awaitTermination(-1L, NANOSECONDS));
243              assertFalse(p.awaitTermination(-1L, MILLISECONDS));
244 <            assertFalse(p.awaitTermination(0L, NANOSECONDS));
245 <            assertFalse(p.awaitTermination(0L, MILLISECONDS));
244 >            assertFalse(p.awaitTermination(randomExpiredTimeout(),
245 >                                           randomTimeUnit()));
246              long timeoutNanos = 999999L;
247              long startTime = System.nanoTime();
248              assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
# Line 256 | 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              }
273        } finally {
274            p.shutdownNow(); // failure might have prevented processing task
275            joinPool(p);
287          }
288      }
289  
# Line 303 | Line 314 | public class ForkJoinPoolTest extends JS
314  
315              assertTrue(p.isQuiescent());
316              assertFalse(p.getAsyncMode());
306            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 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
329          }
330      }
331  
# Line 409 | Line 423 | public class ForkJoinPoolTest extends JS
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());
# Line 548 | 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);
551        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 562 | 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();
568 <        } finally {
569 <            quittingTime.countDown();
570 <            joinPool(p);
581 >            awaitTermination(t);
582          }
583      }
584  
# Line 621 | Line 632 | public class ForkJoinPoolTest extends JS
632      public void testInvokeAny3() throws Throwable {
633          ExecutorService e = new ForkJoinPool(1);
634          try (PoolCleaner cleaner = cleaner(e)) {
635 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
635 >            List<Callable<String>> l = new ArrayList<>();
636              l.add(null);
637              try {
638                  e.invokeAny(l);
# Line 637 | Line 648 | public class ForkJoinPoolTest extends JS
648          CountDownLatch latch = new CountDownLatch(1);
649          ExecutorService e = new ForkJoinPool(1);
650          try (PoolCleaner cleaner = cleaner(e)) {
651 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
651 >            List<Callable<String>> l = new ArrayList<>();
652              l.add(latchAwaitingStringTask(latch));
653              l.add(null);
654              try {
# Line 654 | Line 665 | public class ForkJoinPoolTest extends JS
665      public void testInvokeAny5() throws Throwable {
666          ExecutorService e = new ForkJoinPool(1);
667          try (PoolCleaner cleaner = cleaner(e)) {
668 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
668 >            List<Callable<String>> l = new ArrayList<>();
669              l.add(new NPETask());
670              try {
671                  e.invokeAny(l);
# Line 671 | Line 682 | public class ForkJoinPoolTest extends JS
682      public void testInvokeAny6() throws Throwable {
683          ExecutorService e = new ForkJoinPool(1);
684          try (PoolCleaner cleaner = cleaner(e)) {
685 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
685 >            List<Callable<String>> l = new ArrayList<>();
686              l.add(new StringTask());
687              l.add(new StringTask());
688              String result = e.invokeAny(l);
# Line 693 | Line 704 | public class ForkJoinPoolTest extends JS
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 +        final Collection<Callable<String>> emptyCollection
712 +            = Collections.emptyList();
713          try (PoolCleaner cleaner = cleaner(e)) {
714 <            List<Future<String>> r
702 <                = e.invokeAll(new ArrayList<Callable<String>>());
714 >            List<Future<String>> r = e.invokeAll(emptyCollection);
715              assertTrue(r.isEmpty());
716          }
717      }
# Line 710 | Line 722 | public class ForkJoinPoolTest extends JS
722      public void testInvokeAll3() throws InterruptedException {
723          ExecutorService e = new ForkJoinPool(1);
724          try (PoolCleaner cleaner = cleaner(e)) {
725 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
725 >            List<Callable<String>> l = new ArrayList<>();
726              l.add(new StringTask());
727              l.add(null);
728              try {
# Line 727 | Line 739 | public class ForkJoinPoolTest extends JS
739      public void testInvokeAll4() throws Throwable {
740          ExecutorService e = new ForkJoinPool(1);
741          try (PoolCleaner cleaner = cleaner(e)) {
742 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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());
# Line 746 | Line 758 | public class ForkJoinPoolTest extends JS
758      public void testInvokeAll5() throws Throwable {
759          ExecutorService e = new ForkJoinPool(1);
760          try (PoolCleaner cleaner = cleaner(e)) {
761 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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);
# Line 763 | Line 775 | public class ForkJoinPoolTest extends JS
775          ExecutorService e = new ForkJoinPool(1);
776          try (PoolCleaner cleaner = cleaner(e)) {
777              try {
778 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
778 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
779                  shouldThrow();
780              } catch (NullPointerException success) {}
781          }
# Line 775 | Line 787 | public class ForkJoinPoolTest extends JS
787      public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
788          ExecutorService e = new ForkJoinPool(1);
789          try (PoolCleaner cleaner = cleaner(e)) {
790 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
790 >            List<Callable<String>> l = new ArrayList<>();
791              l.add(new StringTask());
792              try {
793 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
793 >                e.invokeAny(l, randomTimeout(), null);
794                  shouldThrow();
795              } catch (NullPointerException success) {}
796          }
# Line 792 | Line 804 | public class ForkJoinPoolTest extends JS
804          try (PoolCleaner cleaner = cleaner(e)) {
805              try {
806                  e.invokeAny(new ArrayList<Callable<String>>(),
807 <                            MEDIUM_DELAY_MS, MILLISECONDS);
807 >                            randomTimeout(), randomTimeUnit());
808                  shouldThrow();
809              } catch (IllegalArgumentException success) {}
810          }
# Line 805 | Line 817 | public class ForkJoinPoolTest extends JS
817          CountDownLatch latch = new CountDownLatch(1);
818          ExecutorService e = new ForkJoinPool(1);
819          try (PoolCleaner cleaner = cleaner(e)) {
820 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
820 >            List<Callable<String>> l = new ArrayList<>();
821              l.add(latchAwaitingStringTask(latch));
822              l.add(null);
823              try {
824 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
824 >                e.invokeAny(l, randomTimeout(), randomTimeUnit());
825                  shouldThrow();
826              } catch (NullPointerException success) {}
827              latch.countDown();
# Line 822 | Line 834 | public class ForkJoinPoolTest extends JS
834      public void testTimedInvokeAny4() throws Throwable {
835          ExecutorService e = new ForkJoinPool(1);
836          try (PoolCleaner cleaner = cleaner(e)) {
837 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
837 >            long startTime = System.nanoTime();
838 >            List<Callable<String>> l = new ArrayList<>();
839              l.add(new NPETask());
840              try {
841 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
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 839 | Line 853 | public class ForkJoinPoolTest extends JS
853      public void testTimedInvokeAny5() throws Throwable {
854          ExecutorService e = new ForkJoinPool(1);
855          try (PoolCleaner cleaner = cleaner(e)) {
856 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
863          }
864      }
865  
# Line 854 | Line 870 | public class ForkJoinPoolTest extends JS
870          ExecutorService e = new ForkJoinPool(1);
871          try (PoolCleaner cleaner = cleaner(e)) {
872              try {
873 <                e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
873 >                e.invokeAll(null, randomTimeout(), randomTimeUnit());
874                  shouldThrow();
875              } catch (NullPointerException success) {}
876          }
# Line 866 | Line 882 | public class ForkJoinPoolTest extends JS
882      public void testTimedInvokeAllNullTimeUnit() throws Throwable {
883          ExecutorService e = new ForkJoinPool(1);
884          try (PoolCleaner cleaner = cleaner(e)) {
885 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
885 >            List<Callable<String>> l = new ArrayList<>();
886              l.add(new StringTask());
887              try {
888 <                e.invokeAll(l, MEDIUM_DELAY_MS, null);
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 +        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());
906          }
907      }
# Line 894 | Line 912 | public class ForkJoinPoolTest extends JS
912      public void testTimedInvokeAll3() throws InterruptedException {
913          ExecutorService e = new ForkJoinPool(1);
914          try (PoolCleaner cleaner = cleaner(e)) {
915 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
915 >            List<Callable<String>> l = new ArrayList<>();
916              l.add(new StringTask());
917              l.add(null);
918              try {
919 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
919 >                e.invokeAll(l, randomTimeout(), randomTimeUnit());
920                  shouldThrow();
921              } catch (NullPointerException success) {}
922          }
# Line 910 | Line 928 | public class ForkJoinPoolTest extends JS
928      public void testTimedInvokeAll4() throws Throwable {
929          ExecutorService e = new ForkJoinPool(1);
930          try (PoolCleaner cleaner = cleaner(e)) {
931 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
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);
# Line 930 | Line 948 | public class ForkJoinPoolTest extends JS
948      public void testTimedInvokeAll5() throws Throwable {
949          ForkJoinPool e = new ForkJoinPool(1);
950          try (PoolCleaner cleaner = cleaner(e)) {
951 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
951 >            List<Callable<String>> l = new ArrayList<>();
952              l.add(new StringTask());
953              l.add(new StringTask());
954              List<Future<String>> futures

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines