From e9ebf50d12506a0b37c6e23c65039c746c58d01b Mon Sep 17 00:00:00 2001 From: Vishal Gupta Date: Thu, 28 Nov 2024 13:47:26 +0530 Subject: [PATCH] refactor: refactor updation logic of lastActivityTime (#71) Co-authored-by: Vishal Gupta --- .../sessiontracking/SessionTrackingPlugin.kt | 2 +- .../SessionTrackingPluginTest.kt | 58 ++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/android/src/main/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPlugin.kt b/android/src/main/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPlugin.kt index 2966521d..98d83da5 100644 --- a/android/src/main/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPlugin.kt +++ b/android/src/main/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPlugin.kt @@ -78,7 +78,7 @@ internal class SessionTrackingPlugin( override suspend fun execute(message: Message): Message { if (sessionId != DEFAULT_SESSION_ID) { addSessionIdToMessage(message) - if (!isSessionStart) { + if (!isSessionManual) { updateLastActivityTime() } } diff --git a/android/src/test/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPluginTest.kt b/android/src/test/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPluginTest.kt index 83fb1bb9..b21ac961 100644 --- a/android/src/test/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPluginTest.kt +++ b/android/src/test/kotlin/com/rudderstack/android/sdk/plugins/sessiontracking/SessionTrackingPluginTest.kt @@ -294,24 +294,68 @@ class SessionTrackingPluginTest { } @Test - fun `given a value of session timeout in config, when plugin setup called, then session timeout is set correctly`() = runTest { - val sessionTimeout = 600000L - pluginSetup(automaticSessionTracking = true, sessionTimeoutInMillis = sessionTimeout) + fun `given a value of session timeout in config, when plugin setup called, then session timeout is set correctly`() = + runTest { + val sessionTimeout = 600000L + pluginSetup(automaticSessionTracking = true, sessionTimeoutInMillis = sessionTimeout) + + plugin.setup(mockAnalytics) + + assertEquals(sessionTimeout, plugin.sessionTimeout) + } + + @Test + fun `given manual session is ongoing, when an event is made, then last activity time is not updated`() = runTest { + val sessionId = 1234567890L + val currentTime = 100000000L + mockSystemCurrentTime(sessionId * 1000) + mockCurrentMonotonicTime(currentTime) + val message = TrackEvent("test", emptyJsonObject) + pluginSetup(automaticSessionTracking = false) + mockStorage.write(StorageKeys.SESSION_ID, sessionId) + mockStorage.write(StorageKeys.IS_SESSION_MANUAL, true) + mockStorage.write(StorageKeys.LAST_ACTIVITY_TIME, currentTime - 600_000L) plugin.setup(mockAnalytics) + advanceUntilIdle() + + plugin.execute(message) + advanceUntilIdle() - assertEquals(sessionTimeout, plugin.sessionTimeout) + assertEquals(currentTime - 600_000L, mockStorage.readLong(StorageKeys.LAST_ACTIVITY_TIME, 0L)) } @Test - fun `given a negative value of session timeout in config, when plugin setup called, then session timeout set as default`() = runTest { - pluginSetup(automaticSessionTracking = true, sessionTimeoutInMillis = -1) + fun `given an automatic session, when an event is made, then last activity time is updated`() = runTest { + val sessionId = 1234567890L + val currentTime = 100000000L + mockSystemCurrentTime(sessionId * 1000) + mockCurrentMonotonicTime(currentTime) + val message = TrackEvent("test", emptyJsonObject) + pluginSetup(automaticSessionTracking = true) + mockStorage.write(StorageKeys.SESSION_ID, sessionId) + mockStorage.write(StorageKeys.IS_SESSION_MANUAL, false) + mockStorage.write(StorageKeys.LAST_ACTIVITY_TIME, currentTime - 600_000L) plugin.setup(mockAnalytics) + advanceUntilIdle() + + plugin.execute(message) + advanceUntilIdle() - assertEquals(DEFAULT_SESSION_TIMEOUT_IN_MILLIS, plugin.sessionTimeout) + assertEquals(currentTime, mockStorage.readLong(StorageKeys.LAST_ACTIVITY_TIME, 0L)) } + @Test + fun `given a negative value of session timeout in config, when plugin setup called, then session timeout set as default`() = + runTest { + pluginSetup(automaticSessionTracking = true, sessionTimeoutInMillis = -1) + + plugin.setup(mockAnalytics) + + assertEquals(DEFAULT_SESSION_TIMEOUT_IN_MILLIS, plugin.sessionTimeout) + } + private fun pluginSetup( automaticSessionTracking: Boolean = true, sessionTimeoutInMillis: Long = 300000L