Created
July 25, 2020 21:29
-
-
Save mreram/614a1ab77c3f9542562e2a92c7b53956 to your computer and use it in GitHub Desktop.
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
import kotlinx.coroutines.* | |
import kotlinx.coroutines.channels.consumeEach | |
import kotlinx.coroutines.flow.MutableStateFlow | |
import kotlinx.coroutines.flow.broadcastIn | |
import kotlinx.coroutines.test.runBlockingTest | |
import org.junit.Test | |
class StateFlowBroadcastTest { | |
@ExperimentalCoroutinesApi | |
companion object { | |
private val stateFlow = MutableStateFlow("initial value") | |
@FlowPreview | |
val broadcastChannel = | |
stateFlow.broadcastIn(CoroutineScope(Dispatchers.Unconfined), CoroutineStart.LAZY) | |
@FlowPreview | |
val receiveChannel = broadcastChannel.openSubscription() | |
} | |
@FlowPreview | |
@ExperimentalCoroutinesApi | |
@Test | |
fun `send and receive a bullshit`() { | |
runBlockingTest { | |
launch { | |
receiveChannel.consumeEach { | |
println(it) | |
} | |
} | |
launch { | |
delay(2000) | |
broadcastChannel.send("heyyyyy" + 1) | |
delay(2000) | |
broadcastChannel.close() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment