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.122 by jsr166, Sun Sep 6 22:21:07 2015 UTC vs.
Revision 1.124 by jsr166, Thu Sep 10 17:51:37 2015 UTC

# Line 8 | Line 8
8   import static java.util.concurrent.TimeUnit.MILLISECONDS;
9   import static java.util.concurrent.TimeUnit.SECONDS;
10  
11 + import java.lang.reflect.Method;
12 + import java.lang.reflect.Modifier;
13 +
14 + import java.util.stream.Collectors;
15 + import java.util.stream.Stream;
16 +
17   import java.util.ArrayList;
18 + import java.util.Arrays;
19   import java.util.List;
20   import java.util.Objects;
21 + import java.util.Set;
22   import java.util.concurrent.Callable;
23   import java.util.concurrent.CancellationException;
24   import java.util.concurrent.CompletableFuture;
# Line 28 | Line 36 | import java.util.function.BiConsumer;
36   import java.util.function.BiFunction;
37   import java.util.function.Consumer;
38   import java.util.function.Function;
39 + import java.util.function.Predicate;
40   import java.util.function.Supplier;
41  
42   import junit.framework.Test;
# Line 3688 | Line 3697 | public class CompletableFutureTest exten
3697          }
3698      }}
3699  
3700 +    /**
3701 +     * Minimal completion stages throw UOE for all non-CompletionStage methods
3702 +     */
3703 +    public void testMinimalCompletionStage_minimality() {
3704 +        if (!testImplementationDetails) return;
3705 +        Function<Method, String> toSignature =
3706 +            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3707 +        Predicate<Method> isNotStatic =
3708 +            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3709 +        List<Method> minimalMethods =
3710 +            Stream.of(Object.class, CompletionStage.class)
3711 +            .map((klazz) -> Stream.of(klazz.getMethods()))
3712 +            .reduce(Stream::concat)
3713 +            .orElseGet(Stream::empty)
3714 +            .filter(isNotStatic)
3715 +            .collect(Collectors.toList());
3716 +        // Methods from CompletableFuture permitted NOT to throw UOE
3717 +        String[] signatureWhitelist = {
3718 +            "newIncompleteFuture[]",
3719 +            "defaultExecutor[]",
3720 +            "minimalCompletionStage[]",
3721 +            "copy[]",
3722 +        };
3723 +        Set<String> permittedMethodSignatures =
3724 +            Stream.concat(minimalMethods.stream().map(toSignature),
3725 +                          Stream.of(signatureWhitelist))
3726 +            .collect(Collectors.toSet());
3727 +        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3728 +            .filter(isNotStatic)
3729 +            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3730 +            .collect(Collectors.toList());
3731 +
3732 +        CompletionStage<Integer> minimalStage =
3733 +            new CompletableFuture<Integer>().minimalCompletionStage();
3734 +
3735 +        List<Method> bugs = new ArrayList<>();
3736 +        for (Method method : allMethods) {
3737 +            Class<?>[] parameterTypes = method.getParameterTypes();
3738 +            Object[] args = new Object[parameterTypes.length];
3739 +            // Manufacture boxed primitives for primitive params
3740 +            for (int i = 0; i < args.length; i++) {
3741 +                Class<?> type = parameterTypes[i];
3742 +                if (parameterTypes[i] == boolean.class)
3743 +                    args[i] = false;
3744 +                else if (parameterTypes[i] == int.class)
3745 +                    args[i] = 0;
3746 +                else if (parameterTypes[i] == long.class)
3747 +                    args[i] = 0L;
3748 +            }
3749 +            try {
3750 +                method.invoke(minimalStage, args);
3751 +                bugs.add(method);
3752 +            }
3753 +            catch (java.lang.reflect.InvocationTargetException expected) {
3754 +                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3755 +                    bugs.add(method);
3756 +                    // expected.getCause().printStackTrace();
3757 +                }
3758 +            }
3759 +            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3760 +        }
3761 +        if (!bugs.isEmpty())
3762 +            throw new Error("Methods did not throw UOE: " + bugs.toString());
3763 +    }
3764 +
3765 + //     static <U> U join(CompletionStage<U> stage) {
3766 + //         CompletableFuture<U> f = new CompletableFuture<>();
3767 + //         stage.whenComplete((v, ex) -> {
3768 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3769 + //         });
3770 + //         return f.join();
3771 + //     }
3772 +
3773 + //     static <U> boolean isDone(CompletionStage<U> stage) {
3774 + //         CompletableFuture<U> f = new CompletableFuture<>();
3775 + //         stage.whenComplete((v, ex) -> {
3776 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3777 + //         });
3778 + //         return f.isDone();
3779 + //     }
3780 +
3781 + //     static <U> U join2(CompletionStage<U> stage) {
3782 + //         return stage.toCompletableFuture().copy().join();
3783 + //     }
3784 +
3785 + //     static <U> boolean isDone2(CompletionStage<U> stage) {
3786 + //         return stage.toCompletableFuture().copy().isDone();
3787 + //     }
3788 +
3789   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines