Skip to content

Commit 007d8d7

Browse files
committed
Fixed completion sequence of ChannelLFStressTest
A channel must be "cancelled" to abort both working senders & receivers. Fixes #1507
1 parent a2587d2 commit 007d8d7

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ class ChannelLFStressTest : TestBase() {
5050
}
5151

5252
private fun performLockFreedomTest() {
53-
env.onCompletion { channel.close() }
54-
repeat(2) { env.testThread { sender() } }
55-
repeat(2) { env.testThread { receiver() } }
53+
env.onCompletion {
54+
// We must cancel the channel to abort both senders & receivers
55+
channel.cancel(TestCompleted())
56+
}
57+
repeat(2) { env.testThread("sender-$it") { sender() } }
58+
repeat(2) { env.testThread("receiver-$it") { receiver() } }
5659
env.performTest(nSeconds) {
5760
println("Sent: $sendIndex, Received: $receiveCount, dups: $duplicateCount")
5861
}
@@ -70,7 +73,7 @@ class ChannelLFStressTest : TestBase() {
7073
val value = sendIndex.getAndIncrement()
7174
try {
7275
channel.send(value)
73-
} catch (e: ClosedSendChannelException) {
76+
} catch (e: TestCompleted) {
7477
check(env.isCompleted) // expected when test was completed
7578
markReceived(value) // fake received (actually failed to send)
7679
}
@@ -79,7 +82,7 @@ class ChannelLFStressTest : TestBase() {
7982
private suspend fun receiver() {
8083
val value = try {
8184
channel.receive()
82-
} catch (e: ClosedReceiveChannelException) {
85+
} catch (e: TestCompleted) {
8386
check(env.isCompleted) // expected when test was completed
8487
return
8588
}
@@ -107,4 +110,6 @@ class ChannelLFStressTest : TestBase() {
107110
val bits = receivedBits.get(index)
108111
return bits and mask != 0L
109112
}
113+
114+
private class TestCompleted : CancellationException()
110115
}

0 commit comments

Comments
 (0)