-
Notifications
You must be signed in to change notification settings - Fork 7
/
EmbraceApplicationExitInfoServiceTest.kt
417 lines (354 loc) · 14.4 KB
/
EmbraceApplicationExitInfoServiceTest.kt
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package io.embrace.android.embracesdk
import android.app.ActivityManager
import android.app.ApplicationExitInfo
import com.google.common.util.concurrent.MoreExecutors
import io.embrace.android.embracesdk.capture.aei.EmbraceApplicationExitInfoService
import io.embrace.android.embracesdk.config.remote.AppExitInfoConfig
import io.embrace.android.embracesdk.config.remote.RemoteConfig
import io.embrace.android.embracesdk.fakes.FakeConfigService
import io.embrace.android.embracesdk.fakes.FakeMetadataService
import io.embrace.android.embracesdk.fakes.FakePreferenceService
import io.embrace.android.embracesdk.fakes.FakeSessionIdTracker
import io.embrace.android.embracesdk.fakes.FakeUserService
import io.embrace.android.embracesdk.fakes.fakeAppExitInfoBehavior
import io.embrace.android.embracesdk.worker.BackgroundWorker
import io.mockk.every
import io.mockk.mockk
import io.mockk.unmockkAll
import org.junit.AfterClass
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.IOException
private const val TIMESTAMP = 15000000000L
private const val PID = 6952
private const val IMPORTANCE = 125
private const val PSS = 1509123409L
private const val RSS = 1123409L
private const val REASON = 4
private const val STATUS = 1
private const val DESCRIPTION = "testDescription"
private const val TRACE = "testInputStream"
private const val SESSION_ID = "1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d"
internal class EmbraceApplicationExitInfoServiceTest {
private lateinit var applicationExitInfoService: EmbraceApplicationExitInfoService
private val worker = BackgroundWorker(MoreExecutors.newDirectExecutorService())
private var appExitInfoConfig = AppExitInfoConfig(pctAeiCaptureEnabled = 100.0f)
private val configService = FakeConfigService(
appExitInfoBehavior = fakeAppExitInfoBehavior {
RemoteConfig(appExitInfoConfig = appExitInfoConfig)
}
)
private val deliveryService = FakeDeliveryService()
private val preferenceService = FakePreferenceService()
private val metadataService = FakeMetadataService()
private val sessionIdTracker = FakeSessionIdTracker()
private val userService = FakeUserService()
private val mockActivityManager: ActivityManager = mockk {
every { getHistoricalProcessExitReasons(any(), any(), any()) } returns emptyList()
}
private val mockAppExitInfo = mockk<ApplicationExitInfo>(relaxed = true) {
every { timestamp } returns TIMESTAMP
every { pid } returns PID
every { processStateSummary } returns SESSION_ID.toByteArray()
every { importance } returns IMPORTANCE
every { pss } returns PSS
every { reason } returns REASON
every { rss } returns RSS
every { status } returns STATUS
every { description } returns DESCRIPTION
every { traceInputStream } returns TRACE.byteInputStream()
}
companion object {
@AfterClass
@JvmStatic
fun afterClass() {
unmockkAll()
}
}
private fun startApplicationExitInfoService() {
applicationExitInfoService = EmbraceApplicationExitInfoService(
worker,
configService,
mockActivityManager,
preferenceService,
deliveryService,
metadataService,
sessionIdTracker,
userService
)
}
@Test
fun `AEI data capture happy path`() {
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
startApplicationExitInfoService()
// when getCapturedData is called
with(getLastAeiRequest()) {
assertEquals(TIMESTAMP, timestamp)
assertEquals(SESSION_ID, sessionId)
assertEquals(IMPORTANCE, importance)
assertEquals(PSS, pss)
assertEquals(RSS, rss)
assertEquals(STATUS, status)
assertEquals(DESCRIPTION, description)
assertEquals(TRACE, trace)
assertEquals("", sessionIdError)
assertNull(traceStatus)
}
}
@Test
fun `service should stop execution if remote config changed to disabled`() {
// given a service starts with config enabled
appExitInfoConfig = AppExitInfoConfig(pctAeiCaptureEnabled = 100.0f)
startApplicationExitInfoService()
// when config changes to disabled
appExitInfoConfig = AppExitInfoConfig(pctAeiCaptureEnabled = 0.0f)
applicationExitInfoService.onConfigChange(configService)
// then background execution should stop
assertNull(applicationExitInfoService.backgroundExecution)
}
@Test
fun `service should start execution if remote config changed to enabled`() {
// given a service starts with config disabled
appExitInfoConfig = AppExitInfoConfig(pctAeiCaptureEnabled = 0.0f)
startApplicationExitInfoService()
// when config changes to enabled
appExitInfoConfig = AppExitInfoConfig(pctAeiCaptureEnabled = 100.0f)
applicationExitInfoService.onConfigChange(configService)
// then background execution should start
assertNotNull(applicationExitInfoService.backgroundExecution)
}
@Test
fun `getCapturedData should return an empty list when getHistoricalProcessExitInfo returns an empty list`() {
// given getHistoricalProcessExitReasons returns an empty list
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns emptyList()
startApplicationExitInfoService()
// when getCapturedData is called
val capturedData = applicationExitInfoService.getCapturedData()
// then an empty list should be returned
assertTrue(capturedData.isEmpty())
}
@Test
fun `getHistoricalProcessExitInfo should truncate to 32 entries`() {
// given getHistoricalProcessExitReasons returns more than 32 entries
val appExitInfoListWithMoreThan32Entries = mutableListOf<ApplicationExitInfo>()
repeat(33) {
appExitInfoListWithMoreThan32Entries.add(mockAppExitInfo)
}
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns appExitInfoListWithMoreThan32Entries
startApplicationExitInfoService()
// when getCapturedData is called
val capturedData = applicationExitInfoService.getCapturedData()
// then captured data should only have 32 entries
assertEquals(32, capturedData.size)
}
@Test
fun `getUnsentExitReasons should not return AEI that have already been sent`() {
// given getHistoricalProcessExitReasons returns 3 entries, but there are 2 that have already been sent
val appExitInfo1 = mockk<ApplicationExitInfo>(relaxed = true) {
every { timestamp } returns 1L
every { pid } returns STATUS
}
val appExitInfo2 = mockk<ApplicationExitInfo>(relaxed = true) {
every { timestamp } returns 2L
every { pid } returns 2
}
val appExitInfo3 = mockk<ApplicationExitInfo>(relaxed = true) {
every { timestamp } returns 3L
every { pid } returns 3
}
val appExitInfo1Hash = "${appExitInfo1.timestamp}_${appExitInfo1.pid}"
val appExitInfo2Hash = "${appExitInfo2.timestamp}_${appExitInfo2.pid}"
every { mockActivityManager.getHistoricalProcessExitReasons(any(), any(), any()) } returns
listOf(appExitInfo1, appExitInfo2, appExitInfo3)
preferenceService.applicationExitInfoHistory = setOf(
appExitInfo1Hash,
appExitInfo2Hash
)
startApplicationExitInfoService()
// when getCapturedData is called
val capturedData = applicationExitInfoService.getCapturedData()
// then captured data should only have applicationExitInfo3
assertEquals(3L, capturedData[0].timestamp)
assertEquals(STATUS, capturedData.size)
}
@Test
fun `getCapturedData gets AEI without traces`() {
// given an AEI with a trace
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
startApplicationExitInfoService()
// when getCapturedData is called
val capturedData = applicationExitInfoService.getCapturedData()
// then it shouldn't have a trace, and should retrieve the correct data
assertEquals(SESSION_ID, capturedData[0].sessionId)
assertEquals(null, capturedData[0].trace)
assertEquals(null, capturedData[0].traceStatus)
assertEquals("", capturedData[0].sessionIdError)
assertEquals(STATUS, capturedData.size)
}
@Test
fun `invalid session id should show up in ApplicationExitInfoData sessionIdError`() {
// given an AEI with an invalid session ID
val invalidSessionId = "_ 1NV@lid"
every { mockAppExitInfo.processStateSummary } returns invalidSessionId.toByteArray()
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
startApplicationExitInfoService()
// when getCapturedData is called
val capturedData = applicationExitInfoService.getCapturedData()
// then the invalid session ID message should be added to the sessionIdError
assertEquals("invalid session ID: $invalidSessionId", capturedData[0].sessionIdError)
assertEquals(invalidSessionId, capturedData[0].sessionId)
}
@Test
fun `null traces won't be sent to the blob endpoint`() {
// given an application exit info with a null trace
every { mockAppExitInfo.traceInputStream } returns null
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
// when the service is started
startApplicationExitInfoService()
// then no null traces should be sent
assertTrue(deliveryService.blobMessages.isEmpty())
}
@Test
fun `OOM while reading trace`() {
// given an OOM happens when reading a trace
every { mockAppExitInfo.traceInputStream } throws OutOfMemoryError("Ouch")
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
// when the service is started
startApplicationExitInfoService()
// then a null trace should be sent
val payload = checkNotNull(getLastAeiRequest())
assertNull(payload.trace)
assertEquals("oom: Ouch", payload.traceStatus)
}
@Test
fun `IOException while reading trace`() {
// given an IO exception happens when reading a trace
every { mockAppExitInfo.traceInputStream } throws IOException("Ouch")
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
// when the service is started
startApplicationExitInfoService()
// then a null trace should be sent
val payload = getLastAeiRequest()
assertNull(payload.trace)
assertEquals("ioexception: Ouch", payload.traceStatus)
}
@Test
fun `other error while reading trace`() {
val errorMessage = "Please turn your computer screen back on."
// given an IO exception happens when reading a trace
every { mockAppExitInfo.traceInputStream } throws IllegalMonitorStateException(errorMessage)
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
// when the service is started
startApplicationExitInfoService()
// then a null trace should be sent
val payload = getLastAeiRequest()
assertNull(payload.trace)
assertEquals("error: $errorMessage", payload.traceStatus)
}
@Test
fun `Truncate trace if it exceeds limit`() {
// given a trace that exceeds the limit
every { mockAppExitInfo.traceInputStream } returns "a".repeat(500).byteInputStream()
appExitInfoConfig =
AppExitInfoConfig(pctAeiCaptureEnabled = 100.0f, appExitInfoTracesLimit = 100)
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns listOf(mockAppExitInfo)
// when the service is started
startApplicationExitInfoService()
// then a null trace should be sent
val payload = getLastAeiRequest()
assertEquals("a".repeat(100), payload.trace)
}
@Test
fun testActivityManagerException() {
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} throws NullPointerException()
// when the service is started
startApplicationExitInfoService()
// then a null trace should be sent
assertTrue(deliveryService.blobMessages.isEmpty())
}
@Test
fun `one object sent per payload`() {
val entries = (0..32).map { mockAppExitInfo }
every {
mockActivityManager.getHistoricalProcessExitReasons(
any(),
any(),
any()
)
} returns entries
startApplicationExitInfoService()
// each AEI object with a trace should be sent in a separate payload
val payloads = checkNotNull(deliveryService.blobMessages)
assertEquals(32, payloads.size)
}
private fun getLastAeiRequest() = deliveryService.blobMessages.single().applicationExits.single()
}