Skip to content

Commit e114e59

Browse files
committed
Add test that enforces #3330
Marked as "fixes 3330" to automatically close the issue yet it's the original PR that fixed it Fixes #3330
1 parent b6bfa4c commit e114e59

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

kotlinx-coroutines-core/common/test/channels/ChannelUndeliveredElementTest.kt

+20
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,24 @@ class ChannelUndeliveredElementTest : TestBase() {
138138
channel.send(Unit)
139139
finish(3)
140140
}
141+
142+
@Test
143+
fun testChannelBufferOverflow() = runTest {
144+
testBufferOverflowStrategy(3, BufferOverflow.DROP_LATEST)
145+
testBufferOverflowStrategy(1, BufferOverflow.DROP_OLDEST)
146+
}
147+
148+
private suspend fun testBufferOverflowStrategy(expectedDroppedElement: Int, strategy: BufferOverflow) {
149+
val list = ArrayList<Int>()
150+
val channel = Channel<Int>(
151+
capacity = 2,
152+
onBufferOverflow = strategy,
153+
onUndeliveredElement = { value -> list.add(value) }
154+
)
155+
156+
(1..3).forEach { value ->
157+
channel.send(value)
158+
}
159+
assertEquals(expectedDroppedElement, list.single())
160+
}
141161
}

0 commit comments

Comments
 (0)