Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize sync workers #2385

Merged
merged 11 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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 @@ -25,6 +25,8 @@ import java.time.OffsetDateTime
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.hl7.fhir.r4.model.ResourceType

enum class SyncOperation {
Expand Down Expand Up @@ -79,18 +81,20 @@ internal class FhirSynchronizer(
}

suspend fun synchronize(): SyncJobStatus {
setSyncState(SyncJobStatus.Started())

return listOf(download(), upload())
.filterIsInstance<SyncResult.Error>()
.flatMap { it.exceptions }
.let {
if (it.isEmpty()) {
setSyncState(SyncResult.Success())
} else {
setSyncState(SyncResult.Error(it))
mutex.withLock(mutexOwner) {
setSyncState(SyncJobStatus.Started())

return listOf(download(), upload())
.filterIsInstance<SyncResult.Error>()
.flatMap { it.exceptions }
.let {
if (it.isEmpty()) {
setSyncState(SyncResult.Success())
} else {
setSyncState(SyncResult.Error(it))
}
}
}
}
}

private suspend fun download(): SyncResult {
Expand Down Expand Up @@ -141,4 +145,19 @@ internal class FhirSynchronizer(
SyncResult.Error(exceptions)
}
}

companion object {
private val mutex = Mutex()
private var mutexOwner: Any? = null

/**
* This is used in testing. In testing the [mutexOwner] should be non-null to catch an
* [IllegalStateException] thrown when trying to re-synchronize. In production the [mutexOwner]
* should be null so that successive synchronize requests are suspended and resumed when lock is
* released.
*/
fun setMutexOwner(owner: Any) {
mutexOwner = owner
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.google.android.fhir.sync.upload.UploadSyncResult
import com.google.android.fhir.sync.upload.Uploader
import com.google.android.fhir.testing.TestFhirEngineImpl
import com.google.common.truth.Truth.assertThat
import java.lang.IllegalStateException
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -53,8 +54,11 @@ class FhirSynchronizerTest {

private lateinit var fhirSynchronizer: FhirSynchronizer

private val mutexOwner = Object()

@Before
fun setUp() {
FhirSynchronizer.setMutexOwner(mutexOwner)
MockitoAnnotations.openMocks(this)
fhirSynchronizer =
FhirSynchronizer(
Expand Down Expand Up @@ -142,4 +146,36 @@ class FhirSynchronizerTest {
assertThat(result).isInstanceOf(SyncJobStatus.Failed::class.java)
assertThat(listOf(error)).isEqualTo((result as SyncJobStatus.Failed).exceptions)
}

@Test
fun `re-synchronize should emit IllegalStateException on trying to acquire mutex lock`() =
runTest(UnconfinedTestDispatcher()) {
`when`(downloader.download())
.thenReturn(
flowOf(DownloadState.Success(listOf(), 0, 0)),
)
`when`(uploader.upload(any()))
.thenReturn(
UploadSyncResult.Success(
listOf(),
),
)

backgroundScope.launch {
fhirSynchronizer.syncState.collect {
if (it is SyncJobStatus.Started) {
try {
fhirSynchronizer.synchronize()
} catch (e: Exception) {
assertThat(e).isInstanceOf(IllegalStateException::class.java)
assertThat(e.localizedMessage).isNotNull()
assertThat(e.localizedMessage)
.isEqualTo("This mutex is already locked by the specified owner: $mutexOwner")
}
MJ1998 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

val result = fhirSynchronizer.synchronize()
MJ1998 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading