Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions app/src/main/kotlin/com/wire/android/ui/home/AppSyncViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ 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
import com.wire.kalium.logic.feature.e2ei.usecase.ObserveCertificateRevocationForSelfClientUseCase
import com.wire.kalium.logic.feature.featureConfig.FeatureFlagsSyncWorker
import com.wire.kalium.logic.feature.mlsmigration.MLSMigrationManager
import com.wire.kalium.logic.feature.server.UpdateApiVersionsUseCase
import com.wire.kalium.util.KaliumDispatcher
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
Expand All @@ -46,7 +49,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

Expand All @@ -60,7 +64,7 @@ class AppSyncViewModel @Inject constructor(
if (isPullTooRecent(now)) return

lastPullInstant = now
syncDataJob = viewModelScope.launch {
syncDataJob = viewModelScope.launch(dispatcher.io()) {
runSyncTasks()
}
}
Expand All @@ -79,13 +83,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
}
}
}
Loading