Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1ccbddad authored by BK Choi's avatar BK Choi Committed by Android (Google) Code Review
Browse files

Merge "Fix the infinite loop bug for AndroidFuture.ThenCombine."

parents 9807b635 052cba1f
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -455,7 +455,14 @@ public class AndroidFuture<T> extends CompletableFuture<T> implements Parcelable
            if (mSourceU != null) {
                // T done
                mResultT = (T) res;
                mSourceU.whenComplete(this);

                // Subscribe to the second job completion.
                mSourceU.whenComplete((r, e) -> {
                    // Mark the first job completion by setting mSourceU to null, so that next time
                    // the execution flow goes to the else case below.
                    mSourceU = null;
                    accept(r, e);
                });
            } else {
                // U done
                try {
+32 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;

/**
 * Unit test for {@link AndroidFuture}.
@@ -154,4 +155,35 @@ public class AndroidFutureTest {
                expectThrows(ExecutionException.class, future1::get);
        assertThat(executionException.getCause()).isInstanceOf(UnsupportedOperationException.class);
    }

    @Test
    public void testThenCombine() throws Exception {
        String nearFutureString = "near future comes";
        AndroidFuture<String> nearFuture = AndroidFuture.supply(() -> nearFutureString);
        String farFutureString = " before far future.";
        AndroidFuture<String> farFuture = AndroidFuture.supply(() -> farFutureString);
        AndroidFuture<String> combinedFuture =
                nearFuture.thenCombine(farFuture, ((s1, s2) -> s1 + s2));

        assertThat(combinedFuture.get()).isEqualTo(nearFutureString + farFutureString);
    }

    @Test
    public void testThenCombine_functionThrowingException() throws Exception {
        String nearFutureString = "near future comes";
        AndroidFuture<String> nearFuture = AndroidFuture.supply(() -> nearFutureString);
        String farFutureString = " before far future.";
        AndroidFuture<String> farFuture = AndroidFuture.supply(() -> farFutureString);
        UnsupportedOperationException exception = new UnsupportedOperationException(
                "Unsupported operation exception thrown!");
        BiFunction<String, String, String> throwingFunction = (s1, s2) -> {
            throw exception;
        };
        AndroidFuture<String> combinedFuture = nearFuture.thenCombine(farFuture, throwingFunction);

        ExecutionException thrown = expectThrows(ExecutionException.class,
                () -> combinedFuture.get());

        assertThat(thrown.getCause()).isSameInstanceAs(exception);
    }
}