Skip to content

Commit

Permalink
move enum and session properties
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Nov 16, 2023
1 parent e614fbd commit 270b424
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ internal class SessionModuleImpl(
essentialServiceModule.memoryCleanerService,
deliveryModule.deliveryService,
sessionMessageCollator,
sessionProperties,
initModule.clock,
workerThreadModule.scheduledExecutor(ExecutorName.SESSION_CLOSER),
workerThreadModule.scheduledExecutor(ExecutorName.SESSION_CACHING)
Expand All @@ -86,13 +87,12 @@ internal class SessionModuleImpl(
EmbraceSessionService(
essentialServiceModule.processStateService,
nativeModule.ndkService,
sessionProperties,
coreModule.logger,
sessionHandler,
deliveryModule.deliveryService,
essentialServiceModule.configService.autoDataCaptureBehavior.isNdkEnabled(),
initModule.clock,
initModule.spansService
initModule.spansService,
coreModule.logger
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.spans.EmbraceAttributes
import io.embrace.android.embracesdk.internal.spans.SpansService
import io.embrace.android.embracesdk.logging.InternalEmbraceLogger
import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger
import io.embrace.android.embracesdk.ndk.NdkService
import io.embrace.android.embracesdk.payload.Session
import io.embrace.android.embracesdk.session.lifecycle.ProcessStateListener
import io.embrace.android.embracesdk.session.lifecycle.ProcessStateService
import io.embrace.android.embracesdk.session.properties.EmbraceSessionProperties

internal class EmbraceSessionService(
private val processStateService: ProcessStateService,
ndkService: NdkService,
private val sessionProperties: EmbraceSessionProperties,
private val logger: InternalEmbraceLogger,
private val sessionHandler: SessionHandler,
deliveryService: DeliveryService,
isNdkEnabled: Boolean,
private val clock: Clock,
private val spansService: SpansService
private val spansService: SpansService,
private val logger: InternalEmbraceLogger = InternalStaticEmbraceLogger.logger
) : SessionService, ProcessStateListener {

companion object {
Expand Down Expand Up @@ -93,7 +92,6 @@ internal class EmbraceSessionService(
coldStart,
startType,
startTime,
sessionProperties,
automaticSessionCloserCallback,
this::onPeriodicCacheActiveSession
)
Expand All @@ -114,7 +112,6 @@ internal class EmbraceSessionService(
sessionHandler.onCrash(
it,
crashId,
sessionProperties,
sdkStartupDuration,
spansService.flushSpans(EmbraceAttributes.AppTerminationCause.CRASH)
)
Expand All @@ -130,7 +127,6 @@ internal class EmbraceSessionService(
val session = activeSession ?: return
sessionHandler.onPeriodicCacheActiveSession(
session,
sessionProperties,
sdkStartupDuration,
spansService.completedSpans()
)
Expand Down Expand Up @@ -191,7 +187,6 @@ internal class EmbraceSessionService(
sessionHandler.onSessionEnded(
endType,
session,
sessionProperties,
sdkStartupDuration,
endTime,
spansService.flushSpans()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,49 +43,12 @@ internal class SessionHandler(
private val memoryCleanerService: MemoryCleanerService,
private val deliveryService: DeliveryService,
private val sessionMessageCollator: SessionMessageCollator,
private val sessionProperties: EmbraceSessionProperties,
private val clock: Clock,
private val automaticSessionStopper: ScheduledExecutorService,
private val sessionPeriodicCacheExecutorService: ScheduledExecutorService
) : Closeable {

/**
* Defines the states in which a session can end.
*/
private enum class SessionSnapshotType(

/**
* Whether the session ended cleanly (i.e. not because of a crash).
*/
val endedCleanly: Boolean,

/**
* Whether the session process experienced a force quit/unexpected termination.
*/
val forceQuit: Boolean,

/**
* Whether periodic caching of the session should stop or not.
*/
val shouldStopCaching: Boolean
) {

/**
* The end session happened in the normal way (i.e. process state changes or manual/timed end).
*/
NORMAL_END(true, false, true),

/**
* The end session is being constructed so that it can be periodically cached. This avoids
* the scenario of data loss in the event of NDK crashes.
*/
PERIODIC_CACHE(false, true, false),

/**
* The end session is being constructed because of a JVM crash.
*/
JVM_CRASH(false, false, true);
}

@VisibleForTesting
var scheduledFuture: ScheduledFuture<*>? = null

Expand All @@ -101,7 +64,6 @@ internal class SessionHandler(
coldStart: Boolean,
startType: SessionLifeEventType,
startTime: Long,
sessionProperties: EmbraceSessionProperties,
automaticSessionCloserCallback: Runnable,
cacheCallback: Runnable
): SessionMessage? {
Expand Down Expand Up @@ -150,7 +112,6 @@ internal class SessionHandler(
fun onSessionEnded(
endType: SessionLifeEventType,
originSession: Session,
sessionProperties: EmbraceSessionProperties,
sdkStartupDuration: Long,
endTime: Long,
completedSpans: List<EmbraceSpanData>? = null
Expand Down Expand Up @@ -184,7 +145,6 @@ internal class SessionHandler(
fun onCrash(
originSession: Session,
crashId: String,
sessionProperties: EmbraceSessionProperties,
sdkStartupDuration: Long,
completedSpans: List<EmbraceSpanData>? = null
) {
Expand Down Expand Up @@ -212,7 +172,6 @@ internal class SessionHandler(
*/
fun onPeriodicCacheActiveSession(
activeSession: Session?,
sessionProperties: EmbraceSessionProperties,
sdkStartupDuration: Long,
completedSpans: List<EmbraceSpanData>? = null
): SessionMessage? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.embrace.android.embracesdk.session

/**
* Defines the states in which a session can end.
*/
internal enum class SessionSnapshotType(

/**
* Whether the session ended cleanly (i.e. not because of a crash).
*/
val endedCleanly: Boolean,

/**
* Whether the session process experienced a force quit/unexpected termination.
*/
val forceQuit: Boolean,

/**
* Whether periodic caching of the session should stop or not.
*/
val shouldStopCaching: Boolean
) {

/**
* The end session happened in the normal way (i.e. process state changes or manual/timed end).
*/
NORMAL_END(true, false, true),

/**
* The end session is being constructed so that it can be periodically cached. This avoids
* the scenario of data loss in the event of NDK crashes.
*/
PERIODIC_CACHE(false, true, false),

/**
* The end session is being constructed because of a JVM crash.
*/
JVM_CRASH(false, false, true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import io.embrace.android.embracesdk.ndk.NdkService
import io.embrace.android.embracesdk.payload.Session
import io.embrace.android.embracesdk.payload.Session.SessionLifeEventType
import io.embrace.android.embracesdk.payload.SessionMessage
import io.embrace.android.embracesdk.session.properties.EmbraceSessionProperties
import io.mockk.Called
import io.mockk.clearAllMocks
import io.mockk.every
Expand Down Expand Up @@ -44,7 +43,6 @@ internal class EmbraceSessionServiceTest {
private val mockSession: Session = mockk(relaxed = true)
private val mockSessionMessage: SessionMessage = mockk(relaxed = true)
private val mockSessionHandler: SessionHandler = mockk(relaxed = true)
private val mockSessionProperties: EmbraceSessionProperties = mockk(relaxed = true)
private val clock = FakeClock()

@BeforeClass
Expand Down Expand Up @@ -89,7 +87,6 @@ internal class EmbraceSessionServiceTest {
/* automatically detecting a cold start */ true,
SessionLifeEventType.STATE,
any(),
mockSessionProperties,
any(),
any()
)
Expand All @@ -106,7 +103,6 @@ internal class EmbraceSessionServiceTest {
coldStart,
type,
any(),
mockSessionProperties,
any(),
any()
)
Expand All @@ -122,7 +118,6 @@ internal class EmbraceSessionServiceTest {
coldStart,
type,
startTime,
mockSessionProperties,
any(),
any()
)
Expand All @@ -140,7 +135,6 @@ internal class EmbraceSessionServiceTest {
coldStart,
type,
any(),
mockSessionProperties,
any(),
any()
)
Expand All @@ -155,7 +149,6 @@ internal class EmbraceSessionServiceTest {
coldStart,
type,
startTime,
mockSessionProperties,
any(),
any()
)
Expand All @@ -173,7 +166,6 @@ internal class EmbraceSessionServiceTest {
true,
SessionLifeEventType.STATE,
any(),
mockSessionProperties,
any(),
any()
)
Expand All @@ -182,7 +174,7 @@ internal class EmbraceSessionServiceTest {

service.handleCrash(crashId)

verify { mockSessionHandler.onCrash(mockSession, crashId, mockSessionProperties, 0) }
verify { mockSessionHandler.onCrash(mockSession, crashId, 0) }
}

@Test
Expand All @@ -200,7 +192,6 @@ internal class EmbraceSessionServiceTest {
coldStart,
SessionLifeEventType.STATE,
456,
mockSessionProperties,
any(),
any()
)
Expand All @@ -221,7 +212,6 @@ internal class EmbraceSessionServiceTest {
coldStart,
SessionLifeEventType.STATE,
456,
mockSessionProperties,
any(),
any()
)
Expand All @@ -243,7 +233,6 @@ internal class EmbraceSessionServiceTest {
mockSessionHandler.onSessionEnded(
SessionLifeEventType.STATE,
mockSession,
mockSessionProperties,
sdkStartupDuration,
456
)
Expand All @@ -265,7 +254,6 @@ internal class EmbraceSessionServiceTest {
mockSessionHandler.onSessionEnded(
SessionLifeEventType.MANUAL,
mockSession,
mockSessionProperties,
0,
any()
)
Expand All @@ -282,14 +270,13 @@ internal class EmbraceSessionServiceTest {
service.triggerStatelessSessionEnd(endType)

// verify session is ended
verify { mockSessionHandler.onSessionEnded(endType, mockSession, mockSessionProperties, 0, any()) }
verify { mockSessionHandler.onSessionEnded(endType, mockSession, 0, any()) }
// verify that a MANUAL session is started
verify {
mockSessionHandler.onSessionStarted(
false,
endType,
any(),
mockSessionProperties,
any(),
any()
)
Expand Down Expand Up @@ -322,7 +309,6 @@ internal class EmbraceSessionServiceTest {
verify {
mockSessionHandler.onPeriodicCacheActiveSession(
any(),
mockSessionProperties,
0
)
}
Expand All @@ -347,7 +333,6 @@ internal class EmbraceSessionServiceTest {
mockSessionHandler.onSessionEnded(
endType = any(),
originSession = any(),
sessionProperties = any(),
sdkStartupDuration = any(),
endTime = any(),
completedSpans = match {
Expand All @@ -373,7 +358,6 @@ internal class EmbraceSessionServiceTest {
mockSessionHandler.onSessionEnded(
endType = any(),
originSession = any(),
sessionProperties = any(),
sdkStartupDuration = any(),
endTime = any(),
completedSpans = match {
Expand All @@ -399,7 +383,6 @@ internal class EmbraceSessionServiceTest {
mockSessionHandler.onCrash(
originSession = any(),
crashId = any(),
sessionProperties = any(),
sdkStartupDuration = any(),
completedSpans = match {
it.size == 2
Expand Down Expand Up @@ -429,8 +412,6 @@ internal class EmbraceSessionServiceTest {
service = EmbraceSessionService(
activityService,
mockNdkService,
mockSessionProperties,
mockk(relaxed = true),
mockSessionHandler,
deliveryService,
ndkEnabled,
Expand All @@ -445,7 +426,6 @@ internal class EmbraceSessionServiceTest {
true,
SessionLifeEventType.STATE,
any(),
mockSessionProperties,
any(),
any()
)
Expand Down
Loading

0 comments on commit 270b424

Please sign in to comment.