-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathUncaughtExceptionHandlerIntegrationTest.kt
More file actions
470 lines (368 loc) · 17.5 KB
/
UncaughtExceptionHandlerIntegrationTest.kt
File metadata and controls
470 lines (368 loc) · 17.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package io.sentry
import io.sentry.UncaughtExceptionHandlerIntegration.UncaughtExceptionHint
import io.sentry.exception.ExceptionMechanismException
import io.sentry.hints.DiskFlushNotification
import io.sentry.hints.EventDropReason.MULTITHREADED_DEDUPLICATION
import io.sentry.protocol.SentryId
import io.sentry.test.createTestScopes
import io.sentry.util.HintUtils
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.argThat
import org.mockito.kotlin.argWhere
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.file.Files
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Executors
import kotlin.concurrent.thread
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
class UncaughtExceptionHandlerIntegrationTest {
class Fixture {
val file = Files.createTempDirectory("sentry-disk-cache-test").toAbsolutePath().toFile()
internal val handler = mock<UncaughtExceptionHandler>()
val defaultHandler = mock<Thread.UncaughtExceptionHandler>()
val thread = mock<Thread>()
val throwable = Throwable("test")
val scopes = mock<IScopes>()
val options = SentryOptions()
val logger = mock<ILogger>()
fun getSut(
flushTimeoutMillis: Long = 0L,
hasDefaultHandler: Boolean = false,
enableUncaughtExceptionHandler: Boolean = true,
isPrintUncaughtStackTrace: Boolean = false
): UncaughtExceptionHandlerIntegration {
options.flushTimeoutMillis = flushTimeoutMillis
options.isEnableUncaughtExceptionHandler = enableUncaughtExceptionHandler
options.isPrintUncaughtStackTrace = isPrintUncaughtStackTrace
options.setLogger(logger)
options.isDebug = true
whenever(handler.defaultUncaughtExceptionHandler).thenReturn(if (hasDefaultHandler) defaultHandler else null)
return UncaughtExceptionHandlerIntegration(handler)
}
}
private val fixture = Fixture()
@Test
fun `when UncaughtExceptionHandlerIntegration is initialized, uncaught handler is unchanged`() {
fixture.getSut(isPrintUncaughtStackTrace = false)
verify(fixture.handler, never()).defaultUncaughtExceptionHandler = any()
}
@Test
fun `when uncaughtException is called, sentry captures exception`() {
val sut = fixture.getSut(isPrintUncaughtStackTrace = false)
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.scopes).captureEvent(any(), any<Hint>())
}
@Test
fun `when register is called, current handler is not lost`() {
val sut = fixture.getSut(hasDefaultHandler = true, isPrintUncaughtStackTrace = false)
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.defaultHandler).uncaughtException(fixture.thread, fixture.throwable)
}
@Test
fun `when uncaughtException is called, exception captured has handled=false`() {
whenever(fixture.scopes.captureException(any())).thenAnswer { invocation ->
val e = invocation.getArgument<ExceptionMechanismException>(1)
assertNotNull(e)
assertNotNull(e.exceptionMechanism)
assertTrue(e.exceptionMechanism.isHandled!!)
SentryId.EMPTY_ID
}
val sut = fixture.getSut(isPrintUncaughtStackTrace = false)
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.scopes).captureEvent(any(), any<Hint>())
}
@Test
fun `when scopes is closed, integrations should be closed`() {
val integrationMock = mock<UncaughtExceptionHandlerIntegration>()
val options = SentryOptions()
options.dsn = "https://key@sentry.io/proj"
options.addIntegration(integrationMock)
options.cacheDirPath = fixture.file.absolutePath
options.setSerializer(mock())
val scopes = createTestScopes(options)
scopes.close()
verify(integrationMock).close()
}
@Test
fun `When defaultUncaughtExceptionHandler is disabled, should not install Sentry UncaughtExceptionHandler`() {
val sut = fixture.getSut(
enableUncaughtExceptionHandler = false,
isPrintUncaughtStackTrace = false
)
sut.register(fixture.scopes, fixture.options)
verify(fixture.handler, never()).defaultUncaughtExceptionHandler = any()
}
@Test
fun `When defaultUncaughtExceptionHandler is enabled, should install Sentry UncaughtExceptionHandler`() {
val sut = fixture.getSut(isPrintUncaughtStackTrace = false)
sut.register(fixture.scopes, fixture.options)
verify(fixture.handler).defaultUncaughtExceptionHandler =
argWhere { it is UncaughtExceptionHandlerIntegration }
}
@Test
fun `When defaultUncaughtExceptionHandler is set and integration is closed, default uncaught exception handler is reset to previous handler`() {
val sut = fixture.getSut(hasDefaultHandler = true, isPrintUncaughtStackTrace = false)
sut.register(fixture.scopes, fixture.options)
whenever(fixture.handler.defaultUncaughtExceptionHandler)
.thenReturn(sut)
sut.close()
verify(fixture.handler).defaultUncaughtExceptionHandler = fixture.defaultHandler
}
@Test
fun `When defaultUncaughtExceptionHandler is not set and integration is closed, default uncaught exception handler is reset to null`() {
val sut = fixture.getSut(isPrintUncaughtStackTrace = false)
sut.register(fixture.scopes, fixture.options)
whenever(fixture.handler.defaultUncaughtExceptionHandler)
.thenReturn(sut)
sut.close()
verify(fixture.handler).defaultUncaughtExceptionHandler = null
}
@Test
fun `When printUncaughtStackTrace is enabled, prints the stacktrace to standard error`() {
val standardErr = System.err
try {
val outputStreamCaptor = ByteArrayOutputStream()
System.setErr(PrintStream(outputStreamCaptor))
val sut = fixture.getSut(isPrintUncaughtStackTrace = true)
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, RuntimeException("This should be printed!"))
assertTrue(
outputStreamCaptor.toString()
.contains("java.lang.RuntimeException: This should be printed!")
)
assertTrue(
outputStreamCaptor.toString()
.contains("UncaughtExceptionHandlerIntegrationTest.kt:")
)
} finally {
System.setErr(standardErr)
}
}
@Test
fun `waits for event to flush on disk`() {
val capturedEventId = SentryId()
whenever(fixture.scopes.captureEvent(any(), any<Hint>())).thenAnswer { invocation ->
val hint = HintUtils.getSentrySdkHint(invocation.getArgument(1))
as DiskFlushNotification
thread {
Thread.sleep(1000L)
hint.markFlushed()
}
capturedEventId
}
val sut = fixture.getSut(flushTimeoutMillis = 5000)
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.scopes).captureEvent(any(), any<Hint>())
// shouldn't fall into timed out state, because we marked event as flushed on another thread
verify(fixture.logger, never()).log(
any(),
argThat { startsWith("Timed out") },
any<Any>()
)
}
@Test
fun `does not block flushing when the event was dropped`() {
whenever(fixture.scopes.captureEvent(any(), any<Hint>())).thenReturn(SentryId.EMPTY_ID)
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.scopes).captureEvent(any(), any<Hint>())
// we do not call markFlushed, hence it should time out waiting for flush, but because
// we drop the event, it should not even come to this if-check
verify(fixture.logger, never()).log(
any(),
argThat { startsWith("Timed out") },
any<Any>()
)
}
@Test
fun `waits for event to flush on disk if it was dropped by multithreaded deduplicator`() {
val hintCaptor = argumentCaptor<Hint>()
whenever(fixture.scopes.captureEvent(any(), hintCaptor.capture())).thenAnswer {
HintUtils.setEventDropReason(hintCaptor.firstValue, MULTITHREADED_DEDUPLICATION)
return@thenAnswer SentryId.EMPTY_ID
}
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.scopes).captureEvent(any(), any<Hint>())
// we do not call markFlushed, even though we dropped the event, the reason was
// MULTITHREADED_DEDUPLICATION, so it should time out
verify(fixture.logger).log(
any(),
argThat { startsWith("Timed out") },
any<Any>()
)
}
@Test
fun `when there is no active transaction on scope, sets current event id as flushable`() {
val eventCaptor = argumentCaptor<SentryEvent>()
whenever(fixture.scopes.captureEvent(eventCaptor.capture(), any<Hint>()))
.thenReturn(SentryId.EMPTY_ID)
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.scopes).captureEvent(
any(),
argThat<Hint> {
(HintUtils.getSentrySdkHint(this) as UncaughtExceptionHint)
.isFlushable(eventCaptor.firstValue.eventId)
}
)
}
@Test
fun `when there is active transaction on scope, does not set current event id as flushable`() {
val eventCaptor = argumentCaptor<SentryEvent>()
whenever(fixture.scopes.transaction).thenReturn(mock<ITransaction>())
whenever(fixture.scopes.captureEvent(eventCaptor.capture(), any<Hint>()))
.thenReturn(SentryId.EMPTY_ID)
val sut = fixture.getSut()
sut.register(fixture.scopes, fixture.options)
sut.uncaughtException(fixture.thread, fixture.throwable)
verify(fixture.scopes).captureEvent(
any(),
argThat<Hint> {
!(HintUtils.getSentrySdkHint(this) as UncaughtExceptionHint)
.isFlushable(eventCaptor.firstValue.eventId)
}
)
}
@Test
fun `multiple registrations do not cause the build-up of a tree of UncaughtExceptionHandlerIntegrations`() {
var currentDefaultHandler: Thread.UncaughtExceptionHandler? = null
val handler = mock<UncaughtExceptionHandler>()
whenever(handler.defaultUncaughtExceptionHandler).thenAnswer { currentDefaultHandler }
whenever(handler.setDefaultUncaughtExceptionHandler(anyOrNull<Thread.UncaughtExceptionHandler>())).then {
currentDefaultHandler = it.getArgument(0)
null
}
val integration1 = UncaughtExceptionHandlerIntegration(handler)
integration1.register(fixture.scopes, fixture.options)
val integration2 = UncaughtExceptionHandlerIntegration(handler)
integration2.register(fixture.scopes, fixture.options)
assertEquals(integration2, currentDefaultHandler)
integration2.close()
assertEquals(null, currentDefaultHandler)
}
@Test
fun `multiple registrations do not cause the build-up of a tree of UncaughtExceptionHandlerIntegrations, reset to inital`() {
val initialUncaughtExceptionHandler = Thread.UncaughtExceptionHandler { _, _ -> }
var currentDefaultHandler: Thread.UncaughtExceptionHandler? = initialUncaughtExceptionHandler
val handler = mock<UncaughtExceptionHandler>()
whenever(handler.defaultUncaughtExceptionHandler).thenAnswer { currentDefaultHandler }
whenever(handler.setDefaultUncaughtExceptionHandler(anyOrNull<Thread.UncaughtExceptionHandler>())).then {
currentDefaultHandler = it.getArgument(0)
null
}
val integration1 = UncaughtExceptionHandlerIntegration(handler)
integration1.register(fixture.scopes, fixture.options)
val integration2 = UncaughtExceptionHandlerIntegration(handler)
integration2.register(fixture.scopes, fixture.options)
assertEquals(currentDefaultHandler, integration2)
integration2.close()
assertEquals(initialUncaughtExceptionHandler, currentDefaultHandler)
}
@Test
fun `multiple registrations with different global scopes allowed`() {
val scopes2 = mock<IScopes>()
val initialUncaughtExceptionHandler = Thread.UncaughtExceptionHandler { _, _ -> }
var currentDefaultHandler: Thread.UncaughtExceptionHandler? = initialUncaughtExceptionHandler
val handler = mock<UncaughtExceptionHandler>()
whenever(handler.defaultUncaughtExceptionHandler).thenAnswer { currentDefaultHandler }
whenever(handler.setDefaultUncaughtExceptionHandler(anyOrNull<Thread.UncaughtExceptionHandler>())).then {
currentDefaultHandler = it.getArgument(0)
null
}
whenever(scopes2.globalScope).thenReturn(mock<IScope>())
val integration1 = UncaughtExceptionHandlerIntegration(handler)
integration1.register(fixture.scopes, fixture.options)
val integration2 = UncaughtExceptionHandlerIntegration(handler)
integration2.register(scopes2, fixture.options)
assertEquals(currentDefaultHandler, integration2)
integration2.close()
assertEquals(integration1, currentDefaultHandler)
integration1.close()
assertEquals(initialUncaughtExceptionHandler, currentDefaultHandler)
}
@Test
fun `multiple registrations with different global scopes allowed, closed out of order`() {
fixture.getSut()
val scopes2 = mock<IScopes>()
val initialUncaughtExceptionHandler = Thread.UncaughtExceptionHandler { _, _ -> }
var currentDefaultHandler: Thread.UncaughtExceptionHandler? = initialUncaughtExceptionHandler
val handler = mock<UncaughtExceptionHandler>()
whenever(handler.defaultUncaughtExceptionHandler).thenAnswer { currentDefaultHandler }
whenever(handler.setDefaultUncaughtExceptionHandler(anyOrNull<Thread.UncaughtExceptionHandler>())).then {
currentDefaultHandler = it.getArgument(0)
null
}
whenever(scopes2.globalScope).thenReturn(mock<IScope>())
val integration1 = UncaughtExceptionHandlerIntegration(handler)
integration1.register(fixture.scopes, fixture.options)
val integration2 = UncaughtExceptionHandlerIntegration(handler)
integration2.register(scopes2, fixture.options)
assertEquals(currentDefaultHandler, integration2)
integration1.close()
assertEquals(integration2, currentDefaultHandler)
integration2.close()
assertEquals(initialUncaughtExceptionHandler, currentDefaultHandler)
}
@Test
fun `multiple registrations async, closed async, one remains`() {
val executor = Executors.newFixedThreadPool(4)
fixture.getSut()
val scopes2 = mock<IScopes>()
val scopes3 = mock<IScopes>()
val scopes4 = mock<IScopes>()
val scopes5 = mock<IScopes>()
val scopesList = listOf(fixture.scopes, scopes2, scopes3, scopes4, scopes5)
val initialUncaughtExceptionHandler = Thread.UncaughtExceptionHandler { _, _ -> }
var currentDefaultHandler: Thread.UncaughtExceptionHandler? = initialUncaughtExceptionHandler
val handler = mock<UncaughtExceptionHandler>()
whenever(handler.defaultUncaughtExceptionHandler).thenAnswer { currentDefaultHandler }
whenever(handler.setDefaultUncaughtExceptionHandler(anyOrNull<Thread.UncaughtExceptionHandler>())).then {
currentDefaultHandler = it.getArgument(0)
null
}
whenever(scopes2.globalScope).thenReturn(mock<IScope>())
whenever(scopes3.globalScope).thenReturn(mock<IScope>())
whenever(scopes4.globalScope).thenReturn(mock<IScope>())
whenever(scopes5.globalScope).thenReturn(mock<IScope>())
val integrations = scopesList.map { scope ->
CompletableFuture.supplyAsync(
{
UncaughtExceptionHandlerIntegration(handler).apply {
register(scope, fixture.options)
}
},
executor
)
}
CompletableFuture.allOf(*integrations.toTypedArray()).get()
val futures = integrations.minus(integrations[2]).reversed().map { integration ->
CompletableFuture.supplyAsync(
{
integration.get().close()
println(Thread.currentThread().name)
},
executor
)
}
CompletableFuture.allOf(*futures.toTypedArray()).get()
assertEquals(integrations[2].get(), currentDefaultHandler)
}
}