1 package kotlinx.coroutines.flow.internal
2 
3 import kotlinx.coroutines.testing.*
4 import kotlinx.coroutines.*
5 import kotlin.test.*
6 
7 class FlowScopeTest : TestBase() {
8 
9     @Test
testCancellationnull10     fun testCancellation() = runTest {
11         assertFailsWith<CancellationException> {
12             flowScope {
13                 expect(1)
14                 val child = launch {
15                     expect(3)
16                     hang { expect(5) }
17                 }
18                 expect(2)
19                 yield()
20                 expect(4)
21                 child.cancel()
22             }
23         }
24         finish(6)
25     }
26 
27     @Test
<lambda>null28     fun testCancellationWithChildCancelled() = runTest {
29         flowScope {
30             expect(1)
31             val child = launch {
32                 expect(3)
33                 hang { expect(5) }
34             }
35             expect(2)
36             yield()
37             expect(4)
38             child.cancel(ChildCancelledException())
39         }
40         finish(6)
41     }
42 
43     @Test
<lambda>null44     fun testCancellationWithSuspensionPoint() = runTest {
45         assertFailsWith<CancellationException> {
46             flowScope {
47                 expect(1)
48                 val child = launch {
49                     expect(3)
50                     hang { expect(6) }
51                 }
52                 expect(2)
53                 yield()
54                 expect(4)
55                 child.cancel()
56                 hang { expect(5) }
57             }
58         }
59         finish(7)
60     }
61 
62     @Test
<lambda>null63     fun testNestedScopes() = runTest {
64         assertFailsWith<CancellationException> {
65             flowScope {
66                 flowScope {
67                     launch {
68                        throw CancellationException("")
69                     }
70                 }
71             }
72         }
73     }
74 }
75