Skip to content

Fix a flaky check and a TSan violation in StateFlowStressTest. #4483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions kotlinx-coroutines-core/jvm/test/flow/StateFlowStressTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package kotlinx.coroutines.flow

import kotlinx.coroutines.testing.*
import java.util.concurrent.atomic.AtomicLongArray
import kotlin.random.*
import kotlinx.coroutines.*
import kotlinx.coroutines.testing.*
import org.junit.*
import kotlin.random.*

class StateFlowStressTest : TestBase() {
private val nSeconds = 3 * stressTestMultiplier
Expand All @@ -17,7 +18,7 @@ class StateFlowStressTest : TestBase() {

fun stress(nEmitters: Int, nCollectors: Int) = runTest {
pool = newFixedThreadPoolContext(nEmitters + nCollectors, "StateFlowStressTest")
val collected = Array(nCollectors) { LongArray(nEmitters) }
val collected = Array(nCollectors) { AtomicLongArray(nEmitters) }
val collectors = launch {
repeat(nCollectors) { collector ->
launch(pool) {
Expand All @@ -37,38 +38,39 @@ class StateFlowStressTest : TestBase() {
}
c[emitter] = current

}.take(batchSize).map { 1 }.sum()
}.take(batchSize).count()
} while (cnt == batchSize)
}
}
}
val emitted = LongArray(nEmitters)
val emitted = AtomicLongArray(nEmitters)
val emitters = launch {
repeat(nEmitters) { emitter ->
launch(pool) {
var current = 1L
while (true) {
state.value = current * nEmitters + emitter
emitted[emitter] = current
current++
if (current % 1000 == 0L) yield() // make it cancellable
state.value = emitted.incrementAndGet(emitter) * nEmitters + emitter
if (emitted[emitter] % 1000 == 0L) yield() // make it cancellable
}
}
}
}
for (second in 1..nSeconds) {
delay(1000)
val cs = collected.map { it.sum() }
println("$second: emitted=${emitted.sum()}, collected=${cs.minOrNull()}..${cs.maxOrNull()}")
println("$second: emitted=${emitted.sum()}, collected=${cs.min()}..${cs.max()}")
}
emitters.cancelAndJoin()
collectors.cancelAndJoin()
// make sure nothing hanged up
require(collected.all { c ->
c.withIndex().all { (emitter, current) -> current > emitted[emitter] / 2 }
})
for (i in 0..<nCollectors) {
check((0..<nEmitters).any { j -> collected[i][j] > emitted[j] * 0.9 }) {
"collector #$i failed to collect any of the most recently emitted values"
}
}
}

private fun AtomicLongArray.sum() = (0..<length()).sumOf(::get)

@Test
fun testSingleEmitterAndCollector() = stress(1, 1)

Expand Down