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.56 by jsr166, Fri Oct 2 21:52:36 2015 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
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
7   import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 < import java.util.concurrent.locks.*;
9 < import java.security.*;
8 > import static java.util.concurrent.TimeUnit.NANOSECONDS;
9 >
10 > import java.security.PrivilegedAction;
11 > import java.security.PrivilegedExceptionAction;
12 > import java.util.ArrayList;
13 > import java.util.Collection;
14 > import java.util.List;
15 > import java.util.concurrent.Callable;
16 > import java.util.concurrent.CountDownLatch;
17 > import java.util.concurrent.ExecutionException;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.ForkJoinPool;
21 > import java.util.concurrent.ForkJoinTask;
22 > import java.util.concurrent.ForkJoinWorkerThread;
23 > 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.locks.ReentrantLock;
28 >
29 > import junit.framework.AssertionFailedError;
30 > import junit.framework.Test;
31 > import junit.framework.TestSuite;
32  
33   public class ForkJoinPoolTest extends JSR166TestCase {
34      public static void main(String[] args) {
35 <        junit.textui.TestRunner.run (suite());
35 >        main(suite(), args);
36      }
37 +
38      public static Test suite() {
39          return new TestSuite(ForkJoinPoolTest.class);
40      }
41  
42 <    /**
42 >    /*
43       * Testing coverage notes:
44       *
45       * 1. shutdown and related methods are tested via super.joinPool.
# 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 88 | Line 107 | public class ForkJoinPoolTest extends JS
107      static final class FibTask extends RecursiveTask<Integer> {
108          final int number;
109          FibTask(int n) { number = n; }
110 <        public Integer compute() {
110 >        protected Integer compute() {
111              int n = number;
112              if (n <= 1)
113                  return n;
# Line 116 | Line 135 | public class ForkJoinPoolTest extends JS
135              this.locker = locker;
136              this.lock = lock;
137          }
138 <        public Integer compute() {
138 >        protected Integer compute() {
139              int n;
140              LockingFibTask f1 = null;
141              LockingFibTask f2 = null;
# 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 178 | 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) {}
200      }
201  
186
202      /**
203       * getParallelism returns size set in constructor
204       */
205      public void testGetParallelism() {
206 <        ForkJoinPool p = null;
192 <        try {
193 <            p = new ForkJoinPool(1);
194 <            assertTrue(p.getParallelism() == 1);
195 <        } finally {
196 <            joinPool(p);
197 <        }
198 <    }
199 <
200 <    /**
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;
206 >        ForkJoinPool p = new ForkJoinPool(1);
207          try {
208 <            p = new ForkJoinPool(1);
222 <            assertTrue(p.getParallelism() == 1);
223 <            p.setParallelism(-2);
224 <            shouldThrow();
225 <        } catch (IllegalArgumentException success) {
208 >            assertEquals(1, p.getParallelism());
209          } finally {
210              joinPool(p);
211          }
# Line 232 | 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);
238 <            assertTrue(p.getActiveThreadCount() == 0);
220 >            assertEquals(0, p.getActiveThreadCount());
221              Future<String> future = p.submit(new StringTask());
222 <            assertTrue(p.getPoolSize() == 1);
241 <
242 <        } finally {
243 <            joinPool(p);
244 <        }
245 <    }
246 <
247 <    /**
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);
222 >            assertEquals(1, p.getPoolSize());
223          } finally {
224              joinPool(p);
225          }
226      }
227  
228      /**
229 <     * setMaximumPoolSize with argument <= 0 throws exception
229 >     * awaitTermination on a non-shutdown pool times out
230       */
231 <    public void testSetMaximumPoolSize2() {
232 <        ForkJoinPool p = null;
233 <        try {
234 <            p = new ForkJoinPool(1);
235 <            p.setMaximumPoolSize(-2);
236 <            shouldThrow();
237 <        } catch (IllegalArgumentException success) {
238 <        } finally {
239 <            joinPool(p);
240 <        }
241 <    }
242 <
243 <    /**
244 <     * setMaintainsParallelism changes policy reported by
245 <     * getMaintainsParallelism.
246 <     */
247 <    public void testSetMaintainsParallelism() {
248 <        ForkJoinPool p = null;
249 <        try {
250 <            p = new ForkJoinPool(1);
251 <            p.setMaintainsParallelism(false);
252 <            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 <        }
231 >    public void testAwaitTermination_timesOut() throws InterruptedException {
232 >        ForkJoinPool p = new ForkJoinPool(1);
233 >        assertFalse(p.isTerminated());
234 >        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
235 >        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
236 >        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
237 >        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
238 >        assertFalse(p.awaitTermination(0L, NANOSECONDS));
239 >        assertFalse(p.awaitTermination(0L, MILLISECONDS));
240 >        long timeoutNanos = 999999L;
241 >        long startTime = System.nanoTime();
242 >        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
243 >        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
244 >        assertFalse(p.isTerminated());
245 >        startTime = System.nanoTime();
246 >        long timeoutMillis = timeoutMillis();
247 >        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
248 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
249 >        assertFalse(p.isTerminated());
250 >        p.shutdown();
251 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
252 >        assertTrue(p.isTerminated());
253      }
254  
255      /**
# Line 310 | Line 259 | public class ForkJoinPoolTest extends JS
259       * performs its defined action
260       */
261      public void testSetUncaughtExceptionHandler() throws InterruptedException {
262 <        ForkJoinPool p = null;
263 <        try {
264 <            p = new ForkJoinPool(1, new FailingThreadFactory());
265 <            MyHandler eh = new MyHandler();
266 <            p.setUncaughtExceptionHandler(eh);
267 <            assertEquals(eh, p.getUncaughtExceptionHandler());
268 <            p.execute(new FailingTask());
269 <            Thread.sleep(MEDIUM_DELAY_MS);
270 <            assertTrue(eh.catches > 0);
262 >        final CountDownLatch uehInvoked = new CountDownLatch(1);
263 >        final Thread.UncaughtExceptionHandler eh =
264 >            new Thread.UncaughtExceptionHandler() {
265 >                public void uncaughtException(Thread t, Throwable e) {
266 >                    uehInvoked.countDown();
267 >                }};
268 >        ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
269 >                                          eh, false);
270 >        try {
271 >            assertSame(eh, p.getUncaughtExceptionHandler());
272 >            try {
273 >                p.execute(new FibTask(8));
274 >                assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
275 >            } catch (RejectedExecutionException ok) {
276 >            }
277          } finally {
278 <            p.shutdownNow();
278 >            p.shutdownNow(); // failure might have prevented processing task
279              joinPool(p);
280          }
281      }
282  
283      /**
284 <     * setUncaughtExceptionHandler of null removes handler
284 >     * After invoking a single task, isQuiescent eventually becomes
285 >     * true, at which time queues are empty, threads are not active,
286 >     * the task has completed successfully, and construction
287 >     * parameters continue to hold
288       */
289 <    public void testSetUncaughtExceptionHandler2() {
290 <        ForkJoinPool p = null;
289 >    public void testIsQuiescent() throws Exception {
290 >        ForkJoinPool p = new ForkJoinPool(2);
291          try {
292 <            p = new ForkJoinPool(1);
293 <            p.setUncaughtExceptionHandler(null);
294 <            assertNull(p.getUncaughtExceptionHandler());
295 <        } finally {
296 <            joinPool(p);
297 <        }
298 <    }
299 <
292 >            assertTrue(p.isQuiescent());
293 >            long startTime = System.nanoTime();
294 >            FibTask f = new FibTask(20);
295 >            p.invoke(f);
296 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
297 >                       p.getFactory());
298 >            while (! p.isQuiescent()) {
299 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
300 >                    throw new AssertionFailedError("timed out");
301 >                assertFalse(p.getAsyncMode());
302 >                assertFalse(p.isShutdown());
303 >                assertFalse(p.isTerminating());
304 >                assertFalse(p.isTerminated());
305 >                Thread.yield();
306 >            }
307  
343    /**
344     * After invoking a single task, isQuiescent is true,
345     * queues are empty, threads are not active, and
346     * construction parameters continue to hold
347     */
348    public void testisQuiescent() throws InterruptedException {
349        ForkJoinPool p = null;
350        try {
351            p = new ForkJoinPool(2);
352            p.invoke(new FibTask(20));
353            assertTrue(p.getFactory() ==
354                       ForkJoinPool.defaultForkJoinWorkerThreadFactory);
355            Thread.sleep(MEDIUM_DELAY_MS);
308              assertTrue(p.isQuiescent());
357            assertTrue(p.getMaintainsParallelism());
309              assertFalse(p.getAsyncMode());
310 <            assertTrue(p.getActiveThreadCount() == 0);
311 <            assertTrue(p.getQueuedTaskCount() == 0);
312 <            assertTrue(p.getQueuedSubmissionCount() == 0);
310 >            assertEquals(0, p.getActiveThreadCount());
311 >            assertEquals(0, p.getQueuedTaskCount());
312 >            assertEquals(0, p.getQueuedSubmissionCount());
313              assertFalse(p.hasQueuedSubmissions());
314              assertFalse(p.isShutdown());
315              assertFalse(p.isTerminating());
316              assertFalse(p.isTerminated());
317 +            assertTrue(f.isDone());
318 +            assertEquals(6765, (int) f.get());
319          } finally {
320              joinPool(p);
321          }
# Line 372 | Line 325 | public class ForkJoinPoolTest extends JS
325       * Completed submit(ForkJoinTask) returns result
326       */
327      public void testSubmitForkJoinTask() throws Throwable {
328 <        ForkJoinPool p = null;
328 >        ForkJoinPool p = new ForkJoinPool(1);
329          try {
377            p = new ForkJoinPool(1);
330              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
331 <            int r = f.get();
380 <            assertTrue(r == 21);
331 >            assertEquals(21, (int) f.get());
332          } finally {
333              joinPool(p);
334          }
# Line 387 | Line 338 | public class ForkJoinPoolTest extends JS
338       * A task submitted after shutdown is rejected
339       */
340      public void testSubmitAfterShutdown() {
341 <        ForkJoinPool p = null;
341 >        ForkJoinPool p = new ForkJoinPool(1);
342          try {
392            p = new ForkJoinPool(1);
343              p.shutdown();
344              assertTrue(p.isShutdown());
345 <            ForkJoinTask<Integer> f = p.submit(new FibTask(8));
346 <            shouldThrow();
347 <        } catch (RejectedExecutionException success) {
345 >            try {
346 >                ForkJoinTask<Integer> f = p.submit(new FibTask(8));
347 >                shouldThrow();
348 >            } catch (RejectedExecutionException success) {}
349          } finally {
350              joinPool(p);
351          }
# Line 404 | Line 355 | public class ForkJoinPoolTest extends JS
355       * Pool maintains parallelism when using ManagedBlocker
356       */
357      public void testBlockingForkJoinTask() throws Throwable {
358 <        ForkJoinPool p = null;
358 >        ForkJoinPool p = new ForkJoinPool(4);
359          try {
409            p = new ForkJoinPool(4);
360              ReentrantLock lock = new ReentrantLock();
361              ManagedLocker locker = new ManagedLocker(lock);
362 <            ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
362 >            ForkJoinTask<Integer> f = new LockingFibTask(20, locker, lock);
363              p.execute(f);
364 <            assertTrue(p.getPoolSize() >= 4);
415 <            int r = f.get();
416 <            assertTrue(r == 832040);
364 >            assertEquals(6765, (int) f.get());
365          } finally {
366              p.shutdownNow(); // don't wait out shutdown
367          }
# Line 423 | Line 371 | public class ForkJoinPoolTest extends JS
371       * pollSubmission returns unexecuted submitted task, if present
372       */
373      public void testPollSubmission() {
374 <        SubFJP p = null;
374 >        final CountDownLatch done = new CountDownLatch(1);
375 >        SubFJP p = new SubFJP();
376          try {
377 <            p = new SubFJP();
378 <            ForkJoinTask a = p.submit(new MediumRunnable());
379 <            ForkJoinTask b = p.submit(new MediumRunnable());
431 <            ForkJoinTask c = p.submit(new MediumRunnable());
377 >            ForkJoinTask a = p.submit(awaiter(done));
378 >            ForkJoinTask b = p.submit(awaiter(done));
379 >            ForkJoinTask c = p.submit(awaiter(done));
380              ForkJoinTask r = p.pollSubmission();
381              assertTrue(r == a || r == b || r == c);
382              assertFalse(r.isDone());
383          } finally {
384 +            done.countDown();
385              joinPool(p);
386          }
387      }
# Line 441 | Line 390 | public class ForkJoinPoolTest extends JS
390       * drainTasksTo transfers unexecuted submitted tasks, if present
391       */
392      public void testDrainTasksTo() {
393 <        SubFJP p = null;
393 >        final CountDownLatch done = new CountDownLatch(1);
394 >        SubFJP p = new SubFJP();
395          try {
396 <            p = new SubFJP();
397 <            ForkJoinTask a = p.submit(new MediumRunnable());
398 <            ForkJoinTask b = p.submit(new MediumRunnable());
449 <            ForkJoinTask c = p.submit(new MediumRunnable());
396 >            ForkJoinTask a = p.submit(awaiter(done));
397 >            ForkJoinTask b = p.submit(awaiter(done));
398 >            ForkJoinTask c = p.submit(awaiter(done));
399              ArrayList<ForkJoinTask> al = new ArrayList();
400              p.drainTasksTo(al);
401              assertTrue(al.size() > 0);
# Line 455 | Line 404 | public class ForkJoinPoolTest extends JS
404                  assertFalse(r.isDone());
405              }
406          } finally {
407 +            done.countDown();
408              joinPool(p);
409          }
410      }
411  
462
412      // FJ Versions of AbstractExecutorService tests
413  
414      /**
# Line 467 | Line 416 | public class ForkJoinPoolTest extends JS
416       */
417      public void testExecuteRunnable() throws Throwable {
418          ExecutorService e = new ForkJoinPool(1);
419 <        TrackedShortRunnable task = new TrackedShortRunnable();
420 <        assertFalse(task.done);
421 <        Future<?> future = e.submit(task);
422 <        future.get();
423 <        assertTrue(task.done);
419 >        try {
420 >            final AtomicBoolean done = new AtomicBoolean(false);
421 >            Future<?> future = e.submit(new CheckedRunnable() {
422 >                public void realRun() {
423 >                    done.set(true);
424 >                }});
425 >            assertNull(future.get());
426 >            assertNull(future.get(0, MILLISECONDS));
427 >            assertTrue(done.get());
428 >            assertTrue(future.isDone());
429 >            assertFalse(future.isCancelled());
430 >        } finally {
431 >            joinPool(e);
432 >        }
433      }
434  
477
435      /**
436       * Completed submit(callable) returns result
437       */
438      public void testSubmitCallable() throws Throwable {
439          ExecutorService e = new ForkJoinPool(1);
440 <        Future<String> future = e.submit(new StringTask());
441 <        String result = future.get();
442 <        assertSame(TEST_STRING, result);
440 >        try {
441 >            Future<String> future = e.submit(new StringTask());
442 >            assertSame(TEST_STRING, future.get());
443 >            assertTrue(future.isDone());
444 >            assertFalse(future.isCancelled());
445 >        } finally {
446 >            joinPool(e);
447 >        }
448      }
449  
450      /**
# Line 490 | Line 452 | public class ForkJoinPoolTest extends JS
452       */
453      public void testSubmitRunnable() throws Throwable {
454          ExecutorService e = new ForkJoinPool(1);
455 <        Future<?> future = e.submit(new NoOpRunnable());
456 <        future.get();
457 <        assertTrue(future.isDone());
455 >        try {
456 >            Future<?> future = e.submit(new NoOpRunnable());
457 >            assertNull(future.get());
458 >            assertTrue(future.isDone());
459 >            assertFalse(future.isCancelled());
460 >        } finally {
461 >            joinPool(e);
462 >        }
463      }
464  
465      /**
# Line 500 | Line 467 | public class ForkJoinPoolTest extends JS
467       */
468      public void testSubmitRunnable2() throws Throwable {
469          ExecutorService e = new ForkJoinPool(1);
470 <        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
471 <        String result = future.get();
472 <        assertSame(TEST_STRING, result);
470 >        try {
471 >            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
472 >            assertSame(TEST_STRING, future.get());
473 >            assertTrue(future.isDone());
474 >            assertFalse(future.isCancelled());
475 >        } finally {
476 >            joinPool(e);
477 >        }
478      }
479  
480      /**
481       * A submitted privileged action runs to completion
482       */
483 <    public void testSubmitPrivilegedAction() throws Throwable {
483 >    public void testSubmitPrivilegedAction() throws Exception {
484 >        final Callable callable = Executors.callable(new PrivilegedAction() {
485 >                public Object run() { return TEST_STRING; }});
486          Runnable r = new CheckedRunnable() {
487 <            public void realRun() throws Exception {
488 <                ExecutorService e = new ForkJoinPool(1);
489 <                Future future = e.submit(Executors.callable(new PrivilegedAction() {
490 <                    public Object run() {
491 <                        return TEST_STRING;
492 <                    }}));
493 <
494 <                Object result = future.get();
495 <                assertSame(TEST_STRING, result);
522 <            }};
487 >        public void realRun() throws Exception {
488 >            ExecutorService e = new ForkJoinPool(1);
489 >            try {
490 >                Future future = e.submit(callable);
491 >                assertSame(TEST_STRING, future.get());
492 >            } finally {
493 >                joinPool(e);
494 >            }
495 >        }};
496  
497          runWithPermissions(r, new RuntimePermission("modifyThread"));
498      }
# Line 527 | Line 500 | public class ForkJoinPoolTest extends JS
500      /**
501       * A submitted privileged exception action runs to completion
502       */
503 <    public void testSubmitPrivilegedExceptionAction() throws Throwable {
503 >    public void testSubmitPrivilegedExceptionAction() throws Exception {
504 >        final Callable callable =
505 >            Executors.callable(new PrivilegedExceptionAction() {
506 >                public Object run() { return TEST_STRING; }});
507          Runnable r = new CheckedRunnable() {
508 <            public void realRun() throws Exception {
509 <                ExecutorService e = new ForkJoinPool(1);
510 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
511 <                    public Object run() {
512 <                        return TEST_STRING;
513 <                    }}));
514 <
515 <                Object result = future.get();
516 <                assertSame(TEST_STRING, result);
541 <            }};
508 >        public void realRun() throws Exception {
509 >            ExecutorService e = new ForkJoinPool(1);
510 >            try {
511 >                Future future = e.submit(callable);
512 >                assertSame(TEST_STRING, future.get());
513 >            } finally {
514 >                joinPool(e);
515 >            }
516 >        }};
517  
518          runWithPermissions(r, new RuntimePermission("modifyThread"));
519      }
# Line 546 | Line 521 | public class ForkJoinPoolTest extends JS
521      /**
522       * A submitted failed privileged exception action reports exception
523       */
524 <    public void testSubmitFailedPrivilegedExceptionAction() throws Throwable {
524 >    public void testSubmitFailedPrivilegedExceptionAction() throws Exception {
525 >        final Callable callable =
526 >            Executors.callable(new PrivilegedExceptionAction() {
527 >                public Object run() { throw new IndexOutOfBoundsException(); }});
528          Runnable r = new CheckedRunnable() {
529 <            public void realRun() throws Exception {
530 <                ExecutorService e = new ForkJoinPool(1);
531 <                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
532 <                    public Object run() throws Exception {
555 <                        throw new IndexOutOfBoundsException();
556 <                    }}));
557 <
529 >        public void realRun() throws Exception {
530 >            ExecutorService e = new ForkJoinPool(1);
531 >            try {
532 >                Future future = e.submit(callable);
533                  try {
534 <                    Object result = future.get();
534 >                    future.get();
535                      shouldThrow();
536                  } catch (ExecutionException success) {
537                      assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
538 <                }}};
538 >                }
539 >            } finally {
540 >                joinPool(e);
541 >            }
542 >        }};
543  
544          runWithPermissions(r, new RuntimePermission("modifyThread"));
545      }
# Line 569 | Line 548 | public class ForkJoinPoolTest extends JS
548       * execute(null runnable) throws NullPointerException
549       */
550      public void testExecuteNullRunnable() {
551 +        ExecutorService e = new ForkJoinPool(1);
552          try {
553 <            ExecutorService e = new ForkJoinPool(1);
574 <            TrackedShortRunnable task = null;
575 <            Future<?> future = e.submit(task);
553 >            Future<?> future = e.submit((Runnable) null);
554              shouldThrow();
555 <        } catch (NullPointerException success) {}
555 >        } catch (NullPointerException success) {
556 >        } finally {
557 >            joinPool(e);
558 >        }
559      }
560  
580
561      /**
562       * submit(null callable) throws NullPointerException
563       */
564      public void testSubmitNullCallable() {
565 +        ExecutorService e = new ForkJoinPool(1);
566          try {
567 <            ExecutorService e = new ForkJoinPool(1);
587 <            StringTask t = null;
588 <            Future<String> future = e.submit(t);
567 >            Future<String> future = e.submit((Callable) null);
568              shouldThrow();
569 <        } catch (NullPointerException success) {}
569 >        } catch (NullPointerException success) {
570 >        } finally {
571 >            joinPool(e);
572 >        }
573      }
574  
593
575      /**
576 <     * Blocking on submit(callable) throws InterruptedException if
596 <     * caller interrupted.
576 >     * submit(callable).get() throws InterruptedException if interrupted
577       */
578      public void testInterruptedSubmit() throws InterruptedException {
579 <        final ForkJoinPool p = new ForkJoinPool(1);
580 <
581 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
582 <            public void realRun() throws Throwable {
583 <                p.submit(new CheckedCallable<Object>() {
584 <                    public Object realCall() throws Throwable {
585 <                        try {
586 <                            Thread.sleep(MEDIUM_DELAY_MS);
587 <                        } catch (InterruptedException ok) {
588 <                        }
589 <                        return null;
590 <                    }}).get();
591 <            }});
592 <
593 <        t.start();
594 <        Thread.sleep(SHORT_DELAY_MS);
595 <        t.interrupt();
596 <        t.join();
597 <        p.shutdownNow();
598 <        joinPool(p);
579 >        final CountDownLatch submitted    = new CountDownLatch(1);
580 >        final CountDownLatch quittingTime = new CountDownLatch(1);
581 >        final ExecutorService p = new ForkJoinPool(1);
582 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
583 >            public Void realCall() throws InterruptedException {
584 >                assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
585 >                return null;
586 >            }};
587 >        try {
588 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
589 >                public void realRun() throws Exception {
590 >                    Future<Void> future = p.submit(awaiter);
591 >                    submitted.countDown();
592 >                    future.get();
593 >                }});
594 >            t.start();
595 >            assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
596 >            t.interrupt();
597 >            t.join();
598 >        } finally {
599 >            quittingTime.countDown();
600 >            joinPool(p);
601 >        }
602      }
603  
604      /**
# Line 626 | Line 609 | public class ForkJoinPoolTest extends JS
609          ForkJoinPool p = new ForkJoinPool(1);
610          try {
611              p.submit(new Callable() {
612 <                public Object call() {
613 <                    int i = 5/0;
631 <                    return Boolean.TRUE;
632 <                }}).get();
612 >                public Object call() { throw new ArithmeticException(); }})
613 >                .get();
614              shouldThrow();
615          } catch (ExecutionException success) {
616              assertTrue(success.getCause() instanceof ArithmeticException);
617 +        } finally {
618 +            joinPool(p);
619          }
637
638        joinPool(p);
620      }
621  
622      /**
# Line 817 | Line 798 | public class ForkJoinPoolTest extends JS
798          }
799      }
800  
820
801      /**
802       * timed invokeAny(null) throws NullPointerException
803       */
# Line 1007 | Line 987 | public class ForkJoinPoolTest extends JS
987              l.add(new StringTask());
988              l.add(new StringTask());
989              List<Future<String>> futures
990 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
990 >                = e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
991              assertEquals(2, futures.size());
992              for (Future<String> future : futures)
993                  assertSame(TEST_STRING, future.get());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines