Search in sources :

Example 41 with CompletableFuture

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

the class CompletableFutureTest method testThenCompose_normalCompletion.

/**
 * thenCompose result completes normally after normal completion of source
 */
public void testThenCompose_normalCompletion() {
    for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) {
        final CompletableFuture<Integer> f = new CompletableFuture<>();
        final CompletableFutureInc r = new CompletableFutureInc(m);
        if (!createIncomplete)
            assertTrue(f.complete(v1));
        final CompletableFuture<Integer> g = m.thenCompose(f, r);
        if (createIncomplete)
            assertTrue(f.complete(v1));
        checkCompletedNormally(g, inc(v1));
        checkCompletedNormally(f, v1);
        r.assertValue(v1);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture)

Example 42 with CompletableFuture

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

the class CompletableFutureTest method testComplete.

/**
 * complete completes normally, as indicated by methods isDone,
 * isCancelled, join, get, and getNow
 */
public void testComplete() {
    for (Integer v1 : new Integer[] { 1, null }) {
        CompletableFuture<Integer> f = new CompletableFuture<>();
        checkIncomplete(f);
        assertTrue(f.complete(v1));
        assertFalse(f.complete(v1));
        checkCompletedNormally(f, v1);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture)

Example 43 with CompletableFuture

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

the class CompletableFutureTest method testApplyToEither_actionFailed.

/**
 * applyToEither result completes exceptionally if action does
 */
public void testApplyToEither_actionFailed() {
    for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) {
        final CompletableFuture<Integer> f = new CompletableFuture<>();
        final CompletableFuture<Integer> g = new CompletableFuture<>();
        final FailingFunction[] rs = new FailingFunction[6];
        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
        final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
        final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
        f.complete(v1);
        final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
        final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
        checkCompletedWithWrappedException(h0, rs[0].ex);
        checkCompletedWithWrappedException(h1, rs[1].ex);
        checkCompletedWithWrappedException(h2, rs[2].ex);
        checkCompletedWithWrappedException(h3, rs[3].ex);
        for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
        g.complete(v2);
        // 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]);
        checkCompletedWithWrappedException(h4, rs[4].ex);
        assertTrue(Objects.equals(v1, rs[4].value) || Objects.equals(v2, rs[4].value));
        checkCompletedWithWrappedException(h5, rs[5].ex);
        assertTrue(Objects.equals(v1, rs[5].value) || Objects.equals(v2, rs[5].value));
        checkCompletedNormally(f, v1);
        checkCompletedNormally(g, v2);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java8.util.concurrent.CompletableFuture)

Example 44 with CompletableFuture

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

the class CompletableFutureTest method testCompleteOnTimeout_completed.

/**
 * completeOnTimeout has no effect if completed within timeout
 */
public void testCompleteOnTimeout_completed() {
    for (Integer v1 : new Integer[] { 1, null }) {
        CompletableFuture<Integer> f = new CompletableFuture<>();
        CompletableFuture<Integer> g = new CompletableFuture<>();
        long startTime = System.nanoTime();
        f.complete(v1);
        assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
        assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
        g.complete(v1);
        checkCompletedNormally(f, v1);
        checkCompletedNormally(g, v1);
        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
    }
}
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