@@ -33,7 +33,6 @@ import com.google.firebase.sessions.testing.FakeUuidGenerator
3333import kotlin.time.Duration.Companion.hours
3434import kotlinx.coroutines.ExperimentalCoroutinesApi
3535import kotlinx.coroutines.asCoroutineDispatcher
36- import kotlinx.coroutines.launch
3736import kotlinx.coroutines.test.runCurrent
3837import kotlinx.coroutines.test.runTest
3938import 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