From f0cffef0d8951ba3e569bafcd4657727ee8dca8f Mon Sep 17 00:00:00 2001 From: santosh-pingle <86107848+santosh-pingle@users.noreply.github.com> Date: Mon, 8 Jul 2024 23:08:25 +0530 Subject: [PATCH] fix tests. (#2606) Co-authored-by: Santosh Pingle --- .../android/fhir/sync/SyncInstrumentedTest.kt | 134 ++++++++++-------- 1 file changed, 74 insertions(+), 60 deletions(-) diff --git a/engine/src/androidTest/java/com/google/android/fhir/sync/SyncInstrumentedTest.kt b/engine/src/androidTest/java/com/google/android/fhir/sync/SyncInstrumentedTest.kt index c449cd3df0..b716846ae9 100644 --- a/engine/src/androidTest/java/com/google/android/fhir/sync/SyncInstrumentedTest.kt +++ b/engine/src/androidTest/java/com/google/android/fhir/sync/SyncInstrumentedTest.kt @@ -34,9 +34,11 @@ import com.google.android.fhir.testing.TestFhirEngineImpl import com.google.common.truth.Truth.assertThat import java.util.concurrent.TimeUnit import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.transformWhile +import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import org.junit.Test import org.junit.runner.RunWith @@ -106,31 +108,36 @@ class SyncInstrumentedTest { } @Test - fun oneTimeSync_currentSyncJobStatusSucceeded_nextCurrentSyncJobStatusShouldBeRunning() { - WorkManagerTestInitHelper.initializeTestWorkManager(context) - val states = mutableListOf() - val nextExecutionStates = mutableListOf() + fun oneTimeSync_currentSyncJobStatusSucceeded_nextCurrentSyncJobStatusShouldBeRunning() = runBlocking { - Sync.oneTimeSync(context = context) - .transformWhile { - states.add(it) - emit(it is CurrentSyncJobStatus.Succeeded) - it !is CurrentSyncJobStatus.Succeeded + WorkManagerTestInitHelper.initializeTestWorkManager(context) + val states = mutableListOf() + val nextExecutionStates = mutableListOf() + + launch { + Sync.oneTimeSync(context = context).collect { + states.add(it) + if (it is CurrentSyncJobStatus.Succeeded) { + cancel() + } + } } - .shareIn(this, SharingStarted.Eagerly, 5) + .join() - Sync.oneTimeSync(context = context) - .transformWhile { - nextExecutionStates.add(it) - emit(it is CurrentSyncJobStatus.Succeeded) - it !is CurrentSyncJobStatus.Succeeded + launch { + Sync.oneTimeSync(context = context).collect { + nextExecutionStates.add(it) + if (it is CurrentSyncJobStatus.Succeeded) { + cancel() + } + } } - .shareIn(this, SharingStarted.Eagerly, 5) + .join() + + assertThat(states.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) + assertThat(states.last()).isInstanceOf(CurrentSyncJobStatus.Succeeded::class.java) + assertThat(nextExecutionStates.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) } - assertThat(states.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) - assertThat(states.last()).isInstanceOf(CurrentSyncJobStatus.Succeeded::class.java) - assertThat(nextExecutionStates.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) - } @Test fun oneTime_worker_failedSyncState() { @@ -160,51 +167,58 @@ class SyncInstrumentedTest { } @Test - fun oneTimeSync_currentSyncJobStatusFailed_nextCurrentSyncJobStatusShouldBeRunning() { - WorkManagerTestInitHelper.initializeTestWorkManager(context) - val states = mutableListOf() - val nextExecutionStates = mutableListOf() + fun oneTimeSync_currentSyncJobStatusFailed_nextCurrentSyncJobStatusShouldBeRunning() = runBlocking { - Sync.oneTimeSync( - context = context, - RetryConfiguration( - BackoffCriteria( - BackoffPolicy.LINEAR, - 30, - TimeUnit.SECONDS, - ), - 0, - ), - ) - .transformWhile { - states.add(it) - emit(it is CurrentSyncJobStatus.Failed) - it !is CurrentSyncJobStatus.Failed + WorkManagerTestInitHelper.initializeTestWorkManager(context) + val states = mutableListOf() + val nextExecutionStates = mutableListOf() + + launch { + Sync.oneTimeSync( + context = context, + RetryConfiguration( + BackoffCriteria( + BackoffPolicy.LINEAR, + 30, + TimeUnit.SECONDS, + ), + 0, + ), + ) + .collect { + states.add(it) + if (it is CurrentSyncJobStatus.Failed) { + cancel() + } + } } - .shareIn(this, SharingStarted.Eagerly, 5) + .join() - Sync.oneTimeSync( - context = context, - RetryConfiguration( - BackoffCriteria( - BackoffPolicy.LINEAR, - 30, - TimeUnit.SECONDS, - ), - 0, - ), - ) - .transformWhile { - nextExecutionStates.add(it) - emit(it is CurrentSyncJobStatus.Failed) - it !is CurrentSyncJobStatus.Failed + launch { + Sync.oneTimeSync( + context = context, + RetryConfiguration( + BackoffCriteria( + BackoffPolicy.LINEAR, + 30, + TimeUnit.SECONDS, + ), + 0, + ), + ) + .collect { + nextExecutionStates.add(it) + if (it is CurrentSyncJobStatus.Failed) { + cancel() + } + } } - .shareIn(this, SharingStarted.Eagerly, 5) + .join() + + assertThat(states.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) + assertThat(states.last()).isInstanceOf(CurrentSyncJobStatus.Failed::class.java) + assertThat(nextExecutionStates.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) } - assertThat(states.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) - assertThat(states.last()).isInstanceOf(CurrentSyncJobStatus.Failed::class.java) - assertThat(nextExecutionStates.first()).isInstanceOf(CurrentSyncJobStatus.Running::class.java) - } @Test fun periodic_worker_periodicSyncState() {