@Test public void testRun_whenFinishBackupFails_schedulesAlarm() throws Exception { setUpTransport(mTransport); configureTransport(mTransportBinder, TRANSPORT_OK, TRANSPORT_ERROR); PerformInitializeTask performInitializeTask = createPerformInitializeTask(mTransportName); performInitializeTask.run(); verify(mAlarmManager).set(anyInt(), anyLong(), eq(mRunInitIntent)); }
public void run() { String callerLogString = "PerformInitializeTask.run()"; List<TransportClient> transportClientsToDisposeOf = new ArrayList<>(mQueue.length); int result = BackupTransport.TRANSPORT_OK; try { for (String transportName : mQueue) { TransportClient transportClient = mTransportManager.getTransportClient(transportName, callerLogString); if (transportClient == null) { Slog.e(TAG, "Requested init for " + transportName + " but not found"); continue; } transportClientsToDisposeOf.add(transportClient); Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); String transportDirName = mTransportManager.getTransportDirName( transportClient.getTransportComponent()); EventLog.writeEvent(EventLogTags.BACKUP_START, transportDirName); long startRealtime = SystemClock.elapsedRealtime(); IBackupTransport transport = transportClient.connectOrThrow(callerLogString); int status = transport.initializeDevice(); if (status == BackupTransport.TRANSPORT_OK) { status = transport.finishBackup(); } if (status == BackupTransport.TRANSPORT_OK) { Slog.i(TAG, "Device init successful"); int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); File stateFileDir = new File(mBaseStateDir, transportDirName); mBackupManagerService.resetBackupState(stateFileDir); EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); mBackupManagerService.recordInitPending(false, transportName, transportDirName); notifyResult(transportName, BackupTransport.TRANSPORT_OK); } else { Slog.e(TAG, "Transport error in initializeDevice()"); EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); mBackupManagerService.recordInitPending(true, transportName, transportDirName); notifyResult(transportName, status); result = status; long delay = transport.requestBackupTime(); Slog.w(TAG, "Init failed on " + transportName + " resched in " + delay); mBackupManagerService.getAlarmManager().set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, mBackupManagerService.getRunInitIntent()); } } } catch (Exception e) { Slog.e(TAG, "Unexpected error performing init", e); result = BackupTransport.TRANSPORT_ERROR; } finally { for (TransportClient transportClient : transportClientsToDisposeOf) { mTransportManager.disposeOfTransportClient(transportClient, callerLogString); } notifyFinished(result); mListener.onFinished(callerLogString); } }
PerformInitializeTask implements Runnable { public void run() { String callerLogString = "PerformInitializeTask.run()"; List<TransportClient> transportClientsToDisposeOf = new ArrayList<>(mQueue.length); int result = BackupTransport.TRANSPORT_OK; try { for (String transportName : mQueue) { TransportClient transportClient = mTransportManager.getTransportClient(transportName, callerLogString); if (transportClient == null) { Slog.e(TAG, "Requested init for " + transportName + " but not found"); continue; } transportClientsToDisposeOf.add(transportClient); Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); String transportDirName = mTransportManager.getTransportDirName( transportClient.getTransportComponent()); EventLog.writeEvent(EventLogTags.BACKUP_START, transportDirName); long startRealtime = SystemClock.elapsedRealtime(); IBackupTransport transport = transportClient.connectOrThrow(callerLogString); int status = transport.initializeDevice(); if (status == BackupTransport.TRANSPORT_OK) { status = transport.finishBackup(); } if (status == BackupTransport.TRANSPORT_OK) { Slog.i(TAG, "Device init successful"); int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); File stateFileDir = new File(mBaseStateDir, transportDirName); mBackupManagerService.resetBackupState(stateFileDir); EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); mBackupManagerService.recordInitPending(false, transportName, transportDirName); notifyResult(transportName, BackupTransport.TRANSPORT_OK); } else { Slog.e(TAG, "Transport error in initializeDevice()"); EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); mBackupManagerService.recordInitPending(true, transportName, transportDirName); notifyResult(transportName, status); result = status; long delay = transport.requestBackupTime(); Slog.w(TAG, "Init failed on " + transportName + " resched in " + delay); mBackupManagerService.getAlarmManager().set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, mBackupManagerService.getRunInitIntent()); } } } catch (Exception e) { Slog.e(TAG, "Unexpected error performing init", e); result = BackupTransport.TRANSPORT_ERROR; } finally { for (TransportClient transportClient : transportClientsToDisposeOf) { mTransportManager.disposeOfTransportClient(transportClient, callerLogString); } notifyFinished(result); mListener.onFinished(callerLogString); } } }
PerformInitializeTask implements Runnable { public void run() { String callerLogString = "PerformInitializeTask.run()"; List<TransportClient> transportClientsToDisposeOf = new ArrayList<>(mQueue.length); int result = BackupTransport.TRANSPORT_OK; try { for (String transportName : mQueue) { TransportClient transportClient = mTransportManager.getTransportClient(transportName, callerLogString); if (transportClient == null) { Slog.e(TAG, "Requested init for " + transportName + " but not found"); continue; } transportClientsToDisposeOf.add(transportClient); Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); String transportDirName = mTransportManager.getTransportDirName( transportClient.getTransportComponent()); EventLog.writeEvent(EventLogTags.BACKUP_START, transportDirName); long startRealtime = SystemClock.elapsedRealtime(); IBackupTransport transport = transportClient.connectOrThrow(callerLogString); int status = transport.initializeDevice(); if (status == BackupTransport.TRANSPORT_OK) { status = transport.finishBackup(); } if (status == BackupTransport.TRANSPORT_OK) { Slog.i(TAG, "Device init successful"); int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); File stateFileDir = new File(mBaseStateDir, transportDirName); mBackupManagerService.resetBackupState(stateFileDir); EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); mBackupManagerService.recordInitPending(false, transportName, transportDirName); notifyResult(transportName, BackupTransport.TRANSPORT_OK); } else { Slog.e(TAG, "Transport error in initializeDevice()"); EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); mBackupManagerService.recordInitPending(true, transportName, transportDirName); notifyResult(transportName, status); result = status; long delay = transport.requestBackupTime(); Slog.w(TAG, "Init failed on " + transportName + " resched in " + delay); mBackupManagerService.getAlarmManager().set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, mBackupManagerService.getRunInitIntent()); } } } catch (Exception e) { Slog.e(TAG, "Unexpected error performing init", e); result = BackupTransport.TRANSPORT_ERROR; } finally { for (TransportClient transportClient : transportClientsToDisposeOf) { mTransportManager.disposeOfTransportClient(transportClient, callerLogString); } notifyFinished(result); mListener.onFinished(callerLogString); } } PerformInitializeTask(
            BackupManagerService backupManagerService,
            String[] transportNames,
            @Nullable IBackupObserver observer,
            OnTaskFinishedListener listener); @VisibleForTesting  PerformInitializeTask(
            BackupManagerService backupManagerService,
            TransportManager transportManager,
            String[] transportNames,
            @Nullable IBackupObserver observer,
            OnTaskFinishedListener listener,
            File baseStateDir); }
PerformInitializeTask implements Runnable { public void run() { String callerLogString = "PerformInitializeTask.run()"; List<TransportClient> transportClientsToDisposeOf = new ArrayList<>(mQueue.length); int result = BackupTransport.TRANSPORT_OK; try { for (String transportName : mQueue) { TransportClient transportClient = mTransportManager.getTransportClient(transportName, callerLogString); if (transportClient == null) { Slog.e(TAG, "Requested init for " + transportName + " but not found"); continue; } transportClientsToDisposeOf.add(transportClient); Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); String transportDirName = mTransportManager.getTransportDirName( transportClient.getTransportComponent()); EventLog.writeEvent(EventLogTags.BACKUP_START, transportDirName); long startRealtime = SystemClock.elapsedRealtime(); IBackupTransport transport = transportClient.connectOrThrow(callerLogString); int status = transport.initializeDevice(); if (status == BackupTransport.TRANSPORT_OK) { status = transport.finishBackup(); } if (status == BackupTransport.TRANSPORT_OK) { Slog.i(TAG, "Device init successful"); int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); File stateFileDir = new File(mBaseStateDir, transportDirName); mBackupManagerService.resetBackupState(stateFileDir); EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); mBackupManagerService.recordInitPending(false, transportName, transportDirName); notifyResult(transportName, BackupTransport.TRANSPORT_OK); } else { Slog.e(TAG, "Transport error in initializeDevice()"); EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); mBackupManagerService.recordInitPending(true, transportName, transportDirName); notifyResult(transportName, status); result = status; long delay = transport.requestBackupTime(); Slog.w(TAG, "Init failed on " + transportName + " resched in " + delay); mBackupManagerService.getAlarmManager().set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, mBackupManagerService.getRunInitIntent()); } } } catch (Exception e) { Slog.e(TAG, "Unexpected error performing init", e); result = BackupTransport.TRANSPORT_ERROR; } finally { for (TransportClient transportClient : transportClientsToDisposeOf) { mTransportManager.disposeOfTransportClient(transportClient, callerLogString); } notifyFinished(result); mListener.onFinished(callerLogString); } } PerformInitializeTask(
            BackupManagerService backupManagerService,
            String[] transportNames,
            @Nullable IBackupObserver observer,
            OnTaskFinishedListener listener); @VisibleForTesting  PerformInitializeTask(
            BackupManagerService backupManagerService,
            TransportManager transportManager,
            String[] transportNames,
            @Nullable IBackupObserver observer,
            OnTaskFinishedListener listener,
            File baseStateDir); void run(); }
PerformInitializeTask implements Runnable { public void run() { String callerLogString = "PerformInitializeTask.run()"; List<TransportClient> transportClientsToDisposeOf = new ArrayList<>(mQueue.length); int result = BackupTransport.TRANSPORT_OK; try { for (String transportName : mQueue) { TransportClient transportClient = mTransportManager.getTransportClient(transportName, callerLogString); if (transportClient == null) { Slog.e(TAG, "Requested init for " + transportName + " but not found"); continue; } transportClientsToDisposeOf.add(transportClient); Slog.i(TAG, "Initializing (wiping) backup transport storage: " + transportName); String transportDirName = mTransportManager.getTransportDirName( transportClient.getTransportComponent()); EventLog.writeEvent(EventLogTags.BACKUP_START, transportDirName); long startRealtime = SystemClock.elapsedRealtime(); IBackupTransport transport = transportClient.connectOrThrow(callerLogString); int status = transport.initializeDevice(); if (status == BackupTransport.TRANSPORT_OK) { status = transport.finishBackup(); } if (status == BackupTransport.TRANSPORT_OK) { Slog.i(TAG, "Device init successful"); int millis = (int) (SystemClock.elapsedRealtime() - startRealtime); EventLog.writeEvent(EventLogTags.BACKUP_INITIALIZE); File stateFileDir = new File(mBaseStateDir, transportDirName); mBackupManagerService.resetBackupState(stateFileDir); EventLog.writeEvent(EventLogTags.BACKUP_SUCCESS, 0, millis); mBackupManagerService.recordInitPending(false, transportName, transportDirName); notifyResult(transportName, BackupTransport.TRANSPORT_OK); } else { Slog.e(TAG, "Transport error in initializeDevice()"); EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_FAILURE, "(initialize)"); mBackupManagerService.recordInitPending(true, transportName, transportDirName); notifyResult(transportName, status); result = status; long delay = transport.requestBackupTime(); Slog.w(TAG, "Init failed on " + transportName + " resched in " + delay); mBackupManagerService.getAlarmManager().set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, mBackupManagerService.getRunInitIntent()); } } } catch (Exception e) { Slog.e(TAG, "Unexpected error performing init", e); result = BackupTransport.TRANSPORT_ERROR; } finally { for (TransportClient transportClient : transportClientsToDisposeOf) { mTransportManager.disposeOfTransportClient(transportClient, callerLogString); } notifyFinished(result); mListener.onFinished(callerLogString); } } PerformInitializeTask(
            BackupManagerService backupManagerService,
            String[] transportNames,
            @Nullable IBackupObserver observer,
            OnTaskFinishedListener listener); @VisibleForTesting  PerformInitializeTask(
            BackupManagerService backupManagerService,
            TransportManager transportManager,
            String[] transportNames,
            @Nullable IBackupObserver observer,
            OnTaskFinishedListener listener,
            File baseStateDir); void run();  }