/
OnCompletionInterceptedReleaseTest.kt
45 lines (40 loc) · 1.42 KB
/
OnCompletionInterceptedReleaseTest.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
/*
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.flow
import kotlinx.coroutines.*
import org.junit.Test
import kotlin.coroutines.*
import kotlin.test.*
class OnCompletionInterceptedReleaseTest : TestBase() {
@Test
fun testLeak() = runTest {
expect(1)
var cont: Continuation<Unit>? = null
val interceptor = CountingInterceptor()
val job = launch(interceptor, start = CoroutineStart.UNDISPATCHED) {
emptyFlow<Int>()
.onCompletion { emit(1) }
.collect { value ->
expect(2)
assertEquals(1, value)
suspendCoroutine { cont = it }
}
}
cont!!.resume(Unit)
assertTrue(job.isCompleted)
assertEquals(interceptor.intercepted, interceptor.released)
finish(3)
}
class CountingInterceptor : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor {
var intercepted = 0
var released = 0
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> {
intercepted++
return Continuation(continuation.context) { continuation.resumeWith(it) }
}
override fun releaseInterceptedContinuation(continuation: Continuation<*>) {
released++
}
}
}