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.7 by dl, Tue Aug 4 00:23:18 2009 UTC vs.
Revision 1.47 by jsr166, Mon Jan 14 22:05:39 2013 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7
7   import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
8 > import java.util.ArrayList;
9 > import java.util.Collection;
10 > import java.util.List;
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.RecursiveTask;
24 > import java.util.concurrent.TimeUnit;
25 > import java.util.concurrent.atomic.AtomicBoolean;
26 > import java.util.concurrent.locks.ReentrantLock;
27   import static java.util.concurrent.TimeUnit.MILLISECONDS;
28 < import java.util.concurrent.locks.*;
29 < import java.security.*;
28 > import java.security.AccessControlException;
29 > import java.security.Policy;
30 > import java.security.PrivilegedAction;
31 > import java.security.PrivilegedExceptionAction;
32  
33   public class ForkJoinPoolTest extends JSR166TestCase {
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run (suite());
35 >        junit.textui.TestRunner.run(suite());
36      }
37 +
38      public static Test suite() {
39          return new TestSuite(ForkJoinPoolTest.class);
40      }
# Line 39 | Line 58 | public class ForkJoinPoolTest extends JS
58      // Some classes to test extension and factory methods
59  
60      static class MyHandler implements Thread.UncaughtExceptionHandler {
61 <        int catches = 0;
61 >        volatile int catches = 0;
62          public void uncaughtException(Thread t, Throwable e) {
63              ++catches;
64          }
# Line 48 | Line 67 | public class ForkJoinPoolTest extends JS
67      // to test handlers
68      static class FailingFJWSubclass extends ForkJoinWorkerThread {
69          public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
70 <        protected void onStart() { throw new Error(); }
70 >        protected void onStart() { super.onStart(); throw new Error(); }
71      }
72  
73      static class FailingThreadFactory
74              implements ForkJoinPool.ForkJoinWorkerThreadFactory {
75 <        int calls = 0;
75 >        volatile int calls = 0;
76          public ForkJoinWorkerThread newThread(ForkJoinPool p) {
77              if (++calls > 1) return null;
78              return new FailingFJWSubclass(p);
# Line 142 | Line 161 | public class ForkJoinPoolTest extends JS
161       * tasks, and quiescent running state.
162       */
163      public void testDefaultInitialState() {
164 <        ForkJoinPool p = null;
164 >        ForkJoinPool p = new ForkJoinPool(1);
165          try {
166 <            p = new ForkJoinPool(1);
167 <            assertTrue(p.getFactory() ==
149 <                       ForkJoinPool.defaultForkJoinWorkerThreadFactory);
150 <            assertTrue(p.isQuiescent());
151 <            assertTrue(p.getMaintainsParallelism());
166 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
167 >                       p.getFactory());
168              assertFalse(p.getAsyncMode());
169 <            assertTrue(p.getActiveThreadCount() == 0);
170 <            assertTrue(p.getStealCount() == 0);
171 <            assertTrue(p.getQueuedTaskCount() == 0);
172 <            assertTrue(p.getQueuedSubmissionCount() == 0);
169 >            assertEquals(0, p.getActiveThreadCount());
170 >            assertEquals(0, p.getStealCount());
171 >            assertEquals(0, p.getQueuedTaskCount());
172 >            assertEquals(0, p.getQueuedSubmissionCount());
173              assertFalse(p.hasQueuedSubmissions());
174              assertFalse(p.isShutdown());
175              assertFalse(p.isTerminating());
# Line 170 | Line 186 | public class ForkJoinPoolTest extends JS
186          try {
187              new ForkJoinPool(-1);
188              shouldThrow();
189 <        }
174 <        catch (IllegalArgumentException success) {}
189 >        } catch (IllegalArgumentException success) {}
190      }
191  
192      /**
# Line 179 | Line 194 | public class ForkJoinPoolTest extends JS
194       */
195      public void testConstructor2() {
196          try {
197 <            new ForkJoinPool(1, null);
197 >            new ForkJoinPool(1, null, null, false);
198              shouldThrow();
199 <        } catch (NullPointerException success) {
185 <        }
199 >        } catch (NullPointerException success) {}
200      }
201  
188
202      /**
203       * getParallelism returns size set in constructor
204       */
205      public void testGetParallelism() {
206 <        ForkJoinPool p = null;
194 <        try {
195 <            p = new ForkJoinPool(1);
196 <            assertTrue(p.getParallelism() == 1);
197 <        } finally {
198 <            joinPool(p);
199 <        }
200 <    }
201 <
202 <    /**
203 <     * setParallelism changes reported parallelism level.
204 <     */
205 <    public void testSetParallelism() {
206 <        ForkJoinPool p = null;
207 <        try {
208 <            p = new ForkJoinPool(1);
209 <            assertTrue(p.getParallelism() == 1);
210 <            p.setParallelism(2);
211 <            assertTrue(p.getParallelism() == 2);
212 <        } finally {
213 <            joinPool(p);
214 <        }
215 <    }
216 <
217 <    /**
218 <     * setParallelism with argument <= 0 throws exception
219 <     */
220 <    public void testSetParallelism2() {
221 <        ForkJoinPool p = null;
206 >        ForkJoinPool p = new ForkJoinPool(1);
207          try {
208 <            p = new ForkJoinPool(1);
224 <            assertTrue(p.getParallelism() == 1);
225 <            p.setParallelism(-2);
226 <            shouldThrow();
227 <        } catch (IllegalArgumentException success) {
208 >            assertEquals(1, p.getParallelism());
209          } finally {
210              joinPool(p);
211          }
# Line 234 | Line 215 | public class ForkJoinPoolTest extends JS
215       * getPoolSize returns number of started workers.
216       */
217      public void testGetPoolSize() {
218 <        ForkJoinPool p = null;
218 >        ForkJoinPool p = new ForkJoinPool(1);
219          try {
220 <            p = new ForkJoinPool(1);
240 <            assertTrue(p.getPoolSize() == 0);
220 >            assertEquals(0, p.getActiveThreadCount());
221              Future<String> future = p.submit(new StringTask());
222 <            assertTrue(p.getPoolSize() == 1);
243 <
244 <        } finally {
245 <            joinPool(p);
246 <        }
247 <    }
248 <
249 <    /**
250 <     * setMaximumPoolSize changes size reported by getMaximumPoolSize.
251 <     */
252 <    public void testSetMaximumPoolSize() {
253 <        ForkJoinPool p = null;
254 <        try {
255 <            p = new ForkJoinPool(1);
256 <            p.setMaximumPoolSize(2);
257 <            assertTrue(p.getMaximumPoolSize() == 2);
258 <        } finally {
259 <            joinPool(p);
260 <        }
261 <    }
262 <
263 <    /**
264 <     * setMaximumPoolSize with argument <= 0 throws exception
265 <     */
266 <    public void testSetMaximumPoolSize2() {
267 <        ForkJoinPool p = null;
268 <        try {
269 <            p = new ForkJoinPool(1);
270 <            p.setMaximumPoolSize(-2);
271 <            shouldThrow();
272 <        } catch (IllegalArgumentException success) {
273 <        } finally {
274 <            joinPool(p);
275 <        }
276 <    }
277 <
278 <    /**
279 <     * setMaintainsParallelism changes policy reported by
280 <     * getMaintainsParallelism.
281 <     */
282 <    public void testSetMaintainsParallelism() {
283 <        ForkJoinPool p = null;
284 <        try {
285 <            p = new ForkJoinPool(1);
286 <            p.setMaintainsParallelism(false);
287 <            assertFalse(p.getMaintainsParallelism());
288 <        } finally {
289 <            joinPool(p);
290 <        }
291 <    }
292 <
293 <    /**
294 <     * setAsyncMode changes policy reported by
295 <     * getAsyncMode.
296 <     */
297 <    public void testSetAsyncMode() {
298 <        ForkJoinPool p = null;
299 <        try {
300 <            p = new ForkJoinPool(1);
301 <            p.setAsyncMode(true);
302 <            assertTrue(p.getAsyncMode());
222 >            assertEquals(1, p.getPoolSize());
223          } finally {
224              joinPool(p);
225          }
# Line 312 | Line 232 | public class ForkJoinPoolTest extends JS
232       * performs its defined action
233       */
234      public void testSetUncaughtExceptionHandler() throws InterruptedException {
235 <        ForkJoinPool p = null;
236 <        try {
237 <            p = new ForkJoinPool(1, new FailingThreadFactory());
238 <            MyHandler eh = new MyHandler();
239 <            p.setUncaughtExceptionHandler(eh);
240 <            assertEquals(eh, p.getUncaughtExceptionHandler());
241 <            p.execute(new FailingTask());
242 <            Thread.sleep(MEDIUM_DELAY_MS);
243 <            assertTrue(eh.catches > 0);
235 >        final CountDownLatch uehInvoked = new CountDownLatch(1);
236 >        final Thread.UncaughtExceptionHandler eh =
237 >            new Thread.UncaughtExceptionHandler() {
238 >                public void uncaughtException(Thread t, Throwable e) {
239 >                    uehInvoked.countDown();
240 >                }};
241 >        ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
242 >                                          eh, false);
243 >        try {
244 >            assertSame(eh, p.getUncaughtExceptionHandler());
245 >            try {
246 >                p.execute(new FibTask(8));
247 >                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
248 >            } catch (RejectedExecutionException ok) {
249 >            }
250          } finally {
251 +            p.shutdownNow(); // failure might have prevented processing task
252              joinPool(p);
253          }
254      }
255  
256      /**
257 <     * setUncaughtExceptionHandler of null removes handler
257 >     * After invoking a single task, isQuiescent eventually becomes
258 >     * true, at which time queues are empty, threads are not active,
259 >     * the task has completed successfully, and construction
260 >     * parameters continue to hold
261       */
262 <    public void testSetUncaughtExceptionHandler2() {
263 <        ForkJoinPool p = null;
262 >    public void testIsQuiescent() throws Exception {
263 >        ForkJoinPool p = new ForkJoinPool(2);
264          try {
265 <            p = new ForkJoinPool(1);
266 <            p.setUncaughtExceptionHandler(null);
267 <            assertNull(p.getUncaughtExceptionHandler());
268 <        } finally {
269 <            joinPool(p);
270 <        }
271 <    }
272 <
265 >            assertTrue(p.isQuiescent());
266 >            long startTime = System.nanoTime();
267 >            FibTask f = new FibTask(20);
268 >            p.invoke(f);
269 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
270 >                       p.getFactory());
271 >            while (! p.isQuiescent()) {
272 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
273 >                    throw new AssertionFailedError("timed out");
274 >                assertFalse(p.getAsyncMode());
275 >                assertFalse(p.isShutdown());
276 >                assertFalse(p.isTerminating());
277 >                assertFalse(p.isTerminated());
278 >                Thread.yield();
279 >            }
280  
344    /**
345     * After invoking a single task, isQuiescent is true,
346     * queues are empty, threads are not active, and
347     * construction parameters continue to hold
348     */
349    public void testisQuiescent() throws InterruptedException {
350        ForkJoinPool p = null;
351        try {
352            p = new ForkJoinPool(2);
353            p.invoke(new FibTask(20));
354            assertTrue(p.getFactory() ==
355                       ForkJoinPool.defaultForkJoinWorkerThreadFactory);
356            Thread.sleep(MEDIUM_DELAY_MS);
281              assertTrue(p.isQuiescent());
358            assertTrue(p.getMaintainsParallelism());
282              assertFalse(p.getAsyncMode());
283 <            assertTrue(p.getActiveThreadCount() == 0);
284 <            assertTrue(p.getQueuedTaskCount() == 0);
285 <            assertTrue(p.getQueuedSubmissionCount() == 0);
283 >            assertEquals(0, p.getActiveThreadCount());
284 >            assertEquals(0, p.getQueuedTaskCount());
285 >            assertEquals(0, p.getQueuedSubmissionCount());
286              assertFalse(p.hasQueuedSubmissions());
287              assertFalse(p.isShutdown());
288              assertFalse(p.isTerminating());
289              assertFalse(p.isTerminated());
290 +            assertTrue(f.isDone());
291 +            assertEquals(6765, (int) f.get());
292          } finally {
293              joinPool(p);
294          }
# Line 373 | Line 298 | public class ForkJoinPoolTest extends JS
298       * Completed submit(ForkJoinTask) returns result
299       */
300      public void testSubmitForkJoinTask() throws Throwable {
301 <        ForkJoinPool p = null;
301 >        ForkJoinPool p = new ForkJoinPool(1);
302          try {
378            p = new ForkJoinPool(1);
303              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
304 <            int r = f.get();
381 <            assertTrue(r == 21);
304 >            assertEquals(21, (int) f.get());
305          } finally {
306              joinPool(p);
307          }
# Line 388 | Line 311 | public class ForkJoinPoolTest extends JS
311       * A task submitted after shutdown is rejected
312       */
313      public void testSubmitAfterShutdown() {
314 <        ForkJoinPool p = null;
314 >        ForkJoinPool p = new ForkJoinPool(1);
315          try {
393            p = new ForkJoinPool(1);
316              p.shutdown();
317              assertTrue(p.isShutdown());
318 <            ForkJoinTask<Integer> f = p.submit(new FibTask(8));
319 <            shouldThrow();
320 <        } catch (RejectedExecutionException success) {
318 >            try {
319 >                ForkJoinTask<Integer> f = p.submit(new FibTask(8));
320 >                shouldThrow();
321 >            } catch (RejectedExecutionException success) {}
322          } finally {
323              joinPool(p);
324          }
# Line 405 | Line 328 | public class ForkJoinPoolTest extends JS
328       * Pool maintains parallelism when using ManagedBlocker
329       */
330      public void testBlockingForkJoinTask() throws Throwable {
331 <        ForkJoinPool p = null;
331 >        ForkJoinPool p = new ForkJoinPool(4);
332          try {
410            p = new ForkJoinPool(4);
333              ReentrantLock lock = new ReentrantLock();
334              ManagedLocker locker = new ManagedLocker(lock);
335 <            ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
335 >            ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
336              p.execute(f);
337 <            assertTrue(p.getPoolSize() >= 4);
416 <            int r = f.get();
417 <            assertTrue(r ==  832040);
337 >            assertEquals(6765, (int) f.get());
338          } finally {
339              p.shutdownNow(); // don't wait out shutdown
340          }
# Line 424 | Line 344 | public class ForkJoinPoolTest extends JS
344       * pollSubmission returns unexecuted submitted task, if present
345       */
346      public void testPollSubmission() {
347 <        SubFJP p = null;
347 >        final CountDownLatch done = new CountDownLatch(1);
348 >        SubFJP p = new SubFJP();
349          try {
350 <            p = new SubFJP();
351 <            ForkJoinTask a = p.submit(new MediumRunnable());
352 <            ForkJoinTask b = p.submit(new MediumRunnable());
432 <            ForkJoinTask c = p.submit(new MediumRunnable());
350 >            ForkJoinTask a = p.submit(awaiter(done));
351 >            ForkJoinTask b = p.submit(awaiter(done));
352 >            ForkJoinTask c = p.submit(awaiter(done));
353              ForkJoinTask r = p.pollSubmission();
354              assertTrue(r == a || r == b || r == c);
355              assertFalse(r.isDone());
356          } finally {
357 +            done.countDown();
358              joinPool(p);
359          }
360      }
# Line 442 | Line 363 | public class ForkJoinPoolTest extends JS
363       * drainTasksTo transfers unexecuted submitted tasks, if present
364       */
365      public void testDrainTasksTo() {
366 <        SubFJP p = null;
366 >        final CountDownLatch done = new CountDownLatch(1);
367 >        SubFJP p = new SubFJP();
368          try {
369 <            p = new SubFJP();
370 <            ForkJoinTask a = p.submit(new MediumRunnable());
371 <            ForkJoinTask b = p.submit(new MediumRunnable());
450 <            ForkJoinTask c = p.submit(new MediumRunnable());
369 >            ForkJoinTask a = p.submit(awaiter(done));
370 >            ForkJoinTask b = p.submit(awaiter(done));
371 >            ForkJoinTask c = p.submit(awaiter(done));
372              ArrayList<ForkJoinTask> al = new ArrayList();
373              p.drainTasksTo(al);
374              assertTrue(al.size() > 0);
# Line 456 | Line 377 | public class ForkJoinPoolTest extends JS
377                  assertFalse(r.isDone());
378              }
379          } finally {
380 +            done.countDown();
381              joinPool(p);
382          }
383      }
384  
463
385      // FJ Versions of AbstractExecutorService tests
386  
387      /**
# Line 468 | Line 389 | public class ForkJoinPoolTest extends JS
389       */
390      public void testExecuteRunnable() throws Throwable {
391          ExecutorService e = new ForkJoinPool(1);
392 <        TrackedShortRunnable task = new TrackedShortRunnable();
393 <        assertFalse(task.done);
394 <        Future<?> future = e.submit(task);
395 <        future.get();
396 <        assertTrue(task.done);
392 >        try {
393 >            final AtomicBoolean done = new AtomicBoolean(false);
394 >            CheckedRunnable task = new CheckedRunnable() {
395 >                public void realRun() {
396 >                    done.set(true);
397 >                }};
398 >            Future<?> future = e.submit(task);
399 >            assertNull(future.get());
400 >            assertNull(future.get(0, MILLISECONDS));
401 >            assertTrue(done.get());
402 >            assertTrue(future.isDone());
403 >            assertFalse(future.isCancelled());
404 >        } finally {
405 >            joinPool(e);
406 >        }
407      }
408  
478
409      /**
410       * Completed submit(callable) returns result
411       */
412      public void testSubmitCallable() throws Throwable {
413          ExecutorService e = new ForkJoinPool(1);
414 <        Future<String> future = e.submit(new StringTask());
415 <        String result = future.get();
416 <        assertSame(TEST_STRING, result);
414 >        try {
415 >            Future<String> future = e.submit(new StringTask());
416 >            assertSame(TEST_STRING, future.get());
417 >            assertTrue(future.isDone());
418 >            assertFalse(future.isCancelled());
419 >        } finally {
420 >            joinPool(e);
421 >        }
422      }
423  
424      /**
# Line 491 | Line 426 | public class ForkJoinPoolTest extends JS
426       */
427      public void testSubmitRunnable() throws Throwable {
428          ExecutorService e = new ForkJoinPool(1);
429 <        Future<?> future = e.submit(new NoOpRunnable());
430 <        future.get();
431 <        assertTrue(future.isDone());
429 >        try {
430 >            Future<?> future = e.submit(new NoOpRunnable());
431 >            assertNull(future.get());
432 >            assertTrue(future.isDone());
433 >            assertFalse(future.isCancelled());
434 >        } finally {
435 >            joinPool(e);
436 >        }
437      }
438  
439      /**
# Line 501 | Line 441 | public class ForkJoinPoolTest extends JS
441       */
442      public void testSubmitRunnable2() throws Throwable {
443          ExecutorService e = new ForkJoinPool(1);
444 <        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
445 <        String result = future.get();
446 <        assertSame(TEST_STRING, result);
444 >        try {
445 >            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
446 >            assertSame(TEST_STRING, future.get());
447 >            assertTrue(future.isDone());
448 >            assertFalse(future.isCancelled());
449 >        } finally {
450 >            joinPool(e);
451 >        }
452      }
453  
509
454      /**
455 <     * A submitted privileged action to completion
455 >     * A submitted privileged action runs to completion
456       */
457 <    public void testSubmitPrivilegedAction() throws Throwable {
458 <        Policy savedPolicy = null;
459 <        try {
460 <            savedPolicy = Policy.getPolicy();
461 <            AdjustablePolicy policy = new AdjustablePolicy();
518 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
519 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
520 <            Policy.setPolicy(policy);
521 <        } catch (AccessControlException ok) {
522 <            return;
523 <        }
524 <        try {
525 <            ExecutorService e = new ForkJoinPool(1);
526 <            Future future = e.submit(Executors.callable(new PrivilegedAction() {
457 >    public void testSubmitPrivilegedAction() throws Exception {
458 >        Runnable r = new CheckedRunnable() {
459 >            public void realRun() throws Exception {
460 >                ExecutorService e = new ForkJoinPool(1);
461 >                Future future = e.submit(Executors.callable(new PrivilegedAction() {
462                      public Object run() {
463                          return TEST_STRING;
464                      }}));
465  
466 <            Object result = future.get();
467 <            assertSame(TEST_STRING, result);
468 <        }
469 <        finally {
470 <            Policy.setPolicy(savedPolicy);
536 <        }
466 >                assertSame(TEST_STRING, future.get());
467 >            }};
468 >
469 >        runWithPermissions(r,
470 >                           new RuntimePermission("modifyThread"));
471      }
472  
473      /**
474 <     * A submitted a privileged exception action runs to completion
474 >     * A submitted privileged exception action runs to completion
475       */
476 <    public void testSubmitPrivilegedExceptionAction() throws Throwable {
477 <        Policy savedPolicy = null;
478 <        try {
479 <            savedPolicy = Policy.getPolicy();
480 <            AdjustablePolicy policy = new AdjustablePolicy();
547 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
548 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
549 <            Policy.setPolicy(policy);
550 <        } catch (AccessControlException ok) {
551 <            return;
552 <        }
553 <
554 <        try {
555 <            ExecutorService e = new ForkJoinPool(1);
556 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
476 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
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() {
481                      public Object run() {
482                          return TEST_STRING;
483                      }}));
484  
485 <            Object result = future.get();
486 <            assertSame(TEST_STRING, result);
487 <        }
488 <        finally {
565 <            Policy.setPolicy(savedPolicy);
566 <        }
485 >                assertSame(TEST_STRING, future.get());
486 >            }};
487 >
488 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
489      }
490  
491      /**
492       * A submitted failed privileged exception action reports exception
493       */
494 <    public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
495 <        Policy savedPolicy = null;
496 <        try {
497 <            savedPolicy = Policy.getPolicy();
498 <            AdjustablePolicy policy = new AdjustablePolicy();
577 <            policy.addPermission(new RuntimePermission("getContextClassLoader"));
578 <            policy.addPermission(new RuntimePermission("setContextClassLoader"));
579 <            Policy.setPolicy(policy);
580 <        } catch (AccessControlException ok) {
581 <            return;
582 <        }
583 <
584 <
585 <        try {
586 <            ExecutorService e = new ForkJoinPool(1);
587 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
494 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
495 >        Runnable r = new CheckedRunnable() {
496 >            public void realRun() throws Exception {
497 >                ExecutorService e = new ForkJoinPool(1);
498 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
499                      public Object run() throws Exception {
500                          throw new IndexOutOfBoundsException();
501                      }}));
502  
503 <            Object result = future.get();
504 <            shouldThrow();
505 <        } catch (ExecutionException success) {
506 <        } finally {
507 <            Policy.setPolicy(savedPolicy);
508 <        }
503 >                try {
504 >                    future.get();
505 >                    shouldThrow();
506 >                } catch (ExecutionException success) {
507 >                    assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
508 >                }}};
509 >
510 >        runWithPermissions(r, new RuntimePermission("modifyThread"));
511      }
512  
513      /**
514       * execute(null runnable) throws NullPointerException
515       */
516      public void testExecuteNullRunnable() {
517 +        ExecutorService e = new ForkJoinPool(1);
518          try {
519 <            ExecutorService e = new ForkJoinPool(1);
606 <            TrackedShortRunnable task = null;
607 <            Future<?> future = e.submit(task);
519 >            Future<?> future = e.submit((Runnable) null);
520              shouldThrow();
521          } catch (NullPointerException success) {
522 +        } finally {
523 +            joinPool(e);
524          }
525      }
526  
613
527      /**
528       * submit(null callable) throws NullPointerException
529       */
530      public void testSubmitNullCallable() {
531 +        ExecutorService e = new ForkJoinPool(1);
532          try {
533 <            ExecutorService e = new ForkJoinPool(1);
620 <            StringTask t = null;
621 <            Future<String> future = e.submit(t);
533 >            Future<String> future = e.submit((Callable) null);
534              shouldThrow();
535          } catch (NullPointerException success) {
536 +        } finally {
537 +            joinPool(e);
538          }
539      }
540  
627
541      /**
542 <     * Blocking on submit(callable) throws InterruptedException if
630 <     * caller interrupted.
542 >     * submit(callable).get() throws InterruptedException if interrupted
543       */
544      public void testInterruptedSubmit() throws InterruptedException {
545 <        final ForkJoinPool p = new ForkJoinPool(1);
546 <
547 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
548 <            void realRun() throws Throwable {
549 <                p.submit(new CheckedCallable<Object>() {
550 <                    public Object realCall() throws Throwable {
551 <                        Thread.sleep(MEDIUM_DELAY_MS);
552 <                        return null;
553 <                    }}).get();
554 <            }});
555 <
556 <        t.start();
557 <        t.interrupt();
558 <        p.shutdownNow();
545 >        final CountDownLatch submitted    = new CountDownLatch(1);
546 >        final CountDownLatch quittingTime = new CountDownLatch(1);
547 >        final ExecutorService p = new ForkJoinPool(1);
548 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
549 >            public Void realCall() throws InterruptedException {
550 >                assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
551 >                return null;
552 >            }};
553 >        try {
554 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
555 >                public void realRun() throws Exception {
556 >                    Future<Void> future = p.submit(awaiter);
557 >                    submitted.countDown();
558 >                    future.get();
559 >                }});
560 >            t.start();
561 >            assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
562 >            t.interrupt();
563 >            t.join();
564 >        } finally {
565 >            quittingTime.countDown();
566 >            joinPool(p);
567 >        }
568      }
569  
570      /**
# Line 652 | Line 573 | public class ForkJoinPoolTest extends JS
573       */
574      public void testSubmitEE() throws Throwable {
575          ForkJoinPool p = new ForkJoinPool(1);
655
576          try {
577 <            Callable c = new Callable() {
578 <                    public Object call() {
579 <                        int i = 5/0;
660 <                        return Boolean.TRUE;
661 <                    }
662 <                };
663 <
664 <            for (int i = 0; i < 5; i++) {
665 <                p.submit(c).get();
666 <            }
577 >            p.submit(new Callable() {
578 >                public Object call() { throw new ArithmeticException(); }})
579 >                .get();
580              shouldThrow();
581          } catch (ExecutionException success) {
582 +            assertTrue(success.getCause() instanceof ArithmeticException);
583 +        } finally {
584 +            joinPool(p);
585          }
670        joinPool(p);
586      }
587  
588      /**
# Line 699 | Line 614 | public class ForkJoinPoolTest extends JS
614      }
615  
616      /**
617 <     * invokeAny(c) throws NullPointerException if c has null elements
617 >     * invokeAny(c) throws NullPointerException if c has a single null element
618       */
619      public void testInvokeAny3() throws Throwable {
620          ExecutorService e = new ForkJoinPool(1);
621 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
622 +        l.add(null);
623          try {
707            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
708            l.add(new StringTask());
709            l.add(null);
624              e.invokeAny(l);
625              shouldThrow();
626          } catch (NullPointerException success) {
# Line 716 | Line 630 | public class ForkJoinPoolTest extends JS
630      }
631  
632      /**
633 <     * invokeAny(c) throws ExecutionException if no task in c completes
633 >     * invokeAny(c) throws NullPointerException if c has null elements
634       */
635      public void testInvokeAny4() throws Throwable {
636 +        CountDownLatch latch = new CountDownLatch(1);
637 +        ExecutorService e = new ForkJoinPool(1);
638 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
639 +        l.add(latchAwaitingStringTask(latch));
640 +        l.add(null);
641 +        try {
642 +            e.invokeAny(l);
643 +            shouldThrow();
644 +        } catch (NullPointerException success) {
645 +        } finally {
646 +            latch.countDown();
647 +            joinPool(e);
648 +        }
649 +    }
650 +
651 +    /**
652 +     * invokeAny(c) throws ExecutionException if no task in c completes
653 +     */
654 +    public void testInvokeAny5() throws Throwable {
655          ExecutorService e = new ForkJoinPool(1);
656 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
657 +        l.add(new NPETask());
658          try {
724            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
725            l.add(new NPETask());
659              e.invokeAny(l);
660              shouldThrow();
661          } catch (ExecutionException success) {
662 +            assertTrue(success.getCause() instanceof NullPointerException);
663          } finally {
664              joinPool(e);
665          }
# Line 734 | Line 668 | public class ForkJoinPoolTest extends JS
668      /**
669       * invokeAny(c) returns result of some task in c if at least one completes
670       */
671 <    public void testInvokeAny5() throws Throwable {
671 >    public void testInvokeAny6() throws Throwable {
672          ExecutorService e = new ForkJoinPool(1);
673          try {
674 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
674 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
675              l.add(new StringTask());
676              l.add(new StringTask());
677              String result = e.invokeAny(l);
# Line 780 | Line 714 | public class ForkJoinPoolTest extends JS
714       */
715      public void testInvokeAll3() throws InterruptedException {
716          ExecutorService e = new ForkJoinPool(1);
717 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
718 +        l.add(new StringTask());
719 +        l.add(null);
720          try {
784            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
785            l.add(new StringTask());
786            l.add(null);
721              e.invokeAll(l);
722              shouldThrow();
723          } catch (NullPointerException success) {
# Line 798 | Line 732 | public class ForkJoinPoolTest extends JS
732       */
733      public void testInvokeAll4() throws Throwable {
734          ExecutorService e = new ForkJoinPool(1);
735 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
736 +        l.add(new NPETask());
737 +        List<Future<String>> futures = e.invokeAll(l);
738 +        assertEquals(1, futures.size());
739          try {
740 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
803 <            l.add(new NPETask());
804 <            List<Future<String>> result = e.invokeAll(l);
805 <            assertEquals(1, result.size());
806 <            for (Future<String> future : result)
807 <                future.get();
740 >            futures.get(0).get();
741              shouldThrow();
742          } catch (ExecutionException success) {
743 +            assertTrue(success.getCause() instanceof NullPointerException);
744          } finally {
745              joinPool(e);
746          }
# Line 818 | Line 752 | public class ForkJoinPoolTest extends JS
752      public void testInvokeAll5() throws Throwable {
753          ExecutorService e = new ForkJoinPool(1);
754          try {
755 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
755 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
756              l.add(new StringTask());
757              l.add(new StringTask());
758 <            List<Future<String>> result = e.invokeAll(l);
759 <            assertEquals(2, result.size());
760 <            for (Future<String> future : result)
758 >            List<Future<String>> futures = e.invokeAll(l);
759 >            assertEquals(2, futures.size());
760 >            for (Future<String> future : futures)
761                  assertSame(TEST_STRING, future.get());
762          } finally {
763              joinPool(e);
764          }
765      }
766  
833
767      /**
768       * timed invokeAny(null) throws NullPointerException
769       */
# Line 850 | Line 783 | public class ForkJoinPoolTest extends JS
783       */
784      public void testTimedInvokeAnyNullTimeUnit() throws Throwable {
785          ExecutorService e = new ForkJoinPool(1);
786 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
787 +        l.add(new StringTask());
788          try {
854            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
855            l.add(new StringTask());
789              e.invokeAny(l, MEDIUM_DELAY_MS, null);
790              shouldThrow();
791          } catch (NullPointerException success) {
# Line 880 | Line 813 | public class ForkJoinPoolTest extends JS
813       * timed invokeAny(c) throws NullPointerException if c has null elements
814       */
815      public void testTimedInvokeAny3() throws Throwable {
816 +        CountDownLatch latch = new CountDownLatch(1);
817          ExecutorService e = new ForkJoinPool(1);
818 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
819 +        l.add(latchAwaitingStringTask(latch));
820 +        l.add(null);
821          try {
885            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
886            l.add(new StringTask());
887            l.add(null);
822              e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
823              shouldThrow();
824          } catch (NullPointerException success) {
825          } finally {
826 +            latch.countDown();
827              joinPool(e);
828          }
829      }
# Line 898 | 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 {
902            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
903            l.add(new NPETask());
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);
845          }
# Line 915 | Line 851 | public class ForkJoinPoolTest extends JS
851      public void testTimedInvokeAny5() throws Throwable {
852          ExecutorService e = new ForkJoinPool(1);
853          try {
854 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
854 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
855              l.add(new StringTask());
856              l.add(new StringTask());
857              String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
# Line 944 | Line 880 | public class ForkJoinPoolTest extends JS
880       */
881      public void testTimedInvokeAllNullTimeUnit() throws Throwable {
882          ExecutorService e = new ForkJoinPool(1);
883 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
884 +        l.add(new StringTask());
885          try {
948            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
949            l.add(new StringTask());
886              e.invokeAll(l, MEDIUM_DELAY_MS, null);
887              shouldThrow();
888          } catch (NullPointerException success) {
# Line 975 | 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 {
979            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
980            l.add(new StringTask());
981            l.add(null);
918              e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
919              shouldThrow();
920          } catch (NullPointerException success) {
# Line 992 | Line 928 | public class ForkJoinPoolTest extends JS
928       */
929      public void testTimedInvokeAll4() throws Throwable {
930          ExecutorService e = new ForkJoinPool(1);
931 +        List<Callable<String>> l = new ArrayList<Callable<String>>();
932 +        l.add(new NPETask());
933 +        List<Future<String>> futures
934 +            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
935 +        assertEquals(1, futures.size());
936          try {
937 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
997 <            l.add(new NPETask());
998 <            List<Future<String>> result
999 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1000 <            assertEquals(1, result.size());
1001 <            for (Future<String> future : result)
1002 <                future.get();
937 >            futures.get(0).get();
938              shouldThrow();
939          } catch (ExecutionException success) {
940 +            assertTrue(success.getCause() instanceof NullPointerException);
941          } finally {
942              joinPool(e);
943          }
# Line 1013 | Line 949 | public class ForkJoinPoolTest extends JS
949      public void testTimedInvokeAll5() throws Throwable {
950          ExecutorService e = new ForkJoinPool(1);
951          try {
952 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
952 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
953              l.add(new StringTask());
954              l.add(new StringTask());
955 <            List<Future<String>> result
955 >            List<Future<String>> futures
956                  = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
957 <            assertEquals(2, result.size());
958 <            for (Future<String> future : result)
957 >            assertEquals(2, futures.size());
958 >            for (Future<String> future : futures)
959                  assertSame(TEST_STRING, future.get());
960          } finally {
961              joinPool(e);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines