Skip to content

Commit

Permalink
Add integration tests for session props snapshots with bg off (#1277)
Browse files Browse the repository at this point in the history
* Add integration tests for session props snapshots with bg off

* Clarify session transition methods and comments
  • Loading branch information
bidetofevil authored Aug 20, 2024
1 parent 8359427 commit 678d3e1
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ internal class EmbraceSessionProperties(

private fun size(): Int = permanentProperties().size + temporary.size

fun clearTemporary() {
fun prepareForNewSession() {
synchronized(permanentPropertiesReference) {
temporary.clear()
addPermPropsToSessionSpan()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public interface SessionPropertiesService {
public fun getProperties(): Map<String, String>

/**
* Clears any temporary properties
* Apply state change required when a new session starts
*/
public fun clearTemporary()
public fun prepareForNewSession()

/**
* Adds a listener that will be invoked with a Map representation of all the session properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ internal class SessionPropertiesServiceImpl(

override fun getProperties(): Map<String, String> = props.get()

override fun clearTemporary() {
props.clearTemporary()
override fun prepareForNewSession() {
props.prepareForNewSession()
}

override fun addChangeListener(listener: (Map<String, String>) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class OrchestratorBoundaryDelegate(
*/
public fun prepareForNewSession(clearUserInfo: Boolean = false) {
memoryCleanerService.cleanServicesCollections()
sessionPropertiesService.clearTemporary()
sessionPropertiesService.prepareForNewSession()

if (clearUserInfo) {
userService.clearAllUserInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,23 @@ internal class SessionOrchestratorImpl(
processEndMessage(endMessage, transitionType)
}

// third, clean up any previous session state
// the previous session has fully ended at this point
// now, we can clear the SDK state and prepare for the next session
boundaryDelegate.prepareForNewSession(clearUserInfo)

// now, we can start the next session or background activity

// create the next session span if we should, and update the SDK state to reflect the transition
val newState = newSessionAction?.invoke()
activeSession = newState
val sessionId = newState?.sessionId
sessionIdTracker.setActiveSession(sessionId, inForeground)
newState?.let(sessionSpanAttrPopulator::populateSessionSpanStartAttrs)

// initiate periodic caching of the payload if required
// initiate periodic caching of the payload if a new session has started
if (transitionType != TransitionType.CRASH && newState != null) {
initiatePeriodicCaching(endProcessState, newState)
}

// update the current state
// update the current state of the SDK. this should match the value in sessionIdTracker
state = endProcessState

// update data capture orchestrator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import io.embrace.android.embracesdk.getLastSavedBackgroundActivity
import io.embrace.android.embracesdk.getLastSavedSession
import io.embrace.android.embracesdk.getLastSentBackgroundActivity
import io.embrace.android.embracesdk.getSentBackgroundActivities
import io.embrace.android.embracesdk.getSessionId
import io.embrace.android.embracesdk.internal.payload.Span
import io.embrace.android.embracesdk.internal.payload.getSessionSpan
import io.embrace.android.embracesdk.internal.spans.getSessionProperty
import io.embrace.android.embracesdk.recordSession
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Rule
Expand Down Expand Up @@ -144,23 +146,87 @@ internal class SessionPropertiesTest {
fun `permanent properties are persisted in cached payloads`() {
with(testRule) {
startSdk()
harness.recordSession()
var lastSessionSpanId = checkNotNull(harness.recordSession()).getSessionId()
embrace.addSessionProperty("perm", "value", true)
val bgSnapshot = checkNotNull(harness.getLastSavedBackgroundActivity())
checkNotNull(bgSnapshot.getSessionSpan()).assertPropertyExistence(exist = listOf("perm"))
with(checkNotNull(harness.getLastSavedBackgroundActivity()?.getSessionSpan())) {
assertNotEquals(lastSessionSpanId, spanId)
assertPropertyExistence(
exist = listOf("perm")
)
lastSessionSpanId = checkNotNull(spanId)
}

harness.recordSession {
val sessionSnapshot = checkNotNull(harness.getLastSavedSession())
checkNotNull(sessionSnapshot.getSessionSpan()).assertPropertyExistence(exist = listOf("perm"))
with(checkNotNull(harness.getLastSavedSession()?.getSessionSpan())) {
assertNotEquals(lastSessionSpanId, spanId)
assertPropertyExistence(
exist = listOf("perm")
)
lastSessionSpanId = checkNotNull(spanId)
}
embrace.addSessionProperty("perm2", "value", true)
checkNotNull(harness.getLastSavedSession()?.getSessionSpan()).assertPropertyExistence(
exist = listOf("perm", "perm2")
)
}

with(checkNotNull(harness.getLastSavedBackgroundActivity()?.getSessionSpan())) {
assertNotEquals(lastSessionSpanId, spanId)
assertPropertyExistence(
exist = listOf("perm", "perm2")
)
lastSessionSpanId = checkNotNull(spanId)
}

val bgSnapshot2 = checkNotNull(harness.getLastSavedBackgroundActivity())
checkNotNull(bgSnapshot2.getSessionSpan()).assertPropertyExistence(exist = listOf("perm", "perm2"))
embrace.addSessionProperty("perm3", "value", true)
checkNotNull(harness.getLastSavedBackgroundActivity()?.getSessionSpan()).assertPropertyExistence(
exist = listOf("perm", "perm2", "perm3")
)

harness.recordSession {
val sessionSnapshot = checkNotNull(harness.getLastSavedSession())
checkNotNull(sessionSnapshot.getSessionSpan()).assertPropertyExistence(exist = listOf("perm", "perm2"))
with(checkNotNull(harness.getLastSavedSession()?.getSessionSpan())) {
assertNotEquals(lastSessionSpanId, spanId)
assertPropertyExistence(
exist = listOf("perm", "perm2", "perm3")
)
lastSessionSpanId = checkNotNull(spanId)
}
}
}
}

@Test
fun `permanent properties are persisted in cached payloads when bg activities are disabled`() {
with(testRule) {
harness.overriddenConfigService.backgroundActivityCaptureEnabled = false
startSdk()
embrace.addSessionProperty("perm", "value", true)
var lastSessionSpanId = checkNotNull(harness.recordSession()).getSessionId()
harness.recordSession {
with(checkNotNull(harness.getLastSavedSession()?.getSessionSpan())) {
assertNotEquals(lastSessionSpanId, spanId)
assertPropertyExistence(
exist = listOf("perm")
)
lastSessionSpanId = checkNotNull(spanId)
}
embrace.addSessionProperty("perm2", "value", true)
checkNotNull(harness.getLastSavedSession()?.getSessionSpan()).assertPropertyExistence(
exist = listOf("perm", "perm2")
)
}
harness.recordSession {
with(checkNotNull(harness.getLastSavedSession()?.getSessionSpan())) {
assertNotEquals(lastSessionSpanId, spanId)
assertPropertyExistence(
exist = listOf("perm", "perm2")
)
lastSessionSpanId = checkNotNull(spanId)
}
embrace.addSessionProperty("perm3", "value", true)
checkNotNull(harness.getLastSavedSession()?.getSessionSpan()).assertPropertyExistence(
exist = listOf("perm", "perm2", "perm3")
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FakeSessionPropertiesService : SessionPropertiesService {

override fun getProperties(): Map<String, String> = props

override fun clearTemporary() {
override fun prepareForNewSession() {
props.clear()
}

Expand Down

0 comments on commit 678d3e1

Please sign in to comment.