Skip to content

Commit

Permalink
bug(SharedPreference): prevent SharedPreference causing rare app crash
Browse files Browse the repository at this point in the history
  • Loading branch information
YYChen01988 committed Jun 29, 2023
2 parents dfac185 + 2888547 commit 9c09137
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## TBD

### Bug fixes

* Prevent rare app crash while migrating old `SharedPreferences` data from older versions of `bugsnag-android
[#1860](https://github.com/bugsnag/bugsnag-android/pull/1860)

## 5.30.0 (2023-05-11)

### Enhancements
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ GEM
mime-types-data (~> 3.2015)
mime-types-data (3.2023.0218.1)
multi_test (0.1.2)
nokogiri (1.15.1-arm64-darwin)
racc (~> 1.4)
nokogiri (1.15.1-x86_64-darwin)
racc (~> 1.4)
optimist (3.0.1)
Expand Down Expand Up @@ -121,6 +123,7 @@ GEM
rexml

PLATFORMS
arm64-darwin-22
x86_64-darwin-20

DEPENDENCIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
import org.mockito.Mockito.times
Expand All @@ -32,7 +33,7 @@ internal class SharedPrefMigratorTest {

@Before
fun setUp() {
`when`(context.getSharedPreferences(eq("com.bugsnag.android"), eq(0))).thenReturn(prefs)
`when`(context.getSharedPreferences(eq("com.bugsnag.android"), anyInt())).thenReturn(prefs)
prefMigrator = SharedPrefMigrator(context)
}

Expand All @@ -45,7 +46,7 @@ internal class SharedPrefMigratorTest {

@Test
fun gettingSharedPreferencesWithException() {
`when`(context.getSharedPreferences(eq("com.bugsnag.android"), eq(0))).thenThrow(RuntimeException())
`when`(context.getSharedPreferences(eq("com.bugsnag.android"), anyInt())).thenThrow(RuntimeException())
prefMigrator = SharedPrefMigrator(context)
assertFalse(prefMigrator.hasPrefs())
}
Expand Down
18 changes: 9 additions & 9 deletions features/full_tests/trimming.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ Feature: Excess data is trimmed when the payload is too big
Then I wait to receive an error
And the error is valid for the error reporting API version "4.0" for the "Android Bugsnag Notifier" notifier
And the exception "message" equals "EventTooBigScenario"
And the event has 98 breadcrumbs
And the event "breadcrumbs.97.name" equals "Removed, along with 2 older breadcrumbs, to reduce payload size"
And the event "usage.system.breadcrumbsRemoved" equals 3
And the event has less than 99 breadcrumbs
Then the event last breadcrumb has a message that matches the regex "Removed, along with [0-9]+ older breadcrumbs, to reduce payload size"
And the event "usage.system.breadcrumbsRemoved" is not null
And the event "usage.system.breadcrumbBytesRemoved" is not null
And I close and relaunch the app
Then I wait to receive an error
And the error is valid for the error reporting API version "4.0" for the "Android Bugsnag Notifier" notifier
And the exception "message" equals "EventTooBigScenario"
And the event has 98 breadcrumbs
And the event "breadcrumbs.97.name" equals "Removed, along with 2 older breadcrumbs, to reduce payload size"
And the event "usage.system.breadcrumbsRemoved" equals 3
And the event has less than 99 breadcrumbs
Then the event last breadcrumb has a message that matches the regex "Removed, along with [0-9]+ older breadcrumbs, to reduce payload size"
And the event "usage.system.breadcrumbsRemoved" is not null
And the event "usage.system.breadcrumbBytesRemoved" is not null

Scenario: Payload is too big by 3 breadcrumbs, handled exception
Expand All @@ -75,9 +75,9 @@ Feature: Excess data is trimmed when the payload is too big
Then I wait to receive an error
And the error is valid for the error reporting API version "4.0" for the "Android Bugsnag Notifier" notifier
And the exception "message" equals "EventTooBigScenario"
And the event has 98 breadcrumbs
And the event "breadcrumbs.97.name" equals "Removed, along with 2 older breadcrumbs, to reduce payload size"
And the event "usage.system.breadcrumbsRemoved" equals 3
And the event has less than 99 breadcrumbs
Then the event last breadcrumb has a message that matches the regex "Removed, along with [0-9]+ older breadcrumbs, to reduce payload size"
And the event "usage.system.breadcrumbsRemoved" is not null
And the event "usage.system.breadcrumbBytesRemoved" is not null

Scenario: Payload is too big by 3 breadcrumbs, jvm exception
Expand Down
14 changes: 14 additions & 0 deletions features/steps/android_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,17 @@ def click_if_present(element)
end
end
end

Then("the event has less than {int} breadcrumb(s)") do |expected|
breadcrumbs = Maze::Server.errors.current[:body]['events'].first['breadcrumbs']
Maze.check.operator(
breadcrumbs&.length || 0, :<, expected,
"Expected event to have less '#{expected}' breadcrumbs, but got: #{breadcrumbs}"
)
end

Then("the event last breadcrumb has a message that matches the regex {string}") do |pattern|
lastBreadcrumbName = Maze::Server.errors.current[:body]['events'].first['breadcrumbs'].last['name']
regex = Regexp.new pattern
Maze.check.match regex, lastBreadcrumbName
end

0 comments on commit 9c09137

Please sign in to comment.