-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathToStringTest.kt
139 lines (121 loc) · 5.59 KB
/
ToStringTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package kotlinx.coroutines.debug
import kotlinx.coroutines.testing.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import org.junit.*
import org.junit.Test
import java.io.*
import kotlin.coroutines.*
import kotlin.test.*
class ToStringTest : DebugTestBase() {
private suspend fun CoroutineScope.launchNestedScopes(): Job {
return launch {
expect(1)
coroutineScope {
expect(2)
launchDelayed()
supervisorScope {
expect(3)
launchDelayed()
}
}
}
}
private fun CoroutineScope.launchDelayed(): Job {
return launch {
delay(Long.MAX_VALUE)
}
}
@Test
fun testPrintHierarchyWithScopes() = runBlocking {
val tab = '\t'
val expectedString = """
"coroutine":StandaloneCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchNestedScopes$2$1.invokeSuspend(ToStringTest.kt)
$tab"coroutine":StandaloneCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchDelayed$1.invokeSuspend(ToStringTest.kt)
$tab"coroutine":StandaloneCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchDelayed$1.invokeSuspend(ToStringTest.kt)
""".trimIndent()
val job = launchNestedScopes()
try {
repeat(5) { yield() }
val expected = expectedString.trimStackTrace().trimPackage()
expect(4)
assertEquals(expected, DebugProbes.jobToString(job).trimEnd().trimStackTrace().trimPackage())
assertEquals(expected, DebugProbes.scopeToString(CoroutineScope(job)).trimEnd().trimStackTrace().trimPackage())
} finally {
finish(5)
job.cancelAndJoin()
}
}
@Test
fun testCompletingHierarchy() = runBlocking {
val tab = '\t'
val expectedString = """
"coroutine#2":StandaloneCoroutine{Completing}
$tab"foo#3":DeferredCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchHierarchy${'$'}1${'$'}1.invokeSuspend(ToStringTest.kt:30)
$tab"coroutine#4":ActorCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchHierarchy${'$'}1${'$'}2${'$'}1.invokeSuspend(ToStringTest.kt:40)
$tab$tab"coroutine#5":StandaloneCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchHierarchy${'$'}1${'$'}2${'$'}job$1.invokeSuspend(ToStringTest.kt:37)
""".trimIndent()
checkHierarchy(isCompleting = true, expectedString = expectedString)
}
@Test
fun testActiveHierarchy() = runBlocking {
val tab = '\t'
val expectedString = """
"coroutine#2":StandaloneCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchHierarchy${'$'}1.invokeSuspend(ToStringTest.kt:94)
$tab"foo#3":DeferredCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchHierarchy${'$'}1${'$'}1.invokeSuspend(ToStringTest.kt:30)
$tab"coroutine#4":ActorCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchHierarchy${'$'}1${'$'}2${'$'}1.invokeSuspend(ToStringTest.kt:40)
$tab$tab"coroutine#5":StandaloneCoroutine{Active}, continuation is SUSPENDED at line ToStringTest${'$'}launchHierarchy${'$'}1${'$'}2${'$'}job$1.invokeSuspend(ToStringTest.kt:37)
""".trimIndent()
checkHierarchy(isCompleting = false, expectedString = expectedString)
}
private suspend fun CoroutineScope.checkHierarchy(isCompleting: Boolean, expectedString: String) {
val root = launchHierarchy(isCompleting)
repeat(4) { yield() }
val expected = expectedString.trimStackTrace().trimPackage()
expect(6)
assertEquals(expected, DebugProbes.jobToString(root).trimEnd().trimStackTrace().trimPackage())
assertEquals(expected, DebugProbes.scopeToString(CoroutineScope(root)).trimEnd().trimStackTrace().trimPackage())
assertEquals(expected, printToString { DebugProbes.printScope(CoroutineScope(root), it) }.trimEnd().trimStackTrace().trimPackage())
assertEquals(expected, printToString { DebugProbes.printJob(root, it) }.trimEnd().trimStackTrace().trimPackage())
root.cancelAndJoin()
finish(7)
}
private fun CoroutineScope.launchHierarchy(isCompleting: Boolean): Job {
return launch {
expect(1)
async(CoroutineName("foo")) {
expect(2)
delay(Long.MAX_VALUE)
}
actor<Int> {
expect(3)
val job = launch {
expect(4)
delay(Long.MAX_VALUE)
}
withContext(wrapperDispatcher(coroutineContext)) {
expect(5)
job.join()
}
}
if (!isCompleting) {
delay(Long.MAX_VALUE)
}
}
}
private fun wrapperDispatcher(context: CoroutineContext): CoroutineContext {
val dispatcher = context[ContinuationInterceptor] as CoroutineDispatcher
return object : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
dispatcher.dispatch(context, block)
}
}
}
private inline fun printToString(block: (PrintStream) -> Unit): String {
val baos = ByteArrayOutputStream()
val ps = PrintStream(baos)
block(ps)
ps.close()
return baos.toString()
}
}