-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathReusableCancellableContinuationTest.kt
195 lines (179 loc) · 5.91 KB
/
ReusableCancellableContinuationTest.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.selects.*
import org.junit.Test
import kotlin.coroutines.*
import kotlin.test.*
class ReusableCancellableContinuationTest : TestBase() {
@Test
fun testReusable() = runTest {
testContinuationsCount(10, 1, ::suspendAtomicCancellableCoroutineReusable)
}
@Test
fun testRegular() = runTest {
testContinuationsCount(10, 10, ::suspendAtomicCancellableCoroutine)
}
private suspend inline fun CoroutineScope.testContinuationsCount(
iterations: Int,
expectedInstances: Int,
suspender: suspend ((CancellableContinuation<Unit>) -> Unit) -> Unit
) {
val result = mutableSetOf<CancellableContinuation<*>>()
val job = coroutineContext[Job]!!
val channel = Channel<Continuation<Unit>>(1)
launch {
channel.consumeEach {
val f = FieldWalker.walk(job)
result.addAll(f.filterIsInstance<CancellableContinuation<*>>())
it.resumeWith(Result.success(Unit))
}
}
repeat(iterations) {
suspender {
assertTrue(channel.offer(it))
}
}
channel.close()
assertEquals(expectedInstances, result.size - 1)
}
@Test
fun testCancelledOnClaimedCancel() = runTest {
expect(1)
try {
suspendAtomicCancellableCoroutineReusable<Unit> {
it.cancel()
}
expectUnreached()
} catch (e: CancellationException) {
finish(2)
}
}
@Test
fun testNotCancelledOnClaimedResume() = runTest({ it is CancellationException }) {
expect(1)
// Bind child at first
var continuation: Continuation<*>? = null
suspendAtomicCancellableCoroutineReusable<Unit> {
expect(2)
continuation = it
launch { // Attach to the parent, avoid fast path
expect(3)
it.resume(Unit)
}
}
expect(4)
ensureActive()
// Verify child was bound
FieldWalker.assertReachableCount(1, coroutineContext[Job]) { it === continuation }
suspendAtomicCancellableCoroutineReusable<Unit> {
expect(5)
coroutineContext[Job]!!.cancel()
it.resume(Unit)
}
assertFalse(isActive)
finish(6)
}
@Test
fun testResumeReusablePreservesReference() = runTest {
expect(1)
var cont: Continuation<Unit>? = null
launch {
cont!!.resumeWith(Result.success(Unit))
}
suspendAtomicCancellableCoroutineReusable<Unit> {
cont = it
}
ensureActive()
assertTrue { FieldWalker.walk(coroutineContext[Job]).contains(cont!!) }
finish(2)
}
@Test
fun testResumeRegularDoesntPreservesReference() = runTest {
expect(1)
var cont: Continuation<Unit>? = null
launch { // Attach to the parent, avoid fast path
cont!!.resumeWith(Result.success(Unit))
}
suspendAtomicCancellableCoroutine<Unit> {
cont = it
}
ensureActive()
FieldWalker.assertReachableCount(0, coroutineContext[Job]) { it === cont }
finish(2)
}
@Test
fun testDetachedOnCancel() = runTest {
expect(1)
var cont: Continuation<*>? = null
try {
suspendAtomicCancellableCoroutineReusable<Unit> {
cont = it
it.cancel()
}
expectUnreached()
} catch (e: CancellationException) {
FieldWalker.assertReachableCount(0, coroutineContext[Job]) { it === cont }
finish(2)
}
}
@Test
fun testPropagatedCancel() = runTest({it is CancellationException}) {
val currentJob = coroutineContext[Job]!!
expect(1)
// Bind child at first
suspendAtomicCancellableCoroutineReusable<Unit> {
expect(2)
// Attach to the parent, avoid fast path
launch {
expect(3)
it.resume(Unit)
}
}
expect(4)
ensureActive()
// Verify child was bound
FieldWalker.assertReachableCount(1, currentJob) { it is CancellableContinuation<*> }
currentJob.cancel()
assertFalse(isActive)
// Child detached
FieldWalker.assertReachableCount(0, currentJob) { it is CancellableContinuation<*> }
suspendAtomicCancellableCoroutineReusable<Unit> { it.resume(Unit) }
suspendAtomicCancellableCoroutineReusable<Unit> { it.resume(Unit) }
FieldWalker.assertReachableCount(0, currentJob) { it is CancellableContinuation<*> }
try {
suspendAtomicCancellableCoroutineReusable<Unit> {}
} catch (e: CancellationException) {
FieldWalker.assertReachableCount(0, currentJob) { it is CancellableContinuation<*> }
finish(5)
}
}
@Test
fun testChannelMemoryLeak() = runTest {
val iterations = 100
val channel = Channel<Unit>()
launch {
repeat(iterations) {
select {
channel.onSend(Unit) {}
}
}
}
val receiver = launch {
repeat(iterations) {
channel.receive()
}
expect(2)
val job = coroutineContext[Job]!!
// 1 for reusable CC, another one for outer joiner
FieldWalker.assertReachableCount(2, job) { it is CancellableContinuation<*> }
}
expect(1)
receiver.join()
// Reference should be claimed at this point
FieldWalker.assertReachableCount(0, receiver) { it is CancellableContinuation<*> }
finish(3)
}
}