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