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 5 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,13 +16,18 @@

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

Expand All @@ -32,11 +37,34 @@ class NiaPreferencesDataSourceTest {

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 {
throw Exception("Not needed for this test")
nur-shuvo marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

@Test
fun userData_emitDefault_whenDataStoreThrowsIOException() =
testScope.runTest {
// Given: NiaPreferencesDataSource with ioException throwing datastore
val dataSource = NiaPreferencesDataSource(ioExceptionThrowingDataStore)

// When: Retrieving user data from the data source
val actualUserData = dataSource.userData.first()

// Then: The default user data is returned
assertEquals(subject.userData.first(), actualUserData)
nur-shuvo marked this conversation as resolved.
Show resolved Hide resolved
}

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

@Test
fun getChangeListVersions_returnsDefault_whenDataStoreThrowsIOException() =
testScope.runTest {
// Given: NiaPreferencesDataSource with ioException throwing datastore
val dataSource = NiaPreferencesDataSource(ioExceptionThrowingDataStore)

// When: Retrieving change list versions from the data source
val actualResult = dataSource.getChangeListVersions()

// Then: The default value is returned
assertEquals(subject.getChangeListVersions(), actualResult)
}

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