-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix leak of subscription reference in closed ArrayBroadcastChannel (#…
…1885) When ArrayBroadcastChannel was closed it was still retaining a reference to its subscription (even if that subscription was cancelled) while, in fact, either closing a broadcast channel or cancelling subscription should remove the reference. This is no problem with ConflatedBroadcastChannel, but it is added to the test for completeness.
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
kotlinx-coroutines-core/jvm/test/channels/BroadcastChannelLeakTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 } | ||
} | ||
} | ||
} |