xref: /aosp_15_r20/external/kotlinx.coroutines/kotlinx-coroutines-debug/test/ScopedBuildersTest.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)
1 package kotlinx.coroutines.debug
2 
3 import kotlinx.coroutines.testing.*
4 import kotlinx.coroutines.*
5 import org.junit.*
6 import kotlin.coroutines.*
7 
8 class ScopedBuildersTest : DebugTestBase() {
9 
10     @Test
<lambda>null11     fun testNestedScopes() = runBlocking {
12         val job = launch { doInScope() }
13         yield()
14         yield()
15         verifyDump(
16             "Coroutine \"coroutine#1\":BlockingCoroutine{Active}@16612a51, state: RUNNING",
17 
18             "Coroutine \"coroutine#2\":StandaloneCoroutine{Active}@6b53e23f, state: SUSPENDED\n" +
19                 "\tat kotlinx.coroutines.debug.ScopedBuildersTest\$doWithContext\$2.invokeSuspend(ScopedBuildersTest.kt:49)\n" +
20                 "\tat kotlinx.coroutines.debug.ScopedBuildersTest.doWithContext(ScopedBuildersTest.kt:47)\n" +
21                 "\tat kotlinx.coroutines.debug.ScopedBuildersTest\$doInScope\$2.invokeSuspend(ScopedBuildersTest.kt:41)\n" +
22                 "\tat kotlinx.coroutines.debug.ScopedBuildersTest\$testNestedScopes\$1\$job\$1.invokeSuspend(ScopedBuildersTest.kt:30)"
23         )
24         job.cancelAndJoin()
25         finish(4)
26     }
27 
<lambda>null28     private suspend fun doInScope() = coroutineScope {
29         expect(1)
30         doWithContext()
31         expectUnreached()
32     }
33 
doWithContextnull34     private suspend fun doWithContext() {
35         expect(2)
36         withContext(wrapperDispatcher(coroutineContext)) {
37             expect(3)
38             delay(Long.MAX_VALUE)
39         }
40         expectUnreached()
41     }
42 }
43