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

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.154 by jsr166, Sun Jun 26 19:27:42 2016 UTC vs.
Revision 1.161 by jsr166, Tue Jun 28 14:49:48 2016 UTC

# Line 550 | Line 550 | public class CompletableFutureTest exten
550          }
551      }
552  
553 +    static class CountingRejectingExecutor implements Executor {
554 +        final RejectedExecutionException ex = new RejectedExecutionException();
555 +        final AtomicInteger count = new AtomicInteger(0);
556 +        public void execute(Runnable r) {
557 +            count.getAndIncrement();
558 +            throw ex;
559 +        }
560 +    }
561 +
562      // Used for explicit executor tests
563      static final class ThreadExecutor implements Executor {
564          final AtomicInteger count = new AtomicInteger(0);
# Line 1234 | Line 1243 | public class CompletableFutureTest exten
1243          r.assertInvoked();
1244      }}
1245  
1246 +    public void testRunAsync_rejectingExecutor() {
1247 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1248 +        try {
1249 +            CompletableFuture.runAsync(() -> {}, e);
1250 +            shouldThrow();
1251 +        } catch (Throwable t) {
1252 +            assertSame(e.ex, t);
1253 +        }
1254 +
1255 +        assertEquals(1, e.count.get());
1256 +    }
1257 +
1258      /**
1259       * supplyAsync completes with result of supplier
1260       */
# Line 1268 | Line 1289 | public class CompletableFutureTest exten
1289          r.assertInvoked();
1290      }}
1291  
1292 +    public void testSupplyAsync_rejectingExecutor() {
1293 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1294 +        try {
1295 +            CompletableFuture.supplyAsync(() -> null, e);
1296 +            shouldThrow();
1297 +        } catch (Throwable t) {
1298 +            assertSame(e.ex, t);
1299 +        }
1300 +
1301 +        assertEquals(1, e.count.get());
1302 +    }
1303 +
1304      // seq completion methods
1305  
1306      /**
# Line 3317 | Line 3350 | public class CompletableFutureTest exten
3350          assertEquals(0, exec.count.get());
3351      }
3352  
3320    static class CountingRejectingExecutor implements Executor {
3321        final RejectedExecutionException ex = new RejectedExecutionException();
3322        final AtomicInteger count = new AtomicInteger(0);
3323        public void execute(Runnable r) {
3324            count.getAndIncrement();
3325            throw ex;
3326        }
3327    }
3328
3353      /**
3354       * Test submissions to an executor that rejects all tasks.
3355       */
3356      public void testRejectingExecutor() {
3357 <        for (Integer v : new Integer[] { 1, null }) {
3358 <
3357 >        for (Integer v : new Integer[] { 1, null })
3358 >    {
3359          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3360  
3361          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
# Line 3410 | Line 3434 | public class CompletableFutureTest exten
3434              checkCompletedWithWrappedException(future, e.ex);
3435  
3436          assertEquals(futures.size(), e.count.get());
3437 <
3414 <        }
3415 <    }
3437 >    }}
3438  
3439      /**
3440       * Test submissions to an executor that rejects all tasks, but
# Line 3420 | Line 3442 | public class CompletableFutureTest exten
3442       * explicitly completed.
3443       */
3444      public void testRejectingExecutorNeverInvoked() {
3445 +        for (Integer v : new Integer[] { 1, null })
3446 +    {
3447          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3448  
3425        for (Integer v : new Integer[] { 1, null }) {
3426
3449          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3450          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3451  
# Line 3471 | Line 3493 | public class CompletableFutureTest exten
3493              checkCompletedNormally(future, null);
3494  
3495          assertEquals(0, e.count.get());
3496 <
3475 <        }
3476 <    }
3496 >    }}
3497  
3498      /**
3499       * toCompletableFuture returns this CompletableFuture.
# Line 4119 | Line 4139 | public class CompletableFutureTest exten
4139       * A demo of scalability - runtime is O(n).
4140       */
4141      public void testManyDependents() throws Throwable {
4142 <        final int n = 1_000;
4142 >        final int n = expensiveTests ? 1_000_000 : 10;
4143          final CompletableFuture<Void> head = new CompletableFuture<>();
4144          final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
4145          final AtomicInteger count = new AtomicInteger(0);
# Line 4146 | Line 4166 | public class CompletableFutureTest exten
4166          assertEquals(5 * 3 * n, count.get());
4167      }
4168  
4169 +    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4170 +    public void testCoCompletionGarbageRetention() throws Throwable {
4171 +        final int n = expensiveTests ? 1_000_000 : 10;
4172 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4173 +        CompletableFuture<Integer> f;
4174 +        for (int i = 0; i < n; i++) {
4175 +            f = new CompletableFuture<>();
4176 +            f.runAfterEither(incomplete, () -> {});
4177 +            f.complete(null);
4178 +
4179 +            f = new CompletableFuture<>();
4180 +            f.acceptEither(incomplete, (x) -> {});
4181 +            f.complete(null);
4182 +
4183 +            f = new CompletableFuture<>();
4184 +            f.applyToEither(incomplete, (x) -> x);
4185 +            f.complete(null);
4186 +
4187 +            f = new CompletableFuture<>();
4188 +            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4189 +            f.complete(null);
4190 +        }
4191 +
4192 +        for (int i = 0; i < n; i++) {
4193 +            f = new CompletableFuture<>();
4194 +            incomplete.runAfterEither(f, () -> {});
4195 +            f.complete(null);
4196 +
4197 +            f = new CompletableFuture<>();
4198 +            incomplete.acceptEither(f, (x) -> {});
4199 +            f.complete(null);
4200 +
4201 +            f = new CompletableFuture<>();
4202 +            incomplete.applyToEither(f, (x) -> x);
4203 +            f.complete(null);
4204 +
4205 +            f = new CompletableFuture<>();
4206 +            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4207 +            f.complete(null);
4208 +        }
4209 +    }
4210 +
4211 +    /*
4212 +     * Tests below currently fail in stress mode due to memory retention.
4213 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck
4214 +     */
4215 +
4216 +    /** Checks for garbage retention with anyOf. */
4217 +    public void testAnyOfGarbageRetention() throws Throwable {
4218 +        for (Integer v : new Integer[] { 1, null })
4219 +    {
4220 +        final int n = expensiveTests ? 100_000 : 10;
4221 +        CompletableFuture<Integer>[] fs
4222 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4223 +        for (int i = 0; i < fs.length; i++)
4224 +            fs[i] = new CompletableFuture<>();
4225 +        fs[fs.length - 1].complete(v);
4226 +        for (int i = 0; i < n; i++)
4227 +            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4228 +    }}
4229 +
4230 +    /** Checks for garbage retention with allOf. */
4231 +    public void testCancelledAllOfGarbageRetention() throws Throwable {
4232 +        final int n = expensiveTests ? 100_000 : 10;
4233 +        CompletableFuture<Integer>[] fs
4234 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4235 +        for (int i = 0; i < fs.length; i++)
4236 +            fs[i] = new CompletableFuture<>();
4237 +        for (int i = 0; i < n; i++)
4238 +            assertTrue(CompletableFuture.allOf(fs).cancel(false));
4239 +    }
4240 +
4241   //     static <U> U join(CompletionStage<U> stage) {
4242   //         CompletableFuture<U> f = new CompletableFuture<>();
4243   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines