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.62 by jsr166, Sun Oct 4 07:42:07 2015 UTC vs.
Revision 1.74 by jsr166, Mon May 29 19:15:02 2017 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 211 | 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);
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 >                    await(done);
221 >                }};
222 >            Future<?> future = p.submit(task);
223 >            await(taskStarted);
224              assertEquals(1, p.getPoolSize());
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 240 | public class ForkJoinPoolTest extends JS
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));
243 >            assertFalse(p.awaitTermination(randomExpiredTimeout(),
244 >                                           randomTimeUnit()));
245              long timeoutNanos = 999999L;
246              long startTime = System.nanoTime();
247              assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
# Line 256 | 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              }
273        } finally {
274            p.shutdownNow(); // failure might have prevented processing task
275            joinPool(p);
286          }
287      }
288  
# Line 303 | Line 313 | public class ForkJoinPoolTest extends JS
313  
314              assertTrue(p.isQuiescent());
315              assertFalse(p.getAsyncMode());
306            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 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
328          }
329      }
330  
# Line 409 | Line 422 | public class ForkJoinPoolTest extends JS
422                      done.set(true);
423                  }});
424              assertNull(future.get());
425 <            assertNull(future.get(0, MILLISECONDS));
425 >            assertNull(future.get(randomExpiredTimeout(), randomTimeUnit()));
426              assertTrue(done.get());
427              assertTrue(future.isDone());
428              assertFalse(future.isCancelled());
# Line 548 | 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);
551        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 562 | 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();
568 <        } finally {
569 <            quittingTime.countDown();
570 <            joinPool(p);
580 >            awaitTermination(t);
581          }
582      }
583  
# Line 621 | Line 631 | public class ForkJoinPoolTest extends JS
631      public void testInvokeAny3() throws Throwable {
632          ExecutorService e = new ForkJoinPool(1);
633          try (PoolCleaner cleaner = cleaner(e)) {
634 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
634 >            List<Callable<String>> l = new ArrayList<>();
635              l.add(null);
636              try {
637                  e.invokeAny(l);
# Line 637 | Line 647 | public class ForkJoinPoolTest extends JS
647          CountDownLatch latch = new CountDownLatch(1);
648          ExecutorService e = new ForkJoinPool(1);
649          try (PoolCleaner cleaner = cleaner(e)) {
650 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
650 >            List<Callable<String>> l = new ArrayList<>();
651              l.add(latchAwaitingStringTask(latch));
652              l.add(null);
653              try {
# Line 654 | Line 664 | public class ForkJoinPoolTest extends JS
664      public void testInvokeAny5() throws Throwable {
665          ExecutorService e = new ForkJoinPool(1);
666          try (PoolCleaner cleaner = cleaner(e)) {
667 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
667 >            List<Callable<String>> l = new ArrayList<>();
668              l.add(new NPETask());
669              try {
670                  e.invokeAny(l);
# Line 671 | Line 681 | public class ForkJoinPoolTest extends JS
681      public void testInvokeAny6() throws Throwable {
682          ExecutorService e = new ForkJoinPool(1);
683          try (PoolCleaner cleaner = cleaner(e)) {
684 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
684 >            List<Callable<String>> l = new ArrayList<>();
685              l.add(new StringTask());
686              l.add(new StringTask());
687              String result = e.invokeAny(l);
# Line 710 | Line 720 | public class ForkJoinPoolTest extends JS
720      public void testInvokeAll3() throws InterruptedException {
721          ExecutorService e = new ForkJoinPool(1);
722          try (PoolCleaner cleaner = cleaner(e)) {
723 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
723 >            List<Callable<String>> l = new ArrayList<>();
724              l.add(new StringTask());
725              l.add(null);
726              try {
# Line 727 | Line 737 | public class ForkJoinPoolTest extends JS
737      public void testInvokeAll4() throws Throwable {
738          ExecutorService e = new ForkJoinPool(1);
739          try (PoolCleaner cleaner = cleaner(e)) {
740 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
740 >            List<Callable<String>> l = new ArrayList<>();
741              l.add(new NPETask());
742              List<Future<String>> futures = e.invokeAll(l);
743              assertEquals(1, futures.size());
# Line 746 | Line 756 | public class ForkJoinPoolTest extends JS
756      public void testInvokeAll5() throws Throwable {
757          ExecutorService e = new ForkJoinPool(1);
758          try (PoolCleaner cleaner = cleaner(e)) {
759 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
759 >            List<Callable<String>> l = new ArrayList<>();
760              l.add(new StringTask());
761              l.add(new StringTask());
762              List<Future<String>> futures = e.invokeAll(l);
# Line 763 | Line 773 | public class ForkJoinPoolTest extends JS
773          ExecutorService e = new ForkJoinPool(1);
774          try (PoolCleaner cleaner = cleaner(e)) {
775              try {
776 <                e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
776 >                e.invokeAny(null, randomTimeout(), randomTimeUnit());
777                  shouldThrow();
778              } catch (NullPointerException success) {}
779          }
# Line 775 | Line 785 | public class ForkJoinPoolTest extends JS
785      public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
786          ExecutorService e = new ForkJoinPool(1);
787          try (PoolCleaner cleaner = cleaner(e)) {
788 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
788 >            List<Callable<String>> l = new ArrayList<>();
789              l.add(new StringTask());
790              try {
791 <                e.invokeAny(l, MEDIUM_DELAY_MS, null);
791 >                e.invokeAny(l, randomTimeout(), null);
792                  shouldThrow();
793              } catch (NullPointerException success) {}
794          }
# Line 792 | Line 802 | public class ForkJoinPoolTest extends JS
802          try (PoolCleaner cleaner = cleaner(e)) {
803              try {
804                  e.invokeAny(new ArrayList<Callable<String>>(),
805 <                            MEDIUM_DELAY_MS, MILLISECONDS);
805 >                            randomTimeout(), randomTimeUnit());
806                  shouldThrow();
807              } catch (IllegalArgumentException success) {}
808          }
# Line 805 | Line 815 | public class ForkJoinPoolTest extends JS
815          CountDownLatch latch = new CountDownLatch(1);
816          ExecutorService e = new ForkJoinPool(1);
817          try (PoolCleaner cleaner = cleaner(e)) {
818 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
818 >            List<Callable<String>> l = new ArrayList<>();
819              l.add(latchAwaitingStringTask(latch));
820              l.add(null);
821              try {
# Line 822 | Line 832 | public class ForkJoinPoolTest extends JS
832      public void testTimedInvokeAny4() throws Throwable {
833          ExecutorService e = new ForkJoinPool(1);
834          try (PoolCleaner cleaner = cleaner(e)) {
835 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
835 >            long startTime = System.nanoTime();
836 >            List<Callable<String>> l = new ArrayList<>();
837              l.add(new NPETask());
838              try {
839 <                e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
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 839 | Line 851 | public class ForkJoinPoolTest extends JS
851      public void testTimedInvokeAny5() throws Throwable {
852          ExecutorService e = new ForkJoinPool(1);
853          try (PoolCleaner cleaner = cleaner(e)) {
854 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
854 >            long startTime = System.nanoTime();
855 >            List<Callable<String>> l = new ArrayList<>();
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 +            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
861          }
862      }
863  
# Line 866 | Line 880 | public class ForkJoinPoolTest extends JS
880      public void testTimedInvokeAllNullTimeUnit() throws Throwable {
881          ExecutorService e = new ForkJoinPool(1);
882          try (PoolCleaner cleaner = cleaner(e)) {
883 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
883 >            List<Callable<String>> l = new ArrayList<>();
884              l.add(new StringTask());
885              try {
886                  e.invokeAll(l, MEDIUM_DELAY_MS, null);
# Line 894 | Line 908 | public class ForkJoinPoolTest extends JS
908      public void testTimedInvokeAll3() throws InterruptedException {
909          ExecutorService e = new ForkJoinPool(1);
910          try (PoolCleaner cleaner = cleaner(e)) {
911 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
911 >            List<Callable<String>> l = new ArrayList<>();
912              l.add(new StringTask());
913              l.add(null);
914              try {
# Line 910 | Line 924 | public class ForkJoinPoolTest extends JS
924      public void testTimedInvokeAll4() throws Throwable {
925          ExecutorService e = new ForkJoinPool(1);
926          try (PoolCleaner cleaner = cleaner(e)) {
927 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
927 >            List<Callable<String>> l = new ArrayList<>();
928              l.add(new NPETask());
929              List<Future<String>> futures
930 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
930 >                = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
931              assertEquals(1, futures.size());
932              try {
933                  futures.get(0).get();
# Line 930 | Line 944 | public class ForkJoinPoolTest extends JS
944      public void testTimedInvokeAll5() throws Throwable {
945          ForkJoinPool e = new ForkJoinPool(1);
946          try (PoolCleaner cleaner = cleaner(e)) {
947 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
947 >            List<Callable<String>> l = new ArrayList<>();
948              l.add(new StringTask());
949              l.add(new StringTask());
950              List<Future<String>> futures

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines