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.16 by jsr166, Tue Dec 1 22:51:44 2009 UTC vs.
Revision 1.20 by dl, Wed Aug 11 19:50:02 2010 UTC

# Line 7 | Line 7
7  
8   import junit.framework.*;
9   import java.util.*;
10 < import java.util.concurrent.*;
11 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 > import java.util.concurrent.Executor;
11 > import java.util.concurrent.Executors;
12 > import java.util.concurrent.ExecutorService;
13 > import java.util.concurrent.AbstractExecutorService;
14 > import java.util.concurrent.CountDownLatch;
15 > import java.util.concurrent.Callable;
16 > import java.util.concurrent.Future;
17 > import java.util.concurrent.ExecutionException;
18 > import java.util.concurrent.CancellationException;
19 > import java.util.concurrent.RejectedExecutionException;
20 > import java.util.concurrent.ForkJoinPool;
21 > import java.util.concurrent.ForkJoinTask;
22 > import java.util.concurrent.ForkJoinWorkerThread;
23 > import java.util.concurrent.RecursiveAction;
24 > import java.util.concurrent.RecursiveTask;
25 > import java.util.concurrent.TimeUnit;
26   import java.util.concurrent.locks.*;
27   import java.security.*;
28  
# Line 39 | Line 53 | public class ForkJoinPoolTest extends JS
53      // Some classes to test extension and factory methods
54  
55      static class MyHandler implements Thread.UncaughtExceptionHandler {
56 <        int catches = 0;
56 >        volatile int catches = 0;
57          public void uncaughtException(Thread t, Throwable e) {
58              ++catches;
59          }
# Line 48 | Line 62 | public class ForkJoinPoolTest extends JS
62      // to test handlers
63      static class FailingFJWSubclass extends ForkJoinWorkerThread {
64          public FailingFJWSubclass(ForkJoinPool p) { super(p) ; }
65 <        protected void onStart() { throw new Error(); }
65 >        protected void onStart() { super.onStart(); throw new Error(); }
66      }
67  
68      static class FailingThreadFactory
69              implements ForkJoinPool.ForkJoinWorkerThreadFactory {
70 <        int calls = 0;
70 >        volatile int calls = 0;
71          public ForkJoinWorkerThread newThread(ForkJoinPool p) {
72              if (++calls > 1) return null;
73              return new FailingFJWSubclass(p);
# Line 148 | Line 162 | public class ForkJoinPoolTest extends JS
162              assertTrue(p.getFactory() ==
163                         ForkJoinPool.defaultForkJoinWorkerThreadFactory);
164              assertTrue(p.isQuiescent());
151            assertTrue(p.getMaintainsParallelism());
165              assertFalse(p.getAsyncMode());
166              assertTrue(p.getActiveThreadCount() == 0);
167              assertTrue(p.getStealCount() == 0);
# Line 178 | Line 191 | public class ForkJoinPoolTest extends JS
191       */
192      public void testConstructor2() {
193          try {
194 <            new ForkJoinPool(1, null);
194 >            new ForkJoinPool(1, null, null, false);
195              shouldThrow();
196          } catch (NullPointerException success) {}
197      }
# Line 198 | Line 211 | public class ForkJoinPoolTest extends JS
211      }
212  
213      /**
201     * setParallelism changes reported parallelism level.
202     */
203    public void testSetParallelism() {
204        ForkJoinPool p = null;
205        try {
206            p = new ForkJoinPool(1);
207            assertTrue(p.getParallelism() == 1);
208            p.setParallelism(2);
209            assertTrue(p.getParallelism() == 2);
210        } finally {
211            joinPool(p);
212        }
213    }
214
215    /**
216     * setParallelism with argument <= 0 throws exception
217     */
218    public void testSetParallelism2() {
219        ForkJoinPool p = null;
220        try {
221            p = new ForkJoinPool(1);
222            assertTrue(p.getParallelism() == 1);
223            p.setParallelism(-2);
224            shouldThrow();
225        } catch (IllegalArgumentException success) {
226        } finally {
227            joinPool(p);
228        }
229    }
230
231    /**
214       * getPoolSize returns number of started workers.
215       */
216      public void testGetPoolSize() {
217          ForkJoinPool p = null;
218          try {
219              p = new ForkJoinPool(1);
220 <            assertTrue(p.getPoolSize() == 0);
220 >            assertTrue(p.getActiveThreadCount() == 0);
221              Future<String> future = p.submit(new StringTask());
222              assertTrue(p.getPoolSize() == 1);
223  
# Line 245 | Line 227 | public class ForkJoinPoolTest extends JS
227      }
228  
229      /**
248     * setMaximumPoolSize changes size reported by getMaximumPoolSize.
249     */
250    public void testSetMaximumPoolSize() {
251        ForkJoinPool p = null;
252        try {
253            p = new ForkJoinPool(1);
254            p.setMaximumPoolSize(2);
255            assertTrue(p.getMaximumPoolSize() == 2);
256        } finally {
257            joinPool(p);
258        }
259    }
260
261    /**
262     * setMaximumPoolSize with argument <= 0 throws exception
263     */
264    public void testSetMaximumPoolSize2() {
265        ForkJoinPool p = null;
266        try {
267            p = new ForkJoinPool(1);
268            p.setMaximumPoolSize(-2);
269            shouldThrow();
270        } catch (IllegalArgumentException success) {
271        } finally {
272            joinPool(p);
273        }
274    }
275
276    /**
277     * setMaintainsParallelism changes policy reported by
278     * getMaintainsParallelism.
279     */
280    public void testSetMaintainsParallelism() {
281        ForkJoinPool p = null;
282        try {
283            p = new ForkJoinPool(1);
284            p.setMaintainsParallelism(false);
285            assertFalse(p.getMaintainsParallelism());
286        } finally {
287            joinPool(p);
288        }
289    }
290
291    /**
292     * setAsyncMode changes policy reported by
293     * getAsyncMode.
294     */
295    public void testSetAsyncMode() {
296        ForkJoinPool p = null;
297        try {
298            p = new ForkJoinPool(1);
299            p.setAsyncMode(true);
300            assertTrue(p.getAsyncMode());
301        } finally {
302            joinPool(p);
303        }
304    }
305
306    /**
230       * setUncaughtExceptionHandler changes handler for uncaught exceptions.
231       *
232       * Additionally tests: Overriding ForkJoinWorkerThread.onStart
# Line 312 | Line 235 | public class ForkJoinPoolTest extends JS
235      public void testSetUncaughtExceptionHandler() throws InterruptedException {
236          ForkJoinPool p = null;
237          try {
315            p = new ForkJoinPool(1, new FailingThreadFactory());
238              MyHandler eh = new MyHandler();
239 <            p.setUncaughtExceptionHandler(eh);
240 <            assertEquals(eh, p.getUncaughtExceptionHandler());
239 >            p = new ForkJoinPool(1, new FailingThreadFactory(), eh, false);
240 >            assert(eh == p.getUncaughtExceptionHandler());
241              p.execute(new FailingTask());
242              Thread.sleep(MEDIUM_DELAY_MS);
243              assertTrue(eh.catches > 0);
244          } finally {
245 +            p.shutdownNow();
246              joinPool(p);
247          }
248      }
249  
250      /**
328     * setUncaughtExceptionHandler of null removes handler
329     */
330    public void testSetUncaughtExceptionHandler2() {
331        ForkJoinPool p = null;
332        try {
333            p = new ForkJoinPool(1);
334            p.setUncaughtExceptionHandler(null);
335            assertNull(p.getUncaughtExceptionHandler());
336        } finally {
337            joinPool(p);
338        }
339    }
340
341
342    /**
251       * After invoking a single task, isQuiescent is true,
252       * queues are empty, threads are not active, and
253       * construction parameters continue to hold
# Line 353 | Line 261 | public class ForkJoinPoolTest extends JS
261                         ForkJoinPool.defaultForkJoinWorkerThreadFactory);
262              Thread.sleep(MEDIUM_DELAY_MS);
263              assertTrue(p.isQuiescent());
356            assertTrue(p.getMaintainsParallelism());
264              assertFalse(p.getAsyncMode());
265              assertTrue(p.getActiveThreadCount() == 0);
266              assertTrue(p.getQueuedTaskCount() == 0);
# Line 410 | Line 317 | public class ForkJoinPoolTest extends JS
317              ManagedLocker locker = new ManagedLocker(lock);
318              ForkJoinTask<Integer> f = new LockingFibTask(30, locker, lock);
319              p.execute(f);
413            assertTrue(p.getPoolSize() >= 4);
320              int r = f.get();
321              assertTrue(r == 832040);
322          } finally {
# Line 665 | Line 571 | public class ForkJoinPoolTest extends JS
571          } catch (ExecutionException success) {
572              assertTrue(success.getCause() instanceof ArithmeticException);
573          }
574 <
574 >        
575          joinPool(p);
576      }
577  
# Line 855 | Line 761 | public class ForkJoinPoolTest extends JS
761      public void testTimedInvokeAny1() throws Throwable {
762          ExecutorService e = new ForkJoinPool(1);
763          try {
764 <            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
764 >            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
765              shouldThrow();
766          } catch (NullPointerException success) {
767          } finally {
# Line 886 | Line 792 | public class ForkJoinPoolTest extends JS
792          ExecutorService e = new ForkJoinPool(1);
793          try {
794              e.invokeAny(new ArrayList<Callable<String>>(),
795 <                        MEDIUM_DELAY_MS, MILLISECONDS);
795 >                        MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
796              shouldThrow();
797          } catch (IllegalArgumentException success) {
798          } finally {
# Line 904 | Line 810 | public class ForkJoinPoolTest extends JS
810          l.add(latchAwaitingStringTask(latch));
811          l.add(null);
812          try {
813 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
813 >            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
814              shouldThrow();
815          } catch (NullPointerException success) {
816          } finally {
# Line 921 | Line 827 | public class ForkJoinPoolTest extends JS
827          List<Callable<String>> l = new ArrayList<Callable<String>>();
828          l.add(new NPETask());
829          try {
830 <            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
830 >            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
831              shouldThrow();
832          } catch (ExecutionException success) {
833              assertTrue(success.getCause() instanceof NullPointerException);
# Line 939 | Line 845 | public class ForkJoinPoolTest extends JS
845              List<Callable<String>> l = new ArrayList<Callable<String>>();
846              l.add(new StringTask());
847              l.add(new StringTask());
848 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
848 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
849              assertSame(TEST_STRING, result);
850          } finally {
851              joinPool(e);
# Line 952 | Line 858 | public class ForkJoinPoolTest extends JS
858      public void testTimedInvokeAll1() throws Throwable {
859          ExecutorService e = new ForkJoinPool(1);
860          try {
861 <            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
861 >            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
862              shouldThrow();
863          } catch (NullPointerException success) {
864          } finally {
# Line 984 | Line 890 | public class ForkJoinPoolTest extends JS
890          try {
891              List<Future<String>> r
892                  = e.invokeAll(new ArrayList<Callable<String>>(),
893 <                              MEDIUM_DELAY_MS, MILLISECONDS);
893 >                              MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
894              assertTrue(r.isEmpty());
895          } finally {
896              joinPool(e);
# Line 1000 | Line 906 | public class ForkJoinPoolTest extends JS
906          l.add(new StringTask());
907          l.add(null);
908          try {
909 <            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
909 >            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
910              shouldThrow();
911          } catch (NullPointerException success) {
912          } finally {
# Line 1016 | Line 922 | public class ForkJoinPoolTest extends JS
922          List<Callable<String>> l = new ArrayList<Callable<String>>();
923          l.add(new NPETask());
924          List<Future<String>> futures
925 <            = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
925 >            = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
926          assertEquals(1, futures.size());
927          try {
928              futures.get(0).get();
# Line 1038 | Line 944 | public class ForkJoinPoolTest extends JS
944              l.add(new StringTask());
945              l.add(new StringTask());
946              List<Future<String>> futures
947 <                = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
947 >                = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
948              assertEquals(2, futures.size());
949              for (Future<String> future : futures)
950                  assertSame(TEST_STRING, future.get());

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines