Skip to content

Commit 77ebaa6

Browse files
committed
added exception thrown tests
1 parent 98151e2 commit 77ebaa6

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
### Fixes
3434

35-
- Avoid StrictMode warnings (followup) ([#4809](https://github.com/getsentry/sentry-java/pull/4809))
3635
- Avoid StrictMode warnings ([#4724](https://github.com/getsentry/sentry-java/pull/4724))
3736
- Use logger from options for JVM profiler ([#4771](https://github.com/getsentry/sentry-java/pull/4771))
3837
- Session Replay: Avoid deadlock when pausing replay if no connection ([#4788](https://github.com/getsentry/sentry-java/pull/4788))

sentry-android-core/src/test/java/io/sentry/android/core/internal/util/AndroidRuntimeManagerTest.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class AndroidRuntimeManagerTest {
5757
@Test
5858
fun `runWithRelaxedPolicy changes policy and restores it afterwards even if the code throws`() {
5959
var called = false
60+
var exceptionPropagated = false
6061
val threadPolicy = StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build()
6162
val vmPolicy = StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build()
6263

@@ -75,19 +76,25 @@ class AndroidRuntimeManagerTest {
7576
called = true
7677
throw Exception("Test exception")
7778
}
78-
} catch (_: Exception) {}
79+
} catch (e: Exception) {
80+
assertEquals(e.message, "Test exception")
81+
exceptionPropagated = true
82+
}
7983

8084
// Policies should be reverted back
8185
assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString())
8286
assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString())
8387

8488
// Ensure the code ran
8589
assertTrue(called)
90+
// Ensure the exception was propagated
91+
assertTrue(exceptionPropagated)
8692
}
8793

8894
@Test
8995
fun `runWithRelaxedPolicy with Runnable changes policy when running and restores it afterwards even if the code throws`() {
9096
var called = false
97+
var exceptionPropagated = false
9198
val threadPolicy = StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build()
9299
val vmPolicy = StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build()
93100

@@ -106,13 +113,18 @@ class AndroidRuntimeManagerTest {
106113
called = true
107114
throw Exception("Test exception")
108115
}
109-
} catch (_: Exception) {}
116+
} catch (e: Exception) {
117+
assertEquals(e.message, "Test exception")
118+
exceptionPropagated = true
119+
}
110120

111121
// Policies should be reverted back
112122
assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString())
113123
assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString())
114124

115125
// Ensure the code ran
116126
assertTrue(called)
127+
// Ensure the exception was propagated
128+
assertTrue(exceptionPropagated)
117129
}
118130
}

sentry/src/test/java/io/sentry/util/runtime/NeutralRuntimeManagerTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.sentry.util.runtime
22

3+
import java.io.IOException
34
import kotlin.test.Test
5+
import kotlin.test.assertEquals
46
import kotlin.test.assertTrue
57

68
class NeutralRuntimeManagerTest {
@@ -26,4 +28,38 @@ class NeutralRuntimeManagerTest {
2628
// Ensure the code ran
2729
assertTrue(called)
2830
}
31+
32+
@Test
33+
fun `runWithRelaxedPolicy propagates exception`() {
34+
var exceptionPropagated = false
35+
36+
try {
37+
sut.runWithRelaxedPolicy<Unit> {
38+
throw IOException("test")
39+
}
40+
} catch (e: IOException) {
41+
assertEquals("test", e.message)
42+
exceptionPropagated = true
43+
}
44+
45+
// Ensure the exception was propagated
46+
assertTrue(exceptionPropagated)
47+
}
48+
49+
@Test
50+
fun `runWithRelaxedPolicy with runnable propagates exception`() {
51+
var exceptionPropagated = false
52+
53+
try {
54+
sut.runWithRelaxedPolicy {
55+
throw IOException("test")
56+
}
57+
} catch (e: IOException) {
58+
assertEquals("test", e.message)
59+
exceptionPropagated = true
60+
}
61+
62+
// Ensure the exception was propagated
63+
assertTrue(exceptionPropagated)
64+
}
2965
}

0 commit comments

Comments
 (0)