Skip to content

Commit d4eb55c

Browse files
themiswangmrober
authored andcommitted
background time nullable (#6886)
Make background time nullable For each new session generated, reset background time Update unit tests
1 parent 9fd3184 commit d4eb55c

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionData.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ import kotlinx.serialization.json.Json
2727

2828
/** Session data to be persisted. */
2929
@Serializable
30-
internal data class SessionData(val sessionDetails: SessionDetails, val backgroundTime: Time)
30+
internal data class SessionData(
31+
val sessionDetails: SessionDetails,
32+
val backgroundTime: Time? = null
33+
)
3134

3235
/** DataStore json [Serializer] for [SessionData]. */
3336
@Singleton
@@ -38,11 +41,7 @@ constructor(
3841
private val timeProvider: TimeProvider,
3942
) : Serializer<SessionData> {
4043
override val defaultValue: SessionData
41-
get() =
42-
SessionData(
43-
sessionDetails = sessionGenerator.generateNewSession(currentSession = null),
44-
backgroundTime = timeProvider.currentTime(),
45-
)
44+
get() = SessionData(sessionGenerator.generateNewSession(currentSession = null))
4645

4746
override suspend fun readFrom(input: InputStream): SessionData =
4847
try {

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SharedSessionRepository.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ constructor(
6262
internal var previousNotificationType: NotificationType = NotificationType.GENERAL
6363

6464
init {
65-
println("session repo init")
6665
CoroutineScope(backgroundDispatcher).launch {
6766
sessionDataStore.data
6867
.catch {
6968
val newSession =
7069
SessionData(
7170
sessionDetails = sessionGenerator.generateNewSession(null),
72-
backgroundTime = timeProvider.currentTime()
71+
backgroundTime = null
7372
)
7473
Log.d(
7574
TAG,
@@ -123,15 +122,16 @@ constructor(
123122
val newSessionDetails =
124123
sessionGenerator.generateNewSession(sessionData.sessionDetails)
125124
sessionFirelogPublisher.mayLogSession(sessionDetails = newSessionDetails)
126-
currentSessionData.copy(sessionDetails = newSessionDetails)
125+
currentSessionData.copy(sessionDetails = newSessionDetails, backgroundTime = null)
127126
} else {
128127
currentSessionData
129128
}
130129
}
131130
} catch (ex: Exception) {
132131
Log.d(TAG, "App appForegrounded, failed to update data. Message: ${ex.message}")
133132
val newSessionDetails = sessionGenerator.generateNewSession(sessionData.sessionDetails)
134-
localSessionData = localSessionData.copy(sessionDetails = newSessionDetails)
133+
localSessionData =
134+
localSessionData.copy(sessionDetails = newSessionDetails, backgroundTime = null)
135135
sessionFirelogPublisher.mayLogSession(sessionDetails = newSessionDetails)
136136

137137
val sessionId = newSessionDetails.sessionId
@@ -159,8 +159,12 @@ constructor(
159159
}
160160

161161
private fun shouldInitiateNewSession(sessionData: SessionData): Boolean {
162-
val interval = timeProvider.currentTime() - sessionData.backgroundTime
163-
return interval > sessionsSettings.sessionRestartTimeout
162+
sessionData.backgroundTime?.let {
163+
val interval = timeProvider.currentTime() - it
164+
return interval > sessionsSettings.sessionRestartTimeout
165+
}
166+
Log.d(TAG, "No process has backgrounded yet, should not change the session.")
167+
return false
164168
}
165169

166170
private companion object {

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/SharedSessionRepositoryTest.kt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import com.google.firebase.sessions.testing.FakeUuidGenerator
3333
import kotlin.time.Duration.Companion.hours
3434
import kotlinx.coroutines.ExperimentalCoroutinesApi
3535
import kotlinx.coroutines.asCoroutineDispatcher
36-
import kotlinx.coroutines.launch
3736
import kotlinx.coroutines.test.runCurrent
3837
import kotlinx.coroutines.test.runTest
3938
import org.junit.After
@@ -135,7 +134,7 @@ class SharedSessionRepositoryTest {
135134
}
136135

137136
@Test
138-
fun appForegroundSharedSessionRepo_updateSuccess() = runTest {
137+
fun appForegroundGenerateNewSession_updateSuccess() = runTest {
139138
val sessionFirelogPublisher =
140139
SessionFirelogPublisherImpl(
141140
fakeFirebaseApp.firebaseApp,
@@ -166,20 +165,22 @@ class SharedSessionRepositoryTest {
166165
backgroundDispatcher =
167166
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext
168167
)
169-
backgroundScope.launch {
170-
fakeTimeProvider.addInterval(20.hours)
171-
sharedSessionRepository.appForeground()
172-
}
173168
runCurrent()
169+
170+
fakeTimeProvider.addInterval(20.hours)
171+
sharedSessionRepository.appForeground()
172+
runCurrent()
173+
174174
assertThat(sharedSessionRepository.localSessionData.sessionDetails.sessionId)
175175
.isEqualTo(SESSION_ID_1)
176+
assertThat(sharedSessionRepository.localSessionData.backgroundTime).isNull()
176177
assertThat(sharedSessionRepository.previousNotificationType)
177178
.isEqualTo(SharedSessionRepositoryImpl.NotificationType.GENERAL)
178179
fakeDataStore.close()
179180
}
180181

181182
@Test
182-
fun appForegroundSharedSessionRepo_updateFail() = runTest {
183+
fun appForegroundGenerateNewSession_updateFail() = runTest {
183184
val sessionFirelogPublisher =
184185
SessionFirelogPublisherImpl(
185186
fakeFirebaseApp.firebaseApp,
@@ -201,7 +202,6 @@ class SharedSessionRepositoryTest {
201202
),
202203
IllegalArgumentException("Datastore init failed")
203204
)
204-
fakeDataStore.throwOnNextUpdateData(IllegalArgumentException("Datastore update failed"))
205205
val sharedSessionRepository =
206206
SharedSessionRepositoryImpl(
207207
sessionsSettings,
@@ -212,15 +212,23 @@ class SharedSessionRepositoryTest {
212212
backgroundDispatcher =
213213
TestOnlyExecutors.background().asCoroutineDispatcher() + coroutineContext
214214
)
215+
runCurrent()
215216

216-
backgroundScope.launch {
217-
fakeTimeProvider.addInterval(20.hours)
218-
sharedSessionRepository.appForeground()
219-
}
217+
// set background time first
218+
fakeDataStore.throwOnNextUpdateData(IllegalArgumentException("Datastore update failed"))
219+
sharedSessionRepository.appBackground()
220220
runCurrent()
221+
222+
// foreground update session
223+
fakeTimeProvider.addInterval(20.hours)
224+
fakeDataStore.throwOnNextUpdateData(IllegalArgumentException("Datastore update failed"))
225+
sharedSessionRepository.appForeground()
226+
runCurrent()
227+
221228
// session_2 here because session_1 is failed when try to init datastore
222229
assertThat(sharedSessionRepository.localSessionData.sessionDetails.sessionId)
223230
.isEqualTo(SESSION_ID_2)
231+
assertThat(sharedSessionRepository.localSessionData.backgroundTime).isNull()
224232
assertThat(sharedSessionRepository.previousNotificationType)
225233
.isEqualTo(SharedSessionRepositoryImpl.NotificationType.FALLBACK)
226234
fakeDataStore.close()

0 commit comments

Comments
 (0)