Guillermo Alcantara
02/02/2021, 12:58 AM@Test
fun testFoo() = runBlocking {
val testCoroutineScope = TestCoroutineScope().apply {
pauseDispatcher() // This needs to be here because the actual test, handles time.
}
val sharedFlow = MutableSharedFlow<Int>()
val values = mutableListOf<Int>()
println("before launch")
val job = testCoroutineScope.launch {
println("before collect")
sharedFlow.collect {
println("before adding $it")
values.add(it)
}
}
println("before emits")
sharedFlow.emit(1)
sharedFlow.emit(2)
testCoroutineScope.runCurrent()
assertEquals(mutableListOf(1, 2), values)
job.cancel()
}
Guillermo Alcantara
02/02/2021, 12:58 AMFunkyMuse
02/02/2021, 8:00 AMGuillermo Alcantara
02/02/2021, 4:11 PMtateisu
02/02/2021, 8:42 PMtateisu
02/02/2021, 8:43 PMpublic fun <T> MutableSharedFlow( replay: Int = 0, ...)
tateisu
02/02/2021, 8:44 PMtateisu
02/02/2021, 8:49 PMtateisu
02/02/2021, 8:50 PMGuillermo Alcantara
02/02/2021, 9:04 PMextraBufferCapacity
, this doesn't seem to help either. I added logic to have a job.join
and cancel the collect but again doesn't seem to help.tateisu
02/02/2021, 9:26 PMtateisu
02/02/2021, 9:27 PMtateisu
02/02/2021, 9:29 PMGuillermo Alcantara
02/02/2021, 9:30 PMtateisu
02/02/2021, 9:32 PMGuillermo Alcantara
02/02/2021, 9:35 PMGuillermo Alcantara
02/02/2021, 9:46 PMpauseDispatcher()
tateisu
02/02/2021, 9:50 PMGuillermo Alcantara
02/02/2021, 9:54 PMtestCoroutineScope.runCurrent()
tateisu
02/02/2021, 9:57 PMGuillermo Alcantara
02/02/2021, 10:04 PM@Test
fun testFoo() = runBlocking {
val testCoroutineScope = TestCoroutineScope().apply {
pauseDispatcher()
}
val sharedFlow = MutableSharedFlow<Int>(
extraBufferCapacity = 2 // Without it, sharedFlow.emit won't have a space to save data. It will be collected
// next time there's a testCoroutineScope.runCurrent()
)
val values = mutableListOf<Int>()
println("before launch")
val job = testCoroutineScope.launch {
println("before collect")
sharedFlow.collect {
println("before adding $it")
values.add(it)
}
}
testCoroutineScope.runCurrent() // Allows the previous launch to start collecting
println("before emits")
sharedFlow.emit(1)
sharedFlow.emit(2)
testCoroutineScope.runCurrent()
assertEquals(mutableListOf(1, 2), values)
job.cancel()
}
Guillermo Alcantara
02/02/2021, 10:06 PMGuillermo Alcantara
02/02/2021, 10:12 PM