Skip to content

Commit c58f925

Browse files
authored
Consolidate storage service fakes and usage (#2002)
## Goal Consolidate the fake PayloadStorageService and set them up within the setup object if required. I also made it so that each instance of the setup object creates a new `processIdentifier`, which avoids any erroneous tests that checks for a hardcoded ID. If you need a stable one, it can always be overridden.
2 parents 7e13c77 + 10946bc commit c58f925

File tree

9 files changed

+118
-166
lines changed

9 files changed

+118
-166
lines changed

embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/features/DeliveryConnectivityFeatureTest.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import io.embrace.android.embracesdk.fixtures.fakeSessionStoredTelemetryMetadata
77
import io.embrace.android.embracesdk.fixtures.fakeSessionStoredTelemetryMetadata2
88
import io.embrace.android.embracesdk.internal.comms.delivery.NetworkStatus
99
import io.embrace.android.embracesdk.testframework.SdkIntegrationTestRule
10+
import io.embrace.android.embracesdk.testframework.actions.EmbraceSetupInterface
1011
import org.junit.Assert.assertEquals
11-
import org.junit.Before
1212
import org.junit.Rule
1313
import org.junit.Test
1414
import org.junit.runner.RunWith
@@ -17,14 +17,13 @@ import org.junit.runner.RunWith
1717
internal class DeliveryConnectivityFeatureTest {
1818

1919
private lateinit var payloadStorageService: FakePayloadStorageService
20-
20+
2121
@Rule
2222
@JvmField
23-
val testRule: SdkIntegrationTestRule = SdkIntegrationTestRule()
24-
25-
@Before
26-
fun setUp() {
27-
payloadStorageService = FakePayloadStorageService()
23+
val testRule: SdkIntegrationTestRule = SdkIntegrationTestRule {
24+
EmbraceSetupInterface(fakeStorageLayer = true).also {
25+
payloadStorageService = checkNotNull(it.fakePayloadStorageService)
26+
}
2827
}
2928

3029
@Test
@@ -36,7 +35,6 @@ internal class DeliveryConnectivityFeatureTest {
3635
setupAction = {
3736
fakeNetworkConnectivityService.networkStatus = NetworkStatus.NOT_REACHABLE
3837
payloadStorageService.addPayload(sessionMetadata, envelope)
39-
payloadStorageServiceProvider = { payloadStorageService }
4038
},
4139
testCaseAction = {},
4240
assertAction = {
@@ -69,7 +67,6 @@ internal class DeliveryConnectivityFeatureTest {
6967
setupAction = {
7068
fakeNetworkConnectivityService.networkStatus = NetworkStatus.NOT_REACHABLE
7169
payloadStorageService.addPayload(sessionMetadata, envelope)
72-
payloadStorageServiceProvider = { payloadStorageService }
7370
},
7471
testCaseAction = {
7572
recordSession()
@@ -94,7 +91,6 @@ internal class DeliveryConnectivityFeatureTest {
9491
fakeSessionStoredTelemetryMetadata2,
9592
fakeSessionEnvelope(sessionId = "2", startMs = startMs + 1000)
9693
)
97-
payloadStorageServiceProvider = { payloadStorageService }
9894
},
9995
testCaseAction = {},
10096
assertAction = {

embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/features/NativeCrashFeatureTest.kt

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package io.embrace.android.embracesdk.testcases.features
22

33
import androidx.test.ext.junit.runners.AndroidJUnit4
44
import io.embrace.android.embracesdk.assertions.getSessionId
5-
import io.embrace.android.embracesdk.fakes.FakePayloadStorageService
65
import io.embrace.android.embracesdk.fakes.TestPlatformSerializer
76
import io.embrace.android.embracesdk.fakes.config.FakeEnabledFeatureConfig
87
import io.embrace.android.embracesdk.fakes.config.FakeInstrumentedConfig
@@ -26,7 +25,6 @@ import io.embrace.android.embracesdk.testframework.assertions.getLogOfType
2625
import org.junit.Assert.assertEquals
2726
import org.junit.Assert.assertFalse
2827
import org.junit.Assert.assertTrue
29-
import org.junit.Before
3028
import org.junit.Rule
3129
import org.junit.Test
3230
import org.junit.runner.RunWith
@@ -96,32 +94,24 @@ internal class NativeCrashFeatureTest {
9694
)
9795
private val fakeSymbols = mapOf("libfoo.so" to "symbol_content")
9896

99-
private lateinit var cacheStorageService: FakePayloadStorageService
100-
10197
@Rule
10298
@JvmField
10399
val testRule: SdkIntegrationTestRule = SdkIntegrationTestRule {
104100
EmbraceSetupInterface(
105-
processIdentifier = "8115ec91-3e5e-4d8a-816d-cc40306f9822"
101+
fakeStorageLayer = true
106102
).apply {
107103
getEmbLogger().throwOnInternalError = false
108104
}.also {
109105
it.fakeSymbolService.symbolsForCurrentArch.putAll(fakeSymbols)
110106
}
111107
}
112108

113-
@Before
114-
fun setUp() {
115-
cacheStorageService = FakePayloadStorageService()
116-
}
117-
118109
@Test
119110
fun `native crash with foreground session`() {
120111
testRule.runTest(
121112
instrumentedConfig = config,
122113
setupAction = {
123114
setupCachedDataFromNativeCrash(
124-
storageService = cacheStorageService,
125115
crashData = crashData
126116
)
127117
setupFakeNativeCrash(serializer, crashData)
@@ -149,22 +139,17 @@ internal class NativeCrashFeatureTest {
149139
setupAction = {
150140
val modifiedCrashData = crashData.copy(sessionMetadata = null, sessionEnvelope = null)
151141
setupCachedDataFromNativeCrash(
152-
storageService = cacheStorageService,
153142
crashData = modifiedCrashData
154143
)
155144
setupFakeNativeCrash(serializer, modifiedCrashData)
156145
},
157146
testCaseAction = {
158147
recordSession()
148+
clock.tick(2000L)
159149
},
160150
assertAction = {
161-
val envelope = getSingleLogEnvelope()
162-
with(envelope) {
163-
assertEquals(fakeEnvelopeResource, resource)
164-
assertEquals(fakeEnvelopeMetadata, metadata)
165-
}
166-
val log = envelope.getLogOfType(EmbType.System.NativeCrash)
167-
assertNativeCrashSent(log, crashData, fakeSymbols)
151+
val nativeCrash = getSingleLogEnvelope().getLogOfType(EmbType.System.NativeCrash)
152+
assertNativeCrashSent(nativeCrash, crashData, fakeSymbols)
168153
}
169154
)
170155
}
@@ -174,7 +159,7 @@ internal class NativeCrashFeatureTest {
174159
testRule.runTest(
175160
instrumentedConfig = config,
176161
setupAction = {
177-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
162+
setupCachedDataFromNativeCrash(crashData = crashData)
178163
},
179164
testCaseAction = {},
180165
assertAction = {
@@ -191,8 +176,8 @@ internal class NativeCrashFeatureTest {
191176
testRule.runTest(
192177
instrumentedConfig = config,
193178
setupAction = {
194-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
195-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData2)
179+
setupCachedDataFromNativeCrash(crashData = crashData)
180+
setupCachedDataFromNativeCrash(crashData = crashData2)
196181
setupFakeNativeCrash(serializer, crashData)
197182
setupFakeNativeCrash(serializer, crashData2)
198183
},
@@ -237,7 +222,7 @@ internal class NativeCrashFeatureTest {
237222
testRule.runTest(
238223
instrumentedConfig = FakeInstrumentedConfig(),
239224
setupAction = {
240-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
225+
setupCachedDataFromNativeCrash(crashData = crashData)
241226
setupFakeNativeCrash(serializer, crashData)
242227
},
243228
testCaseAction = {},
@@ -256,7 +241,7 @@ internal class NativeCrashFeatureTest {
256241
testRule.runTest(
257242
instrumentedConfig = config,
258243
setupAction = {
259-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
244+
setupCachedDataFromNativeCrash(crashData = crashData)
260245
setupFakeNativeCrash(serializer, crashData)
261246

262247
// simulate JNI call failing to load struct
@@ -277,7 +262,7 @@ internal class NativeCrashFeatureTest {
277262
testRule.runTest(
278263
instrumentedConfig = config,
279264
setupAction = {
280-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
265+
setupCachedDataFromNativeCrash(crashData = crashData)
281266
setupFakeNativeCrash(serializer, crashData)
282267

283268
// simulate bad JSON

embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/features/ResurrectionFeatureTest.kt

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ internal class ResurrectionFeatureTest {
4141
@Rule
4242
@JvmField
4343
val testRule: SdkIntegrationTestRule = SdkIntegrationTestRule {
44-
EmbraceSetupInterface().apply {
44+
EmbraceSetupInterface(
45+
fakeStorageLayer = true
46+
).apply {
4547
getEmbLogger().throwOnInternalError = false
46-
cacheStorageService = FakePayloadStorageService(processIdProvider = getProcessIdentifierProvider())
48+
fakeSymbolService.symbolsForCurrentArch.putAll(fakeSymbols)
4749
}.also {
48-
it.fakeSymbolService.symbolsForCurrentArch.putAll(fakeSymbols)
50+
cacheStorageService = checkNotNull(it.fakeCacheStorageService)
4951
}
5052
}
5153

@@ -60,7 +62,7 @@ internal class ResurrectionFeatureTest {
6062
testRule.runTest(
6163
instrumentedConfig = FakeInstrumentedConfig(enabledFeatures = FakeEnabledFeatureConfig(nativeCrashCapture = true)),
6264
setupAction = {
63-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
65+
setupCachedDataFromNativeCrash(crashData = crashData)
6466
setupFakeNativeCrash(serializer, crashData)
6567
},
6668
testCaseAction = {},
@@ -90,7 +92,7 @@ internal class ResurrectionFeatureTest {
9092
testRule.runTest(
9193
instrumentedConfig = FakeInstrumentedConfig(enabledFeatures = FakeEnabledFeatureConfig(nativeCrashCapture = true)),
9294
setupAction = {
93-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
95+
setupCachedDataFromNativeCrash(crashData = crashData)
9496
setupFakeNativeCrash(serializer, crashData)
9597
},
9698
testCaseAction = {},
@@ -125,7 +127,7 @@ internal class ResurrectionFeatureTest {
125127
)
126128
),
127129
setupAction = {
128-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
130+
setupCachedDataFromNativeCrash(crashData = crashData)
129131
setupFakeNativeCrash(serializer, crashData)
130132
},
131133
testCaseAction = {
@@ -155,7 +157,7 @@ internal class ResurrectionFeatureTest {
155157
testRule.runTest(
156158
instrumentedConfig = FakeInstrumentedConfig(enabledFeatures = FakeEnabledFeatureConfig(nativeCrashCapture = true)),
157159
setupAction = {
158-
setupCachedDataFromNativeCrash(cacheStorageService, crashData = crashData)
160+
setupCachedDataFromNativeCrash(crashData = crashData)
159161
},
160162
testCaseAction = {},
161163
assertAction = {
@@ -173,9 +175,6 @@ internal class ResurrectionFeatureTest {
173175
persistedRemoteConfig = RemoteConfig(
174176
backgroundActivityConfig = BackgroundActivityRemoteConfig(100f)
175177
),
176-
setupAction = {
177-
cacheStorageServiceProvider = { cacheStorageService }
178-
},
179178
testCaseAction = {
180179
recordSession()
181180
recordSession()
@@ -193,9 +192,6 @@ internal class ResurrectionFeatureTest {
193192
persistedRemoteConfig = RemoteConfig(
194193
backgroundActivityConfig = BackgroundActivityRemoteConfig(0f)
195194
),
196-
setupAction = {
197-
cacheStorageServiceProvider = { cacheStorageService }
198-
},
199195
testCaseAction = {
200196
recordSession()
201197
recordSession()

embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/session/PeriodicSessionCacheTest.kt

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
55
import io.embrace.android.embracesdk.assertions.findSessionSpan
66
import io.embrace.android.embracesdk.assertions.findSpanSnapshotOfType
77
import io.embrace.android.embracesdk.assertions.returnIfConditionMet
8+
import io.embrace.android.embracesdk.fakes.FakePayloadStorageService
89
import io.embrace.android.embracesdk.fakes.TestPlatformSerializer
910
import io.embrace.android.embracesdk.internal.arch.schema.EmbType
11+
import io.embrace.android.embracesdk.internal.delivery.PayloadType
1012
import io.embrace.android.embracesdk.internal.delivery.SupportedEnvelopeType
1113
import io.embrace.android.embracesdk.internal.payload.Envelope
1214
import io.embrace.android.embracesdk.internal.payload.SessionPayload
1315
import io.embrace.android.embracesdk.internal.spans.getSessionProperty
14-
import io.embrace.android.embracesdk.testframework.FakeCacheStorageService
1516
import io.embrace.android.embracesdk.testframework.SdkIntegrationTestRule
17+
import io.embrace.android.embracesdk.testframework.actions.EmbraceSetupInterface
1618
import io.embrace.android.embracesdk.testframework.assertions.assertMatches
1719
import org.junit.Assert.assertEquals
1820
import org.junit.Rule
1921
import org.junit.Test
2022
import org.junit.runner.RunWith
2123
import org.robolectric.annotation.Config
24+
import java.util.zip.GZIPInputStream
2225

2326
/**
2427
* Asserts that the session is periodically cached.
@@ -27,30 +30,30 @@ import org.robolectric.annotation.Config
2730
@RunWith(AndroidJUnit4::class)
2831
internal class PeriodicSessionCacheTest {
2932

33+
private lateinit var cacheStorageService: FakePayloadStorageService
34+
3035
@Rule
3136
@JvmField
32-
val testRule: SdkIntegrationTestRule = SdkIntegrationTestRule()
37+
val testRule: SdkIntegrationTestRule = SdkIntegrationTestRule {
38+
EmbraceSetupInterface(fakeStorageLayer = true).also {
39+
cacheStorageService = checkNotNull(it.fakeCacheStorageService)
40+
}
41+
}
3342

3443
@Test
3544
fun `session is periodically cached`() {
3645
var snapshot: Envelope<SessionPayload>? = null
3746
testRule.runTest(
38-
setupAction = {
39-
cacheStorageServiceProvider = ::FakeCacheStorageService
40-
},
4147
testCaseAction = {
42-
val cacheStorageService = getCacheStorageService()
4348
recordSession {
44-
assertEquals(0, cacheStorageService.storedPayloads.size)
49+
assertEquals(0, cacheStorageService.storedPayloadCount())
4550
embrace.addSessionProperty("Test", "Test", true)
46-
val dataSupplier = { cacheStorageService.storedPayloads }
4751
snapshot = returnIfConditionMet(
4852
waitTimeMs = 10000,
4953
desiredValueSupplier = { cacheStorageService.getLastCachedSession() },
50-
dataProvider = dataSupplier,
54+
dataProvider = { cacheStorageService.getLastCachedSession() },
5155
condition = { data ->
52-
data.size > 0 && cacheStorageService.getLastCachedSession().findSpanSnapshotOfType(EmbType.Ux.Session)
53-
.getSessionProperty("Test") != null
56+
data != null && data.findSpanSnapshotOfType(EmbType.Ux.Session).getSessionProperty("Test") != null
5457
},
5558
errorMessageSupplier = { "Timeout waiting for cached session" }
5659
)
@@ -80,14 +83,18 @@ internal class PeriodicSessionCacheTest {
8083
)
8184
}
8285

83-
private fun FakeCacheStorageService.getLastCachedSession(): Envelope<SessionPayload> {
84-
return TestPlatformSerializer().fromJson(
85-
checkNotNull(loadPayloadAsStream(storedPayloads.keys.last())),
86-
checkNotNull(SupportedEnvelopeType.SESSION.serializedType)
87-
)
88-
}
89-
90-
private fun getCacheStorageService(): FakeCacheStorageService {
91-
return testRule.bootstrapper.deliveryModule.cacheStorageService as FakeCacheStorageService
92-
}
86+
private fun FakePayloadStorageService.getLastCachedSession(): Envelope<SessionPayload>? =
87+
storedPayloadMetadata()
88+
.filter { it.payloadType == PayloadType.SESSION }
89+
.let { sessions ->
90+
return if (sessions.isEmpty()) {
91+
null
92+
} else {
93+
val lastSessionMetadata = sessions.last()
94+
return TestPlatformSerializer().fromJson(
95+
GZIPInputStream(loadPayloadAsStream(lastSessionMetadata)),
96+
checkNotNull(SupportedEnvelopeType.SESSION.serializedType)
97+
)
98+
}
99+
}
93100
}

embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testframework/FakeCacheStorageService.kt

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)