From 64b97c1ba4a58efcc2094590061e9d618363d760 Mon Sep 17 00:00:00 2001 From: Fabio Tacke Date: Tue, 16 Nov 2021 14:37:48 +0100 Subject: [PATCH 1/2] Fix session start tracking --- app/src/main/java/tice/AppFlow.kt | 32 ++++++++++++++++++--------- app/src/test/java/tice/AppFlowTest.kt | 15 ------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/tice/AppFlow.kt b/app/src/main/java/tice/AppFlow.kt index 0c2b9fa..736198e 100644 --- a/app/src/main/java/tice/AppFlow.kt +++ b/app/src/main/java/tice/AppFlow.kt @@ -126,6 +126,8 @@ class AppFlow constructor(val application: TICEApplication) : LifecycleObserver, private lateinit var workManager: WorkManager + private var appInitialized = false + @OptIn(ExperimentalStdlibApi::class) fun initApp(): Job { val initJob = Job() @@ -203,22 +205,32 @@ class AppFlow constructor(val application: TICEApplication) : LifecycleObserver, } val versionCode = BuildConfig.VERSION_CODE + tracker.get().setProperty(0, "android-$versionCode") + tracker.get().start() + trackSessionStart() - if (BuildConfig.APPLICATION_ID != "app.tice.TICE.development") { - tracker.get().start() - } + appInitialized = true return initJob } + private fun trackSessionStart() { + val lang = + ConfigurationCompat.getLocales(application.applicationContext.resources.configuration)[0].language + tracker.get().track(TrackerEvent.sessionStart(lang)) + } + fun onStop() { logger.debug("OnStop called") - val sessionDuration = Date().time - (sessionStart ?: Date()).time - tracker.get().track(TrackerEvent.sessionEnd(sessionDuration)) - CoroutineScope(coroutineContextProvider.get().IO).launch { - tracker.get().dispatch() + if (appInitialized) { + val sessionDuration = Date().time - (sessionStart ?: Date()).time + tracker.get().track(TrackerEvent.sessionEnd(sessionDuration)) + + CoroutineScope(coroutineContextProvider.get().IO).launch { + tracker.get().dispatch() + } } webSocketReceiver.get().disconnect() @@ -228,10 +240,10 @@ class AppFlow constructor(val application: TICEApplication) : LifecycleObserver, fun onMoveToForeground() { _isInForeground = AppStatusProvider.Status.FOREGROUND - val lang = - ConfigurationCompat.getLocales(application.applicationContext.resources.configuration)[0].language - tracker.get().track(TrackerEvent.sessionStart(lang)) sessionStart = Date() + if (appInitialized) { + trackSessionStart() + } } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) diff --git a/app/src/test/java/tice/AppFlowTest.kt b/app/src/test/java/tice/AppFlowTest.kt index e2e45a2..76bc5e7 100644 --- a/app/src/test/java/tice/AppFlowTest.kt +++ b/app/src/test/java/tice/AppFlowTest.kt @@ -307,16 +307,8 @@ internal class AppFlowTest { @Test fun onStop() = runBlocking { - val slot = slot() - every { mockTracker.track(capture(slot), any()) } answers { } - appFlow.onStop() - testDispatcher.advanceUntilIdle() - - coVerify(exactly = 1) { mockTracker.dispatch() } - verify(exactly = 1) { mockTracker.track(any()) } - Assertions.assertEquals(slot.captured.name, "SessionEnd") coVerify(exactly = 1) { mockWebSocketReceiver.disconnect() } } @@ -324,19 +316,12 @@ internal class AppFlowTest { fun onMoveToForeground() = runBlocking { val defaultStatus = appFlow.status - val slot = slot() - every { mockTracker.track(capture(slot), any()) } answers { } - appFlow.onMoveToForeground() val newStatus = appFlow.status Assertions.assertEquals(defaultStatus, AppStatusProvider.Status.BACKGROUND) Assertions.assertEquals(newStatus, AppStatusProvider.Status.FOREGROUND) - - verify(exactly = 1) { mockTracker.track(any()) } - Assertions.assertEquals(slot.captured.name, "SessionStart") - Assertions.assertEquals(slot.captured.detail, testLanguage) } @Test From 7eb51d9149b0341b7b890485cfcaf485f9eaaf8e Mon Sep 17 00:00:00 2001 From: Fabio Tacke Date: Thu, 18 Nov 2021 09:37:26 +0100 Subject: [PATCH 2/2] Fix flaky test --- .../androidTest/java/tice/managers/services/AuthManagerTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/androidTest/java/tice/managers/services/AuthManagerTest.kt b/app/src/androidTest/java/tice/managers/services/AuthManagerTest.kt index 427a79a..dc7dff8 100644 --- a/app/src/androidTest/java/tice/managers/services/AuthManagerTest.kt +++ b/app/src/androidTest/java/tice/managers/services/AuthManagerTest.kt @@ -144,7 +144,7 @@ class AuthManagerTest { val exp = Date(Date().time + 100 * 1000) val cert = createMembershipCertificate(userId, groupId, true, adminUserId, Date(Date().time - 10 * 1000), exp, privateKey) - Assertions.assertEquals(authManager.membershipCertificateExpirationDate(cert, publicKey).time.toFloat(), exp.time.toFloat(), 1000.0.toFloat()) + Assertions.assertEquals(authManager.membershipCertificateExpirationDate(cert, publicKey).time.toFloat(), exp.time.toFloat(), 2000.0.toFloat()) } @Test