2024-05-12 13:17:16 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.SpecialMutableStateFlow
|
2024-05-12 15:34:17 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.asDeferred
|
2024-05-12 13:17:16 +00:00
|
|
|
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
|
2024-05-12 13:17:16 +00:00
|
|
|
|
|
|
|
class SpecialMutableStateFlowTests {
|
|
|
|
@Test
|
2024-05-12 15:34:17 +00:00
|
|
|
fun simpleTest() = runTest {
|
2024-05-12 13:17:16 +00:00
|
|
|
val specialMutableStateFlow = SpecialMutableStateFlow(0)
|
2024-05-12 15:34:17 +00:00
|
|
|
specialMutableStateFlow.value = 1
|
|
|
|
specialMutableStateFlow.first { it == 1 }
|
2024-05-12 13:17:16 +00:00
|
|
|
assertEquals(1, specialMutableStateFlow.value)
|
|
|
|
}
|
|
|
|
@Test
|
2024-05-12 15:34:17 +00:00
|
|
|
fun specialTest() = runTest {
|
2024-05-12 13:17:16 +00:00
|
|
|
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 13:17:16 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-12 15:34:17 +00:00
|
|
|
specialMutableStateFlow.value = 1
|
|
|
|
subscriberJob.join()
|
2024-05-12 13:17:16 +00:00
|
|
|
assertEquals(2, specialMutableStateFlow.value)
|
|
|
|
}
|
|
|
|
}
|