private BackupState acquireLocksAndBackup(BackupConfig config) { try { service.acquireLocks(); return fetchAndBackupItems(config); } finally { service.releaseLocks(); } }
@Test public void shouldAcquireAndReleaseLocksDuringBackup() throws Exception { mockAllFetchEmpty(); task.doInBackground(config); verify(service).acquireLocks(); verify(service).releaseLocks(); verify(service).transition(SmsSyncState.FINISHED_BACKUP, null); }
@Test public void shouldHandleAuthErrorAndTokenCannotBeRefreshed() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), notNull(DataType.class))).thenReturn(result(SMS, 1)); XOAuth2AuthenticationFailedException exception = mock(XOAuth2AuthenticationFailedException.class); when(exception.getStatus()).thenReturn(400); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenThrow(exception); doThrow(new TokenRefreshException("failed")).when(tokenRefresher).refreshOAuth2Token(); task.doInBackground(config); verify(tokenRefresher, times(1)).refreshOAuth2Token(); verify(service).transition(SmsSyncState.ERROR, exception); // make sure locks only get acquired+released once verify(service).acquireLocks(); verify(service).releaseLocks(); }
@Test public void shouldHandleAuthErrorAndTokenCouldBeRefreshed() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), notNull(DataType.class))).thenReturn(result(SMS, 1)); XOAuth2AuthenticationFailedException exception = mock(XOAuth2AuthenticationFailedException.class); when(exception.getStatus()).thenReturn(400); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenThrow(exception); when(service.getBackupImapStore()).thenReturn(store); task.doInBackground(config); verify(tokenRefresher).refreshOAuth2Token(); verify(service, times(2)).transition(SmsSyncState.LOGIN, null); verify(service, times(2)).transition(SmsSyncState.CALC, null); verify(service).transition(SmsSyncState.ERROR, exception); // make sure locks only get acquired+released once verify(service).acquireLocks(); verify(service).releaseLocks(); }