fix(#240): a failed read of the sync targets is retried instead of hiding Sync now
This commit is contained in:
1 parent
1bd055a0b7
commit
524c4cbb90
4 files changed
+40
-3
No files matched your search
+9
-1
@@ -201,7 +201,15 @@ class CalendarRepositoryImpl @Inject constructor(
|
||||
|
||||
override val syncTargets: Flow<SyncTargets>
|
||||
get() = combine(calendars(), prefs.syncAccessDismissed, accessChecks) { calendars, dismissed, _ ->
|
||||
targetsOf(calendars, dismissed)
|
||||
// One failed read leaves the rows off until the next change, not for good.
|
||||
try {
|
||||
targetsOf(calendars, dismissed)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "Sync targets unreadable", e)
|
||||
SyncTargets.None
|
||||
}
|
||||
}.flowOn(io)
|
||||
|
||||
override fun refreshSyncAccess() {
|
||||
|
||||
@@ -16,7 +16,8 @@ import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.retryWhen
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -33,7 +34,13 @@ class SyncViewModel @Inject constructor(
|
||||
|
||||
/** Who "Sync now" can reach, and who first needs the user's grant; null until first read. */
|
||||
val targets: StateFlow<SyncTargets?> = repository.syncTargets
|
||||
.catch { emit(SyncTargets.None) }
|
||||
// A failed read hides the rows for now, then reads again rather than for good.
|
||||
.retryWhen { e, attempt ->
|
||||
if (e is CancellationException) return@retryWhen false
|
||||
emit(SyncTargets.None)
|
||||
delay((RETRY_BASE_MILLIS shl attempt.coerceAtMost(5).toInt()).coerceAtMost(RETRY_MAX_MILLIS))
|
||||
true
|
||||
}
|
||||
.stateWhileSubscribed(scope = viewModelScope, initialValue = null)
|
||||
|
||||
/** Re-read account access: on resume (which includes the picker returning) and drawer open. */
|
||||
@@ -81,6 +88,8 @@ class SyncViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val RETRY_BASE_MILLIS = 1_000L
|
||||
const val RETRY_MAX_MILLIS = 30_000L
|
||||
const val TAG = "SyncViewModel"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,8 +79,12 @@ internal class FakeCalendarDataSource : CalendarDataSource {
|
||||
var calendarQueries = 0
|
||||
private set
|
||||
|
||||
/** Set to make [calendars] throw until cleared. */
|
||||
var calendarsError: Exception? = null
|
||||
|
||||
override fun calendars(): List<CalendarSource> {
|
||||
calendarQueries++
|
||||
calendarsError?.let { throw it }
|
||||
return calendarsResult
|
||||
}
|
||||
override fun instances(beginMillis: Long, endMillis: Long): List<EventInstance> =
|
||||
|
||||
@@ -190,4 +190,20 @@ class SyncViewModelTest {
|
||||
|
||||
assertThat(vm.targets.value).isEqualTo(SyncTargets(emptySet(), emptySet(), dismissed = setOf(davAccount)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a failed read of the targets is retried rather than hiding Sync now for good`(@TempDir dir: Path) =
|
||||
runTest(dispatcher) {
|
||||
val (vm, source) = viewModel(dir)
|
||||
source.calendarsError = IllegalStateException("provider gone")
|
||||
backgroundScope.launch { vm.targets.collect {} }
|
||||
runCurrent()
|
||||
assertThat(vm.targets.value?.reachable.orEmpty()).isEmpty()
|
||||
|
||||
source.calendarsError = null
|
||||
advanceTimeBy(31_000)
|
||||
runCurrent()
|
||||
|
||||
assertThat(vm.targets.value?.reachable).containsExactly(davAccount)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user