Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception handling when reading datastore. #1710

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.datastore.core.DataStore
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
import com.google.samples.apps.nowinandroid.core.model.data.UserData
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import java.io.IOException
Expand All @@ -30,6 +31,14 @@ class NiaPreferencesDataSource @Inject constructor(
private val userPreferences: DataStore<UserPreferences>,
) {
val userData = userPreferences.data
.catch { exception ->
if (exception is IOException) {
Log.e("NiaPreferences", "Error reading user preferences.", exception)
emit(UserPreferences.getDefaultInstance())
} else {
throw exception
}
}
.map {
UserData(
bookmarkedNewsResources = it.bookmarkedNewsResourceIdsMap.keys,
Expand Down Expand Up @@ -155,6 +164,14 @@ class NiaPreferencesDataSource @Inject constructor(
}

suspend fun getChangeListVersions() = userPreferences.data
.catch { exception ->
if (exception is IOException) {
Log.e("NiaPreferences", "Error reading user preferences.", exception)
emit(UserPreferences.getDefaultInstance())
} else {
throw exception
}
}
.map {
ChangeListVersions(
topicVersion = it.topicChangeListVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,52 @@

package com.google.samples.apps.nowinandroid.core.datastore

import androidx.datastore.core.DataStore
import androidx.datastore.core.IOException
import com.google.samples.apps.nowinandroid.core.datastore.test.InMemoryDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.test.fail

class NiaPreferencesDataSourceTest {

private val testScope = TestScope(UnconfinedTestDispatcher())

private lateinit var subject: NiaPreferencesDataSource

// A DataStore implementation that throws an IOException when accessed
private val ioExceptionThrowingDataStore = object : DataStore<UserPreferences> {
override val data: Flow<UserPreferences>
get() = flow { throw IOException("Failed to read proto") }

override suspend fun updateData(transform: suspend (t: UserPreferences) -> UserPreferences): UserPreferences {
fail("Not needed for this test")
}
}

@Before
fun setup() {
subject = NiaPreferencesDataSource(InMemoryDataStore(UserPreferences.getDefaultInstance()))
}

@Test
fun userData_emitDefault_whenDataStoreThrowsIOException() =
testScope.runTest {
val dataSource = NiaPreferencesDataSource(ioExceptionThrowingDataStore)
val actualUserData = dataSource.userData.first()

assertEquals(subject.userData.first(), actualUserData)
}

@Test
fun shouldHideOnboardingIsFalseByDefault() = testScope.runTest {
assertFalse(subject.userData.first().shouldHideOnboarding)
Expand Down Expand Up @@ -76,6 +101,15 @@ class NiaPreferencesDataSourceTest {
assertFalse(subject.userData.first().shouldHideOnboarding)
}

@Test
fun getChangeListVersions_returnsDefault_whenDataStoreThrowsIOException() =
testScope.runTest {
val dataSource = NiaPreferencesDataSource(ioExceptionThrowingDataStore)
val actualResult = dataSource.getChangeListVersions()

assertEquals(subject.getChangeListVersions(), actualResult)
}

@Test
fun shouldUseDynamicColorFalseByDefault() = testScope.runTest {
assertFalse(subject.userData.first().useDynamicColor)
Expand Down
Loading