Loading core/java/com/android/internal/infra/AndroidFuture.java +8 −1 Original line number Diff line number Diff line Loading @@ -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 { Loading core/tests/coretests/src/com/android/internal/infra/AndroidFutureTest.java +32 −0 Original line number Diff line number Diff line Loading @@ -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}. Loading Loading @@ -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); } } Loading
core/java/com/android/internal/infra/AndroidFuture.java +8 −1 Original line number Diff line number Diff line Loading @@ -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 { Loading
core/tests/coretests/src/com/android/internal/infra/AndroidFutureTest.java +32 −0 Original line number Diff line number Diff line Loading @@ -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}. Loading Loading @@ -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); } }