-
-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathSentryTracedTest.kt
More file actions
113 lines (99 loc) · 3.93 KB
/
Copy pathSentryTracedTest.kt
File metadata and controls
113 lines (99 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package io.sentry.compose
import android.app.Application
import android.content.ComponentName
import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.IScopes
import io.sentry.ITransaction
import io.sentry.SentryOptions
import io.sentry.TransactionOptions
import io.sentry.test.createTestScopes
import kotlin.test.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import org.junit.runner.RunWith
import org.robolectric.Shadows
import org.robolectric.annotation.Config
@OptIn(ExperimentalComposeUiApi::class)
@RunWith(AndroidJUnit4::class)
@Config(sdk = [30])
class SentryTracedTest {
// workaround for robolectric tests with composeRule
// from https://github.com/robolectric/robolectric/pull/4736#issuecomment-1831034882
@get:Rule(order = 1)
val addActivityToRobolectricRule =
object : TestWatcher() {
override fun starting(description: Description?) {
super.starting(description)
val appContext: Application = ApplicationProvider.getApplicationContext()
Shadows.shadowOf(appContext.packageManager)
.addActivityIfNotPresent(
ComponentName(appContext.packageName, ComponentActivity::class.java.name)
)
}
}
@get:Rule(order = 2) val rule = createAndroidComposeRule<ComponentActivity>()
private fun newTracingScopes(): IScopes =
createTestScopes(
SentryOptions().apply {
dsn = "https://key@sentry.io/proj"
tracesSampleRate = 1.0
}
)
private fun IScopes.startBoundTransaction(name: String): ITransaction =
startTransaction(name, "test", TransactionOptions().apply { isBindToScope = true })
@Test
fun `SentryTraced creates its root span on the scopes provided via LocalSentryScopes`() {
val scopes = newTracingScopes()
val tx = scopes.startBoundTransaction("custom-scopes-tx")
rule.setContent {
CompositionLocalProvider(LocalSentryScopes provides scopes) {
SentryTraced(tag = "custom") { Box {} }
}
}
rule.waitForIdle()
assertEquals(1, tx.spans.count { it.operation == "ui.compose.composition" })
assertEquals(1, tx.spans.count { it.operation == "ui.compose" })
}
@Test
fun `sibling SentryTraced composables under the same scopes share one root span`() {
val scopes = newTracingScopes()
val tx = scopes.startBoundTransaction("custom-scopes-tx")
rule.setContent {
CompositionLocalProvider(LocalSentryScopes provides scopes) {
SentryTraced(tag = "first") { Box {} }
SentryTraced(tag = "second") { Box {} }
}
}
rule.waitForIdle()
assertEquals(1, tx.spans.count { it.operation == "ui.compose.composition" })
assertEquals(2, tx.spans.count { it.operation == "ui.compose" })
}
@Test
fun `SentryTraced composables under different scopes do not interfere with each other`() {
val scopesA = newTracingScopes()
val txA = scopesA.startBoundTransaction("scopes-a-tx")
val scopesB = newTracingScopes()
val txB = scopesB.startBoundTransaction("scopes-b-tx")
rule.setContent {
CompositionLocalProvider(LocalSentryScopes provides scopesA) {
SentryTraced(tag = "a") { Box {} }
}
CompositionLocalProvider(LocalSentryScopes provides scopesB) {
SentryTraced(tag = "b") { Box {} }
}
}
rule.waitForIdle()
assertEquals(1, txA.spans.count { it.operation == "ui.compose.composition" })
assertEquals(1, txA.spans.count { it.operation == "ui.compose" })
assertEquals(1, txB.spans.count { it.operation == "ui.compose.composition" })
assertEquals(1, txB.spans.count { it.operation == "ui.compose" })
}
}