Skip to content

Commit ec61f30

Browse files
committed
tests
1 parent 9f3f129 commit ec61f30

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

buildSrc/src/main/java/Config.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ object Config {
213213
val msgpack = "org.msgpack:msgpack-core:0.9.8"
214214
val leakCanaryInstrumentation = "com.squareup.leakcanary:leakcanary-android-instrumentation:2.14"
215215
val composeUiTestJunit4 = "androidx.compose.ui:ui-test-junit4:1.6.8"
216+
val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.1"
216217
}
217218

218219
object QualityPlugins {

sentry-apollo-4/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies {
4141
testImplementation(Config.TestLibs.mockitoInline)
4242
testImplementation(Config.TestLibs.mockWebserver)
4343
testImplementation(Config.Libs.apolloKotlin4)
44-
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
44+
testImplementation(Config.TestLibs.coroutinesTest)
4545
testImplementation("org.jetbrains.kotlin:kotlin-reflect:2.0.0")
4646
}
4747

sentry-kotlin-extensions/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
testImplementation(Config.TestLibs.kotlinTestJunit)
3131
testImplementation(Config.TestLibs.mockitoKotlin)
3232
testImplementation(Config.Libs.coroutinesCore)
33+
testImplementation(Config.TestLibs.coroutinesTest)
3334
}
3435

3536
configure<SourceSetContainer> {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package io.sentry.kotlin
2+
3+
import io.sentry.IScopes
4+
import kotlinx.coroutines.GlobalScope
5+
import kotlinx.coroutines.async
6+
import kotlinx.coroutines.launch
7+
import kotlinx.coroutines.test.runTest
8+
import org.mockito.kotlin.check
9+
import org.mockito.kotlin.mock
10+
import org.mockito.kotlin.verify
11+
import kotlin.test.Test
12+
import kotlin.test.assertSame
13+
import kotlin.test.assertTrue
14+
15+
class SentryCoroutineExceptionHandlerTest {
16+
17+
class Fixture {
18+
val scopes = mock<IScopes>()
19+
20+
fun getSut(): SentryCoroutineExceptionHandler {
21+
return SentryCoroutineExceptionHandler(scopes)
22+
}
23+
}
24+
25+
@Test
26+
fun `captures unhandled exception in launch coroutine`() = runTest {
27+
val fixture = Fixture()
28+
val handler = fixture.getSut()
29+
val exception = RuntimeException("test")
30+
31+
GlobalScope.launch(handler) {
32+
throw exception
33+
}.join()
34+
35+
verify(fixture.scopes).captureEvent(
36+
check {
37+
assertSame(exception, it.throwable)
38+
}
39+
)
40+
}
41+
42+
@Test
43+
fun `captures unhandled exception in launch coroutine with child`() = runTest {
44+
val fixture = Fixture()
45+
val handler = fixture.getSut()
46+
val exception = RuntimeException("test")
47+
48+
GlobalScope.launch(handler) {
49+
launch {
50+
throw exception
51+
}.join()
52+
}.join()
53+
54+
verify(fixture.scopes).captureEvent(
55+
check {
56+
assertSame(exception, it.throwable)
57+
}
58+
)
59+
}
60+
61+
@Test
62+
fun `captures unhandled exception in async coroutine`() = runTest {
63+
val fixture = Fixture()
64+
val handler = fixture.getSut()
65+
val exception = RuntimeException("test")
66+
67+
val deferred = GlobalScope.async() {
68+
throw exception
69+
}
70+
GlobalScope.launch(handler) {
71+
deferred.await()
72+
}.join()
73+
74+
verify(fixture.scopes).captureEvent(
75+
check {
76+
assertTrue { exception.toString().equals(it.throwable.toString()) } // stack trace will differ
77+
}
78+
)
79+
}
80+
}

0 commit comments

Comments
 (0)