Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ internal class ValidateSubjectPoolViewModel @Inject constructor(
val state: LiveData<LiveDataEventWithContent<ValidateSubjectPoolState>>
get() = _state
private var _state = MutableLiveData<LiveDataEventWithContent<ValidateSubjectPoolState>>()
private var isSyncing: Boolean = false

fun checkIdentificationPool(subjectQuery: SubjectQuery) = viewModelScope.launch {
if (isSyncing) {
return@launch
}
_state.send(ValidateSubjectPoolState.Validating)

val validationState = when {
Expand All @@ -43,7 +47,9 @@ internal class ValidateSubjectPoolViewModel @Inject constructor(

fun syncAndRetry(subjectQuery: SubjectQuery) = viewModelScope.launch {
_state.send(ValidateSubjectPoolState.SyncInProgress)
isSyncing = true
runBlockingSync()
isSyncing = false
checkIdentificationPool(subjectQuery)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.simprints.feature.validatepool.screen
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.google.common.truth.Truth.assertThat
import com.jraska.livedata.test
import com.simprints.core.livedata.LiveDataEventWithContent
import com.simprints.feature.validatepool.usecase.HasRecordsUseCase
import com.simprints.feature.validatepool.usecase.IsModuleIdNotSyncedUseCase
import com.simprints.feature.validatepool.usecase.RunBlockingEventSyncUseCase
Expand All @@ -15,7 +14,9 @@ import io.mockk.coEvery
import io.mockk.coJustRun
import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import io.mockk.justRun
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -186,4 +187,20 @@ class ValidateSubjectPoolViewModelTest {
)
coVerify(exactly = 1) { runBlockingSync() }
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun `checkIdentificationPool not re-validating another time when sync in progress`() = runTest {
val subjectQuery = SubjectQuery(projectId = "projectId")
coEvery { hasRecordsUseCase(subjectQuery) } returns true
coEvery { runBlockingSync() } coAnswers {
delay(1000)
}
viewModel.syncAndRetry(subjectQuery)

viewModel.checkIdentificationPool(subjectQuery)

advanceUntilIdle()
coVerify(exactly = 1) { hasRecordsUseCase(any()) }
}
}