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

concurrentTest.flow.FlowCancellationTest.kt Maven / Gradle / Ivy

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

package kotlinx.coroutines.flow

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlin.test.*

class FlowCancellationTest : TestBase() {

    @Test
    fun testEmitIsCooperative() = runMtTest {
        val latch = Channel(1)
        val job = flow {
            expect(1)
            latch.send(Unit)
            while (true) {
                emit(42)
            }
        }.launchIn(this + Dispatchers.Default)

        latch.receive()
        expect(2)
        job.cancelAndJoin()
        finish(3)
    }

    @Test
    fun testIsActiveOnCurrentContext() = runMtTest {
        val latch = Channel(1)
        val job = flow {
            expect(1)
            latch.send(Unit)
            while (currentCoroutineContext().isActive) {
                // Do nothing
            }
        }.launchIn(this + Dispatchers.Default)

        latch.receive()
        expect(2)
        job.cancelAndJoin()
        finish(3)
    }

    @Test
    fun testFlowWithEmptyContext() = runMtTest {
        expect(1)
        withEmptyContext {
            val flow = flow {
                expect(2)
                emit("OK")
            }
            flow.collect {
                expect(3)
                assertEquals("OK", it)
            }
        }
        finish(4)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy