Skip to content

Commit a151e1b

Browse files
runningcodeclaude
andcommitted
test(android): Run the UI tests with standalone app start tracing on
Instead of turning the new default off for all UI tests, MockRelay keeps app start transactions apart. They don't count as expected requests and don't use up custom responses, since whether a headless app start is sent depends on the process importance. Tests that launch an Activity assert exactly one app start transaction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6f0879b commit a151e1b

6 files changed

Lines changed: 47 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AutomaticSpansTest : BaseUiTest() {
4141
activity.moveToState(Lifecycle.State.DESTROYED)
4242

4343
relay.assert {
44+
assertAppStartTransaction()
4445
assertFirstEnvelope {
4546
val transactionItem: SentryTransaction = it.assertTransaction()
4647
assertTrue("TTID span missing") {
@@ -89,6 +90,7 @@ class AutomaticSpansTest : BaseUiTest() {
8990
"Expected total frames ($totalFrames) to be higher than the sum of slow ($slowFrames) and frozen ($frozenFrames) frames.",
9091
)
9192
}
93+
assertAppStartTransaction()
9294
assertNoOtherEnvelopes()
9395
}
9496
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ abstract class BaseUiTest {
114114
// actually be sent.
115115
// To avoid any interference between tests we can just disable them by default.
116116
it.isEnableAutoSessionTracking = false
117-
// Whichever test launches the first activity of the process would otherwise also get a
118-
// standalone app start transaction, which these tests don't expect.
119-
it.isEnableStandaloneAppStartTracing = false
120117
optionsConfiguration?.invoke(it)
121118
}
122119
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class EnvelopeTests : BaseUiTest() {
6161

6262
transaction.finish()
6363
relay.assert {
64+
assertAppStartTransaction()
6465
findEnvelope {
6566
assertEnvelopeTransaction(it.items.toList(), AndroidLogger()).transaction ==
6667
"ProfilingSampleActivity"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class SdkInitTests : BaseUiTest() {
7474
transaction2.finish()
7575

7676
relay.assert {
77+
assertAppStartTransaction()
7778
findEnvelope {
7879
assertEnvelopeTransaction(it.items.toList(), AndroidLogger()).transaction == "e2etests"
7980
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class MockRelay(
2424
/** List of unasserted requests sent to the [envelopePath]. */
2525
private val unassertedEnvelopes = mutableListOf<RelayAsserter.RelayResponse>()
2626

27+
/** Standalone app start transactions, kept out of [unassertedEnvelopes]. */
28+
private val appStartEnvelopes = mutableListOf<RelayAsserter.RelayResponse>()
29+
2730
/** List of responses to return when a request is sent. */
2831
private val responses = mutableListOf<(RecordedRequest) -> MockResponse?>()
2932

@@ -34,6 +37,17 @@ class MockRelay(
3437
relay.dispatcher =
3538
object : Dispatcher() {
3639
override fun dispatch(request: RecordedRequest): MockResponse {
40+
// Whether a headless app start is sent depends on the process importance, which tests
41+
// can't control. So app start transactions aren't counted as requests and don't use up
42+
// custom responses.
43+
if (request.path == envelopePath) {
44+
val appStart = RelayAsserter.RelayResponse(request, MockResponse())
45+
if (appStart.isAppStartTransaction()) {
46+
appStartEnvelopes.add(appStart)
47+
return appStart.response
48+
}
49+
}
50+
3751
// We check if there is any custom response previously set to return to this request,
3852
// otherwise we return a successful MockResponse.
3953
val response = responses.removeFirstOrNull()?.let { it(request) } ?: MockResponse()
@@ -133,6 +147,6 @@ class MockRelay(
133147
}
134148
}
135149
}
136-
assertion(RelayAsserter(unassertedEnvelopes))
150+
assertion(RelayAsserter(unassertedEnvelopes, appStartEnvelopes))
137151
}
138152
}

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ package io.sentry.uitest.android.mockservers
33
import io.sentry.EnvelopeReader
44
import io.sentry.Sentry
55
import io.sentry.SentryEnvelope
6+
import io.sentry.SentryItemType
7+
import io.sentry.assertEnvelopeTransaction
68
import io.sentry.uitest.android.describeForTest
79
import java.io.IOException
810
import java.util.zip.GZIPInputStream
11+
import kotlin.test.assertEquals
912
import okhttp3.mockwebserver.MockResponse
1013
import okhttp3.mockwebserver.RecordedRequest
1114
import okio.GzipSource
1215
import okio.buffer
1316

1417
/** Class used to assert requests sent to [MockRelay]. */
15-
class RelayAsserter(private val unassertedEnvelopes: MutableList<RelayResponse>) {
18+
class RelayAsserter(
19+
private val unassertedEnvelopes: MutableList<RelayResponse>,
20+
private val appStartEnvelopes: List<RelayResponse>,
21+
) {
1622
private val originalUnassertedEnvelopes: MutableList<RelayResponse> =
1723
ArrayList(unassertedEnvelopes)
1824

@@ -43,6 +49,19 @@ class RelayAsserter(private val unassertedEnvelopes: MutableList<RelayResponse>)
4349
return unassertedEnvelopes.removeAt(relayResponseIndex)
4450
}
4551

52+
/**
53+
* Asserts exactly one standalone app start transaction was sent. Only for tests that launch an
54+
* Activity: each test runs in a new process, so its first Activity is an app start.
55+
*/
56+
fun assertAppStartTransaction() {
57+
val appStart =
58+
appStartEnvelopes.singleOrNull()
59+
?: throw AssertionError(
60+
"Expected one app start transaction, but got ${appStartEnvelopes.size}",
61+
)
62+
appStart.assert { assertEquals("app.start", it.assertTransaction().contexts.trace?.operation) }
63+
}
64+
4665
/** Asserts no other envelopes were sent. */
4766
fun assertNoOtherEnvelopes() {
4867
if (unassertedEnvelopes.isNotEmpty()) {
@@ -59,12 +78,19 @@ class RelayAsserter(private val unassertedEnvelopes: MutableList<RelayResponse>)
5978
val envelope: SentryEnvelope? by lazy {
6079
try {
6180
EnvelopeReader(Sentry.getCurrentScopes().options.serializer)
62-
.read(GZIPInputStream(request.body.inputStream()))
81+
// A copy, so that MockRelay can check for an app start before keeping the request
82+
.read(GZIPInputStream(request.body.clone().inputStream()))
6383
} catch (e: IOException) {
6484
null
6585
}
6686
}
6787

88+
fun isAppStartTransaction(): Boolean {
89+
val items = envelope?.items?.toList() ?: return false
90+
return items.any { it.header.type == SentryItemType.Transaction } &&
91+
assertEnvelopeTransaction(items).transaction == "App Start"
92+
}
93+
6894
/** Run [assertion] on this request parsed as an envelope. */
6995
fun assert(assertion: (asserter: EnvelopeAsserter) -> Unit) {
7096
// Parse the request to rebuild the original envelope. If it fails we throw an assertion

0 commit comments

Comments
 (0)