aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/common/test/flow/internal/FlowScopeTest.kt
blob: e866647930869a9783ec94b17cc198c576a1d279 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * 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<CancellationException> {
            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<CancellationException> {
            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<CancellationException> {
            flowScope {
                flowScope {
                    launch {
                       throw CancellationException("")
                    }
                }
            }
        }
    }
}