All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.channels.ChannelsTest.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

@file:Suppress("DEPRECATION")

package kotlinx.coroutines.channels

import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.math.*
import kotlin.test.*

class ChannelsTest: TestBase() {
    private val testList = listOf(1, 2, 3)

    @Test
    fun testIterableAsReceiveChannel() = runTest {
        assertEquals(testList, testList.asReceiveChannel().toList())
    }

    @Test
    fun testCloseWithMultipleWaiters() = runTest {
        val channel = Channel()
        launch {
            try {
                expect(2)
                channel.receive()
                expectUnreached()
            } catch (e: ClosedReceiveChannelException) {
                expect(5)
            }
        }

        launch {
            try {
                expect(3)
                channel.receive()
                expectUnreached()
            } catch (e: ClosedReceiveChannelException) {
                expect(6)
            }
        }

        expect(1)
        yield()
        expect(4)
        channel.close()
        yield()
        finish(7)
    }

    @Test
    fun testEmptyList() = runTest {
        assertTrue(emptyList().asReceiveChannel().toList().isEmpty())
    }

    @Test
    fun testToList() = runTest {
        assertEquals(testList, testList.asReceiveChannel().toList())

    }

    private fun  Iterable.asReceiveChannel(context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel =
        GlobalScope.produce(context) {
            for (element in this@asReceiveChannel)
                send(element)
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy