Search in sources :

Example 1 with Collectors.toSet

use of java8.util.stream.Collectors.toSet in project streamsupport by stefan-zobel.

the class CompletableFutureTest method testMinimalCompletionStage_minimality.

/**
 * Minimal completion stages throw UOE for most non-CompletionStage methods
 */
public void testMinimalCompletionStage_minimality() {
    if (!testImplementationDetails)
        return;
    Function<Method, String> toSignature = method -> method.getName() + Arrays.toString(method.getParameterTypes());
    Predicate<Method> isNotStatic = method -> (method.getModifiers() & Modifier.STATIC) == 0;
    List<Method> minimalMethods = RefStreams.of(Object.class, CompletionStage.class).flatMap(klazz -> RefStreams.of(klazz.getMethods())).filter(isNotStatic).collect(Collectors.toList());
    // Methods from CompletableFuture permitted NOT to throw UOE
    String[] signatureWhitelist = { "newIncompleteFuture[]", "defaultExecutor[]", "minimalCompletionStage[]", "copy[]" };
    Set<String> permittedMethodSignatures = RefStreams.concat(StreamSupport.stream(minimalMethods).map(toSignature), RefStreams.of(signatureWhitelist)).collect(Collectors.toSet());
    List<Method> allMethods = RefStreams.of(CompletableFuture.class.getMethods()).filter(isNotStatic).filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))).collect(Collectors.toList());
    List<CompletionStage<Integer>> stages = new ArrayList<CompletionStage<Integer>>();
    CompletionStage<Integer> min = new CompletableFuture<Integer>().minimalCompletionStage();
    stages.add(min);
    stages.add(min.thenApply(x -> x));
    stages.add(CompletableFuture.completedStage(1));
    stages.add(CompletableFuture.failedStage(new CFException()));
    List<Method> bugs = new ArrayList<>();
    for (Method method : allMethods) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        Object[] args = new Object[parameterTypes.length];
        // Manufacture boxed primitives for primitive params
        for (int i = 0; i < args.length; i++) {
            Class<?> type = parameterTypes[i];
            if (type == boolean.class)
                args[i] = false;
            else if (type == int.class)
                args[i] = 0;
            else if (type == long.class)
                args[i] = 0L;
        }
        for (CompletionStage<Integer> stage : stages) {
            try {
                method.invoke(stage, args);
                bugs.add(method);
            } catch (java.lang.reflect.InvocationTargetException expected) {
                if (!(expected.getCause() instanceof UnsupportedOperationException)) {
                    bugs.add(method);
                // expected.getCause().printStackTrace();
                }
            } catch (Exception bad) {
                throw new Error(bad);
            }
        }
    }
    if (!bugs.isEmpty())
        throw new Error("Methods did not throw UOE: " + bugs);
}
Also used : Arrays(java.util.Arrays) Test(junit.framework.Test) TimeoutException(java.util.concurrent.TimeoutException) Callable(java.util.concurrent.Callable) CompletableFuture.completedFuture(java8.util.concurrent.CompletableFuture.completedFuture) Predicate(java8.util.function.Predicate) AtomicReference(java.util.concurrent.atomic.AtomicReference) CompletableFuture.failedFuture(java8.util.concurrent.CompletableFuture.failedFuture) Function(java8.util.function.Function) ArrayList(java.util.ArrayList) TestSuite(junit.framework.TestSuite) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) BiFunction(java8.util.function.BiFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Consumer(java8.util.function.Consumer) ForkJoinPool(java8.util.concurrent.ForkJoinPool) Method(java.lang.reflect.Method) CompletionStage(java8.util.concurrent.CompletionStage) Objects(java8.util.Objects) CancellationException(java.util.concurrent.CancellationException) Executor(java.util.concurrent.Executor) ForkJoinTask(java8.util.concurrent.ForkJoinTask) Set(java.util.Set) Collectors(java8.util.stream.Collectors) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) StreamSupport(java8.util.stream.StreamSupport) Supplier(java8.util.function.Supplier) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) CompletableFuture(java8.util.concurrent.CompletableFuture) BiConsumer(java8.util.function.BiConsumer) RefStreams(java8.util.stream.RefStreams) Modifier(java.lang.reflect.Modifier) CompletionException(java8.util.concurrent.CompletionException) SECONDS(java.util.concurrent.TimeUnit.SECONDS) ArrayList(java.util.ArrayList) CompletionStage(java8.util.concurrent.CompletionStage) Method(java.lang.reflect.Method) TimeoutException(java.util.concurrent.TimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) CompletionException(java8.util.concurrent.CompletionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executor (java.util.concurrent.Executor)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Objects (java8.util.Objects)1 CompletableFuture (java8.util.concurrent.CompletableFuture)1 CompletableFuture.completedFuture (java8.util.concurrent.CompletableFuture.completedFuture)1 CompletableFuture.failedFuture (java8.util.concurrent.CompletableFuture.failedFuture)1