Search in sources :

Example 31 with CompletableFuture

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

the class CompletableFutureTest method testRejectingExecutorNeverInvoked.

/**
 * Test submissions to an executor that rejects all tasks, but
 * should never be invoked because the dependent future is
 * explicitly completed.
 */
public void testRejectingExecutorNeverInvoked() {
    final CountingRejectingExecutor e = new CountingRejectingExecutor();
    for (Integer v : new Integer[] { 1, null }) {
        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
        // List<CompletableFuture<?>> futures = new ArrayList<>();
        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
        srcs.add(complete);
        srcs.add(incomplete);
        List<CompletableFuture<?>> fs = new ArrayList<>();
        fs.add(incomplete.thenRunAsync(() -> {
        }, e));
        fs.add(incomplete.thenAcceptAsync(z -> {
        }, e));
        fs.add(incomplete.thenApplyAsync(z -> z, e));
        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {
        }, e));
        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {
        }, e));
        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {
        }, e));
        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {
        }, e));
        fs.add(incomplete.thenComposeAsync(z -> null, e));
        fs.add(incomplete.whenCompleteAsync((z, t) -> {
        }, e));
        fs.add(incomplete.handleAsync((z, t) -> null, e));
        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {
        }, e));
        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {
        }, e));
        fs.add(complete.runAfterBothAsync(incomplete, () -> {
        }, e));
        fs.add(incomplete.runAfterBothAsync(complete, () -> {
        }, e));
        for (CompletableFuture<?> future : fs) checkIncomplete(future);
        for (CompletableFuture<?> future : fs) future.complete(null);
        incomplete.complete(v);
        for (CompletableFuture<?> future : fs) checkCompletedNormally(future, null);
        assertEquals(0, e.count.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) CompletableFuture(java8.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList)

Example 32 with CompletableFuture

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

the class CompletableFutureTest method testApplyToEither_sourceCancelled.

/**
 * applyToEither result completes exceptionally if either source cancelled
 */
public void testApplyToEither_sourceCancelled() {
    for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
        final CompletableFuture<Integer> f = new CompletableFuture<>();
        final CompletableFuture<Integer> g = new CompletableFuture<>();
        final IncFunction[] rs = new IncFunction[6];
        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
        checkIncomplete(h0);
        checkIncomplete(h1);
        rs[0].assertNotInvoked();
        rs[1].assertNotInvoked();
        f.cancel(mayInterruptIfRunning);
        checkCompletedWithWrappedCancellationException(h0);
        checkCompletedWithWrappedCancellationException(h1);
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
        checkCompletedWithWrappedCancellationException(h2);
        checkCompletedWithWrappedCancellationException(h3);
        g.complete(v1);
        // unspecified behavior - both source completions available
        final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
        final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
        try {
            assertEquals(inc(v1), h4.join());
            rs[4].assertValue(inc(v1));
        } catch (CompletionException ok) {
            checkCompletedWithWrappedCancellationException(h4);
            rs[4].assertNotInvoked();
        }
        try {
            assertEquals(inc(v1), h5.join());
            rs[5].assertValue(inc(v1));
        } catch (CompletionException ok) {
            checkCompletedWithWrappedCancellationException(h5);
            rs[5].assertNotInvoked();
        }
        checkCancelled(f);
        checkCompletedNormally(g, v1);
        checkCompletedWithWrappedCancellationException(h0);
        checkCompletedWithWrappedCancellationException(h1);
        checkCompletedWithWrappedCancellationException(h2);
        checkCompletedWithWrappedCancellationException(h3);
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture) CompletionException(java8.util.concurrent.CompletionException)

Example 33 with CompletableFuture

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

the class CompletableFutureTest method testAcceptEither_sourceCancelled.

/**
 * acceptEither result completes exceptionally if either source cancelled
 */
public void testAcceptEither_sourceCancelled() {
    for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
        final CompletableFuture<Integer> f = new CompletableFuture<>();
        final CompletableFuture<Integer> g = new CompletableFuture<>();
        final NoopConsumer[] rs = new NoopConsumer[6];
        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
        final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
        final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
        checkIncomplete(h0);
        checkIncomplete(h1);
        rs[0].assertNotInvoked();
        rs[1].assertNotInvoked();
        f.cancel(mayInterruptIfRunning);
        checkCompletedWithWrappedCancellationException(h0);
        checkCompletedWithWrappedCancellationException(h1);
        final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
        final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
        checkCompletedWithWrappedCancellationException(h2);
        checkCompletedWithWrappedCancellationException(h3);
        g.complete(v1);
        // unspecified behavior - both source completions available
        final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
        final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
        try {
            assertNull(h4.join());
            rs[4].assertValue(v1);
        } catch (CompletionException ok) {
            checkCompletedWithWrappedCancellationException(h4);
            rs[4].assertNotInvoked();
        }
        try {
            assertNull(h5.join());
            rs[5].assertValue(v1);
        } catch (CompletionException ok) {
            checkCompletedWithWrappedCancellationException(h5);
            rs[5].assertNotInvoked();
        }
        checkCancelled(f);
        checkCompletedNormally(g, v1);
        checkCompletedWithWrappedCancellationException(h0);
        checkCompletedWithWrappedCancellationException(h1);
        checkCompletedWithWrappedCancellationException(h2);
        checkCompletedWithWrappedCancellationException(h3);
        for (int i = 0; i < 4; i++) rs[i].assertNotInvoked();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture) CompletionException(java8.util.concurrent.CompletionException)

Example 34 with CompletableFuture

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

Example 35 with CompletableFuture

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

the class CompletableFutureTest method testRunAfterBoth_sourceCancelled.

/**
 * runAfterBoth result completes exceptionally if either source cancelled
 */
public void testRunAfterBoth_sourceCancelled() throws Throwable {
    for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (boolean fFirst : new boolean[] { true, false }) for (boolean failFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
        final CompletableFuture<Integer> f = new CompletableFuture<>();
        final CompletableFuture<Integer> g = new CompletableFuture<>();
        final Noop r1 = new Noop(m);
        final Noop r2 = new Noop(m);
        final Noop r3 = new Noop(m);
        final CompletableFuture<Integer> fst = fFirst ? f : g;
        final CompletableFuture<Integer> snd = !fFirst ? f : g;
        final Callable<Boolean> complete1 = failFirst ? () -> fst.cancel(mayInterruptIfRunning) : () -> fst.complete(v1);
        final Callable<Boolean> complete2 = failFirst ? () -> snd.complete(v1) : () -> snd.cancel(mayInterruptIfRunning);
        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
        assertTrue(complete1.call());
        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
        checkIncomplete(h1);
        checkIncomplete(h2);
        assertTrue(complete2.call());
        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
        checkCompletedWithWrappedCancellationException(h1);
        checkCompletedWithWrappedCancellationException(h2);
        checkCompletedWithWrappedCancellationException(h3);
        r1.assertNotInvoked();
        r2.assertNotInvoked();
        r3.assertNotInvoked();
        checkCompletedNormally(failFirst ? snd : fst, v1);
        checkCancelled(failFirst ? fst : snd);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture)

Aggregations

CompletableFuture (java8.util.concurrent.CompletableFuture)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 CompletionException (java8.util.concurrent.CompletionException)11 CancellationException (java.util.concurrent.CancellationException)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Set (java.util.Set)4 ExecutionException (java.util.concurrent.ExecutionException)4 Executor (java.util.concurrent.Executor)4 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)4 SECONDS (java.util.concurrent.TimeUnit.SECONDS)4 CompletionStage (java8.util.concurrent.CompletionStage)4 Supplier (java8.util.function.Supplier)4 Method (java.lang.reflect.Method)3 Modifier (java.lang.reflect.Modifier)3 Arrays (java.util.Arrays)3 Callable (java.util.concurrent.Callable)3 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3