-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changing a MutableState inside a coroutine seemingly leads to skipped…
… emissions Repro of issue submitted at: cashapp/molecule#249
- Loading branch information
Marco Romano
committed
Jun 14, 2023
1 parent
b698aa2
commit 1ee8913
Showing
2 changed files
with
84 additions
and
0 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
83 changes: 83 additions & 0 deletions
83
...eroom/impl/src/test/kotlin/io/element/android/features/leaveroom/impl/MoleculeTestCase.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,83 @@ | ||
/* | ||
* Copyright (c) 2023 New Vector Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package example.test | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import app.cash.molecule.RecompositionClock | ||
import app.cash.molecule.moleculeFlow | ||
import app.cash.turbine.test | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
data class MyState( | ||
val anInt: Int, | ||
val eventSink: (MyEvent) -> Unit, | ||
) | ||
|
||
sealed interface MyEvent { | ||
object Increment : MyEvent | ||
object IncrementSuspending : MyEvent | ||
} | ||
|
||
@Composable | ||
fun myPresenter(): MyState { | ||
val scope = rememberCoroutineScope() | ||
val anInt: MutableState<Int> = remember { mutableStateOf(0) } | ||
return MyState(anInt.value) { | ||
when (it) { | ||
MyEvent.Increment -> { | ||
anInt.value++ | ||
anInt.value++ | ||
} | ||
MyEvent.IncrementSuspending -> scope.launch { | ||
anInt.value++ | ||
anInt.value++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
class MoleculeTestCase { | ||
@Test | ||
fun `process Increment event`() = runTest { | ||
moleculeFlow(RecompositionClock.Immediate) { myPresenter() }.test { | ||
awaitItem().apply { | ||
assertEquals(0, anInt) | ||
eventSink(MyEvent.Increment) | ||
} | ||
assertEquals(1, awaitItem().anInt) | ||
assertEquals(2, awaitItem().anInt) | ||
} | ||
} | ||
|
||
@Test | ||
fun `process IncrementSuspending event`() = runTest { | ||
moleculeFlow(RecompositionClock.Immediate) { myPresenter() }.test { | ||
awaitItem().apply { | ||
assertEquals(0, anInt) | ||
eventSink(MyEvent.IncrementSuspending) | ||
} | ||
assertEquals(2, awaitItem().anInt) | ||
} | ||
} | ||
} |