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.24 by jsr166, Mon Sep 13 15:34:42 2010 UTC vs.
Revision 1.30 by jsr166, Fri Sep 17 17:07:47 2010 UTC

# Line 5 | Line 5
5   */
6  
7   import junit.framework.*;
8 < import java.util.*;
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 21 | Line 23 | import java.util.concurrent.ForkJoinWork
23   import java.util.concurrent.RecursiveTask;
24   import java.util.concurrent.TimeUnit;
25   import java.util.concurrent.locks.ReentrantLock;
26 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
27   import java.security.AccessControlException;
28   import java.security.Policy;
29   import java.security.PrivilegedAction;
# Line 159 | Line 162 | public class ForkJoinPoolTest extends JS
162      public void testDefaultInitialState() {
163          ForkJoinPool p = new ForkJoinPool(1);
164          try {
165 <            assertTrue(p.getFactory() ==
166 <                       ForkJoinPool.defaultForkJoinWorkerThreadFactory);
165 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
166 >                       p.getFactory());
167              assertTrue(p.isQuiescent());
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 203 | Line 206 | public class ForkJoinPoolTest extends JS
206      public void testGetParallelism() {
207          ForkJoinPool p = new ForkJoinPool(1);
208          try {
209 <            assertTrue(p.getParallelism() == 1);
209 >            assertEquals(1, p.getParallelism());
210          } finally {
211              joinPool(p);
212          }
# Line 215 | Line 218 | public class ForkJoinPoolTest extends JS
218      public void testGetPoolSize() {
219          ForkJoinPool p = new ForkJoinPool(1);
220          try {
221 <            assertTrue(p.getActiveThreadCount() == 0);
221 >            assertEquals(0, p.getActiveThreadCount());
222              Future<String> future = p.submit(new StringTask());
223 <            assertTrue(p.getPoolSize() == 1);
223 >            assertEquals(1, p.getPoolSize());
224          } finally {
225              joinPool(p);
226          }
# Line 230 | Line 233 | public class ForkJoinPoolTest extends JS
233       * performs its defined action
234       */
235      public void testSetUncaughtExceptionHandler() throws InterruptedException {
236 <        MyHandler eh = new MyHandler();
237 <        ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false);
238 <        try {
239 <            assert(eh == p.getUncaughtExceptionHandler());
240 <            p.execute(new FailingTask());
241 <            Thread.sleep(MEDIUM_DELAY_MS);
242 <            assertTrue(eh.catches > 0);
236 >        final CountDownLatch uehInvoked = new CountDownLatch(1);
237 >        final Thread.UncaughtExceptionHandler eh =
238 >            new Thread.UncaughtExceptionHandler() {
239 >                public void uncaughtException(Thread t, Throwable e) {
240 >                    uehInvoked.countDown();
241 >                }};
242 >        ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(),
243 >                                          eh, false);
244 >        try {
245 >            assertSame(eh, p.getUncaughtExceptionHandler());
246 >            p.execute(new FibTask(8));
247 >            assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS));
248          } finally {
249 <            p.shutdownNow();
249 >            p.shutdownNow(); // failure might have prevented processing task
250              joinPool(p);
251          }
252      }
# Line 251 | Line 259 | public class ForkJoinPoolTest extends JS
259      public void testisQuiescent() throws InterruptedException {
260          ForkJoinPool p = new ForkJoinPool(2);
261          try {
262 +            assertTrue(p.isQuiescent());
263              p.invoke(new FibTask(20));
264 <            assertTrue(p.getFactory() ==
265 <                       ForkJoinPool.defaultForkJoinWorkerThreadFactory);
266 <            Thread.sleep(MEDIUM_DELAY_MS);
264 >            assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory,
265 >                       p.getFactory());
266 >            Thread.sleep(SMALL_DELAY_MS);
267              assertTrue(p.isQuiescent());
268              assertFalse(p.getAsyncMode());
269 <            assertTrue(p.getActiveThreadCount() == 0);
270 <            assertTrue(p.getQueuedTaskCount() == 0);
271 <            assertTrue(p.getQueuedSubmissionCount() == 0);
269 >            assertEquals(0, p.getActiveThreadCount());
270 >            assertEquals(0, p.getQueuedTaskCount());
271 >            assertEquals(0, p.getQueuedSubmissionCount());
272              assertFalse(p.hasQueuedSubmissions());
273              assertFalse(p.isShutdown());
274              assertFalse(p.isTerminating());
# Line 276 | Line 285 | public class ForkJoinPoolTest extends JS
285          ForkJoinPool p = new ForkJoinPool(1);
286          try {
287              ForkJoinTask<Integer> f = p.submit(new FibTask(8));
288 <            int r = f.get();
280 <            assertTrue(r == 21);
288 >            assertEquals(21, (int) f.get());
289          } finally {
290              joinPool(p);
291          }
# Line 291 | Line 299 | public class ForkJoinPoolTest extends JS
299          try {
300              p.shutdown();
301              assertTrue(p.isShutdown());
302 <            ForkJoinTask<Integer> f = p.submit(new FibTask(8));
303 <            shouldThrow();
304 <        } catch (RejectedExecutionException success) {
302 >            try {
303 >                ForkJoinTask<Integer> f = p.submit(new FibTask(8));
304 >                shouldThrow();
305 >            } catch (RejectedExecutionException success) {}
306          } finally {
307              joinPool(p);
308          }
# Line 309 | Line 318 | public class ForkJoinPoolTest extends JS
318              ManagedLocker locker = new ManagedLocker(lock);
319              ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
320              p.execute(f);
321 <            int r = f.get();
313 <            assertTrue(r == 832040);
321 >            assertEquals(832040, (int) f.get());
322          } finally {
323              p.shutdownNow(); // don't wait out shutdown
324          }
# Line 523 | Line 531 | public class ForkJoinPoolTest extends JS
531       */
532      public void testExecuteNullRunnable() {
533          ExecutorService e = new ForkJoinPool(1);
534 +        TrackedShortRunnable task = null;
535          try {
527            TrackedShortRunnable task = null;
536              Future<?> future = e.submit(task);
537              shouldThrow();
538          } catch (NullPointerException success) {
# Line 539 | Line 547 | public class ForkJoinPoolTest extends JS
547       */
548      public void testSubmitNullCallable() {
549          ExecutorService e = new ForkJoinPool(1);
550 +        StringTask t = null;
551          try {
543            StringTask t = null;
552              Future<String> future = e.submit(t);
553              shouldThrow();
554          } catch (NullPointerException success) {
# Line 551 | Line 559 | public class ForkJoinPoolTest extends JS
559  
560  
561      /**
562 <     * Blocking on submit(callable) throws InterruptedException if
555 <     * caller interrupted.
562 >     * submit(callable).get() throws InterruptedException if interrupted
563       */
564      public void testInterruptedSubmit() throws InterruptedException {
565 <        final ForkJoinPool p = new ForkJoinPool(1);
566 <
567 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
568 <            public void realRun() throws Throwable {
569 <                p.submit(new CheckedCallable<Object>() {
570 <                    public Object realCall() throws Throwable {
571 <                        try {
572 <                            Thread.sleep(MEDIUM_DELAY_MS);
573 <                        } catch (InterruptedException ok) {
574 <                        }
575 <                        return null;
576 <                    }}).get();
577 <            }});
578 <
579 <        t.start();
580 <        Thread.sleep(SHORT_DELAY_MS);
581 <        t.interrupt();
582 <        t.join();
583 <        p.shutdownNow();
584 <        joinPool(p);
565 >        final CountDownLatch submitted    = new CountDownLatch(1);
566 >        final CountDownLatch quittingTime = new CountDownLatch(1);
567 >        final ExecutorService p = new ForkJoinPool(1);
568 >        final Callable<Void> awaiter = new CheckedCallable<Void>() {
569 >            public Void realCall() throws InterruptedException {
570 >                assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS));
571 >                return null;
572 >            }};
573 >        try {
574 >            Thread t = new Thread(new CheckedInterruptedRunnable() {
575 >                public void realRun() throws Exception {
576 >                    Future<Void> future = p.submit(awaiter);
577 >                    submitted.countDown();
578 >                    future.get();
579 >                }});
580 >            t.start();
581 >            assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS));
582 >            t.interrupt();
583 >            t.join();
584 >        } finally {
585 >            quittingTime.countDown();
586 >            joinPool(p);
587 >        }
588      }
589  
590      /**
# Line 783 | Line 793 | public class ForkJoinPoolTest extends JS
793      public void testTimedInvokeAny1() throws Throwable {
794          ExecutorService e = new ForkJoinPool(1);
795          try {
796 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
796 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
797              shouldThrow();
798          } catch (NullPointerException success) {
799          } finally {
# Line 814 | Line 824 | public class ForkJoinPoolTest extends JS
824          ExecutorService e = new ForkJoinPool(1);
825          try {
826              e.invokeAny(new ArrayList<Callable<String>>(),
827 <                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
827 >                        MEDIUM_DELAY_MS, MILLISECONDS);
828              shouldThrow();
829          } catch (IllegalArgumentException success) {
830          } finally {
# Line 832 | Line 842 | public class ForkJoinPoolTest extends JS
842          l.add(latchAwaitingStringTask(latch));
843          l.add(null);
844          try {
845 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
845 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
846              shouldThrow();
847          } catch (NullPointerException success) {
848          } finally {
# Line 849 | Line 859 | public class ForkJoinPoolTest extends JS
859          List<Callable<String>> l = new ArrayList<Callable<String>>();
860          l.add(new NPETask());
861          try {
862 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
862 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
863              shouldThrow();
864          } catch (ExecutionException success) {
865              assertTrue(success.getCause() instanceof NullPointerException);
# Line 867 | Line 877 | public class ForkJoinPoolTest extends JS
877              List<Callable<String>> l = new ArrayList<Callable<String>>();
878              l.add(new StringTask());
879              l.add(new StringTask());
880 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
880 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
881              assertSame(TEST_STRING, result);
882          } finally {
883              joinPool(e);
# Line 880 | Line 890 | public class ForkJoinPoolTest extends JS
890      public void testTimedInvokeAll1() throws Throwable {
891          ExecutorService e = new ForkJoinPool(1);
892          try {
893 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
893 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
894              shouldThrow();
895          } catch (NullPointerException success) {
896          } finally {
# Line 912 | Line 922 | public class ForkJoinPoolTest extends JS
922          try {
923              List<Future<String>> r
924                  = e.invokeAll(new ArrayList<Callable<String>>(),
925 <                              MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
925 >                              MEDIUM_DELAY_MS, MILLISECONDS);
926              assertTrue(r.isEmpty());
927          } finally {
928              joinPool(e);
# Line 928 | Line 938 | public class ForkJoinPoolTest extends JS
938          l.add(new StringTask());
939          l.add(null);
940          try {
941 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
941 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
942              shouldThrow();
943          } catch (NullPointerException success) {
944          } finally {
# Line 944 | Line 954 | public class ForkJoinPoolTest extends JS
954          List<Callable<String>> l = new ArrayList<Callable<String>>();
955          l.add(new NPETask());
956          List<Future<String>> futures
957 <            = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
957 >            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
958          assertEquals(1, futures.size());
959          try {
960              futures.get(0).get();
# Line 966 | Line 976 | public class ForkJoinPoolTest extends JS
976              l.add(new StringTask());
977              l.add(new StringTask());
978              List<Future<String>> futures
979 <                = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
979 >                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
980              assertEquals(2, futures.size());
981              for (Future<String> future : futures)
982                  assertSame(TEST_STRING, future.get());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines