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.19 by dl, Sun Feb 28 13:35:22 2010 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() {
# 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);
# Line 326 | Line 248 | public class ForkJoinPoolTest extends JS
248      }
249  
250      /**
329     * setUncaughtExceptionHandler of null removes handler
330     */
331    public void testSetUncaughtExceptionHandler2() {
332        ForkJoinPool p = null;
333        try {
334            p = new ForkJoinPool(1);
335            p.setUncaughtExceptionHandler(null);
336            assertNull(p.getUncaughtExceptionHandler());
337        } finally {
338            joinPool(p);
339        }
340    }
341
342
343    /**
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 354 | Line 261 | public class ForkJoinPoolTest extends JS
261                         ForkJoinPool.defaultForkJoinWorkerThreadFactory);
262              Thread.sleep(MEDIUM_DELAY_MS);
263              assertTrue(p.isQuiescent());
357            assertTrue(p.getMaintainsParallelism());
264              assertFalse(p.getAsyncMode());
265              assertTrue(p.getActiveThreadCount() == 0);
266              assertTrue(p.getQueuedTaskCount() == 0);
# Line 411 | 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);
414            assertTrue(p.getPoolSize() >= 4);
320              int r = f.get();
321              assertTrue(r == 832040);
322          } finally {
# Line 505 | Line 410 | public class ForkJoinPoolTest extends JS
410          assertSame(TEST_STRING, result);
411      }
412  
413 +
414      /**
415 <     * A submitted privileged action runs to completion
415 >     * A submitted privileged action to completion
416       */
417      public void testSubmitPrivilegedAction() throws Throwable {
418 <        Runnable r = new CheckedRunnable() {
419 <            public void realRun() throws Exception {
420 <                ExecutorService e = new ForkJoinPool(1);
421 <                Future future = e.submit(Executors.callable(new PrivilegedAction() {
418 >        Policy savedPolicy = null;
419 >        try {
420 >            savedPolicy = Policy.getPolicy();
421 >            AdjustablePolicy policy = new AdjustablePolicy();
422 >            policy.addPermission(new RuntimePermission("getContextClassLoader"));
423 >            policy.addPermission(new RuntimePermission("setContextClassLoader"));
424 >            Policy.setPolicy(policy);
425 >        } catch (AccessControlException ok) {
426 >            return;
427 >        }
428 >        try {
429 >            ExecutorService e = new ForkJoinPool(1);
430 >            Future future = e.submit(Executors.callable(new PrivilegedAction() {
431                      public Object run() {
432                          return TEST_STRING;
433                      }}));
434  
435 <                Object result = future.get();
436 <                assertSame(TEST_STRING, result);
437 <            }};
438 <
439 <        runWithPermissions(r, new RuntimePermission("modifyThread"));
435 >            Object result = future.get();
436 >            assertSame(TEST_STRING, result);
437 >        }
438 >        finally {
439 >            Policy.setPolicy(savedPolicy);
440 >        }
441      }
442  
443      /**
444 <     * A submitted privileged exception action runs to completion
444 >     * A submitted a privileged exception action runs to completion
445       */
446      public void testSubmitPrivilegedExceptionAction() throws Throwable {
447 <        Runnable r = new CheckedRunnable() {
448 <            public void realRun() throws Exception {
449 <                ExecutorService e = new ForkJoinPool(1);
450 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
447 >        Policy savedPolicy = null;
448 >        try {
449 >            savedPolicy = Policy.getPolicy();
450 >            AdjustablePolicy policy = new AdjustablePolicy();
451 >            policy.addPermission(new RuntimePermission("getContextClassLoader"));
452 >            policy.addPermission(new RuntimePermission("setContextClassLoader"));
453 >            Policy.setPolicy(policy);
454 >        } catch (AccessControlException ok) {
455 >            return;
456 >        }
457 >
458 >        try {
459 >            ExecutorService e = new ForkJoinPool(1);
460 >            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
461                      public Object run() {
462                          return TEST_STRING;
463                      }}));
464  
465 <                Object result = future.get();
466 <                assertSame(TEST_STRING, result);
467 <            }};
468 <
469 <        runWithPermissions(r, new RuntimePermission("modifyThread"));
465 >            Object result = future.get();
466 >            assertSame(TEST_STRING, result);
467 >        }
468 >        finally {
469 >            Policy.setPolicy(savedPolicy);
470 >        }
471      }
472  
473      /**
474       * A submitted failed privileged exception action reports exception
475       */
476      public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
477 <        Runnable r = new CheckedRunnable() {
478 <            public void realRun() throws Exception {
479 <                ExecutorService e = new ForkJoinPool(1);
480 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
477 >        Policy savedPolicy = null;
478 >        try {
479 >            savedPolicy = Policy.getPolicy();
480 >            AdjustablePolicy policy = new AdjustablePolicy();
481 >            policy.addPermission(new RuntimePermission("getContextClassLoader"));
482 >            policy.addPermission(new RuntimePermission("setContextClassLoader"));
483 >            Policy.setPolicy(policy);
484 >        } catch (AccessControlException ok) {
485 >            return;
486 >        }
487 >
488 >
489 >        try {
490 >            ExecutorService e = new ForkJoinPool(1);
491 >            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
492                      public Object run() throws Exception {
493                          throw new IndexOutOfBoundsException();
494                      }}));
495  
496 <                try {
497 <                    Object result = future.get();
498 <                    shouldThrow();
499 <                } catch (ExecutionException success) {
500 <                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
501 <                }}};
502 <
565 <        runWithPermissions(r, new RuntimePermission("modifyThread"));
496 >            Object result = future.get();
497 >            shouldThrow();
498 >        } catch (ExecutionException success) {
499 >            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
500 >        } finally {
501 >            Policy.setPolicy(savedPolicy);
502 >        }
503      }
504  
505      /**
# Line 634 | 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 824 | 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 855 | 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 873 | Line 810 | public class ForkJoinPoolTest extends JS
810          l.add(latchAwaitingStringTask(latch));
811          l.add(null);
812          try {
813 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
813 >            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
814              shouldThrow();
815          } catch (NullPointerException success) {
816          } finally {
# Line 890 | Line 827 | public class ForkJoinPoolTest extends JS
827          List<Callable<String>> l = new ArrayList<Callable<String>>();
828          l.add(new NPETask());
829          try {
830 <            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 908 | Line 845 | public class ForkJoinPoolTest extends JS
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 921 | 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 953 | 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 969 | Line 906 | public class ForkJoinPoolTest extends JS
906          l.add(new StringTask());
907          l.add(null);
908          try {
909 <            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 985 | Line 922 | public class ForkJoinPoolTest extends JS
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, MILLISECONDS);
925 >            = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
926          assertEquals(1, futures.size());
927          try {
928              futures.get(0).get();
# Line 1007 | Line 944 | public class ForkJoinPoolTest extends JS
944              l.add(new StringTask());
945              l.add(new StringTask());
946              List<Future<String>> futures
947 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
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());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines