Skip to content

Commit 9ac34bb

Browse files
committed
pass through whether cache stored in AndroidEnvelopeCache + test
1 parent c1a6766 commit 9ac34bb

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/cache/AndroidEnvelopeCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public boolean storeEnvelope(@NotNull SentryEnvelope envelope, @NotNull Hint hin
5959
}
6060

6161
private boolean storeInternalAndroid(@NotNull SentryEnvelope envelope, @NotNull Hint hint) {
62-
super.store(envelope, hint);
62+
final boolean didStore = super.storeEnvelope(envelope, hint);
6363

6464
final SentryAndroidOptions options = (SentryAndroidOptions) this.options;
6565
final TimeSpan sdkInitTimeSpan = AppStartMetrics.getInstance().getSdkInitTimeSpan();
@@ -93,7 +93,7 @@ private boolean storeInternalAndroid(@NotNull SentryEnvelope envelope, @NotNull
9393

9494
writeLastReportedAnrMarker(timestamp);
9595
});
96-
return true;
96+
return didStore;
9797
}
9898

9999
@TestOnly

sentry-android-core/src/test/java/io/sentry/android/core/cache/AndroidEnvelopeCacheTest.kt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.sentry.android.core.cache
22

3+
import io.sentry.ISerializer
34
import io.sentry.NoOpLogger
45
import io.sentry.SentryEnvelope
6+
import io.sentry.SentryOptions
57
import io.sentry.UncaughtExceptionHandlerIntegration.UncaughtExceptionHint
68
import io.sentry.android.core.AnrV2Integration.AnrV2Hint
79
import io.sentry.android.core.SentryAndroidOptions
@@ -18,7 +20,9 @@ import kotlin.test.assertFalse
1820
import kotlin.test.assertTrue
1921
import org.junit.Rule
2022
import org.junit.rules.TemporaryFolder
23+
import org.mockito.kotlin.any
2124
import org.mockito.kotlin.mock
25+
import org.mockito.kotlin.same
2226
import org.mockito.kotlin.whenever
2327

2428
class 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

Comments
 (0)