Skip to content

Commit

Permalink
Update currentSyncJobStatus for oneTimeSync when syncJobStatus is null (
Browse files Browse the repository at this point in the history
google#2511)

* currentSyncJobStatus when sync state in workmanager is null.

* unit tests.

* update kdoc

* address review comments.

---------

Co-authored-by: Santosh Pingle <[email protected]>
  • Loading branch information
santosh-pingle and Santosh Pingle committed Jul 8, 2024
1 parent f363237 commit f3e2842
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SyncInstrumentedTest {
}

@Test
fun oneTime_worker_nextExecutionAfterSucceeded() {
fun oneTimeSync_currentSyncJobStatusSucceeded_nextCurrentSyncJobStatusShouldBeRunning() {
WorkManagerTestInitHelper.initializeTestWorkManager(context)
val states = mutableListOf<CurrentSyncJobStatus>()
val nextExecutionStates = mutableListOf<CurrentSyncJobStatus>()
Expand Down Expand Up @@ -160,7 +160,7 @@ class SyncInstrumentedTest {
}

@Test
fun oneTime_worker_nextExecutionAfterFailed() {
fun oneTimeSync_currentSyncJobStatusFailed_nextCurrentSyncJobStatusShouldBeRunning() {
WorkManagerTestInitHelper.initializeTestWorkManager(context)
val states = mutableListOf<CurrentSyncJobStatus>()
val nextExecutionStates = mutableListOf<CurrentSyncJobStatus>()
Expand Down
23 changes: 11 additions & 12 deletions engine/src/main/java/com/google/android/fhir/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,16 @@ object Sync {
return combineSyncStateForPeriodicSync(context, uniqueWorkName, flow)
}

/** Gets the worker info for the [FhirSyncWorker] */
/**
* Retrieves the work information for a specific unique work name as a flow of pairs containing
* the work state and the corresponding progress data if available.
*
* @param context The application context.
* @param workName The unique name of the work to retrieve information for.
* @return A flow emitting pairs of [WorkInfo.State] and [SyncJobStatus]. The flow will emit only
* when the progress data contains a non-empty key-value map and includes a key of type [String]
* with the name "StateType".
*/
@PublishedApi
internal fun getWorkerInfo(context: Context, workName: String) =
WorkManager.getInstance(context)
Expand Down Expand Up @@ -306,20 +315,10 @@ object Sync {
}

/**
* Only call this API when syncJobStatusFromWorkManager is null. Create a [CurrentSyncJobStatus]
* Only call this API when syncJobStatus From WorkManager is null. Create a [CurrentSyncJobStatus]
* from [WorkInfo.State]. (Note: syncJobStatusFromDataStore is updated as lastSynJobStatus, which
* is the terminalSyncJobStatus.)
*/
private fun handleNullWorkManagerStatusForPeriodicSync(
workInfoState: WorkInfo.State,
): CurrentSyncJobStatus =
when (workInfoState) {
RUNNING -> Running(SyncJobStatus.Started())
ENQUEUED -> Enqueued
CANCELLED -> Cancelled
else -> error("Inconsistent WorkInfo.State in periodic sync : $workInfoState.")
}

private fun handleNullWorkManagerStatusForPeriodicSync(
workInfoState: WorkInfo.State,
syncJobStatusFromDataStore: SyncJobStatus?,
Expand Down

0 comments on commit f3e2842

Please sign in to comment.