Merge "Handle CancellationException in BackupTransportClient" into tm-dev

This commit is contained in:
Ruslan Tkhakokhov
2022-04-05 16:59:50 +00:00
committed by Android (Google) Code Review
2 changed files with 10 additions and 7 deletions

View File

@@ -34,6 +34,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
@@ -374,7 +375,8 @@ public class BackupTransportClient {
private <T> T getFutureResult(AndroidFuture<T> future) { private <T> T getFutureResult(AndroidFuture<T> future) {
try { try {
return future.get(600, TimeUnit.SECONDS); return future.get(600, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException
| CancellationException e) {
Slog.w(TAG, "Failed to get result from transport:", e); Slog.w(TAG, "Failed to get result from transport:", e);
return null; return null;
} finally { } finally {
@@ -403,7 +405,11 @@ public class BackupTransportClient {
void cancelActiveFutures() { void cancelActiveFutures() {
synchronized (mActiveFuturesLock) { synchronized (mActiveFuturesLock) {
for (AndroidFuture<?> future : mActiveFutures) { for (AndroidFuture<?> future : mActiveFutures) {
future.cancel(true); try {
future.cancel(true);
} catch (CancellationException ignored) {
// This is expected, so ignore the exception.
}
} }
mActiveFutures.clear(); mActiveFutures.clear();
} }

View File

@@ -110,10 +110,7 @@ public class BackupTransportClientTest {
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
try { try {
/*String name =*/ client.transportDirName(); assertThat(client.transportDirName()).isNull();
fail("transportDirName should be cancelled");
} catch (CancellationException ex) {
// This is expected.
} catch (Exception ex) { } catch (Exception ex) {
fail("unexpected Exception: " + ex.getClass().getCanonicalName()); fail("unexpected Exception: " + ex.getClass().getCanonicalName());
} }
@@ -189,7 +186,7 @@ public class BackupTransportClientTest {
} }
@Test @Test
public void testFinishBackup_canceledBeforeCompletion_throwsException() throws Exception { public void testFinishBackup_canceledBeforeCompletion_returnsError() throws Exception {
TestCallbacksFakeTransportBinder binder = new TestCallbacksFakeTransportBinder(); TestCallbacksFakeTransportBinder binder = new TestCallbacksFakeTransportBinder();
BackupTransportClient client = new BackupTransportClient(binder); BackupTransportClient client = new BackupTransportClient(binder);