Skip to content

Commit

Permalink
chore: remove unneeded session logic in Timeline.process
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-fiedler committed Mar 22, 2024
1 parent e963b2e commit 94daa66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
8 changes: 1 addition & 7 deletions android/src/main/java/com/amplitude/android/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,9 @@ class Timeline : Timeline() {
val event = message.event
var sessionEvents: Iterable<BaseEvent>? = null
val eventTimestamp = event.timestamp!!
val eventSessionId = event.sessionId
var skipEvent = false

if (event.eventType == Amplitude.START_SESSION_EVENT) {
session.setSessionId(eventSessionId ?: eventTimestamp)
session.refreshSessionTime(eventTimestamp)
} else if (event.eventType == Amplitude.END_SESSION_EVENT) {
// do nothing
} else if (event.eventType == Amplitude.DUMMY_ENTER_FOREGROUND_EVENT) {
if (event.eventType == Amplitude.DUMMY_ENTER_FOREGROUND_EVENT) {
skipEvent = true
sessionEvents = session.startNewSessionIfNeeded(eventTimestamp)
} else if (event.eventType == Amplitude.DUMMY_EXIT_FOREGROUND_EVENT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.net.ConnectivityManager
import com.amplitude.android.plugins.AndroidLifecyclePlugin
import com.amplitude.android.utils.mockSystemTime
import com.amplitude.common.Logger
import com.amplitude.common.android.AndroidContextProvider
import com.amplitude.core.Storage
import com.amplitude.core.StorageProvider
Expand Down Expand Up @@ -634,19 +635,20 @@ class AmplitudeSessionTest {

@Test
fun amplitude_shouldStartNewSessionOnInitializationInForegroundBasedOnSessionTimeout() = runTest {
val startTime: Long = 1000

mockSystemTime(startTime)

val storageProvider = InstanceStorageProvider(InMemoryStorage())
val config = createConfiguration(storageProvider)

val startTime: Long = 1000
mockSystemTime(startTime)

// Create an instance in the background
// This will start a new session (1)
val amplitude1 = Amplitude(config)
amplitude1.logger.logMode = Logger.LogMode.DEBUG
setDispatcher(amplitude1, testScheduler)
amplitude1.isBuilt.await()

// enter foreground (will start a session)
// enter foreground (2)
val enterForegroundTime = mockSystemTime(startTime)
amplitude1.onEnterForeground(enterForegroundTime)

Expand All @@ -658,7 +660,7 @@ class AmplitudeSessionTest {
Assertions.assertEquals(enterForegroundTime, session1.lastEventTime)
Assertions.assertEquals(1, session1.lastEventId)

// track event (set last event time)
// track event (set last event time) (3)
val event1Time = mockSystemTime(enterForegroundTime + 200)
amplitude1.track(createEvent(event1Time, "test event 1"))

Expand All @@ -670,7 +672,7 @@ class AmplitudeSessionTest {
Assertions.assertEquals(event1Time, session1.lastEventTime)
Assertions.assertEquals(2, session1.lastEventId)

// exit foreground
// exit foreground (4)
val exitForegroundTime = mockSystemTime(event1Time + 100)
amplitude1.onExitForeground(exitForegroundTime)

Expand All @@ -679,7 +681,9 @@ class AmplitudeSessionTest {

// advance to new session
val newSessionTime = mockSystemTime(exitForegroundTime + config.minTimeBetweenSessionsMillis + 100)

// Create a new instance to simulate recreation at startup in foreground
// This will end current session and start a new session (5 + 6)
val amplitude2 = Amplitude(createConfiguration(storageProvider))
setDispatcher(amplitude2, testScheduler)
amplitude2.isBuilt.await()
Expand All @@ -690,8 +694,8 @@ class AmplitudeSessionTest {
val session2 = (amplitude2.timeline as Timeline).session
Assertions.assertEquals(newSessionTime, session2.sessionId)
Assertions.assertEquals(newSessionTime, session2.lastEventTime)
// 4 events = enter foreground, track, exit foreground,
Assertions.assertEquals(4, session2.lastEventId)
// 6 events = session_start, enter foreground, track, exit foreground, session_end, session_start
Assertions.assertEquals(6, session2.lastEventId)
}

private fun createEvent(timestamp: Long, eventType: String, sessionId: Long? = null): BaseEvent {
Expand Down

0 comments on commit 94daa66

Please sign in to comment.