forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly detect non-released reusable continuations in non-reusable o… (
Kotlin#2772) * Properly detect non-released reusable continuations in non-reusable ones and await for reusability to have a consistent state * Ensure that the caller to DispatchedContinuation.isReusable is reusable itself * Using the previous invariant, simplify DispatchedContinuation.isReusable to a single null-check * It also restores the invariant that `cc.isReusable() == cc.resumeMode.isReusableMode` Fixes Kotlin#2736 Fixes Kotlin#2768
1 parent
b6ff228
commit f661918
Showing
3 changed files
with
52 additions
and
22 deletions.
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
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
41 changes: 41 additions & 0 deletions
41
kotlinx-coroutines-core/jvm/test/ReusableContinuationStressTest.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,41 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import kotlinx.coroutines.flow.* | ||
import org.junit.* | ||
|
||
class ReusableContinuationStressTest : TestBase() { | ||
|
||
private val iterations = 1000 * stressTestMultiplierSqrt | ||
|
||
@Test // Originally reported by @denis-bezrukov in #2736 | ||
fun testDebounceWithStateFlow() = runBlocking<Unit> { | ||
withContext(Dispatchers.Default) { | ||
repeat(iterations) { | ||
launch { // <- load the dispatcher and OS scheduler | ||
runStressTestOnce(1, 1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun runStressTestOnce(delay: Int, debounce: Int) = coroutineScope { | ||
val stateFlow = MutableStateFlow(0) | ||
val emitter = launch { | ||
repeat(1000) { i -> | ||
stateFlow.emit(i) | ||
delay(delay.toLong()) | ||
} | ||
} | ||
var last = 0 | ||
stateFlow.debounce(debounce.toLong()).take(100).collect { i -> | ||
if (i - last > 100) { | ||
last = i | ||
} | ||
} | ||
emitter.cancel() | ||
} | ||
} |