package com.swago.app

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.junit.Test

import org.junit.Assert.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun produceAndConsume(){

        GlobalScope.launch {
            val channel1 = Channel<Int>()
            val producer = GlobalScope.launch {
                var i = 0
                while (true){
                    println("生产者生产了:$i")
                    channel1.send(i++)
                    delay(1000)
                }
            }

            val consumer = GlobalScope.launch {
                while (true){
                    val element = channel1.receive()
                    println("消费者小费了:$element")
                }
            }

            producer.join()
            consumer.join()
        }

    }
}