Skip to content

Commit

Permalink
Merge pull request #855 from embrace-io/hho/session-caching
Browse files Browse the repository at this point in the history
Only cache payloads matching the current session
  • Loading branch information
bidetofevil committed May 17, 2024
2 parents a3ee76a + 9b8e510 commit e875418
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,27 +284,35 @@ 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
}
}
}

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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e875418

Please sign in to comment.