11package io.sentry.android.core.cache
22
3+ import io.sentry.ISerializer
34import io.sentry.NoOpLogger
45import io.sentry.SentryEnvelope
6+ import io.sentry.SentryOptions
57import io.sentry.UncaughtExceptionHandlerIntegration.UncaughtExceptionHint
68import io.sentry.android.core.AnrV2Integration.AnrV2Hint
79import io.sentry.android.core.SentryAndroidOptions
@@ -18,7 +20,9 @@ import kotlin.test.assertFalse
1820import kotlin.test.assertTrue
1921import org.junit.Rule
2022import org.junit.rules.TemporaryFolder
23+ import org.mockito.kotlin.any
2124import org.mockito.kotlin.mock
25+ import org.mockito.kotlin.same
2226import org.mockito.kotlin.whenever
2327
2428class AndroidEnvelopeCacheTest {
@@ -35,8 +39,10 @@ class AndroidEnvelopeCacheTest {
3539 dir : TemporaryFolder ,
3640 appStartMillis : Long? = null,
3741 currentTimeMillis : Long? = null,
42+ optionsCallback : ((SentryOptions ) -> Unit )? = null
3843 ): AndroidEnvelopeCache {
3944 options.cacheDirPath = dir.newFolder(" sentry-cache" ).absolutePath
45+ optionsCallback?.invoke(options)
4046 val outboxDir = File (options.outboxPath!! )
4147 outboxDir.mkdirs()
4248
@@ -82,7 +88,7 @@ class AndroidEnvelopeCacheTest {
8288 val cache = fixture.getSut(tmpDir)
8389
8490 val hints = HintUtils .createWithTypeCheckHint(UncaughtHint ())
85- cache.store (fixture.envelope, hints)
91+ cache.storeEnvelope (fixture.envelope, hints)
8692
8793 assertFalse(fixture.startupCrashMarkerFile.exists())
8894 }
@@ -92,7 +98,7 @@ class AndroidEnvelopeCacheTest {
9298 val cache = fixture.getSut(dir = tmpDir, appStartMillis = 1000L , currentTimeMillis = 5000L )
9399
94100 val hints = HintUtils .createWithTypeCheckHint(UncaughtHint ())
95- cache.store (fixture.envelope, hints)
101+ cache.storeEnvelope (fixture.envelope, hints)
96102
97103 assertFalse(fixture.startupCrashMarkerFile.exists())
98104 }
@@ -104,7 +110,7 @@ class AndroidEnvelopeCacheTest {
104110 fixture.options.cacheDirPath = null
105111
106112 val hints = HintUtils .createWithTypeCheckHint(UncaughtHint ())
107- cache.store (fixture.envelope, hints)
113+ cache.storeEnvelope (fixture.envelope, hints)
108114
109115 assertFalse(fixture.startupCrashMarkerFile.exists())
110116 }
@@ -114,7 +120,7 @@ class AndroidEnvelopeCacheTest {
114120 val cache = fixture.getSut(dir = tmpDir, appStartMillis = 1000L , currentTimeMillis = 2000L )
115121
116122 val hints = HintUtils .createWithTypeCheckHint(UncaughtHint ())
117- cache.store (fixture.envelope, hints)
123+ cache.storeEnvelope (fixture.envelope, hints)
118124
119125 assertTrue(fixture.startupCrashMarkerFile.exists())
120126 }
@@ -138,7 +144,7 @@ class AndroidEnvelopeCacheTest {
138144 HintUtils .createWithTypeCheckHint(
139145 AnrV2Hint (0 , NoOpLogger .getInstance(), 12345678L , false , false )
140146 )
141- cache.store (fixture.envelope, hints)
147+ cache.storeEnvelope (fixture.envelope, hints)
142148
143149 assertFalse(fixture.lastReportedAnrFile.exists())
144150 }
@@ -151,7 +157,7 @@ class AndroidEnvelopeCacheTest {
151157 HintUtils .createWithTypeCheckHint(
152158 AnrV2Hint (0 , NoOpLogger .getInstance(), 12345678L , false , false )
153159 )
154- cache.store (fixture.envelope, hints)
160+ cache.storeEnvelope (fixture.envelope, hints)
155161
156162 assertTrue(fixture.lastReportedAnrFile.exists())
157163 assertEquals(" 12345678" , fixture.lastReportedAnrFile.readText())
@@ -189,5 +195,18 @@ class AndroidEnvelopeCacheTest {
189195 assertEquals(87654321L , lastReportedAnr)
190196 }
191197
198+ @Test
199+ fun `returns false if storing fails` () {
200+ val serializer = mock<ISerializer >()
201+ val cache = fixture.getSut(tmpDir) { options ->
202+ options.setSerializer(serializer)
203+ }
204+ whenever(serializer.serialize(same(fixture.envelope), any())).thenThrow(RuntimeException (" forced ex" ))
205+ val hints = HintUtils .createWithTypeCheckHint(UncaughtHint ())
206+
207+ val didStore = cache.storeEnvelope(fixture.envelope, hints)
208+ assertFalse(didStore)
209+ }
210+
192211 internal class UncaughtHint : UncaughtExceptionHint (0 , NoOpLogger .getInstance())
193212}
0 commit comments