Skip to content

Commit

Permalink
Bug(persistedUser) warning would not show when persisted user file is…
Browse files Browse the repository at this point in the history
… not loaded
  • Loading branch information
YYChen01988 committed Sep 28, 2023
1 parent fa51e42 commit a23352c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
1 change: 1 addition & 0 deletions bugsnag-android-core/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<ID>SwallowedException:JsonHelperTest.kt$JsonHelperTest$e: IllegalArgumentException</ID>
<ID>SwallowedException:PluginClient.kt$PluginClient$exc: ClassNotFoundException</ID>
<ID>SwallowedException:SharedPrefMigrator.kt$SharedPrefMigrator$e: RuntimeException</ID>
<ID>SwallowedException:UserStore.kt$UserStore$e: Exception</ID>
<ID>ThrowsCount:JsonHelper.kt$JsonHelper$fun jsonToLong(value: Any?): Long?</ID>
<ID>TooManyFunctions:ConfigInternal.kt$ConfigInternal : CallbackAwareMetadataAwareUserAwareFeatureFlagAware</ID>
<ID>TooManyFunctions:DeviceDataCollector.kt$DeviceDataCollector</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ internal class UserStoreTest {
/**
* If persistUser is false a user is still returned
*/
@Test
@Test(expected = Exception::class)
fun loadWithoutPersistUser() {
val store = UserStore(
generateConfig(false),
Expand All @@ -145,21 +145,18 @@ internal class UserStoreTest {
prefMigrator,
NoopLogger
)
val user = store.load(User()).user
assertEquals("device-id-123", user.id)
assertNull(user.email)
assertNull(user.name)
assertEquals("", file.readText())
store.load(User()).user
file.readText()
}

/**
* If persistUser is false a user is not saved
*/
@Test
@Test(expected = Exception::class)
fun saveWithoutPersistUser() {
val store = UserStore(generateConfig(false), "", file, prefMigrator, NoopLogger)
store.save(User("123", "[email protected]", "Joe Bloggs"))
assertEquals("", file.readText())
file.readText()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.concurrent.withLock
* This class is made thread safe through the use of a [ReadWriteLock].
*/
internal class SynchronizedStreamableStore<T : JsonStream.Streamable>(
private val file: File
internal val file: File
) {

private val lock = ReentrantReadWriteLock()
Expand Down
20 changes: 10 additions & 10 deletions bugsnag-android-core/src/main/java/com/bugsnag/android/UserStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.bugsnag.android
import com.bugsnag.android.internal.ImmutableConfig
import com.bugsnag.android.internal.StateObserver
import java.io.File
import java.io.IOException
import java.util.concurrent.atomic.AtomicReference

/**
Expand All @@ -22,11 +21,6 @@ internal class UserStore @JvmOverloads constructor(
private val previousUser = AtomicReference<User?>(null)

init {
try {
file.createNewFile()
} catch (exc: IOException) {
logger.w("Failed to created device ID file", exc)
}
this.synchronizedStreamableStore = SynchronizedStreamableStore(file)
}

Expand Down Expand Up @@ -87,13 +81,19 @@ internal class UserStore @JvmOverloads constructor(
val legacyUser = sharedPrefMigrator.loadUser(deviceId)
save(legacyUser)
legacyUser
} else {
return try {
} else if (
synchronizedStreamableStore.file.canRead() &&
synchronizedStreamableStore.file.length() > 0L &&
persist
) {
try {
synchronizedStreamableStore.load(User.Companion::fromReader)
} catch (exc: Exception) {
logger.w("Failed to load user info", exc)
} catch (e: Exception) {
logger.w("Failed to load user info")
null
}
} else {
null
}
}
}

0 comments on commit a23352c

Please sign in to comment.