diff --git a/app/src/main/kotlin/com/wire/android/ui/home/AppSyncViewModel.kt b/app/src/main/kotlin/com/wire/android/ui/home/AppSyncViewModel.kt index 857285c47f8..be561279771 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/AppSyncViewModel.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/AppSyncViewModel.kt @@ -20,6 +20,7 @@ package com.wire.android.ui.home import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.wire.android.appLogger +import com.wire.android.util.dispatchers.DispatcherProvider import com.wire.kalium.logic.feature.client.MLSClientManager import com.wire.kalium.logic.feature.conversation.keyingmaterials.KeyingMaterialsManager import com.wire.kalium.logic.feature.e2ei.SyncCertificateRevocationListUseCase @@ -46,7 +47,8 @@ class AppSyncViewModel @Inject constructor( private val mLSMigrationManager: MLSMigrationManager, private val keyingMaterialsManager: KeyingMaterialsManager, private val mLSClientManager: MLSClientManager, - ) : ViewModel() { + private val dispatcher: DispatcherProvider, +) : ViewModel() { private val minIntervalBetweenPulls: Duration = MIN_INTERVAL_BETWEEN_PULLS @@ -60,7 +62,7 @@ class AppSyncViewModel @Inject constructor( if (isPullTooRecent(now)) return lastPullInstant = now - syncDataJob = viewModelScope.launch { + syncDataJob = viewModelScope.launch(dispatcher.io()) { runSyncTasks() } } @@ -79,13 +81,13 @@ class AppSyncViewModel @Inject constructor( private suspend fun runSyncTasks() { try { listOf( - viewModelScope.launch { syncCertificateRevocationListUseCase() }, - viewModelScope.launch { featureFlagsSyncWorker.execute() }, - viewModelScope.launch { observeCertificateRevocationForSelfClient.invoke() }, - viewModelScope.launch { updateApiVersions() }, - viewModelScope.launch { mLSClientManager() }, - viewModelScope.launch { mLSMigrationManager() }, - viewModelScope.launch { keyingMaterialsManager() }, + viewModelScope.launch(dispatcher.io()) { syncCertificateRevocationListUseCase() }, + viewModelScope.launch(dispatcher.io()) { featureFlagsSyncWorker.execute() }, + viewModelScope.launch(dispatcher.io()) { observeCertificateRevocationForSelfClient.invoke() }, + viewModelScope.launch(dispatcher.io()) { updateApiVersions() }, + viewModelScope.launch(dispatcher.io()) { mLSClientManager() }, + viewModelScope.launch(dispatcher.io()) { mLSMigrationManager() }, + viewModelScope.launch(dispatcher.io()) { keyingMaterialsManager() }, ).joinAll() } catch (e: Exception) { appLogger.e("Error while syncing app config", e) diff --git a/app/src/test/kotlin/com/wire/android/ui/home/AppSyncViewModelTest.kt b/app/src/test/kotlin/com/wire/android/ui/home/AppSyncViewModelTest.kt index 14364a3839c..687094ab9fb 100644 --- a/app/src/test/kotlin/com/wire/android/ui/home/AppSyncViewModelTest.kt +++ b/app/src/test/kotlin/com/wire/android/ui/home/AppSyncViewModelTest.kt @@ -18,6 +18,7 @@ package com.wire.android.ui.home import com.wire.android.config.CoroutineTestExtension +import com.wire.android.config.TestDispatcherProvider import com.wire.kalium.logic.feature.client.MLSClientManager import com.wire.kalium.logic.feature.conversation.keyingmaterials.KeyingMaterialsManager import com.wire.kalium.logic.feature.e2ei.SyncCertificateRevocationListUseCase @@ -37,9 +38,11 @@ import org.junit.jupiter.api.extension.ExtendWith @ExtendWith(CoroutineTestExtension::class) class AppSyncViewModelTest { + private val testDispatcher = TestDispatcherProvider() + @Test - fun `when startSyncingAppConfig is called then it should call the use case`() = runTest { - val (arrangement, viewModel) = Arrangement().arrange { + fun `when startSyncingAppConfig is called then it should call the use case`() = runTest(testDispatcher.io()) { + val (arrangement, viewModel) = Arrangement().arrange(testDispatcher) { withObserveCertificateRevocationForSelfClient() withFeatureFlagsSyncWorker() withSyncCertificateRevocationListUseCase() @@ -62,8 +65,8 @@ class AppSyncViewModelTest { } @Test - fun `when startSyncingAppConfig is called multiple times then it should call the use case with delay`() = runTest { - val (arrangement, viewModel) = Arrangement().arrange { + fun `when startSyncingAppConfig is called multiple times then it should call the use case with delay`() = runTest(testDispatcher.io()) { + val (arrangement, viewModel) = Arrangement().arrange(testDispatcher) { withObserveCertificateRevocationForSelfClient(1000) withFeatureFlagsSyncWorker(1000) withSyncCertificateRevocationListUseCase(1000) @@ -114,16 +117,6 @@ class AppSyncViewModelTest { MockKAnnotations.init(this) } - private val viewModel = AppSyncViewModel( - syncCertificateRevocationListUseCase, - observeCertificateRevocationForSelfClient, - featureFlagsSyncWorker, - updateApiVersions, - mLSClientManager = mlsClientManager, - mLSMigrationManager = mlsMigrationManager, - keyingMaterialsManager = keyingMaterialsManager - ) - fun withObserveCertificateRevocationForSelfClient(delayMs: Long = 0) { coEvery { observeCertificateRevocationForSelfClient.invoke() } coAnswers { delay(delayMs) @@ -166,8 +159,17 @@ class AppSyncViewModelTest { } } - fun arrange(block: Arrangement.() -> Unit) = apply(block).let { - this to viewModel + fun arrange(testDispatcher: TestDispatcherProvider, block: Arrangement.() -> Unit) = apply(block).let { + this to AppSyncViewModel( + syncCertificateRevocationListUseCase, + observeCertificateRevocationForSelfClient, + featureFlagsSyncWorker, + updateApiVersions, + mLSClientManager = mlsClientManager, + mLSMigrationManager = mlsMigrationManager, + keyingMaterialsManager = keyingMaterialsManager, + dispatcher = testDispatcher + ) } } }