/
BroadcastChannelLeakTest.kt
34 lines (29 loc) · 1.1 KB
/
BroadcastChannelLeakTest.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
package kotlinx.coroutines.channels
import kotlinx.coroutines.*
import org.junit.Test
import kotlin.test.*
class BroadcastChannelLeakTest : TestBase() {
@Test
fun testArrayBroadcastChannelSubscriptionLeak() {
checkLeak { ArrayBroadcastChannel(1) }
}
@Test
fun testConflatedBroadcastChannelSubscriptionLeak() {
checkLeak { ConflatedBroadcastChannel() }
}
enum class TestKind { BROADCAST_CLOSE, SUB_CANCEL, BOTH }
private fun checkLeak(factory: () -> BroadcastChannel<String>) = runTest {
for (kind in TestKind.values()) {
val broadcast = factory()
val sub = broadcast.openSubscription()
broadcast.send("OK")
assertEquals("OK", sub.receive())
// now close broadcast
if (kind != TestKind.SUB_CANCEL) broadcast.close()
// and then cancel subscription
if (kind != TestKind.BROADCAST_CLOSE) sub.cancel()
// subscription should not be reachable from the channel anymore
FieldWalker.assertReachableCount(0, broadcast) { it === sub }
}
}
}