Created
July 30, 2021 22:08
-
-
Save hrach/94ba0dc427ad2f08a73467d8972516a2 to your computer and use it in GitHub Desktop.
Difference between MutableSharedFlow() & Channel()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo1 { | |
private val unpause = MutableSharedFlow<Unit>() | |
suspend fun await() { | |
delay(1000) | |
unpause.first() | |
} | |
fun unpause() { | |
unpause.tryEmit(Unit) | |
} | |
} | |
class Foo2 { | |
private val unpause = Channel<Unit>() | |
suspend fun await() { | |
delay(1000) | |
unpause.receiveCatching() | |
} | |
fun unpause() { | |
unpause.trySend(Unit) | |
} | |
} | |
class ExampleUnitTest { | |
@Test | |
fun testUnpause() = runBlockingTest { | |
var afterPause = false | |
val foo = Foo1() | |
launch { | |
foo.await() | |
afterPause = true | |
} | |
advanceTimeBy(1000) | |
assertEquals(false, afterPause) | |
foo.unpause() | |
assertEquals(true, afterPause) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment