<lambda>null1 package kotlinx.coroutines.debug
2 
3 import kotlinx.coroutines.testing.*
4 import kotlinx.coroutines.*
5 import kotlinx.coroutines.flow.*
6 import org.junit.*
7 
8 // Test four our internal optimization "withContextUndispatched"
9 class WithContextUndispatchedTest : DebugTestBase() {
10 
11     @Test
12     fun testZip() = runTest {
13         val f1 = flowOf("a")
14         val f2 = flow {
15             nestedEmit()
16             yield()
17         }
18         f1.zip(f2) { i, j -> i + j }.collect {
19             bar(false)
20         }
21     }
22 
23     private suspend fun FlowCollector<Int>.nestedEmit() {
24         emit(1)
25         emit(2)
26     }
27 
28     @Test
29     fun testUndispatchedFlowOn() = runTest {
30         val flow = flowOf(1, 2, 3).flowOn(CoroutineName("..."))
31         flow.collect {
32             bar(true)
33         }
34     }
35 
36     @Test
37     fun testUndispatchedFlowOnWithNestedCaller() = runTest {
38         val flow = flow {
39             nestedEmit()
40         }.flowOn(CoroutineName("..."))
41         flow.collect {
42             bar(true)
43         }
44     }
45 
46     private suspend fun bar(forFlowOn: Boolean) {
47         yield()
48         if (forFlowOn) {
49             verifyFlowOn()
50         } else {
51             verifyZip()
52         }
53         yield()
54     }
55 
56     private suspend fun verifyFlowOn() {
57         yield() // suspend
58         verifyPartialDump(1, "verifyFlowOn", "bar")
59     }
60 
61     private suspend fun verifyZip() {
62         yield() // suspend
63         verifyPartialDump(2, "verifyZip", "bar", "nestedEmit")
64     }
65 }
66