-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSdkInitTests.kt
More file actions
296 lines (263 loc) · 9.83 KB
/
SdkInitTests.kt
File metadata and controls
296 lines (263 loc) · 9.83 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package io.sentry.uitest.android
import android.os.StrictMode
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.launchActivity
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.ProfilingTraceData
import io.sentry.Sentry
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.android.core.AndroidLogger
import io.sentry.android.core.CurrentActivityHolder
import io.sentry.android.core.NdkIntegration
import io.sentry.android.core.SentryAndroidOptions
import io.sentry.assertEnvelopeTransaction
import io.sentry.protocol.SentryTransaction
import java.util.concurrent.CountDownLatch
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import leakcanary.LeakAssertions
import leakcanary.LeakCanary
import org.junit.runner.RunWith
import shark.AndroidReferenceMatchers
import shark.IgnoredReferenceMatcher
import shark.ReferencePattern
@RunWith(AndroidJUnit4::class)
class SdkInitTests : BaseUiTest() {
@Test
fun doubleInitDoesNotThrow() {
initSentry(false) { options: SentryAndroidOptions ->
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
val transaction = Sentry.startTransaction("e2etests", "testInit")
val sampleScenario = launchActivity<EmptyActivity>()
initSentry(false) { options: SentryAndroidOptions ->
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
transaction.finish()
sampleScenario.moveToState(Lifecycle.State.DESTROYED)
val transaction2 = Sentry.startTransaction("e2etests", "testInit")
transaction2.finish()
}
@Test
fun doubleInitWithSameOptionsDoesNotThrow() {
val options = SentryAndroidOptions()
initSentry(true) {
it.tracesSampleRate = 1.0
it.profilesSampleRate = 1.0
// We use the same executorService before and after closing the SDK
it.executorService = options.executorService
it.isDebug = true
}
val transaction = Sentry.startTransaction("e2etests", "testInit")
val sampleScenario = launchActivity<EmptyActivity>()
initSentry(true) {
it.tracesSampleRate = 1.0
it.profilesSampleRate = 1.0
// We use the same executorService before and after closing the SDK
it.executorService = options.executorService
it.isDebug = true
}
relayIdlingResource.increment()
relayIdlingResource.increment()
relayIdlingResource.increment()
transaction.finish()
sampleScenario.moveToState(Lifecycle.State.DESTROYED)
val transaction2 = Sentry.startTransaction("e2etests2", "testInit")
transaction2.finish()
relay.assert {
findEnvelope {
assertEnvelopeTransaction(it.items.toList(), AndroidLogger()).transaction == "e2etests"
}
.assert {
val transactionItem: SentryTransaction = it.assertTransaction()
it.assertNoOtherItems()
assertEquals("e2etests", transactionItem.transaction)
}
findEnvelope {
assertEnvelopeTransaction(it.items.toList(), AndroidLogger()).transaction ==
"EmptyActivity"
}
.assert {
val transactionItem: SentryTransaction = it.assertTransaction()
// Transaction-based Profiling is already in e2etests transaction, so it won't run again
// here
it.assertNoOtherItems()
assertEquals("EmptyActivity", transactionItem.transaction)
}
findEnvelope {
assertEnvelopeTransaction(it.items.toList(), AndroidLogger()).transaction == "e2etests2"
}
.assert {
val transactionItem: SentryTransaction = it.assertTransaction()
// Profiling uses executorService, so if the executorService is shutdown it would fail
val profilingTraceData: ProfilingTraceData = it.assertProfile()
it.assertNoOtherItems()
assertEquals("e2etests2", transactionItem.transaction)
assertEquals("e2etests2", profilingTraceData.transactionName)
}
assertNoOtherEnvelopes()
}
}
@Test
fun doubleInitDoesNotWait() {
initSentry(true)
val beforeRestart = System.currentTimeMillis()
// We restart the SDK. This shouldn't block the main thread, but new options (e.g. profiling)
// should work
initSentry(true) { options: SentryAndroidOptions ->
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
val afterRestart = System.currentTimeMillis()
val restartMs = afterRestart - beforeRestart
relayIdlingResource.increment()
Sentry.startTransaction("afterRestart", "emptyTransaction").finish()
// We assert for less than 1 second just to account for slow devices in saucelabs or headless
// emulator
assertTrue(restartMs < 1000, "Expected less than 1000 ms for SDK restart. Got $restartMs ms")
relay.assert {
findEnvelope { assertEnvelopeTransaction(it.items.toList()).transaction == "afterRestart" }
.assert {
it.assertTransaction()
// There is a profiling item, as in the second init it was enabled
it.assertProfile()
it.assertNoOtherItems()
}
assertNoOtherEnvelopes()
}
}
@Test
fun initCloseInitWaits() {
relayIdlingResource.increment()
// Let's make the first request timeout
relay.addTimeoutResponse()
initSentry(true) { options: SentryAndroidOptions ->
options.tracesSampleRate = 1.0
options.flushTimeoutMillis = 3000
}
Sentry.startTransaction("beforeRestart", "emptyTransaction").finish()
// We want the SDK to start sending the event. If we don't wait, it's possible we don't send
// anything before the SDK is restarted
waitUntilIdle()
val beforeRestart = System.currentTimeMillis()
Sentry.close()
// We stop the SDK. This should block the main thread. Then we start it again with new options
initSentry(true) { options: SentryAndroidOptions ->
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
val afterRestart = System.currentTimeMillis()
val restartMs = afterRestart - beforeRestart
assertTrue(
restartMs > 3000,
"Expected more than 3000 ms for SDK close and restart. Got $restartMs ms",
)
}
@Test
fun initViaActivityDoesNotLeak() {
LeakCanary.config =
LeakCanary.config.copy(
referenceMatchers =
AndroidReferenceMatchers.appDefaults +
listOf(
IgnoredReferenceMatcher(
ReferencePattern.InstanceFieldPattern(
"com.saucelabs.rdcinjector.testfairy.TestFairyEventQueue",
"context",
)
)
) +
('a'..'z').map { char ->
IgnoredReferenceMatcher(
ReferencePattern.StaticFieldPattern(
"com.testfairy.modules.capture.TouchListener",
"$char",
)
)
}
)
val activityScenario = launchActivity<ComposeActivity>()
activityScenario.moveToState(Lifecycle.State.RESUMED)
activityScenario.onActivity { activity -> initSentry(context = activity) }
activityScenario.moveToState(Lifecycle.State.DESTROYED)
LeakAssertions.assertNoLeaks()
}
@Test
fun foregroundInitInstallsDefaultIntegrations() {
val activityScenario = launchActivity<ComposeActivity>()
activityScenario.moveToState(Lifecycle.State.RESUMED)
activityScenario.onActivity { activity ->
// Our SentryInitProvider does not run in this test
// so we need to set the current activity manually
CurrentActivityHolder.getInstance().setActivity(activity)
initSentry(false) { options: SentryAndroidOptions ->
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
}
activityScenario.moveToState(Lifecycle.State.DESTROYED)
assertDefaultIntegrations()
}
@Test
fun backgroundInitInstallsDefaultIntegrations() {
val initLatch = CountDownLatch(1)
val activityScenario = launchActivity<ComposeActivity>()
activityScenario.moveToState(Lifecycle.State.RESUMED)
activityScenario.onActivity { activity ->
// Our SentryInitProvider does not run in this test
// so we need to set the current activity manually
CurrentActivityHolder.getInstance().setActivity(activity)
Thread {
initSentry(false) { options: SentryAndroidOptions ->
options.tracesSampleRate = 1.0
options.profilesSampleRate = 1.0
}
initLatch.countDown()
}
.start()
}
initLatch.await()
activityScenario.moveToState(Lifecycle.State.DESTROYED)
assertDefaultIntegrations()
}
@Test
fun initNotThrowStrictMode() {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build())
val sampleScenario = launchActivity<EmptyActivity>()
initSentry()
sampleScenario.moveToState(Lifecycle.State.DESTROYED)
}
private fun assertDefaultIntegrations() {
val integrations =
mutableListOf(
"UncaughtExceptionHandler",
"ShutdownHook",
"SendCachedEnvelope",
"AppLifecycle",
"EnvelopeFileObserver",
"AnrV2",
"ActivityLifecycle",
"ActivityBreadcrumbs",
"UserInteraction",
"AppComponentsBreadcrumbs",
"NetworkBreadcrumbs",
)
// NdkIntegration is not always available, so we check for its presence
try {
Class.forName(NdkIntegration.SENTRY_NDK_CLASS_NAME)
integrations.add("Ndk")
} catch (_: ClassNotFoundException) {
// ignored, in case the app is build without NDK support
}
for (integration in integrations) {
assertTrue(
SentryIntegrationPackageStorage.getInstance().integrations.contains(integration),
"Integration $integration was expected, but was not registered",
)
}
}
}