Search in sources :

Example 16 with CompletableFuture

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

the class CompletableFutureTest method testWhenComplete_sourceCompletedNormallyActionFailed.

/**
 * If a whenComplete action throws an exception when triggered by
 * a normal completion, it completes exceptionally
 */
public void testWhenComplete_sourceCompletedNormallyActionFailed() {
    for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) {
        final AtomicInteger a = new AtomicInteger(0);
        final CFException ex = new CFException();
        final CompletableFuture<Integer> f = new CompletableFuture<>();
        if (!createIncomplete)
            assertTrue(f.complete(v1));
        final CompletableFuture<Integer> g = m.whenComplete(f, (Integer result, Throwable t) -> {
            m.checkExecutionMode();
            threadAssertSame(result, v1);
            threadAssertNull(t);
            a.getAndIncrement();
            throw ex;
        });
        if (createIncomplete)
            assertTrue(f.complete(v1));
        checkCompletedWithWrappedException(g, ex);
        checkCompletedNormally(f, v1);
        assertEquals(1, a.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 17 with CompletableFuture

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

the class CompletableFutureTest method testThenAcceptBoth_sourceCancelled.

/**
 * thenAcceptBoth result completes exceptionally if either source cancelled
 */
public void testThenAcceptBoth_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 SubtractAction r1 = new SubtractAction(m);
        final SubtractAction r2 = new SubtractAction(m);
        final SubtractAction r3 = new SubtractAction(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.thenAcceptBoth(f, g, r1);
        assertTrue(complete1.call());
        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
        checkIncomplete(h1);
        checkIncomplete(h2);
        assertTrue(complete2.call());
        final CompletableFuture<Void> h3 = m.thenAcceptBoth(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)

Example 18 with CompletableFuture

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

the class CompletableFutureTest method testCompleteAsync3.

/**
 * completeAsync with given executor completes with value of given supplier
 */
public void testCompleteAsync3() {
    for (Integer v1 : new Integer[] { 1, null }) {
        CompletableFuture<Integer> f = new CompletableFuture<>();
        ThreadExecutor executor = new ThreadExecutor();
        f.completeAsync(() -> v1, executor);
        assertSame(v1, f.join());
        checkCompletedNormally(f, v1);
        assertEquals(1, executor.count.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture)

Example 19 with CompletableFuture

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

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

the class CompletableFutureTest method testApplyToEither_exceptionalCompletion2.

public void testApplyToEither_exceptionalCompletion2() {
    for (ExecutionMode m : ExecutionMode.values()) for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
        final CompletableFuture<Integer> f = new CompletableFuture<>();
        final CompletableFuture<Integer> g = new CompletableFuture<>();
        final CFException ex = new CFException();
        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]);
        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
        // unspecified behavior - both source completions available
        try {
            assertEquals(inc(v1), h0.join());
            rs[0].assertValue(inc(v1));
        } catch (CompletionException ok) {
            checkCompletedWithWrappedException(h0, ex);
            rs[0].assertNotInvoked();
        }
        try {
            assertEquals(inc(v1), h1.join());
            rs[1].assertValue(inc(v1));
        } catch (CompletionException ok) {
            checkCompletedWithWrappedException(h1, ex);
            rs[1].assertNotInvoked();
        }
        try {
            assertEquals(inc(v1), h2.join());
            rs[2].assertValue(inc(v1));
        } catch (CompletionException ok) {
            checkCompletedWithWrappedException(h2, ex);
            rs[2].assertNotInvoked();
        }
        try {
            assertEquals(inc(v1), h3.join());
            rs[3].assertValue(inc(v1));
        } catch (CompletionException ok) {
            checkCompletedWithWrappedException(h3, ex);
            rs[3].assertNotInvoked();
        }
        checkCompletedNormally(f, v1);
        checkCompletedExceptionally(g, ex);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture) CompletionException(java8.util.concurrent.CompletionException)

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