title: Unit testing Kotlin's ConflatedBroadcastChannel behavior
body: <p>In the new project that I'm currently working on I have no RxJava dependency at all, because until now I didn't need that - coroutines solve threading problem pretty gracefully.</p>
  
  <p>At this point I stumbled upon on a requirement to have a <a href="https://kotlinlang.org/docs/reference/coroutines/channels.html" rel="nofollow noreferrer"><code>BehaviorSubject</code></a>-alike behavior, where one can subscribe to a stream of data and receive the latest value upon subscription. As I've learned, <a href="https://kotlinlang.org/docs/reference/coroutines/channels.html" rel="nofollow noreferrer"><code>Channel</code></a>s provide very similar behavior in Kotlin, so I decided to give them a try.</p>
  
  <p>From <a href="https://proandroiddev.com/part-3-transmitting-streams-of-value-with-channels-70cf8dccf2b" rel="nofollow noreferrer">this</a> article I've learned, that <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-conflated-broadcast-channel/" rel="nofollow noreferrer"><code>ConflatedBroadcastChannel</code></a> is the type of channel that mimics <code>BehaviorSubject</code>, so I declared following:</p>
  
  <pre><code>class ChannelSender {
  
      val channel = ConflatedBroadcastChannel&lt;String&gt;()
  
      fun sendToChannel(someString: String) {
           GlobalScope.launch(Dispatchers.Main) { channel.send(someString) }
      }
  }
  </code></pre>
  
  <p>For listening to the channel I do this:</p>
  
  <pre>
  <code>
      class ChannelListener(val channelSender: ChannelSender) {
          fun listenToChannel() {
              channelSender.channel.consumeEach { someString ->
                  if (someString == "A") foo.perform() 
                  else bar.perform()
              }
          }
      }
  </code>
  </pre>
  
  <p>This works as expected, but at this point I'm having difficulties understanding how to unit test <code>ChannelListener</code>.</p>
  
  <p>I've tried to find something related <a href="https://github.com/Kotlin/kotlinx.coroutines/tree/master/kotlinx-coroutines-core/jvm/test/guide" rel="nofollow noreferrer">here</a>, but none of <code>example-channel-**.kt</code> classes were helpful.</p>
  
  <p>Any help, suggestion or correction related to my incorrect assumptions is appreciated. Thanks.</p>