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.13 by jsr166, Thu Nov 26 15:42:15 2009 UTC vs.
Revision 1.20 by dl, Wed Aug 11 19:50:02 2010 UTC

# Line 7 | Line 7
7  
8   import junit.framework.*;
9   import java.util.*;
10 < import java.util.concurrent.*;
11 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 > import java.util.concurrent.Executor;
11 > import java.util.concurrent.Executors;
12 > import java.util.concurrent.ExecutorService;
13 > import java.util.concurrent.AbstractExecutorService;
14 > import java.util.concurrent.CountDownLatch;
15 > import java.util.concurrent.Callable;
16 > import java.util.concurrent.Future;
17 > import java.util.concurrent.ExecutionException;
18 > import java.util.concurrent.CancellationException;
19 > import java.util.concurrent.RejectedExecutionException;
20 > import java.util.concurrent.ForkJoinPool;
21 > import java.util.concurrent.ForkJoinTask;
22 > import java.util.concurrent.ForkJoinWorkerThread;
23 > import java.util.concurrent.RecursiveAction;
24 > import java.util.concurrent.RecursiveTask;
25 > import java.util.concurrent.TimeUnit;
26   import java.util.concurrent.locks.*;
27   import java.security.*;
28  
# Line 39 | Line 53 | public class ForkJoinPoolTest extends JS
53      // Some classes to test extension and factory methods
54  
55      static class MyHandler implements Thread.UncaughtExceptionHandler {
56 <        int catches = 0;
56 >        volatile int catches = 0;
57          public void uncaughtException(Thread t, Throwable e) {
58              ++catches;
59          }
# Line 48 | Line 62 | public class ForkJoinPoolTest extends JS
62      // to test handlers
63      static class FailingFJWSubclass extends ForkJoinWorkerThread {
64          public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
65 <        protected void onStart() { throw new Error(); }
65 >        protected void onStart() { super.onStart(); throw new Error(); }
66      }
67  
68      static class FailingThreadFactory
69              implements ForkJoinPool.ForkJoinWorkerThreadFactory {
70 <        int calls = 0;
70 >        volatile int calls = 0;
71          public ForkJoinWorkerThread newThread(ForkJoinPool p) {
72              if (++calls > 1) return null;
73              return new FailingFJWSubclass(p);
# Line 148 | Line 162 | public class ForkJoinPoolTest extends JS
162              assertTrue(p.getFactory() ==
163                         ForkJoinPool.defaultForkJoinWorkerThreadFactory);
164              assertTrue(p.isQuiescent());
151            assertTrue(p.getMaintainsParallelism());
165              assertFalse(p.getAsyncMode());
166              assertTrue(p.getActiveThreadCount() == 0);
167              assertTrue(p.getStealCount() == 0);
# Line 178 | Line 191 | public class ForkJoinPoolTest extends JS
191       */
192      public void testConstructor2() {
193          try {
194 <            new ForkJoinPool(1, null);
194 >            new ForkJoinPool(1, null, null, false);
195              shouldThrow();
196          } catch (NullPointerException success) {}
197      }
# Line 198 | Line 211 | public class ForkJoinPoolTest extends JS
211      }
212  
213      /**
201     * setParallelism changes reported parallelism level.
202     */
203    public void testSetParallelism() {
204        ForkJoinPool p = null;
205        try {
206            p = new ForkJoinPool(1);
207            assertTrue(p.getParallelism() == 1);
208            p.setParallelism(2);
209            assertTrue(p.getParallelism() == 2);
210        } finally {
211            joinPool(p);
212        }
213    }
214
215    /**
216     * setParallelism with argument <= 0 throws exception
217     */
218    public void testSetParallelism2() {
219        ForkJoinPool p = null;
220        try {
221            p = new ForkJoinPool(1);
222            assertTrue(p.getParallelism() == 1);
223            p.setParallelism(-2);
224            shouldThrow();
225        } catch (IllegalArgumentException success) {
226        } finally {
227            joinPool(p);
228        }
229    }
230
231    /**
214       * getPoolSize returns number of started workers.
215       */
216      public void testGetPoolSize() {
217          ForkJoinPool p = null;
218          try {
219              p = new ForkJoinPool(1);
220 <            assertTrue(p.getPoolSize() == 0);
220 >            assertTrue(p.getActiveThreadCount() == 0);
221              Future<String> future = p.submit(new StringTask());
222              assertTrue(p.getPoolSize() == 1);
223  
# Line 245 | Line 227 | public class ForkJoinPoolTest extends JS
227      }
228  
229      /**
248     * setMaximumPoolSize changes size reported by getMaximumPoolSize.
249     */
250    public void testSetMaximumPoolSize() {
251        ForkJoinPool p = null;
252        try {
253            p = new ForkJoinPool(1);
254            p.setMaximumPoolSize(2);
255            assertTrue(p.getMaximumPoolSize() == 2);
256        } finally {
257            joinPool(p);
258        }
259    }
260
261    /**
262     * setMaximumPoolSize with argument <= 0 throws exception
263     */
264    public void testSetMaximumPoolSize2() {
265        ForkJoinPool p = null;
266        try {
267            p = new ForkJoinPool(1);
268            p.setMaximumPoolSize(-2);
269            shouldThrow();
270        } catch (IllegalArgumentException success) {
271        } finally {
272            joinPool(p);
273        }
274    }
275
276    /**
277     * setMaintainsParallelism changes policy reported by
278     * getMaintainsParallelism.
279     */
280    public void testSetMaintainsParallelism() {
281        ForkJoinPool p = null;
282        try {
283            p = new ForkJoinPool(1);
284            p.setMaintainsParallelism(false);
285            assertFalse(p.getMaintainsParallelism());
286        } finally {
287            joinPool(p);
288        }
289    }
290
291    /**
292     * setAsyncMode changes policy reported by
293     * getAsyncMode.
294     */
295    public void testSetAsyncMode() {
296        ForkJoinPool p = null;
297        try {
298            p = new ForkJoinPool(1);
299            p.setAsyncMode(true);
300            assertTrue(p.getAsyncMode());
301        } finally {
302            joinPool(p);
303        }
304    }
305
306    /**
230       * setUncaughtExceptionHandler changes handler for uncaught exceptions.
231       *
232       * Additionally tests: Overriding ForkJoinWorkerThread.onStart
# Line 312 | Line 235 | public class ForkJoinPoolTest extends JS
235      public void testSetUncaughtExceptionHandler() throws InterruptedException {
236          ForkJoinPool p = null;
237          try {
315            p = new ForkJoinPool(1, new FailingThreadFactory());
238              MyHandler eh = new MyHandler();
239 <            p.setUncaughtExceptionHandler(eh);
240 <            assertEquals(eh, p.getUncaughtExceptionHandler());
239 >            p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false);
240 >            assert(eh == p.getUncaughtExceptionHandler());
241              p.execute(new FailingTask());
242              Thread.sleep(MEDIUM_DELAY_MS);
243              assertTrue(eh.catches > 0);
244          } finally {
245 +            p.shutdownNow();
246              joinPool(p);
247          }
248      }
249  
250      /**
328     * setUncaughtExceptionHandler of null removes handler
329     */
330    public void testSetUncaughtExceptionHandler2() {
331        ForkJoinPool p = null;
332        try {
333            p = new ForkJoinPool(1);
334            p.setUncaughtExceptionHandler(null);
335            assertNull(p.getUncaughtExceptionHandler());
336        } finally {
337            joinPool(p);
338        }
339    }
340
341
342    /**
251       * After invoking a single task, isQuiescent is true,
252       * queues are empty, threads are not active, and
253       * construction parameters continue to hold
# Line 353 | Line 261 | public class ForkJoinPoolTest extends JS
261                         ForkJoinPool.defaultForkJoinWorkerThreadFactory);
262              Thread.sleep(MEDIUM_DELAY_MS);
263              assertTrue(p.isQuiescent());
356            assertTrue(p.getMaintainsParallelism());
264              assertFalse(p.getAsyncMode());
265              assertTrue(p.getActiveThreadCount() == 0);
266              assertTrue(p.getQueuedTaskCount() == 0);
# Line 410 | Line 317 | public class ForkJoinPoolTest extends JS
317              ManagedLocker locker = new ManagedLocker(lock);
318              ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
319              p.execute(f);
413            assertTrue(p.getPoolSize() >= 4);
320              int r = f.get();
321 <            assertTrue(r ==  832040);
321 >            assertTrue(r == 832040);
322          } finally {
323              p.shutdownNow(); // don't wait out shutdown
324          }
# Line 665 | Line 571 | public class ForkJoinPoolTest extends JS
571          } catch (ExecutionException success) {
572              assertTrue(success.getCause() instanceof ArithmeticException);
573          }
574 <
574 >        
575          joinPool(p);
576      }
577  
# Line 702 | Line 608 | public class ForkJoinPoolTest extends JS
608       */
609      public void testInvokeAny3() throws Throwable {
610          ExecutorService e = new ForkJoinPool(1);
611 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
612 +        l.add(null);
613          try {
706            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
707            l.add(null);
614              e.invokeAny(l);
615              shouldThrow();
616          } catch (NullPointerException success) {
# Line 717 | Line 623 | public class ForkJoinPoolTest extends JS
623       * invokeAny(c) throws NullPointerException if c has null elements
624       */
625      public void testInvokeAny4() throws Throwable {
626 +        CountDownLatch latch = new CountDownLatch(1);
627          ExecutorService e = new ForkJoinPool(1);
628 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
629 +        l.add(latchAwaitingStringTask(latch));
630 +        l.add(null);
631          try {
722            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
723            l.add(new Callable<String>() {
724                public String call() {
725                    // The delay gives the pool a chance to notice
726                    // the null element.
727                    sleepTillInterrupted(SMALL_DELAY_MS);
728                    return "foo";
729                }});
730            l.add(null);
632              e.invokeAny(l);
633              shouldThrow();
634          } catch (NullPointerException success) {
635          } finally {
636 +            latch.countDown();
637              joinPool(e);
638          }
639      }
# Line 741 | Line 643 | public class ForkJoinPoolTest extends JS
643       */
644      public void testInvokeAny5() throws Throwable {
645          ExecutorService e = new ForkJoinPool(1);
646 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
647 +        l.add(new NPETask());
648          try {
745            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
746            l.add(new NPETask());
649              e.invokeAny(l);
650              shouldThrow();
651          } catch (ExecutionException success) {
# Line 759 | Line 661 | public class ForkJoinPoolTest extends JS
661      public void testInvokeAny6() throws Throwable {
662          ExecutorService e = new ForkJoinPool(1);
663          try {
664 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
664 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
665              l.add(new StringTask());
666              l.add(new StringTask());
667              String result = e.invokeAny(l);
# Line 802 | Line 704 | public class ForkJoinPoolTest extends JS
704       */
705      public void testInvokeAll3() throws InterruptedException {
706          ExecutorService e = new ForkJoinPool(1);
707 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
708 +        l.add(new StringTask());
709 +        l.add(null);
710          try {
806            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
807            l.add(new StringTask());
808            l.add(null);
711              e.invokeAll(l);
712              shouldThrow();
713          } catch (NullPointerException success) {
# Line 820 | Line 722 | public class ForkJoinPoolTest extends JS
722       */
723      public void testInvokeAll4() throws Throwable {
724          ExecutorService e = new ForkJoinPool(1);
725 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
726 +        l.add(new NPETask());
727 +        List<Future<String>> futures = e.invokeAll(l);
728 +        assertEquals(1, futures.size());
729          try {
730 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
825 <            l.add(new NPETask());
826 <            List<Future<String>> result = e.invokeAll(l);
827 <            assertEquals(1, result.size());
828 <            for (Future<String> future : result)
829 <                future.get();
730 >            futures.get(0).get();
731              shouldThrow();
732          } catch (ExecutionException success) {
733              assertTrue(success.getCause() instanceof NullPointerException);
# Line 841 | Line 742 | public class ForkJoinPoolTest extends JS
742      public void testInvokeAll5() throws Throwable {
743          ExecutorService e = new ForkJoinPool(1);
744          try {
745 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
745 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
746              l.add(new StringTask());
747              l.add(new StringTask());
748 <            List<Future<String>> result = e.invokeAll(l);
749 <            assertEquals(2, result.size());
750 <            for (Future<String> future : result)
748 >            List<Future<String>> futures = e.invokeAll(l);
749 >            assertEquals(2, futures.size());
750 >            for (Future<String> future : futures)
751                  assertSame(TEST_STRING, future.get());
752          } finally {
753              joinPool(e);
# Line 860 | Line 761 | public class ForkJoinPoolTest extends JS
761      public void testTimedInvokeAny1() throws Throwable {
762          ExecutorService e = new ForkJoinPool(1);
763          try {
764 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
764 >            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
765              shouldThrow();
766          } catch (NullPointerException success) {
767          } finally {
# Line 873 | Line 774 | public class ForkJoinPoolTest extends JS
774       */
775      public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
776          ExecutorService e = new ForkJoinPool(1);
777 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
778 +        l.add(new StringTask());
779          try {
877            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
878            l.add(new StringTask());
780              e.invokeAny(l, MEDIUM_DELAY_MS, null);
781              shouldThrow();
782          } catch (NullPointerException success) {
# Line 891 | Line 792 | public class ForkJoinPoolTest extends JS
792          ExecutorService e = new ForkJoinPool(1);
793          try {
794              e.invokeAny(new ArrayList<Callable<String>>(),
795 <                        MEDIUM_DELAY_MS, MILLISECONDS);
795 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
796              shouldThrow();
797          } catch (IllegalArgumentException success) {
798          } finally {
# Line 903 | Line 804 | public class ForkJoinPoolTest extends JS
804       * timed invokeAny(c) throws NullPointerException if c has null elements
805       */
806      public void testTimedInvokeAny3() throws Throwable {
807 +        CountDownLatch latch = new CountDownLatch(1);
808          ExecutorService e = new ForkJoinPool(1);
809 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
810 +        l.add(latchAwaitingStringTask(latch));
811 +        l.add(null);
812          try {
813 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
909 <            l.add(new StringTask());
910 <            l.add(null);
911 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
813 >            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
814              shouldThrow();
815          } catch (NullPointerException success) {
816          } finally {
817 +            latch.countDown();
818              joinPool(e);
819          }
820      }
# Line 921 | Line 824 | public class ForkJoinPoolTest extends JS
824       */
825      public void testTimedInvokeAny4() throws Throwable {
826          ExecutorService e = new ForkJoinPool(1);
827 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
828 +        l.add(new NPETask());
829          try {
830 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
926 <            l.add(new NPETask());
927 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
830 >            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
831              shouldThrow();
832          } catch (ExecutionException success) {
833              assertTrue(success.getCause() instanceof NullPointerException);
# Line 939 | Line 842 | public class ForkJoinPoolTest extends JS
842      public void testTimedInvokeAny5() throws Throwable {
843          ExecutorService e = new ForkJoinPool(1);
844          try {
845 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
845 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
846              l.add(new StringTask());
847              l.add(new StringTask());
848 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
848 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
849              assertSame(TEST_STRING, result);
850          } finally {
851              joinPool(e);
# Line 955 | Line 858 | public class ForkJoinPoolTest extends JS
858      public void testTimedInvokeAll1() throws Throwable {
859          ExecutorService e = new ForkJoinPool(1);
860          try {
861 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
861 >            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
862              shouldThrow();
863          } catch (NullPointerException success) {
864          } finally {
# Line 968 | Line 871 | public class ForkJoinPoolTest extends JS
871       */
872      public void testTimedInvokeAllNullTimeUnit() throws Throwable {
873          ExecutorService e = new ForkJoinPool(1);
874 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
875 +        l.add(new StringTask());
876          try {
972            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
973            l.add(new StringTask());
877              e.invokeAll(l, MEDIUM_DELAY_MS, null);
878              shouldThrow();
879          } catch (NullPointerException success) {
# Line 987 | Line 890 | public class ForkJoinPoolTest extends JS
890          try {
891              List<Future<String>> r
892                  = e.invokeAll(new ArrayList<Callable<String>>(),
893 <                              MEDIUM_DELAY_MS, MILLISECONDS);
893 >                              MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
894              assertTrue(r.isEmpty());
895          } finally {
896              joinPool(e);
# Line 999 | Line 902 | public class ForkJoinPoolTest extends JS
902       */
903      public void testTimedInvokeAll3() throws InterruptedException {
904          ExecutorService e = new ForkJoinPool(1);
905 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
906 +        l.add(new StringTask());
907 +        l.add(null);
908          try {
909 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1004 <            l.add(new StringTask());
1005 <            l.add(null);
1006 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
909 >            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
910              shouldThrow();
911          } catch (NullPointerException success) {
912          } finally {
# Line 1016 | Line 919 | public class ForkJoinPoolTest extends JS
919       */
920      public void testTimedInvokeAll4() throws Throwable {
921          ExecutorService e = new ForkJoinPool(1);
922 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
923 +        l.add(new NPETask());
924 +        List<Future<String>> futures
925 +            = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
926 +        assertEquals(1, futures.size());
927          try {
928 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1021 <            l.add(new NPETask());
1022 <            List<Future<String>> result
1023 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1024 <            assertEquals(1, result.size());
1025 <            for (Future<String> future : result)
1026 <                future.get();
928 >            futures.get(0).get();
929              shouldThrow();
930          } catch (ExecutionException success) {
931              assertTrue(success.getCause() instanceof NullPointerException);
# Line 1038 | Line 940 | public class ForkJoinPoolTest extends JS
940      public void testTimedInvokeAll5() throws Throwable {
941          ExecutorService e = new ForkJoinPool(1);
942          try {
943 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
943 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
944              l.add(new StringTask());
945              l.add(new StringTask());
946 <            List<Future<String>> result
947 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
948 <            assertEquals(2, result.size());
949 <            for (Future<String> future : result)
946 >            List<Future<String>> futures
947 >                = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
948 >            assertEquals(2, futures.size());
949 >            for (Future<String> future : futures)
950                  assertSame(TEST_STRING, future.get());
951          } finally {
952              joinPool(e);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines