Skip to content

Commit 5d64606

Browse files
runningcodeclaude
andcommitted
test(android): Fix UI tests flaking on the standalone app start
doubleInitDoesNotWait: the second init sends a headless app start from a main thread IdleHandler, which could take the transaction profiler from the afterRestart transaction. Wait for the main thread to be idle first. checkAppStartFramesMeasurements: the app start is sent right after init, while SendCachedEnvelopeIntegration scans the disk cache, so the same envelope can arrive twice. Deduplicate app starts by event ID, as MockRelay already does for other envelopes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8248747 commit 5d64606

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

  • sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android

‎sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/SdkInitTests.kt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.sentry.uitest.android
33
import androidx.lifecycle.Lifecycle
44
import androidx.test.core.app.launchActivity
55
import androidx.test.ext.junit.runners.AndroidJUnit4
6+
import androidx.test.platform.app.InstrumentationRegistry
67
import io.sentry.ProfilingTraceData
78
import io.sentry.Sentry
89
import io.sentry.SentryIntegrationPackageStorage
@@ -123,6 +124,10 @@ class SdkInitTests : BaseUiTest() {
123124
}
124125
val afterRestart = System.currentTimeMillis()
125126
val restartMs = afterRestart - beforeRestart
127+
// With tracing on, the second init sends a headless app start from a main thread IdleHandler.
128+
// It would take the profiler, so we let it finish before starting our transaction. Espresso's
129+
// onIdle() can return before IdleHandlers run, waitForIdleSync() can't.
130+
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
126131

127132
relayIdlingResource.increment()
128133
Sentry.startTransaction("afterRestart", "emptyTransaction").finish()

‎sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/mockservers/MockRelay.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ class MockRelay(
4343
if (request.path == envelopePath) {
4444
val appStart = RelayAsserter.RelayResponse(request, MockResponse())
4545
if (appStart.isAppStartTransaction()) {
46-
appStartEnvelopes.add(appStart)
46+
// The app start is sent right after init, while the cached envelopes are being sent,
47+
// which can send it a second time from the disk cache
48+
if (receivedEnvelopes.add(appStart.envelope!!.header.eventId.toString())) {
49+
appStartEnvelopes.add(appStart)
50+
}
4751
return appStart.response
4852
}
4953
}

0 commit comments

Comments
 (0)