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

commonTest.flow.internal.FlowScopeTest.kt Maven / Gradle / Ivy

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

package kotlinx.coroutines.flow.internal

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

class FlowScopeTest : TestBase() {

    @Test
    fun testCancellation() = runTest {
        assertFailsWith {
            flowScope {
                expect(1)
                val child = launch {
                    expect(3)
                    hang { expect(5) }
                }
                expect(2)
                yield()
                expect(4)
                child.cancel()
            }
        }
        finish(6)
    }

    @Test
    fun testCancellationWithChildCancelled() = runTest {
        flowScope {
            expect(1)
            val child = launch {
                expect(3)
                hang { expect(5) }
            }
            expect(2)
            yield()
            expect(4)
            child.cancel(ChildCancelledException())
        }
        finish(6)
    }

    @Test
    fun testCancellationWithSuspensionPoint() = runTest {
        assertFailsWith {
            flowScope {
                expect(1)
                val child = launch {
                    expect(3)
                    hang { expect(6) }
                }
                expect(2)
                yield()
                expect(4)
                child.cancel()
                hang { expect(5) }
            }
        }
        finish(7)
    }

    @Test
    fun testNestedScopes() = runTest {
        assertFailsWith {
            flowScope {
                flowScope {
                    launch {
                       throw CancellationException("")
                    }
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy