1 /* 2 * Copyright 2016-2023 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 org.junit.Test 7 import kotlin.test.* 8 9 class StandardBuildersDebugTest : DebugTestBase() { 10 11 @Test <lambda>null12 fun testBuildersAreMissingFromDumpByDefault() = runTest { 13 val (b1, b2) = createBuilders() 14 15 val coroutines = DebugProbes.dumpCoroutinesInfo() 16 assertEquals(1, coroutines.size) 17 assertTrue { b1.hasNext() && b2.hasNext() } // Don't let GC collect our coroutines until the test is complete 18 } 19 20 @Test <lambda>null21 fun testBuildersCanBeEnabled() = runTest { 22 try { 23 DebugProbes.ignoreCoroutinesWithEmptyContext = false 24 val (b1, b2) = createBuilders() 25 val coroutines = DebugProbes.dumpCoroutinesInfo() 26 assertEquals(3, coroutines.size) 27 assertTrue { b1.hasNext() && b2.hasNext() } // Don't let GC collect our coroutines until the test is complete 28 } finally { 29 DebugProbes.ignoreCoroutinesWithEmptyContext = true 30 } 31 } 32 createBuildersnull33 private fun createBuilders(): Pair<Iterator<Int>, Iterator<Int>> { 34 val fromSequence = sequence { 35 while (true) { 36 yield(1) 37 } 38 }.iterator() 39 40 val fromIterator = iterator { 41 while (true) { 42 yield(1) 43 } 44 } 45 // Start coroutines 46 fromIterator.hasNext() 47 fromSequence.hasNext() 48 return fromSequence to fromIterator 49 } 50 } 51