From 9b8e5109f14aeb9734409ed5cf49f9d395f065b8 Mon Sep 17 00:00:00 2001 From: bidetofevil Date: Thu, 16 May 2024 16:54:42 -0700 Subject: [PATCH] Only cache payloads matching the current session --- .../orchestrator/SessionOrchestratorImpl.kt | 26 +++++++---- .../orchestrator/SessionOrchestratorTest.kt | 44 +++++++++++++++++-- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorImpl.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorImpl.kt index cb8fc7f312..8bee0b0efe 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorImpl.kt +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorImpl.kt @@ -284,15 +284,19 @@ internal class SessionOrchestratorImpl( private fun scheduleSessionSave( endProcessState: ProcessState, - newState: Session + initial: Session ) { updatePeriodicCacheAttrs() periodicSessionCacher.start { - synchronized(lock) { - updatePeriodicCacheAttrs() - payloadFactory.snapshotPayload(endProcessState, clock.now(), newState)?.apply { - deliveryService.sendSession(this, SessionSnapshotType.PERIODIC_CACHE) + if (initial.sessionId == sessionIdTracker.getActiveSessionId()) { + synchronized(lock) { + updatePeriodicCacheAttrs() + payloadFactory.snapshotPayload(endProcessState, clock.now(), initial)?.apply { + deliveryService.sendSession(this, SessionSnapshotType.PERIODIC_CACHE) + } } + } else { + null } } } @@ -300,11 +304,15 @@ internal class SessionOrchestratorImpl( private fun scheduleBackgroundActivitySave(endProcessState: ProcessState, initial: Session) { updatePeriodicCacheAttrs() periodicBackgroundActivityCacher.scheduleSave { - synchronized(lock) { - updatePeriodicCacheAttrs() - payloadFactory.snapshotPayload(endProcessState, clock.now(), initial)?.apply { - deliveryService.sendSession(this, SessionSnapshotType.PERIODIC_CACHE) + if (initial.sessionId == sessionIdTracker.getActiveSessionId()) { + synchronized(lock) { + updatePeriodicCacheAttrs() + payloadFactory.snapshotPayload(endProcessState, clock.now(), initial)?.apply { + deliveryService.sendSession(this, SessionSnapshotType.PERIODIC_CACHE) + } } + } else { + null } } } diff --git a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorTest.kt b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorTest.kt index 8b8e90d255..83eacb8a99 100644 --- a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorTest.kt +++ b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/session/orchestrator/SessionOrchestratorTest.kt @@ -129,13 +129,51 @@ internal class SessionOrchestratorTest { fun `saved background activity save overridden after is sent`() { createOrchestrator(true) clock.tick() - orchestrator.reportBackgroundActivityStateChange() baCacheExecutor.runCurrentlyBlocked() + assertEquals(SessionSnapshotType.PERIODIC_CACHE, checkNotNull(deliveryService.savedSessionMessages.last().second)) + assertEquals(1, deliveryService.getSavedBackgroundActivities().count()) orchestrator.onForeground(true, clock.now()) clock.tick() orchestrator.onBackground(clock.now()) - val lastBackgroundActivitySaveType = checkNotNull(deliveryService.savedSessionMessages.last().second) - assertEquals(SessionSnapshotType.NORMAL_END, lastBackgroundActivitySaveType) + assertEquals(SessionSnapshotType.NORMAL_END, checkNotNull(deliveryService.savedSessionMessages.last().second)) + assertEquals(2, deliveryService.getSavedBackgroundActivities().count()) + } + + @Test + fun `background activity save invoked after ending will not save it again`() { + createOrchestrator(true) + clock.tick() + orchestrator.onForeground(true, clock.now()) + clock.tick() + assertEquals(1, deliveryService.savedSessionMessages.count()) + baCacheExecutor.runCurrentlyBlocked() + assertEquals(1, deliveryService.savedSessionMessages.count()) + assertEquals(SessionSnapshotType.NORMAL_END, checkNotNull(deliveryService.savedSessionMessages.last().second)) + } + + @Test + fun `saved session overridden after it is sent`() { + createOrchestrator(false) + clock.tick() + sessionCacheExecutor.runCurrentlyBlocked() + assertEquals(SessionSnapshotType.PERIODIC_CACHE, checkNotNull(deliveryService.savedSessionMessages.last().second)) + assertEquals(1, deliveryService.getSavedSessions().count()) + orchestrator.onBackground(clock.now()) + clock.tick() + assertEquals(SessionSnapshotType.NORMAL_END, checkNotNull(deliveryService.savedSessionMessages.last().second)) + assertEquals(2, deliveryService.getSavedSessions().count()) + } + + @Test + fun `session save invoked after ending will not save it again`() { + createOrchestrator(false) + clock.tick() + orchestrator.onBackground(clock.now()) + clock.tick() + assertEquals(1, deliveryService.savedSessionMessages.count()) + sessionCacheExecutor.runCurrentlyBlocked() + assertEquals(1, deliveryService.savedSessionMessages.count()) + assertEquals(SessionSnapshotType.NORMAL_END, checkNotNull(deliveryService.savedSessionMessages.last().second)) } @Test