@@ -3,6 +3,7 @@ package io.sentry.cache
33import io.sentry.DateUtils
44import io.sentry.Hint
55import io.sentry.ILogger
6+ import io.sentry.ISerializer
67import io.sentry.NoOpLogger
78import io.sentry.SentryCrashLastRunState
89import io.sentry.SentryEnvelope
@@ -31,20 +32,25 @@ import kotlin.test.assertEquals
3132import kotlin.test.assertFalse
3233import kotlin.test.assertNotNull
3334import kotlin.test.assertTrue
35+ import org.mockito.kotlin.any
3436import org.mockito.kotlin.mock
37+ import org.mockito.kotlin.same
38+ import org.mockito.kotlin.whenever
3539
3640class EnvelopeCacheTest {
3741 private class Fixture {
3842 val dir: Path = Files .createTempDirectory(" sentry-session-cache-test" )
3943 val options = SentryOptions ()
4044 val logger = mock<ILogger >()
4145
42- fun getSUT (): EnvelopeCache {
46+ fun getSUT (optionsCallback : (( SentryOptions ) -> Unit ) ? = null ): EnvelopeCache {
4347 options.cacheDirPath = dir.toAbsolutePath().toFile().absolutePath
4448
4549 options.setLogger(logger)
4650 options.setDebug(true )
4751
52+ optionsCallback?.invoke(options)
53+
4854 return EnvelopeCache .create(options) as EnvelopeCache
4955 }
5056 }
@@ -90,13 +96,15 @@ class EnvelopeCacheTest {
9096 val envelope = SentryEnvelope .from(fixture.options.serializer, createSession(), null )
9197
9298 val hints = HintUtils .createWithTypeCheckHint(SessionStartHint ())
93- cache.store (envelope, hints)
99+ val didStore = cache.storeEnvelope (envelope, hints)
94100
95101 val currentFile =
96102 File (fixture.options.cacheDirPath!! , " $PREFIX_CURRENT_SESSION_FILE$SUFFIX_SESSION_FILE " )
97103 assertTrue(currentFile.exists())
98104
99105 file.deleteRecursively()
106+
107+ assertTrue(didStore)
100108 }
101109
102110 @Test
@@ -108,7 +116,7 @@ class EnvelopeCacheTest {
108116 val envelope = SentryEnvelope .from(fixture.options.serializer, createSession(), null )
109117
110118 val hints = HintUtils .createWithTypeCheckHint(SessionStartHint ())
111- cache.store (envelope, hints)
119+ val didStore = cache.storeEnvelope (envelope, hints)
112120
113121 val currentFile =
114122 File (fixture.options.cacheDirPath!! , " $PREFIX_CURRENT_SESSION_FILE$SUFFIX_SESSION_FILE " )
@@ -119,6 +127,7 @@ class EnvelopeCacheTest {
119127 assertFalse(currentFile.exists())
120128
121129 file.deleteRecursively()
130+ assertTrue(didStore)
122131 }
123132
124133 @Test
@@ -130,7 +139,7 @@ class EnvelopeCacheTest {
130139 val envelope = SentryEnvelope .from(fixture.options.serializer, createSession(), null )
131140
132141 val hints = HintUtils .createWithTypeCheckHint(SessionStartHint ())
133- cache.store (envelope, hints)
142+ val didStore = cache.storeEnvelope (envelope, hints)
134143
135144 val currentFile =
136145 File (fixture.options.cacheDirPath!! , " $PREFIX_CURRENT_SESSION_FILE$SUFFIX_SESSION_FILE " )
@@ -146,6 +155,7 @@ class EnvelopeCacheTest {
146155 currentFile.delete()
147156
148157 file.deleteRecursively()
158+ assertTrue(didStore)
149159 }
150160
151161 @Test
@@ -160,7 +170,7 @@ class EnvelopeCacheTest {
160170 val envelope = SentryEnvelope .from(fixture.options.serializer, createSession(), null )
161171
162172 val hints = HintUtils .createWithTypeCheckHint(SessionStartHint ())
163- cache.store (envelope, hints)
173+ val didStore = cache.storeEnvelope (envelope, hints)
164174
165175 val newEnvelope = SentryEnvelope .from(fixture.options.serializer, createSession(), null )
166176
@@ -172,6 +182,7 @@ class EnvelopeCacheTest {
172182
173183 // passing empty string since readCrashedLastRun is already set
174184 assertTrue(SentryCrashLastRunState .getInstance().isCrashedLastRun(" " , false )!! )
185+ assertTrue(didStore)
175186 }
176187
177188 @Test
@@ -185,11 +196,12 @@ class EnvelopeCacheTest {
185196 val envelope = SentryEnvelope .from(fixture.options.serializer, createSession(), null )
186197
187198 val hints = HintUtils .createWithTypeCheckHint(SessionStartHint ())
188- cache.store (envelope, hints)
199+ val didStore = cache.storeEnvelope (envelope, hints)
189200
190201 // passing empty string since readCrashedLastRun is already set
191202 assertTrue(SentryCrashLastRunState .getInstance().isCrashedLastRun(" " , false )!! )
192203 assertFalse(markerFile.exists())
204+ assertTrue(didStore)
193205 }
194206
195207 @Test
@@ -203,9 +215,10 @@ class EnvelopeCacheTest {
203215
204216 val hints =
205217 HintUtils .createWithTypeCheckHint(UncaughtExceptionHint (0 , NoOpLogger .getInstance()))
206- cache.store (envelope, hints)
218+ val didStore = cache.storeEnvelope (envelope, hints)
207219
208220 assertTrue(markerFile.exists())
221+ assertTrue(didStore)
209222 }
210223
211224 @Test
@@ -214,7 +227,7 @@ class EnvelopeCacheTest {
214227
215228 val envelope = SentryEnvelope .from(fixture.options.serializer, SentryEvent (), null )
216229 val hints = HintUtils .createWithTypeCheckHint(SessionStartHint ())
217- cache.store (envelope, hints)
230+ cache.storeEnvelope (envelope, hints)
218231
219232 assertTrue(cache.waitPreviousSessionFlush())
220233 }
@@ -232,7 +245,7 @@ class EnvelopeCacheTest {
232245
233246 val envelope = SentryEnvelope .from(fixture.options.serializer, SentryEvent (), null )
234247 val hints = HintUtils .createWithTypeCheckHint(SessionStartHint ())
235- cache.store (envelope, hints)
248+ cache.storeEnvelope (envelope, hints)
236249
237250 assertTrue(previousSessionFile.exists())
238251 val persistedSession =
@@ -261,7 +274,7 @@ class EnvelopeCacheTest {
261274 override fun timestamp (): Long? = null
262275 }
263276 val hints = HintUtils .createWithTypeCheckHint(abnormalHint)
264- cache.store (envelope, hints)
277+ cache.storeEnvelope (envelope, hints)
265278
266279 val updatedSession =
267280 fixture.options.serializer.deserialize(
@@ -293,7 +306,7 @@ class EnvelopeCacheTest {
293306 override fun timestamp (): Long = sessionExitedWithAbnormal
294307 }
295308 val hints = HintUtils .createWithTypeCheckHint(abnormalHint)
296- cache.store (envelope, hints)
309+ cache.storeEnvelope (envelope, hints)
297310
298311 val updatedSession =
299312 fixture.options.serializer.deserialize(
@@ -323,7 +336,7 @@ class EnvelopeCacheTest {
323336 override fun timestamp (): Long = sessionExitedWithAbnormal
324337 }
325338 val hints = HintUtils .createWithTypeCheckHint(abnormalHint)
326- cache.store (envelope, hints)
339+ cache.storeEnvelope (envelope, hints)
327340
328341 val updatedSession =
329342 fixture.options.serializer.deserialize(
@@ -334,6 +347,20 @@ class EnvelopeCacheTest {
334347 assertEquals(null , updatedSession.abnormalMechanism)
335348 }
336349
350+ @Test
351+ fun `failing to store returns false` () {
352+ val serializer = mock<ISerializer >()
353+ val envelope = SentryEnvelope .from(SentryOptions .empty().serializer, createSession(), null )
354+
355+ whenever(serializer.serialize(same(envelope), any())).thenThrow(RuntimeException (" forced ex" ))
356+
357+ val cache = fixture.getSUT { options -> options.setSerializer(serializer) }
358+
359+ val didStore = cache.storeEnvelope(envelope, Hint ())
360+
361+ assertFalse(didStore)
362+ }
363+
337364 private fun createSession (started : Date ? = null): Session =
338365 Session (
339366 Ok ,
0 commit comments