Skip to content

Commit

Permalink
Fix race condition in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Melchior committed Jan 18, 2024
1 parent 1659328 commit 207c239
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import io.realm.kotlin.test.mongodb.TestApp
import io.realm.kotlin.test.mongodb.common.utils.assertFailsWithMessage
import io.realm.kotlin.test.mongodb.createUserAndLogIn
import io.realm.kotlin.test.mongodb.use
import io.realm.kotlin.test.util.TestChannel
import io.realm.kotlin.test.util.receiveOrFail
import io.realm.kotlin.test.util.use
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
Expand Down Expand Up @@ -253,19 +256,26 @@ class ProgressListenerTests {

@Test
fun completesOnClose() = runBlocking {
val channel = TestChannel<Boolean>(capacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
TestApp("completesOnClose", TEST_APP_PARTITION).use { app ->
val user = app.createUserAndLogIn()
val realm = Realm.open(createSyncConfig(user))
try {
val flow = realm.syncSession.progressAsFlow(Direction.DOWNLOAD, ProgressMode.INDEFINITELY)
val job = async {
withTimeout(10.seconds) {
flow.collect { }
flow.collect {
channel.send(true)
}
}
}
// Wait for Flow to start, so we do not close the Realm before
// `flow.collect()` can be called.
channel.receiveOrFail()
realm.close()
job.await()
} finally {
channel.close()
if (!realm.isClosed()) {
realm.close()
}
Expand Down

0 comments on commit 207c239

Please sign in to comment.