jdk/test/java/util/concurrent/tck/RecursiveTaskTest.java
changeset 45937 646816090183
parent 44589 64d9270bd24c
equal deleted inserted replaced
45936:d564ef4bed8f 45937:646816090183
    29  * Written by Doug Lea with assistance from members of JCP JSR-166
    29  * Written by Doug Lea with assistance from members of JCP JSR-166
    30  * Expert Group and released to the public domain, as explained at
    30  * Expert Group and released to the public domain, as explained at
    31  * http://creativecommons.org/publicdomain/zero/1.0/
    31  * http://creativecommons.org/publicdomain/zero/1.0/
    32  */
    32  */
    33 
    33 
    34 import static java.util.concurrent.TimeUnit.SECONDS;
    34 import static java.util.concurrent.TimeUnit.MILLISECONDS;
    35 
    35 
    36 import java.util.HashSet;
    36 import java.util.HashSet;
    37 import java.util.concurrent.CancellationException;
    37 import java.util.concurrent.CancellationException;
    38 import java.util.concurrent.ExecutionException;
    38 import java.util.concurrent.ExecutionException;
    39 import java.util.concurrent.ForkJoinPool;
    39 import java.util.concurrent.ForkJoinPool;
    94             } catch (InterruptedException success) {
    94             } catch (InterruptedException success) {
    95             } catch (Throwable fail) { threadUnexpectedException(fail); }
    95             } catch (Throwable fail) { threadUnexpectedException(fail); }
    96 
    96 
    97             Thread.currentThread().interrupt();
    97             Thread.currentThread().interrupt();
    98             try {
    98             try {
    99                 a.get(5L, SECONDS);
    99                 a.get(randomTimeout(), randomTimeUnit());
   100                 shouldThrow();
   100                 shouldThrow();
   101             } catch (InterruptedException success) {
   101             } catch (InterruptedException success) {
   102             } catch (Throwable fail) { threadUnexpectedException(fail); }
   102             } catch (Throwable fail) { threadUnexpectedException(fail); }
   103         }
   103         }
   104 
   104 
   105         try {
   105         try {
   106             a.get(0L, SECONDS);
   106             a.get(randomExpiredTimeout(), randomTimeUnit());
   107             shouldThrow();
   107             shouldThrow();
   108         } catch (TimeoutException success) {
   108         } catch (TimeoutException success) {
   109         } catch (Throwable fail) { threadUnexpectedException(fail); }
   109         } catch (Throwable fail) { threadUnexpectedException(fail); }
   110     }
   110     }
   111 
   111 
   119         assertSame(expected, a.join());
   119         assertSame(expected, a.join());
   120         assertFalse(a.cancel(false));
   120         assertFalse(a.cancel(false));
   121         assertFalse(a.cancel(true));
   121         assertFalse(a.cancel(true));
   122         try {
   122         try {
   123             assertSame(expected, a.get());
   123             assertSame(expected, a.get());
   124         } catch (Throwable fail) { threadUnexpectedException(fail); }
   124             assertSame(expected, a.get(randomTimeout(), randomTimeUnit()));
   125         try {
       
   126             assertSame(expected, a.get(5L, SECONDS));
       
   127         } catch (Throwable fail) { threadUnexpectedException(fail); }
   125         } catch (Throwable fail) { threadUnexpectedException(fail); }
   128     }
   126     }
   129 
   127 
   130     /**
   128     /**
   131      * Waits for the task to complete, and checks that when it does,
   129      * Waits for the task to complete, and checks that when it does,
   166             shouldThrow();
   164             shouldThrow();
   167         } catch (CancellationException success) {
   165         } catch (CancellationException success) {
   168         } catch (Throwable fail) { threadUnexpectedException(fail); }
   166         } catch (Throwable fail) { threadUnexpectedException(fail); }
   169 
   167 
   170         try {
   168         try {
   171             a.get(5L, SECONDS);
   169             a.get(randomTimeout(), randomTimeUnit());
   172             shouldThrow();
   170             shouldThrow();
   173         } catch (CancellationException success) {
   171         } catch (CancellationException success) {
   174         } catch (Throwable fail) { threadUnexpectedException(fail); }
   172         } catch (Throwable fail) { threadUnexpectedException(fail); }
   175     }
   173     }
   176 
   174 
   197         } catch (ExecutionException success) {
   195         } catch (ExecutionException success) {
   198             assertSame(t.getClass(), success.getCause().getClass());
   196             assertSame(t.getClass(), success.getCause().getClass());
   199         } catch (Throwable fail) { threadUnexpectedException(fail); }
   197         } catch (Throwable fail) { threadUnexpectedException(fail); }
   200 
   198 
   201         try {
   199         try {
   202             a.get(5L, SECONDS);
   200             a.get(randomTimeout(), randomTimeUnit());
   203             shouldThrow();
   201             shouldThrow();
   204         } catch (ExecutionException success) {
   202         } catch (ExecutionException success) {
   205             assertSame(t.getClass(), success.getCause().getClass());
   203             assertSame(t.getClass(), success.getCause().getClass());
   206         } catch (Throwable fail) { threadUnexpectedException(fail); }
   204         } catch (Throwable fail) { threadUnexpectedException(fail); }
   207     }
   205     }
   318     public void testForkTimedGet() {
   316     public void testForkTimedGet() {
   319         RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
   317         RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
   320             public Integer realCompute() throws Exception {
   318             public Integer realCompute() throws Exception {
   321                 FibTask f = new FibTask(8);
   319                 FibTask f = new FibTask(8);
   322                 assertSame(f, f.fork());
   320                 assertSame(f, f.fork());
   323                 Integer r = f.get(5L, SECONDS);
   321                 Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
   324                 assertEquals(21, (int) r);
   322                 assertEquals(21, (int) r);
   325                 checkCompletedNormally(f, r);
   323                 checkCompletedNormally(f, r);
   326                 return r;
   324                 return r;
   327             }};
   325             }};
   328         assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
   326         assertEquals(21, (int) testInvokeOnPool(mainPool(), a));
   444         RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
   442         RecursiveTask<Integer> a = new CheckedRecursiveTask<Integer>() {
   445             public Integer realCompute() throws Exception {
   443             public Integer realCompute() throws Exception {
   446                 FailingFibTask f = new FailingFibTask(8);
   444                 FailingFibTask f = new FailingFibTask(8);
   447                 assertSame(f, f.fork());
   445                 assertSame(f, f.fork());
   448                 try {
   446                 try {
   449                     Integer r = f.get(5L, SECONDS);
   447                     Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
   450                     shouldThrow();
   448                     shouldThrow();
   451                 } catch (ExecutionException success) {
   449                 } catch (ExecutionException success) {
   452                     Throwable cause = success.getCause();
   450                     Throwable cause = success.getCause();
   453                     assertTrue(cause instanceof FJException);
   451                     assertTrue(cause instanceof FJException);
   454                     checkCompletedAbnormally(f, cause);
   452                     checkCompletedAbnormally(f, cause);
   541             public Integer realCompute() throws Exception {
   539             public Integer realCompute() throws Exception {
   542                 FibTask f = new FibTask(8);
   540                 FibTask f = new FibTask(8);
   543                 assertTrue(f.cancel(true));
   541                 assertTrue(f.cancel(true));
   544                 assertSame(f, f.fork());
   542                 assertSame(f, f.fork());
   545                 try {
   543                 try {
   546                     Integer r = f.get(5L, SECONDS);
   544                     Integer r = f.get(LONG_DELAY_MS, MILLISECONDS);
   547                     shouldThrow();
   545                     shouldThrow();
   548                 } catch (CancellationException success) {
   546                 } catch (CancellationException success) {
   549                     checkCancelled(f);
   547                     checkCancelled(f);
   550                 }
   548                 }
   551                 return NoResult;
   549                 return NoResult;