Java Code Examples for java.util.concurrent.CompletableFuture#minimalCompletionStage()

The following examples show how to use java.util.concurrent.CompletableFuture#minimalCompletionStage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * minimalStage.toCompletableFuture() returns a CompletableFuture that
 * is completed normally, with the same value, when source is.
 */
public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
    for (boolean createIncomplete : new boolean[] { true, false })
    for (Integer v1 : new Integer[] { 1, null })
{
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage<Integer> minimal = f.minimalCompletionStage();
    if (!createIncomplete) assertTrue(f.complete(v1));
    CompletableFuture<Integer> g = minimal.toCompletableFuture();
    if (createIncomplete) {
        checkIncomplete(f);
        checkIncomplete(g);
        assertTrue(f.complete(v1));
    }
    checkCompletedNormally(f, v1);
    checkCompletedNormally(g, v1);
}}
 
Example 2
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * minimalStage.toCompletableFuture() returns a CompletableFuture that
 * is completed exceptionally when source is.
 */
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
    for (boolean createIncomplete : new boolean[] { true, false })
{
    CFException ex = new CFException();
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage<Integer> minimal = f.minimalCompletionStage();
    if (!createIncomplete) f.completeExceptionally(ex);
    CompletableFuture<Integer> g = minimal.toCompletableFuture();
    if (createIncomplete) {
        checkIncomplete(f);
        checkIncomplete(g);
        f.completeExceptionally(ex);
    }
    checkCompletedExceptionally(f, ex);
    checkCompletedWithWrappedException(g, ex);
}}
 
Example 3
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Joining a minimal stage "by hand" works
 */
public void testMinimalCompletionStage_join_by_hand() {
    for (boolean createIncomplete : new boolean[] { true, false })
    for (Integer v1 : new Integer[] { 1, null })
{
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage<Integer> minimal = f.minimalCompletionStage();
    CompletableFuture<Integer> g = new CompletableFuture<>();
    if (!createIncomplete) assertTrue(f.complete(v1));
    minimal.thenAccept(x -> g.complete(x));
    if (createIncomplete) assertTrue(f.complete(v1));
    g.join();
    checkCompletedNormally(g, v1);
    checkCompletedNormally(f, v1);
    assertEquals(v1, join(minimal));
}}
 
Example 4
Source File: Java9RequestContextAwareFutureTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void minimalCompletionStageUsingWhenComplete() throws Exception {
    final RequestContext context =
            ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, "/")).build();
    final CompletableFuture<Integer> originalFuture = new CompletableFuture<>();
    final CompletableFuture<Integer> contextAwareFuture = context.makeContextAware(originalFuture);
    final CompletionStage<Integer> completionStage = contextAwareFuture.minimalCompletionStage();

    final AtomicInteger atomicInteger = new AtomicInteger();
    final AtomicReference<Throwable> causeCaptor = new AtomicReference<>();
    completionStage.whenComplete((result, error) -> {
        if (error != null) {
            causeCaptor.set(error);
        } else {
            atomicInteger.set(result);
        }
    });
    contextAwareFuture.complete(1);

    assertThat(contextAwareFuture.join()).isEqualTo(1);
    assertThat(contextAwareFuture.getNow(null)).isEqualTo(1);
    assertThat(contextAwareFuture.get()).isEqualTo(1);
    assertThat(atomicInteger.get()).isEqualTo(1);
    assertThat(causeCaptor.get()).isNull();
}
 
Example 5
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 6
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * minimalStage.toCompletableFuture() gives mutable CompletableFuture
 */
public void testMinimalCompletionStage_toCompletableFuture_mutable() {
    for (Integer v1 : new Integer[] { 1, null })
{
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage minimal = f.minimalCompletionStage();
    CompletableFuture<Integer> g = minimal.toCompletableFuture();
    assertTrue(g.complete(v1));
    checkCompletedNormally(g, v1);
    checkIncomplete(f);
    checkIncomplete(minimal.toCompletableFuture());
}}
 
Example 8
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * minimalStage.toCompletableFuture().join() awaits completion
 */
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
    for (boolean createIncomplete : new boolean[] { true, false })
    for (Integer v1 : new Integer[] { 1, null })
{
    CompletableFuture<Integer> f = new CompletableFuture<>();
    if (!createIncomplete) assertTrue(f.complete(v1));
    CompletionStage<Integer> minimal = f.minimalCompletionStage();
    if (createIncomplete) assertTrue(f.complete(v1));
    assertEquals(v1, minimal.toCompletableFuture().join());
    assertEquals(v1, minimal.toCompletableFuture().get());
    checkCompletedNormally(minimal.toCompletableFuture(), v1);
}}
 
Example 9
Source File: CompletableFutureTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Completion of a toCompletableFuture copy of a minimal stage
 * does not complete its source.
 */
public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage<Integer> g = f.minimalCompletionStage();
    assertTrue(g.toCompletableFuture().complete(1));
    assertTrue(g.toCompletableFuture().complete(null));
    assertTrue(g.toCompletableFuture().cancel(true));
    assertTrue(g.toCompletableFuture().cancel(false));
    assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
    checkIncomplete(g.toCompletableFuture());
    f.complete(1);
    checkCompletedNormally(g.toCompletableFuture(), 1);
}
 
Example 10
Source File: Java9RequestContextAwareFutureTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void minimalCompletionStageUsingToCompletableFutureMutable() throws Exception {
    final RequestContext context =
            ServiceRequestContext.builder(HttpRequest.of(HttpMethod.GET, "/")).build();
    final CompletableFuture<Integer> originalFuture = new CompletableFuture<>();
    final CompletableFuture<Integer> contextAwareFuture = context.makeContextAware(originalFuture);
    final CompletionStage<Integer> completionStage = contextAwareFuture.minimalCompletionStage();

    assertThat(contextAwareFuture.complete(1)).isTrue();
    assertThat(contextAwareFuture.join()).isEqualTo(1);
    assertThat(contextAwareFuture.getNow(null)).isEqualTo(1);
    assertThat(contextAwareFuture.get()).isEqualTo(1);
    assertThat(completionStage.toCompletableFuture().get()).isEqualTo(1);
}
 
Example 11
Source File: CompletableFutureTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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<Throwable>();
    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());
}
 
Example 12
Source File: CompletableFutureTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * 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<Throwable>();
    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);
}