ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/RecursiveActionTest.java
(Generate patch)

Comparing jsr166/src/test/tck/RecursiveActionTest.java (file contents):
Revision 1.39 by jsr166, Mon Jun 3 17:55:49 2013 UTC vs.
Revision 1.51 by jsr166, Sat Oct 21 06:49:04 2017 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9 > import java.util.Arrays;
10 > import java.util.HashSet;
11   import java.util.concurrent.CancellationException;
9 import java.util.concurrent.SynchronousQueue;
12   import java.util.concurrent.ExecutionException;
13   import java.util.concurrent.ForkJoinPool;
14   import java.util.concurrent.ForkJoinTask;
15   import java.util.concurrent.ForkJoinWorkerThread;
16   import java.util.concurrent.RecursiveAction;
17 + import java.util.concurrent.SynchronousQueue;
18   import java.util.concurrent.ThreadLocalRandom;
16 import java.util.concurrent.TimeUnit;
19   import java.util.concurrent.TimeoutException;
20 < import static java.util.concurrent.TimeUnit.SECONDS;
21 < import java.util.Arrays;
22 < import java.util.HashSet;
20 >
21 > import junit.framework.Test;
22 > import junit.framework.TestSuite;
23  
24   public class RecursiveActionTest extends JSR166TestCase {
25  
26      public static void main(String[] args) {
27 <        junit.textui.TestRunner.run(suite());
27 >        main(suite(), args);
28      }
29  
30      public static Test suite() {
# Line 44 | Line 46 | public class RecursiveActionTest extends
46      }
47  
48      private void testInvokeOnPool(ForkJoinPool pool, RecursiveAction a) {
49 <        try {
49 >        try (PoolCleaner cleaner = cleaner(pool)) {
50              checkNotDone(a);
51  
52              assertNull(pool.invoke(a));
53  
54              checkCompletedNormally(a);
53        } finally {
54            joinPool(pool);
55          }
56      }
57  
# Line 73 | Line 73 | public class RecursiveActionTest extends
73  
74              Thread.currentThread().interrupt();
75              try {
76 <                a.get(5L, SECONDS);
76 >                a.get(randomTimeout(), randomTimeUnit());
77                  shouldThrow();
78              } catch (InterruptedException success) {
79              } catch (Throwable fail) { threadUnexpectedException(fail); }
80          }
81  
82          try {
83 <            a.get(0L, SECONDS);
83 >            a.get(randomExpiredTimeout(), randomTimeUnit());
84              shouldThrow();
85          } catch (TimeoutException success) {
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 98 | Line 98 | public class RecursiveActionTest extends
98          assertFalse(a.cancel(true));
99          try {
100              assertNull(a.get());
101 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
102 <        try {
103 <            assertNull(a.get(5L, SECONDS));
104 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
101 >            assertNull(a.get(randomTimeout(), randomTimeUnit()));
102 >        } catch (Exception fail) { threadUnexpectedException(fail); }
103      }
104  
105      void checkCancelled(RecursiveAction a) {
# Line 125 | Line 123 | public class RecursiveActionTest extends
123          } catch (Throwable fail) { threadUnexpectedException(fail); }
124  
125          try {
126 <            a.get(5L, SECONDS);
126 >            a.get(randomTimeout(), randomTimeUnit());
127              shouldThrow();
128          } catch (CancellationException success) {
129          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 156 | Line 154 | public class RecursiveActionTest extends
154          } catch (Throwable fail) { threadUnexpectedException(fail); }
155  
156          try {
157 <            a.get(5L, SECONDS);
157 >            a.get(randomTimeout(), randomTimeUnit());
158              shouldThrow();
159          } catch (ExecutionException success) {
160              assertSame(t.getClass(), success.getCause().getClass());
# Line 168 | Line 166 | public class RecursiveActionTest extends
166          public FJException(Throwable cause) { super(cause); }
167      }
168  
169 <    // A simple recursive action for testing
169 >    /** A simple recursive action for testing. */
170      final class FibAction extends CheckedRecursiveAction {
171          final int number;
172          int result;
# Line 186 | Line 184 | public class RecursiveActionTest extends
184          }
185      }
186  
187 <    // A recursive action failing in base case
187 >    /** A recursive action failing in base case. */
188      static final class FailingFibAction extends RecursiveAction {
189          final int number;
190          int result;
# Line 258 | Line 256 | public class RecursiveActionTest extends
256          RecursiveAction a = new CheckedRecursiveAction() {
257              protected void realCompute() {
258                  FibAction f = new FibAction(8);
259 <                final Thread myself = Thread.currentThread();
259 >                final Thread currentThread = Thread.currentThread();
260  
261                  // test join()
262                  assertSame(f, f.fork());
263 <                myself.interrupt();
266 <                assertTrue(myself.isInterrupted());
263 >                currentThread.interrupt();
264                  assertNull(f.join());
265                  Thread.interrupted();
266                  assertEquals(21, f.result);
# Line 272 | Line 269 | public class RecursiveActionTest extends
269                  f = new FibAction(8);
270                  f.cancel(true);
271                  assertSame(f, f.fork());
272 <                myself.interrupt();
276 <                assertTrue(myself.isInterrupted());
272 >                currentThread.interrupt();
273                  try {
274                      f.join();
275                      shouldThrow();
# Line 285 | Line 281 | public class RecursiveActionTest extends
281                  f = new FibAction(8);
282                  f.completeExceptionally(new FJException());
283                  assertSame(f, f.fork());
284 <                myself.interrupt();
289 <                assertTrue(myself.isInterrupted());
284 >                currentThread.interrupt();
285                  try {
286                      f.join();
287                      shouldThrow();
# Line 298 | Line 293 | public class RecursiveActionTest extends
293                  // test quietlyJoin()
294                  f = new FibAction(8);
295                  assertSame(f, f.fork());
296 <                myself.interrupt();
302 <                assertTrue(myself.isInterrupted());
296 >                currentThread.interrupt();
297                  f.quietlyJoin();
298                  Thread.interrupted();
299                  assertEquals(21, f.result);
# Line 308 | Line 302 | public class RecursiveActionTest extends
302                  f = new FibAction(8);
303                  f.cancel(true);
304                  assertSame(f, f.fork());
305 <                myself.interrupt();
312 <                assertTrue(myself.isInterrupted());
305 >                currentThread.interrupt();
306                  f.quietlyJoin();
307                  Thread.interrupted();
308                  checkCancelled(f);
# Line 317 | Line 310 | public class RecursiveActionTest extends
310                  f = new FibAction(8);
311                  f.completeExceptionally(new FJException());
312                  assertSame(f, f.fork());
313 <                myself.interrupt();
321 <                assertTrue(myself.isInterrupted());
313 >                currentThread.interrupt();
314                  f.quietlyJoin();
315                  Thread.interrupted();
316                  checkCompletedAbnormally(f, f.getException());
# Line 358 | Line 350 | public class RecursiveActionTest extends
350              public void realRun() throws InterruptedException {
351                  FibAction[] fibActions = sq.take();
352                  FibAction f;
353 <                final Thread myself = Thread.currentThread();
353 >                final Thread currentThread = Thread.currentThread();
354  
355                  // test join() ------------
356  
357                  f = fibActions[0];
358                  assertFalse(ForkJoinTask.inForkJoinPool());
359 <                myself.interrupt();
368 <                assertTrue(myself.isInterrupted());
359 >                currentThread.interrupt();
360                  assertNull(f.join());
361                  assertTrue(Thread.interrupted());
362                  assertEquals(21, f.result);
363                  checkCompletedNormally(f);
364  
365                  f = fibActions[1];
366 <                myself.interrupt();
376 <                assertTrue(myself.isInterrupted());
366 >                currentThread.interrupt();
367                  try {
368                      f.join();
369                      shouldThrow();
# Line 383 | Line 373 | public class RecursiveActionTest extends
373                  }
374  
375                  f = fibActions[2];
376 <                myself.interrupt();
387 <                assertTrue(myself.isInterrupted());
376 >                currentThread.interrupt();
377                  try {
378                      f.join();
379                      shouldThrow();
# Line 396 | Line 385 | public class RecursiveActionTest extends
385                  // test quietlyJoin() ---------
386  
387                  f = fibActions[3];
388 <                myself.interrupt();
400 <                assertTrue(myself.isInterrupted());
388 >                currentThread.interrupt();
389                  f.quietlyJoin();
390                  assertTrue(Thread.interrupted());
391                  assertEquals(21, f.result);
392                  checkCompletedNormally(f);
393  
394                  f = fibActions[4];
395 <                myself.interrupt();
408 <                assertTrue(myself.isInterrupted());
395 >                currentThread.interrupt();
396                  f.quietlyJoin();
397                  assertTrue(Thread.interrupted());
398                  checkCancelled(f);
399  
400                  f = fibActions[5];
401 <                myself.interrupt();
415 <                assertTrue(myself.isInterrupted());
401 >                currentThread.interrupt();
402                  f.quietlyJoin();
403                  assertTrue(Thread.interrupted());
404                  assertTrue(f.getException() instanceof FJException);
# Line 423 | Line 409 | public class RecursiveActionTest extends
409  
410          t = newStartedThread(r);
411          testInvokeOnPool(mainPool(), a);
412 <        awaitTermination(t, LONG_DELAY_MS);
412 >        awaitTermination(t);
413  
414          a.reinitialize();
415          t = newStartedThread(r);
416          testInvokeOnPool(singletonPool(), a);
417 <        awaitTermination(t, LONG_DELAY_MS);
417 >        awaitTermination(t);
418      }
419  
420      /**
# Line 454 | Line 440 | public class RecursiveActionTest extends
440              protected void realCompute() throws Exception {
441                  FibAction f = new FibAction(8);
442                  assertSame(f, f.fork());
443 <                assertNull(f.get(5L, SECONDS));
443 >                assertNull(f.get(LONG_DELAY_MS, MILLISECONDS));
444                  assertEquals(21, f.result);
445                  checkCompletedNormally(f);
446              }};
# Line 470 | Line 456 | public class RecursiveActionTest extends
456                  FibAction f = new FibAction(8);
457                  assertSame(f, f.fork());
458                  try {
459 <                    f.get(5L, null);
459 >                    f.get(randomTimeout(), null);
460                      shouldThrow();
461                  } catch (NullPointerException success) {}
462              }};
# Line 502 | Line 488 | public class RecursiveActionTest extends
488                  FibAction f = new FibAction(8);
489                  assertSame(f, f.fork());
490                  helpQuiesce();
491 +                while (!f.isDone()) // wait out race
492 +                    ;
493                  assertEquals(21, f.result);
494                  assertEquals(0, getQueuedTaskCount());
495                  checkCompletedNormally(f);
# Line 587 | Line 575 | public class RecursiveActionTest extends
575                  FailingFibAction f = new FailingFibAction(8);
576                  assertSame(f, f.fork());
577                  try {
578 <                    f.get(5L, TimeUnit.SECONDS);
578 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
579                      shouldThrow();
580                  } catch (ExecutionException success) {
581                      Throwable cause = success.getCause();
# Line 679 | Line 667 | public class RecursiveActionTest extends
667                  assertTrue(f.cancel(true));
668                  assertSame(f, f.fork());
669                  try {
670 <                    f.get(5L, SECONDS);
670 >                    f.get(LONG_DELAY_MS, MILLISECONDS);
671                      shouldThrow();
672                  } catch (CancellationException success) {
673                      checkCancelled(f);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines