1 package kotlinx.coroutines.debug
2 
3 import kotlinx.coroutines.testing.*
4 import org.junit.Test
5 import kotlin.test.*
6 
7 class StandardBuildersDebugTest : DebugTestBase() {
8 
9     @Test
<lambda>null10     fun testBuildersAreMissingFromDumpByDefault() = runTest {
11         val (b1, b2) = createBuilders()
12 
13         val coroutines = DebugProbes.dumpCoroutinesInfo()
14         assertEquals(1, coroutines.size)
15         assertTrue { b1.hasNext() && b2.hasNext() } // Don't let GC collect our coroutines until the test is complete
16     }
17 
18     @Test
<lambda>null19     fun testBuildersCanBeEnabled() = runTest {
20         try {
21             DebugProbes.ignoreCoroutinesWithEmptyContext = false
22             val (b1, b2) = createBuilders()
23             val coroutines = DebugProbes.dumpCoroutinesInfo()
24             assertEquals(3, coroutines.size)
25             assertTrue { b1.hasNext() && b2.hasNext() } // Don't let GC collect our coroutines until the test is complete
26         } finally {
27             DebugProbes.ignoreCoroutinesWithEmptyContext = true
28         }
29     }
30 
createBuildersnull31     private fun createBuilders(): Pair<Iterator<Int>, Iterator<Int>> {
32         val fromSequence = sequence {
33             while (true) {
34                 yield(1)
35             }
36         }.iterator()
37 
38         val fromIterator = iterator {
39             while (true) {
40                 yield(1)
41             }
42         }
43         // Start coroutines
44         fromIterator.hasNext()
45         fromSequence.hasNext()
46         return fromSequence to fromIterator
47     }
48 }
49