MicroUtils/coroutines/src/commonTest/kotlin/SpecialMutableStateFlowTests.kt

33 lines
1.1 KiB
Kotlin
Raw Normal View History

import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
2024-05-12 15:34:17 +00:00
import dev.inmo.micro_utils.coroutines.asDeferred
import dev.inmo.micro_utils.coroutines.subscribe
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
2024-05-12 15:34:17 +00:00
import kotlin.test.assertTrue
class SpecialMutableStateFlowTests {
@Test
2024-05-12 15:34:17 +00:00
fun simpleTest() = runTest {
val specialMutableStateFlow = SpecialMutableStateFlow(0)
2024-05-12 15:34:17 +00:00
specialMutableStateFlow.value = 1
specialMutableStateFlow.first { it == 1 }
assertEquals(1, specialMutableStateFlow.value)
}
@Test
2024-05-12 15:34:17 +00:00
fun specialTest() = runTest {
val specialMutableStateFlow = SpecialMutableStateFlow(0)
2024-05-12 15:34:17 +00:00
lateinit var subscriberJob: Job
subscriberJob = specialMutableStateFlow.subscribe(this) {
when (it) {
1 -> specialMutableStateFlow.value = 2
2 -> subscriberJob.cancel()
}
}
2024-05-12 15:34:17 +00:00
specialMutableStateFlow.value = 1
subscriberJob.join()
assertEquals(2, specialMutableStateFlow.value)
}
}