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 (#1908)
  • Loading branch information
SmartbearYing authored Oct 4, 2023
1 parent 215f252 commit 6ec18c2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.test.core.app.ApplicationProvider
import com.bugsnag.android.internal.ImmutableConfig
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -145,11 +146,8 @@ 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
assertFalse(file.exists())
}

/**
Expand All @@ -159,7 +157,7 @@ internal class UserStoreTest {
fun saveWithoutPersistUser() {
val store = UserStore(generateConfig(false), "", file, prefMigrator, NoopLogger)
store.save(User("123", "[email protected]", "Joe Bloggs"))
assertEquals("", file.readText())
assertFalse(file.exists())
}

/**
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
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)
null
}
} else {
null
}
}
}

0 comments on commit 6ec18c2

Please sign in to comment.