< prev index next >

test/jdk/java/util/concurrent/tck/CompletableFutureTest.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


3762         CompletableFuture<Integer> f = new CompletableFuture<>();
3763         Executor e = f.defaultExecutor();
3764         Executor c = ForkJoinPool.commonPool();
3765         if (ForkJoinPool.getCommonPoolParallelism() > 1)
3766             assertSame(e, c);
3767         else
3768             assertNotSame(e, c);
3769     }
3770 
3771     /**
3772      * failedFuture returns a CompletableFuture completed
3773      * exceptionally with the given Exception
3774      */
3775     public void testFailedFuture() {
3776         CFException ex = new CFException();
3777         CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3778         checkCompletedExceptionally(f, ex);
3779     }
3780 
3781     /**
3782      * failedFuture(null) throws NPE
3783      */
3784     public void testFailedFuture_null() {
3785         try {
3786             CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3787             shouldThrow();
3788         } catch (NullPointerException success) {}
3789     }
3790 
3791     /**
3792      * copy returns a CompletableFuture that is completed normally,
3793      * with the same value, when source is.
3794      */
3795     public void testCopy_normalCompletion() {
3796         for (boolean createIncomplete : new boolean[] { true, false })
3797         for (Integer v1 : new Integer[] { 1, null })
3798     {
3799         CompletableFuture<Integer> f = new CompletableFuture<>();
3800         if (!createIncomplete) assertTrue(f.complete(v1));
3801         CompletableFuture<Integer> g = f.copy();
3802         if (createIncomplete) {
3803             checkIncomplete(f);
3804             checkIncomplete(g);
3805             assertTrue(f.complete(v1));
3806         }
3807         checkCompletedNormally(f, v1);
3808         checkCompletedNormally(g, v1);
3809     }}
3810 
3811     /**


4200         List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
4201             .filter(isNotStatic)
4202             .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4203             .collect(Collectors.toList());
4204 
4205         List<CompletionStage<Integer>> stages = new ArrayList<>();
4206         CompletionStage<Integer> min =
4207             new CompletableFuture<Integer>().minimalCompletionStage();
4208         stages.add(min);
4209         stages.add(min.thenApply(x -> x));
4210         stages.add(CompletableFuture.completedStage(1));
4211         stages.add(CompletableFuture.failedStage(new CFException()));
4212 
4213         List<Method> bugs = new ArrayList<>();
4214         for (Method method : allMethods) {
4215             Class<?>[] parameterTypes = method.getParameterTypes();
4216             Object[] args = new Object[parameterTypes.length];
4217             // Manufacture boxed primitives for primitive params
4218             for (int i = 0; i < args.length; i++) {
4219                 Class<?> type = parameterTypes[i];
4220                 if (parameterTypes[i] == boolean.class)
4221                     args[i] = false;
4222                 else if (parameterTypes[i] == int.class)
4223                     args[i] = 0;
4224                 else if (parameterTypes[i] == long.class)
4225                     args[i] = 0L;
4226             }
4227             for (CompletionStage<Integer> stage : stages) {
4228                 try {
4229                     method.invoke(stage, args);
4230                     bugs.add(method);
4231                 }
4232                 catch (java.lang.reflect.InvocationTargetException expected) {
4233                     if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4234                         bugs.add(method);
4235                         // expected.getCause().printStackTrace();
4236                     }
4237                 }
4238                 catch (ReflectiveOperationException bad) { throw new Error(bad); }
4239             }
4240         }
4241         if (!bugs.isEmpty())
4242             throw new Error("Methods did not throw UOE: " + bugs);
4243     }
4244 
4245     /**




3762         CompletableFuture<Integer> f = new CompletableFuture<>();
3763         Executor e = f.defaultExecutor();
3764         Executor c = ForkJoinPool.commonPool();
3765         if (ForkJoinPool.getCommonPoolParallelism() > 1)
3766             assertSame(e, c);
3767         else
3768             assertNotSame(e, c);
3769     }
3770 
3771     /**
3772      * failedFuture returns a CompletableFuture completed
3773      * exceptionally with the given Exception
3774      */
3775     public void testFailedFuture() {
3776         CFException ex = new CFException();
3777         CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3778         checkCompletedExceptionally(f, ex);
3779     }
3780 
3781     /**










3782      * copy returns a CompletableFuture that is completed normally,
3783      * with the same value, when source is.
3784      */
3785     public void testCopy_normalCompletion() {
3786         for (boolean createIncomplete : new boolean[] { true, false })
3787         for (Integer v1 : new Integer[] { 1, null })
3788     {
3789         CompletableFuture<Integer> f = new CompletableFuture<>();
3790         if (!createIncomplete) assertTrue(f.complete(v1));
3791         CompletableFuture<Integer> g = f.copy();
3792         if (createIncomplete) {
3793             checkIncomplete(f);
3794             checkIncomplete(g);
3795             assertTrue(f.complete(v1));
3796         }
3797         checkCompletedNormally(f, v1);
3798         checkCompletedNormally(g, v1);
3799     }}
3800 
3801     /**


4190         List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
4191             .filter(isNotStatic)
4192             .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4193             .collect(Collectors.toList());
4194 
4195         List<CompletionStage<Integer>> stages = new ArrayList<>();
4196         CompletionStage<Integer> min =
4197             new CompletableFuture<Integer>().minimalCompletionStage();
4198         stages.add(min);
4199         stages.add(min.thenApply(x -> x));
4200         stages.add(CompletableFuture.completedStage(1));
4201         stages.add(CompletableFuture.failedStage(new CFException()));
4202 
4203         List<Method> bugs = new ArrayList<>();
4204         for (Method method : allMethods) {
4205             Class<?>[] parameterTypes = method.getParameterTypes();
4206             Object[] args = new Object[parameterTypes.length];
4207             // Manufacture boxed primitives for primitive params
4208             for (int i = 0; i < args.length; i++) {
4209                 Class<?> type = parameterTypes[i];
4210                 if      (type == boolean.class) args[i] = false;
4211                 else if (type == int.class)     args[i] = 0;
4212                 else if (type == long.class)    args[i] = 0L;



4213             }
4214             for (CompletionStage<Integer> stage : stages) {
4215                 try {
4216                     method.invoke(stage, args);
4217                     bugs.add(method);
4218                 }
4219                 catch (java.lang.reflect.InvocationTargetException expected) {
4220                     if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4221                         bugs.add(method);
4222                         // expected.getCause().printStackTrace();
4223                     }
4224                 }
4225                 catch (ReflectiveOperationException bad) { throw new Error(bad); }
4226             }
4227         }
4228         if (!bugs.isEmpty())
4229             throw new Error("Methods did not throw UOE: " + bugs);
4230     }
4231 
4232     /**


< prev index next >