Skip to content

Commit ca8a3da

Browse files
authored
Fix flakiness in ChannelSelectStressTest. (#4481)
It's possible for another sender to close the channel between incrementing `sent` and suspending for `select`. If that happens, the `select` just throws `CancellationException`, cancelling the coroutine and causing an element to be missing at the end of the test.
1 parent 7a32e3e commit ca8a3da

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

kotlinx-coroutines-core/jvm/test/channels/ChannelSelectStressTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import kotlinx.coroutines.*
66
import kotlinx.coroutines.selects.*
77
import org.junit.After
88
import org.junit.Test
9+
import java.util.concurrent.CyclicBarrier
910
import java.util.concurrent.atomic.AtomicLongArray
1011
import kotlin.test.*
1112

@@ -17,6 +18,7 @@ class ChannelSelectStressTest : TestBase() {
1718
private val received = atomic(0)
1819
private val receivedArray = AtomicLongArray(elementsToSend / Long.SIZE_BITS)
1920
private val channel = Channel<Int>()
21+
private val senderFinished = CyclicBarrier(pairedCoroutines)
2022

2123
@After
2224
fun tearDown() {
@@ -51,6 +53,7 @@ class ChannelSelectStressTest : TestBase() {
5153
if (element >= elementsToSend) break
5254
select<Unit> { channel.onSend(element) {} }
5355
}
56+
senderFinished.await()
5457
channel.close(CancellationException())
5558
}
5659
}

0 commit comments

Comments
 (0)