Skip to content

Instantly share code, notes, and snippets.

@AJIEKCX
Last active May 25, 2022 17:12
Show Gist options
  • Save AJIEKCX/4999dcffffea860147c77475bd841362 to your computer and use it in GitHub Desktop.
Save AJIEKCX/4999dcffffea860147c77475bd841362 to your computer and use it in GitHub Desktop.
@Test
fun sharedFlowSampleTest() = runTest {
val values = mutableListOf<Int>()
val sharedFlow = MutableSharedFlow<Int>()
val job = launch {
sharedFlow.toList(values)
}
advanceUntilIdle()
sharedFlow.emit(1)
sharedFlow.emit(2)
sharedFlow.emit(3)
job.cancel()
assertEquals(listOf(1, 2, 3), values)
}
@Test
fun sharedFlowSampleTest2() = runTest {
val values = mutableListOf<Int>()
val sharedFlow = MutableSharedFlow<Int>()
val job = launch(UnconfinedTestDispatcher()) {
sharedFlow.toList(values)
}
sharedFlow.emit(1)
sharedFlow.emit(2)
sharedFlow.emit(3)
job.cancel()
assertEquals(listOf(1, 2, 3), values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment