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

Commit 98be60dd authored by Alexander Dorokhine's avatar Alexander Dorokhine Committed by Android (Google) Code Review
Browse files

Merge "Fix double-wrapping of exceptions in remote AndroidFutures."

parents af109096 3c5b567b
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -531,6 +531,12 @@ public class AndroidFuture<T> extends CompletableFuture<T> implements Parcelable
                    try {
                        AndroidFuture.this.complete((T) resultContainer.get());
                    } catch (Throwable t) {
                        // If resultContainer was completed exceptionally, get() wraps the
                        // underlying exception in an ExecutionException. Unwrap it now to avoid
                        // double-layering ExecutionExceptions.
                        if (t instanceof ExecutionException && t.getCause() != null) {
                            t = t.getCause();
                        }
                        completeExceptionally(t);
                    }
                }
+27 −1
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ public class AndroidFutureTest {
    }

    @Test
    public void testWriteToParcel_Exceptionally() throws Exception {
    public void testWriteToParcel_Exception() throws Exception {
        Parcel parcel = Parcel.obtain();
        AndroidFuture<Integer> future1 = new AndroidFuture<>();
        future1.completeExceptionally(new UnsupportedOperationException());
@@ -123,4 +123,30 @@ public class AndroidFutureTest {
                expectThrows(ExecutionException.class, future2::get);
        assertThat(executionException.getCause()).isInstanceOf(UnsupportedOperationException.class);
    }

    @Test
    public void testWriteToParcel_Incomplete() throws Exception {
        Parcel parcel = Parcel.obtain();
        AndroidFuture<Integer> future1 = new AndroidFuture<>();
        future1.writeToParcel(parcel, 0);

        parcel.setDataPosition(0);
        AndroidFuture future2 = AndroidFuture.CREATOR.createFromParcel(parcel);
        future2.complete(5);
        assertThat(future1.get()).isEqualTo(5);
    }

    @Test
    public void testWriteToParcel_Incomplete_Exception() throws Exception {
        Parcel parcel = Parcel.obtain();
        AndroidFuture<Integer> future1 = new AndroidFuture<>();
        future1.writeToParcel(parcel, 0);

        parcel.setDataPosition(0);
        AndroidFuture future2 = AndroidFuture.CREATOR.createFromParcel(parcel);
        future2.completeExceptionally(new UnsupportedOperationException());
        ExecutionException executionException =
                expectThrows(ExecutionException.class, future1::get);
        assertThat(executionException.getCause()).isInstanceOf(UnsupportedOperationException.class);
    }
}