-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add update, updateAndGet, and getAndUpdate extension functions to Mut…
…ableStateFlow (#2729) * Add update, updateAndGet, and getAndUpdate extension functions to MutableStateFlow (#2720). Fixes #2720 Co-authored-by: Louis Wasserman <lowasser@google.com>
- Loading branch information
Showing
4 changed files
with
106 additions
and
24 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
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
44 changes: 44 additions & 0 deletions
44
kotlinx-coroutines-core/jvm/test/flow/StateFlowUpdateStressTest.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,44 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow | ||
|
||
import kotlinx.coroutines.* | ||
import org.junit.* | ||
import kotlin.test.* | ||
import kotlin.test.Test | ||
|
||
class StateFlowUpdateStressTest : TestBase() { | ||
private val iterations = 1_000_000 * stressTestMultiplier | ||
|
||
@get:Rule | ||
public val executor = ExecutorRule(2) | ||
|
||
@Test | ||
fun testUpdate() = doTest { update { it + 1 } } | ||
|
||
@Test | ||
fun testUpdateAndGet() = doTest { updateAndGet { it + 1 } } | ||
|
||
@Test | ||
fun testGetAndUpdate() = doTest { getAndUpdate { it + 1 } } | ||
|
||
private fun doTest(increment: MutableStateFlow<Int>.() -> Unit) = runTest { | ||
val flow = MutableStateFlow(0) | ||
val j1 = launch(Dispatchers.Default) { | ||
repeat(iterations / 2) { | ||
flow.increment() | ||
} | ||
} | ||
|
||
val j2 = launch(Dispatchers.Default) { | ||
repeat(iterations / 2) { | ||
flow.increment() | ||
} | ||
} | ||
|
||
joinAll(j1, j2) | ||
assertEquals(iterations, flow.value) | ||
} | ||
} |