Search in sources :

Example 1 with CompletionStage

use of java8.util.concurrent.CompletionStage 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)

Example 2 with CompletionStage

use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.

the class CompletableFutureTest method testMinimalCompletionStage.

/**
 * minimalCompletionStage returns a CompletableFuture that is
 * completed normally, with the same value, when source is.
 */
public void testMinimalCompletionStage() {
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage<Integer> g = f.minimalCompletionStage();
    AtomicInteger x = new AtomicInteger(0);
    AtomicReference<Throwable> r = new AtomicReference<>();
    checkIncomplete(f);
    g.whenComplete((v, e) -> {
        if (e != null)
            r.set(e);
        else
            x.set(v);
    });
    f.complete(1);
    checkCompletedNormally(f, 1);
    assertEquals(x.get(), 1);
    assertNull(r.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 3 with CompletionStage

use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.

the class CompletableFutureTest method testToCompletableFutureGarbageRetention.

/**
 * Checks for garbage retention when MinimalStage.toCompletableFuture()
 * is invoked many times.
 * 8161600: Garbage retention when source CompletableFutures are never completed
 *
 * As of 2016-07, fails with OOME:
 * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
 */
public void testToCompletableFutureGarbageRetention() throws Throwable {
    final int n = expensiveTests ? 900_000 : 10;
    CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
    CompletionStage minimal = neverCompleted.minimalCompletionStage();
    for (int i = 0; i < n; i++) assertTrue(minimal.toCompletableFuture().cancel(true));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture) CompletionStage(java8.util.concurrent.CompletionStage)

Example 4 with CompletionStage

use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.

the class CompletableFutureTest method testMinimalCompletionStage_toCompletableFuture_mutable.

/**
 * minimalStage.toCompletableFuture() gives mutable CompletableFuture
 */
public void testMinimalCompletionStage_toCompletableFuture_mutable() {
    for (Integer v1 : new Integer[] { 1, null }) {
        CompletableFuture<Integer> f = new CompletableFuture<>();
        CompletionStage<Integer> minimal = f.minimalCompletionStage();
        CompletableFuture<Integer> g = minimal.toCompletableFuture();
        assertTrue(g.complete(v1));
        checkCompletedNormally(g, v1);
        checkIncomplete(f);
        checkIncomplete(minimal.toCompletableFuture());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture)

Example 5 with CompletionStage

use of java8.util.concurrent.CompletionStage in project streamsupport by stefan-zobel.

the class CompletableFutureTest method testMinimalCompletionStage2.

/**
 * minimalCompletionStage returns a CompletableFuture that is
 * completed exceptionally when source is.
 */
public void testMinimalCompletionStage2() {
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage<Integer> g = f.minimalCompletionStage();
    AtomicInteger x = new AtomicInteger(0);
    AtomicReference<Throwable> r = new AtomicReference<>();
    g.whenComplete((v, e) -> {
        if (e != null)
            r.set(e);
        else
            x.set(v);
    });
    checkIncomplete(f);
    CFException ex = new CFException();
    f.completeExceptionally(ex);
    checkCompletedExceptionally(f, ex);
    assertEquals(x.get(), 0);
    assertEquals(r.get().getCause(), ex);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CompletableFuture (java8.util.concurrent.CompletableFuture)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 CompletionStage (java8.util.concurrent.CompletionStage)2 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 Objects (java8.util.Objects)1 CompletableFuture.completedFuture (java8.util.concurrent.CompletableFuture.completedFuture)1