Skip to content

Commit 1ee8913

Browse files
author
Marco Romano
committed
Changing a MutableState inside a coroutine seemingly leads to skipped emissions
Repro of issue submitted at: cashapp/molecule#249
1 parent b698aa2 commit 1ee8913

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Diff for: features/leaveroom/impl/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ dependencies {
4141
testImplementation(libs.molecule.runtime)
4242
testImplementation(libs.test.truth)
4343
testImplementation(libs.test.turbine)
44+
testImplementation(kotlin("test"))
4445
testImplementation(projects.libraries.matrix.test)
4546
testImplementation(projects.tests.testutils)
4647
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.test
18+
19+
import androidx.compose.runtime.Composable
20+
import androidx.compose.runtime.MutableState
21+
import androidx.compose.runtime.mutableStateOf
22+
import androidx.compose.runtime.remember
23+
import androidx.compose.runtime.rememberCoroutineScope
24+
import app.cash.molecule.RecompositionClock
25+
import app.cash.molecule.moleculeFlow
26+
import app.cash.turbine.test
27+
import kotlinx.coroutines.launch
28+
import kotlinx.coroutines.test.runTest
29+
import org.junit.Test
30+
import kotlin.test.assertEquals
31+
32+
data class MyState(
33+
val anInt: Int,
34+
val eventSink: (MyEvent) -> Unit,
35+
)
36+
37+
sealed interface MyEvent {
38+
object Increment : MyEvent
39+
object IncrementSuspending : MyEvent
40+
}
41+
42+
@Composable
43+
fun myPresenter(): MyState {
44+
val scope = rememberCoroutineScope()
45+
val anInt: MutableState<Int> = remember { mutableStateOf(0) }
46+
return MyState(anInt.value) {
47+
when (it) {
48+
MyEvent.Increment -> {
49+
anInt.value++
50+
anInt.value++
51+
}
52+
MyEvent.IncrementSuspending -> scope.launch {
53+
anInt.value++
54+
anInt.value++
55+
}
56+
}
57+
}
58+
}
59+
60+
class MoleculeTestCase {
61+
@Test
62+
fun `process Increment event`() = runTest {
63+
moleculeFlow(RecompositionClock.Immediate) { myPresenter() }.test {
64+
awaitItem().apply {
65+
assertEquals(0, anInt)
66+
eventSink(MyEvent.Increment)
67+
}
68+
assertEquals(1, awaitItem().anInt)
69+
assertEquals(2, awaitItem().anInt)
70+
}
71+
}
72+
73+
@Test
74+
fun `process IncrementSuspending event`() = runTest {
75+
moleculeFlow(RecompositionClock.Immediate) { myPresenter() }.test {
76+
awaitItem().apply {
77+
assertEquals(0, anInt)
78+
eventSink(MyEvent.IncrementSuspending)
79+
}
80+
assertEquals(2, awaitItem().anInt)
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)