Skip to content

Commit 7ec7370

Browse files
runningcodeclaude
andauthored
test(android): Fix flaky AnrV2 scope persistence assertion (#6031)
* test(android): Fix flaky AnrV2 scope persistence assertion The final assertions of the AnrV2 enrichment test read transaction.json and release.json straight off disk, but both are written from the Sentry executor and nothing in the test joins it. The awaited beforeSend callback runs on the ANR processing task, which is queued ahead of the scope flush, so it flipping says nothing about whether the writes have landed. Awaitility's 100ms poll interval normally hides this; on a loaded CI runner it does not, and the read returns the value prefilled by the test setup instead of the new one. Poll for the persisted values rather than reading them once. * test(android): Cap the AnrV2 persistence poll at 5 seconds Awaitility's 10 second default is the wait a genuine regression now pays before the test reports it. By this point the only work left in the executor queue is two short file writes: the long ANR processing task has already run, which is what let the awaited beforeSend callback flip. Scheduler starvation on a loaded runner is the only plausible source of delay, so 5 seconds leaves ample headroom while halving the failure wait. * test(android): Lower the AnrV2 persistence poll cap to 2 seconds The remaining executor work is two short file writes, so the cap only has to cover scheduler starvation on a loaded runner. 2 seconds still leaves an order of magnitude of headroom and shortens the wait a genuine regression pays before the test reports it. * fix(core): Clear the persisted replay id when resetting the scope cache resetCache() cleared every other persisted scope value on init but left replay.json in place, so a replay id written by a previous process could still be attached to events from the current one. The reset already runs after the integrations that consume those values, so deleting it here is safe. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(android): Observe the scope-reset assertions instead of dropping them The reset assertions were submitted to the Sentry executor and their Future discarded, so an AssertionError never reached the test runner and the test passed regardless of what they found. Joining the Future revives them; the replay expectation is corrected to assertNull to match what resetCache does. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(android): Lower the scope-reset join timeout to 2 seconds The join only guards against a wedged executor; the task is expected to be long finished by then. 30 seconds was arbitrary and 15x the cap the sibling assertion in this test uses, so a hang burned CI time for no added coverage. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * test(android): Move the replay id reset out of the flakiness fix The scope-reset assertion revived by the previous commit expected resetCache() to clear replay.json, which it has never done. Fixing that is a product change unrelated to the flaky assertion this branch is about, so it moves to its own PR along with its test and changelog entry. The replay expectation is dropped rather than corrected: resetCache's behaviour belongs in PersistingScopeObserverBatchingTest, not incidental to a test about ANR enrichment. The breadcrumbs assertion beside it stays and is now observed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 0df52e7 commit 7ec7370

1 file changed

Lines changed: 35 additions & 26 deletions

File tree

‎sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt‎

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ import io.sentry.cache.PersistingScopeObserver.SCOPE_CACHE
4646
import io.sentry.cache.PersistingScopeObserver.TRANSACTION_FILENAME
4747
import io.sentry.cache.tape.QueueFile
4848
import io.sentry.protocol.Contexts
49-
import io.sentry.protocol.SentryId
5049
import io.sentry.spotlight.SpotlightIntegration
5150
import io.sentry.test.applyTestOptions
5251
import io.sentry.transport.NoOpEnvelopeCache
5352
import io.sentry.util.StringUtils
5453
import java.io.ByteArrayOutputStream
5554
import java.io.File
5655
import java.nio.file.Files
56+
import java.util.concurrent.ExecutionException
5757
import java.util.concurrent.TimeUnit
5858
import java.util.concurrent.atomic.AtomicBoolean
5959
import kotlin.io.path.absolutePathString
@@ -437,20 +437,16 @@ class SentryAndroidTest {
437437
it.environment = "debug"
438438
options = it
439439
}
440-
options.executorService.submit {
441-
// verify we reset the persisted scope values after the init bg tasks have run to ensure
442-
// clean state for a new process.
443-
assertEquals(
444-
emptyList<Breadcrumb>(),
445-
options
446-
.findPersistingScopeObserver()
447-
?.read(options, BREADCRUMBS_FILENAME, List::class.java),
448-
)
449-
assertEquals(
450-
SentryId.EMPTY_ID.toString(),
451-
options.findPersistingScopeObserver()?.read(options, REPLAY_FILENAME, String::class.java),
452-
)
453-
}
440+
val scopeObserver = assertNotNull(options.findPersistingScopeObserver())
441+
val resetAssertions =
442+
options.executorService.submit {
443+
// verify we reset the persisted scope values after the init bg tasks have run to ensure
444+
// clean state for a new process.
445+
assertEquals(
446+
emptyList<Breadcrumb>(),
447+
scopeObserver.read(options, BREADCRUMBS_FILENAME, List::class.java),
448+
)
449+
}
454450
Sentry.configureScope {
455451
it.setTransaction("TestActivity")
456452
it.addBreadcrumb(Breadcrumb.error("Error!"))
@@ -464,17 +460,30 @@ class SentryAndroidTest {
464460
// Execute all posted tasks
465461
Shadows.shadowOf(Looper.getMainLooper()).idle()
466462

467-
// assert that persisted values have changed
468-
assertEquals(
469-
"TestActivity",
470-
options
471-
.findPersistingScopeObserver()
472-
?.read(options, TRANSACTION_FILENAME, String::class.java),
473-
)
474-
assertEquals(
475-
"io.sentry.sample@1.1.0+220",
476-
PersistingOptionsObserver.read(options, RELEASE_FILENAME, String::class.java),
477-
)
463+
// the reset assertions run on the executor, so nothing reports their AssertionError unless we
464+
// observe the Future: without this the test would pass no matter what they found
465+
try {
466+
resetAssertions.get(2, TimeUnit.SECONDS)
467+
} catch (e: ExecutionException) {
468+
throw requireNotNull(e.cause)
469+
}
470+
471+
// Both values are persisted from the Sentry executor, and nothing above joins it: the
472+
// beforeSend callback we awaited runs on an earlier task in the same queue, so it flipping
473+
// says nothing about the writes. Poll instead of reading once and racing them.
474+
await
475+
.withAlias("Persisted scope and options values are written from the Sentry executor")
476+
.atMost(2, TimeUnit.SECONDS)
477+
.untilAsserted {
478+
assertEquals(
479+
"TestActivity",
480+
scopeObserver.read(options, TRANSACTION_FILENAME, String::class.java),
481+
)
482+
assertEquals(
483+
"io.sentry.sample@1.1.0+220",
484+
PersistingOptionsObserver.read(options, RELEASE_FILENAME, String::class.java),
485+
)
486+
}
478487
}
479488

480489
@Test

0 commit comments

Comments
 (0)