-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathBufferCaptureStrategyTest.kt
More file actions
381 lines (320 loc) · 11.5 KB
/
BufferCaptureStrategyTest.kt
File metadata and controls
381 lines (320 loc) · 11.5 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
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
package io.sentry.android.replay.capture
import android.graphics.Bitmap
import android.view.MotionEvent
import io.sentry.IScopes
import io.sentry.Scope
import io.sentry.ScopeCallback
import io.sentry.SentryOptions
import io.sentry.SentryReplayEvent.ReplayType
import io.sentry.android.replay.DefaultReplayBreadcrumbConverter
import io.sentry.android.replay.GeneratedVideo
import io.sentry.android.replay.ReplayCache
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_ID
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_REPLAY_ID
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_REPLAY_TYPE
import io.sentry.android.replay.ReplayCache.Companion.SEGMENT_KEY_TIMESTAMP
import io.sentry.android.replay.ReplayFrame
import io.sentry.android.replay.ScreenshotRecorderConfig
import io.sentry.android.replay.capture.BufferCaptureStrategyTest.Fixture.Companion.VIDEO_DURATION
import io.sentry.protocol.SentryId
import io.sentry.transport.CurrentDateProvider
import io.sentry.transport.ICurrentDateProvider
import io.sentry.util.Random
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import org.awaitility.kotlin.await
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
class BufferCaptureStrategyTest {
@get:Rule val tmpDir = TemporaryFolder()
internal class Fixture {
companion object {
const val VIDEO_DURATION = 5000L
}
val options =
SentryOptions().apply {
setReplayController(
mock { on { breadcrumbConverter }.thenReturn(DefaultReplayBreadcrumbConverter()) }
)
}
val scope = Scope(options)
val scopes =
mock<IScopes> {
doAnswer { (it.arguments[0] as ScopeCallback).run(scope) }
.whenever(it)
.configureScope(any())
}
var persistedSegment = LinkedHashMap<String, String?>()
val replayCache =
mock<ReplayCache> {
on { frames }
.thenReturn(mutableListOf(ReplayFrame(File("1720693523997.jpg"), 1720693523997)))
on { persistSegmentValues(any(), anyOrNull()) }
.then { persistedSegment.put(it.arguments[0].toString(), it.arguments[1]?.toString()) }
on {
createVideoOf(
anyLong(),
anyLong(),
anyInt(),
anyInt(),
anyInt(),
anyInt(),
anyInt(),
any(),
)
}
.thenReturn(GeneratedVideo(File("0.mp4"), 5, VIDEO_DURATION))
}
val recorderConfig =
ScreenshotRecorderConfig(
recordingWidth = 1080,
recordingHeight = 1920,
scaleFactorX = 1f,
scaleFactorY = 1f,
frameRate = 1,
bitRate = 20_000,
)
fun getSut(
onErrorSampleRate: Double = 1.0,
dateProvider: ICurrentDateProvider = CurrentDateProvider.getInstance(),
replayCacheDir: File? = null,
): BufferCaptureStrategy {
replayCacheDir?.let { whenever(replayCache.replayCacheDir).thenReturn(it) }
options.run { sessionReplay.onErrorSampleRate = onErrorSampleRate }
return BufferCaptureStrategy(
options,
scopes,
dateProvider,
Random(),
mock {
doAnswer { invocation ->
(invocation.arguments[0] as Runnable).run()
null
}
.whenever(it)
.submit(any<Runnable>())
},
) { _ ->
replayCache
}
}
fun mockedMotionEvent(action: Int): MotionEvent = mock {
on { actionMasked }.thenReturn(action)
on { getPointerId(anyInt()) }.thenReturn(0)
on { findPointerIndex(anyInt()) }.thenReturn(0)
on { getX(anyInt()) }.thenReturn(1f)
on { getY(anyInt()) }.thenReturn(1f)
}
}
private val fixture = Fixture()
@Test
fun `start does not set replayId on scope for buffered session`() {
val strategy = fixture.getSut()
val replayId = SentryId()
strategy.start(0, replayId)
assertEquals(SentryId.EMPTY_ID, fixture.scope.replayId)
assertEquals(replayId, strategy.currentReplayId)
assertEquals(0, strategy.currentSegment)
}
@Test
fun `start persists segment values`() {
val strategy = fixture.getSut()
val replayId = SentryId()
strategy.start(0, replayId)
assertEquals("0", fixture.persistedSegment[SEGMENT_KEY_ID])
assertEquals(replayId.toString(), fixture.persistedSegment[SEGMENT_KEY_REPLAY_ID])
assertEquals(ReplayType.BUFFER.toString(), fixture.persistedSegment[SEGMENT_KEY_REPLAY_TYPE])
assertTrue(fixture.persistedSegment[SEGMENT_KEY_TIMESTAMP]?.isNotEmpty() == true)
}
@Test
fun `pause creates but does not capture current segment`() {
val strategy = fixture.getSut()
strategy.start(0, SentryId())
strategy.onConfigurationChanged(fixture.recorderConfig)
strategy.pause()
await.until { strategy.currentSegment == 1 }
verify(fixture.scopes, never()).captureReplay(any(), any())
assertEquals(1, strategy.currentSegment)
}
@Test
fun `stop clears replay cache dir`() {
val replayId = SentryId()
val currentReplay = File(fixture.options.cacheDirPath, "replay_$replayId").also { it.mkdirs() }
val strategy = fixture.getSut(replayCacheDir = currentReplay)
strategy.start(0, replayId)
strategy.stop()
verify(fixture.scopes, never()).captureReplay(any(), any())
assertEquals(SentryId.EMPTY_ID, strategy.currentReplayId)
assertEquals(-1, strategy.currentSegment)
assertFalse(currentReplay.exists())
verify(fixture.replayCache).close()
}
@Test
fun `onScreenshotRecorded adds screenshot to cache`() {
val now = System.currentTimeMillis() + (fixture.options.sessionReplay.errorReplayDuration * 5)
val strategy = fixture.getSut(dateProvider = { now })
strategy.start()
strategy.onScreenshotRecorded(mock<Bitmap>()) { frameTimestamp ->
assertEquals(now, frameTimestamp)
}
}
@Test
fun `onScreenshotRecorded rotates screenshots when out of buffer bounds`() {
val now = System.currentTimeMillis() + (fixture.options.sessionReplay.errorReplayDuration * 5)
val strategy = fixture.getSut(dateProvider = { now })
strategy.start()
strategy.onScreenshotRecorded(mock<Bitmap>()) { frameTimestamp ->
assertEquals(now, frameTimestamp)
}
verify(fixture.replayCache).rotate(eq(now - fixture.options.sessionReplay.errorReplayDuration))
}
@Test
fun `onConfigurationChanged creates new segment and updates config`() {
val strategy = fixture.getSut()
strategy.start()
strategy.onConfigurationChanged(fixture.recorderConfig)
val newConfig = fixture.recorderConfig.copy(recordingHeight = 1080, recordingWidth = 1920)
strategy.onConfigurationChanged(newConfig)
await.until { strategy.currentSegment == 1 }
verify(fixture.scopes, never()).captureReplay(any(), any())
assertEquals(1, strategy.currentSegment)
}
@Test
fun `convert does nothing when process is terminating`() {
val strategy = fixture.getSut()
strategy.start()
strategy.captureReplay(true) {}
val converted = strategy.convert()
assertTrue(converted is BufferCaptureStrategy)
}
@Test
fun `convert converts to session strategy and sets replayId to scope`() {
val strategy = fixture.getSut()
strategy.start()
val converted = strategy.convert()
assertTrue(converted is SessionCaptureStrategy)
assertEquals(strategy.currentReplayId, fixture.scope.replayId)
}
@Test
fun `convert persists buffer replayType when converting to session strategy`() {
val strategy = fixture.getSut()
strategy.start()
val converted = strategy.convert()
assertEquals(ReplayType.BUFFER, converted.replayType)
}
@Test
fun `createCurrentSegment uses first frame timestamp when available`() {
val now = System.currentTimeMillis()
val strategy = fixture.getSut(dateProvider = { now })
strategy.start()
strategy.onConfigurationChanged(fixture.recorderConfig)
// Stub first frame timestamp and capture the 'from' argument to createVideoOf
whenever(fixture.replayCache.firstFrameTimestamp()).thenReturn(1234L)
var capturedFrom: Long = -1
whenever(
fixture.replayCache.createVideoOf(
anyLong(),
anyLong(),
anyInt(),
anyInt(),
anyInt(),
anyInt(),
anyInt(),
any(),
)
)
.thenAnswer { invocation ->
capturedFrom = invocation.arguments[1] as Long
GeneratedVideo(File("0.mp4"), 5, VIDEO_DURATION)
}
strategy.pause()
assertEquals(1234L, capturedFrom)
assertEquals(1, strategy.currentSegment)
}
@Test
fun `createCurrentSegment falls back to buffer start when no frames`() {
val now = System.currentTimeMillis()
val strategy = fixture.getSut(dateProvider = { now })
strategy.start()
strategy.onConfigurationChanged(fixture.recorderConfig)
// No frames available
whenever(fixture.replayCache.firstFrameTimestamp()).thenReturn(null)
var capturedFrom: Long = -1
whenever(
fixture.replayCache.createVideoOf(
anyLong(),
anyLong(),
anyInt(),
anyInt(),
anyInt(),
anyInt(),
anyInt(),
any(),
)
)
.thenAnswer { invocation ->
capturedFrom = invocation.arguments[1] as Long
GeneratedVideo(File("0.mp4"), 5, VIDEO_DURATION)
}
strategy.pause()
assertEquals(now - fixture.options.sessionReplay.errorReplayDuration, capturedFrom)
assertEquals(1, strategy.currentSegment)
}
@Test
fun `captureReplay does not replayId to scope when not sampled`() {
val strategy = fixture.getSut(onErrorSampleRate = 0.0)
strategy.start()
strategy.captureReplay(false) {}
assertEquals(SentryId.EMPTY_ID, fixture.scope.replayId)
}
@Test
fun `captureReplay sets replayId to scope and captures buffered segments`() {
var called = false
val strategy = fixture.getSut()
strategy.start()
strategy.onConfigurationChanged(fixture.recorderConfig)
strategy.pause()
strategy.captureReplay(false) { called = true }
// buffered + current = 2
verify(fixture.scopes, times(2)).captureReplay(any(), any())
assertEquals(strategy.currentReplayId, fixture.scope.replayId)
assertTrue(called)
}
@Test
fun `captureReplay sets new segment timestamp to new strategy after successful creation`() {
val strategy = fixture.getSut()
strategy.start()
strategy.onConfigurationChanged(fixture.recorderConfig)
val oldTimestamp = strategy.segmentTimestamp
strategy.captureReplay(false) { newTimestamp ->
assertEquals(oldTimestamp!!.time + VIDEO_DURATION, newTimestamp.time)
}
verify(fixture.scopes).captureReplay(any(), any())
}
@Test
fun `replayId should be set and serialized first`() {
val strategy = fixture.getSut()
val replayId = SentryId()
strategy.start(0, replayId)
assertEquals(
replayId.toString(),
fixture.persistedSegment.values.first(),
"The replayId must be set first, so when we clean up stale replays" +
"the current replay cache folder is not being deleted.",
)
}
}