-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathConsumeAsFlowLeakTest.kt
48 lines (39 loc) · 1.23 KB
/
ConsumeAsFlowLeakTest.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
/*
* Copyright 2016-2019 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 kotlinx.coroutines.channels.*
import org.junit.Test
import kotlin.test.*
class ConsumeAsFlowLeakTest : TestBase() {
private data class Box(val i: Int)
// In companion to avoid references through runTest
companion object {
private val first = Box(4)
private val second = Box(5)
}
// @Test //ignored until KT-33986
fun testReferenceIsNotRetained() = testReferenceNotRetained(true)
@Test
fun testReferenceIsNotRetainedNoSuspension() = testReferenceNotRetained(false)
private fun testReferenceNotRetained(shouldSuspendOnSend: Boolean) = runTest {
val channel = BroadcastChannel<Box>(1)
val job = launch {
expect(2)
channel.asFlow().collect {
expect(it.i)
}
}
expect(1)
yield()
expect(3)
channel.send(first)
if (shouldSuspendOnSend) yield()
channel.send(second)
yield()
FieldWalker.assertReachableCount(0, channel) { it === second }
finish(6)
job.cancelAndJoin()
}
}