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.22 by jsr166, Wed Sep 1 06:41:55 2010 UTC vs.
Revision 1.25 by jsr166, Mon Sep 13 20:48:58 2010 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/licenses/publicdomain
5   */
6  
7
7   import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.Executor;
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;
# Line 20 | Line 20 | import java.util.concurrent.RejectedExec
20   import java.util.concurrent.ForkJoinPool;
21   import java.util.concurrent.ForkJoinTask;
22   import java.util.concurrent.ForkJoinWorkerThread;
23 import java.util.concurrent.RecursiveAction;
23   import java.util.concurrent.RecursiveTask;
24   import java.util.concurrent.TimeUnit;
25 < import java.util.concurrent.locks.*;
26 < import java.security.*;
25 > import java.util.concurrent.locks.ReentrantLock;
26 > import java.security.AccessControlException;
27 > import java.security.Policy;
28 > import java.security.PrivilegedAction;
29 > import java.security.PrivilegedExceptionAction;
30  
31   public class ForkJoinPoolTest extends JSR166TestCase {
32      public static void main(String[] args) {
33          junit.textui.TestRunner.run(suite());
34      }
35 +
36      public static Test suite() {
37          return new TestSuite(ForkJoinPoolTest.class);
38      }
# Line 156 | Line 159 | public class ForkJoinPoolTest extends JS
159       * tasks, and quiescent running state.
160       */
161      public void testDefaultInitialState() {
162 <        ForkJoinPool p = null;
162 >        ForkJoinPool p = new ForkJoinPool(1);
163          try {
161            p = new ForkJoinPool(1);
164              assertTrue(p.getFactory() ==
165                         ForkJoinPool.defaultForkJoinWorkerThreadFactory);
166              assertTrue(p.isQuiescent());
# Line 201 | Line 203 | public class ForkJoinPoolTest extends JS
203       * getParallelism returns size set in constructor
204       */
205      public void testGetParallelism() {
206 <        ForkJoinPool p = null;
206 >        ForkJoinPool p = new ForkJoinPool(1);
207          try {
206            p = new ForkJoinPool(1);
208              assertTrue(p.getParallelism() == 1);
209          } finally {
210              joinPool(p);
# Line 214 | 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 {
219            p = new ForkJoinPool(1);
220              assertTrue(p.getActiveThreadCount() == 0);
221              Future<String> future = p.submit(new StringTask());
222              assertTrue(p.getPoolSize() == 1);
223
223          } finally {
224              joinPool(p);
225          }
# Line 233 | Line 232 | public class ForkJoinPoolTest extends JS
232       * performs its defined action
233       */
234      public void testSetUncaughtExceptionHandler() throws InterruptedException {
235 <        ForkJoinPool p = null;
235 >        MyHandler eh = new MyHandler();
236 >        ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false);
237          try {
238            MyHandler eh = new MyHandler();
239            p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false);
238              assert(eh == p.getUncaughtExceptionHandler());
239              p.execute(new FailingTask());
240              Thread.sleep(MEDIUM_DELAY_MS);
# Line 253 | Line 251 | public class ForkJoinPoolTest extends JS
251       * construction parameters continue to hold
252       */
253      public void testisQuiescent() throws InterruptedException {
254 <        ForkJoinPool p = null;
254 >        ForkJoinPool p = new ForkJoinPool(2);
255          try {
258            p = new ForkJoinPool(2);
256              p.invoke(new FibTask(20));
257              assertTrue(p.getFactory() ==
258                         ForkJoinPool.defaultForkJoinWorkerThreadFactory);
# Line 278 | Line 275 | public class ForkJoinPoolTest extends JS
275       * Completed submit(ForkJoinTask) returns result
276       */
277      public void testSubmitForkJoinTask() throws Throwable {
278 <        ForkJoinPool p = null;
278 >        ForkJoinPool p = new ForkJoinPool(1);
279          try {
283            p = new ForkJoinPool(1);
280              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
281              int r = f.get();
282              assertTrue(r == 21);
# Line 293 | Line 289 | public class ForkJoinPoolTest extends JS
289       * A task submitted after shutdown is rejected
290       */
291      public void testSubmitAfterShutdown() {
292 <        ForkJoinPool p = null;
292 >        ForkJoinPool p = new ForkJoinPool(1);
293          try {
298            p = new ForkJoinPool(1);
294              p.shutdown();
295              assertTrue(p.isShutdown());
296              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
# Line 310 | Line 305 | public class ForkJoinPoolTest extends JS
305       * Pool maintains parallelism when using ManagedBlocker
306       */
307      public void testBlockingForkJoinTask() throws Throwable {
308 <        ForkJoinPool p = null;
308 >        ForkJoinPool p = new ForkJoinPool(4);
309          try {
315            p = new ForkJoinPool(4);
310              ReentrantLock lock = new ReentrantLock();
311              ManagedLocker locker = new ManagedLocker(lock);
312              ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
# Line 328 | Line 322 | public class ForkJoinPoolTest extends JS
322       * pollSubmission returns unexecuted submitted task, if present
323       */
324      public void testPollSubmission() {
325 <        SubFJP p = null;
325 >        SubFJP p = new SubFJP();
326          try {
333            p = new SubFJP();
327              ForkJoinTask a = p.submit(new MediumRunnable());
328              ForkJoinTask b = p.submit(new MediumRunnable());
329              ForkJoinTask c = p.submit(new MediumRunnable());
# Line 346 | Line 339 | public class ForkJoinPoolTest extends JS
339       * drainTasksTo transfers unexecuted submitted tasks, if present
340       */
341      public void testDrainTasksTo() {
342 <        SubFJP p = null;
342 >        SubFJP p = new SubFJP();
343          try {
351            p = new SubFJP();
344              ForkJoinTask a = p.submit(new MediumRunnable());
345              ForkJoinTask b = p.submit(new MediumRunnable());
346              ForkJoinTask c = p.submit(new MediumRunnable());
# Line 372 | Line 364 | public class ForkJoinPoolTest extends JS
364       */
365      public void testExecuteRunnable() throws Throwable {
366          ExecutorService e = new ForkJoinPool(1);
367 <        TrackedShortRunnable task = new TrackedShortRunnable();
368 <        assertFalse(task.done);
369 <        Future<?> future = e.submit(task);
370 <        future.get();
371 <        assertTrue(task.done);
367 >        try {
368 >            TrackedShortRunnable task = new TrackedShortRunnable();
369 >            assertFalse(task.done);
370 >            Future<?> future = e.submit(task);
371 >            future.get();
372 >            assertTrue(task.done);
373 >        } finally {
374 >            joinPool(e);
375 >        }
376      }
377  
378  
# Line 385 | Line 381 | public class ForkJoinPoolTest extends JS
381       */
382      public void testSubmitCallable() throws Throwable {
383          ExecutorService e = new ForkJoinPool(1);
384 <        Future<String> future = e.submit(new StringTask());
385 <        String result = future.get();
386 <        assertSame(TEST_STRING, result);
384 >        try {
385 >            Future<String> future = e.submit(new StringTask());
386 >            String result = future.get();
387 >            assertSame(TEST_STRING, result);
388 >        } finally {
389 >            joinPool(e);
390 >        }
391      }
392  
393      /**
# Line 395 | Line 395 | public class ForkJoinPoolTest extends JS
395       */
396      public void testSubmitRunnable() throws Throwable {
397          ExecutorService e = new ForkJoinPool(1);
398 <        Future<?> future = e.submit(new NoOpRunnable());
399 <        future.get();
400 <        assertTrue(future.isDone());
398 >        try {
399 >            Future<?> future = e.submit(new NoOpRunnable());
400 >            future.get();
401 >            assertTrue(future.isDone());
402 >        } finally {
403 >            joinPool(e);
404 >        }
405      }
406  
407      /**
# Line 405 | Line 409 | public class ForkJoinPoolTest extends JS
409       */
410      public void testSubmitRunnable2() throws Throwable {
411          ExecutorService e = new ForkJoinPool(1);
412 <        Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
413 <        String result = future.get();
414 <        assertSame(TEST_STRING, result);
412 >        try {
413 >            Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
414 >            String result = future.get();
415 >            assertSame(TEST_STRING, result);
416 >        } finally {
417 >            joinPool(e);
418 >        }
419      }
420  
421  
# Line 425 | Line 433 | public class ForkJoinPoolTest extends JS
433          } catch (AccessControlException ok) {
434              return;
435          }
436 +
437          try {
438              ExecutorService e = new ForkJoinPool(1);
439 <            Future future = e.submit(Executors.callable(new PrivilegedAction() {
439 >            try {
440 >                Future future = e.submit(Executors.callable(new PrivilegedAction() {
441                      public Object run() {
442                          return TEST_STRING;
443                      }}));
444  
445 <            Object result = future.get();
446 <            assertSame(TEST_STRING, result);
447 <        }
448 <        finally {
445 >                Object result = future.get();
446 >                assertSame(TEST_STRING, result);
447 >            } finally {
448 >                joinPool(e);
449 >            }
450 >        } finally {
451              Policy.setPolicy(savedPolicy);
452          }
453      }
# Line 457 | Line 469 | public class ForkJoinPoolTest extends JS
469  
470          try {
471              ExecutorService e = new ForkJoinPool(1);
472 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
472 >            try {
473 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
474                      public Object run() {
475                          return TEST_STRING;
476                      }}));
477  
478 <            Object result = future.get();
479 <            assertSame(TEST_STRING, result);
480 <        }
481 <        finally {
478 >                Object result = future.get();
479 >                assertSame(TEST_STRING, result);
480 >            } finally {
481 >                joinPool(e);
482 >            }
483 >        } finally {
484              Policy.setPolicy(savedPolicy);
485          }
486      }
# Line 485 | Line 500 | public class ForkJoinPoolTest extends JS
500              return;
501          }
502  
488
503          try {
504              ExecutorService e = new ForkJoinPool(1);
505 <            Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
505 >            try {
506 >                Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() {
507                      public Object run() throws Exception {
508                          throw new IndexOutOfBoundsException();
509                      }}));
510  
511 <            Object result = future.get();
512 <            shouldThrow();
513 <        } catch (ExecutionException success) {
514 <            assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
511 >                Object result = future.get();
512 >                shouldThrow();
513 >            } catch (ExecutionException success) {
514 >                assertTrue(success.getCause() instanceof IndexOutOfBoundsException);
515 >            } finally {
516 >                joinPool(e);
517 >            }
518          } finally {
519              Policy.setPolicy(savedPolicy);
520          }
# Line 506 | Line 524 | public class ForkJoinPoolTest extends JS
524       * execute(null runnable) throws NullPointerException
525       */
526      public void testExecuteNullRunnable() {
527 +        ExecutorService e = new ForkJoinPool(1);
528          try {
510            ExecutorService e = new ForkJoinPool(1);
529              TrackedShortRunnable task = null;
530              Future<?> future = e.submit(task);
531              shouldThrow();
532 <        } catch (NullPointerException success) {}
532 >        } catch (NullPointerException success) {
533 >        } finally {
534 >            joinPool(e);
535 >        }
536      }
537  
538  
# Line 519 | Line 540 | public class ForkJoinPoolTest extends JS
540       * submit(null callable) throws NullPointerException
541       */
542      public void testSubmitNullCallable() {
543 +        ExecutorService e = new ForkJoinPool(1);
544          try {
523            ExecutorService e = new ForkJoinPool(1);
545              StringTask t = null;
546              Future<String> future = e.submit(t);
547              shouldThrow();
548 <        } catch (NullPointerException success) {}
548 >        } catch (NullPointerException success) {
549 >        } finally {
550 >            joinPool(e);
551 >        }
552      }
553  
554  
# Line 570 | Line 594 | public class ForkJoinPoolTest extends JS
594              shouldThrow();
595          } catch (ExecutionException success) {
596              assertTrue(success.getCause() instanceof ArithmeticException);
597 +        } finally {
598 +            joinPool(p);
599          }
574
575        joinPool(p);
600      }
601  
602      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines