Skip to content
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

Fix flakey test (issue 45) #67

Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import org.junit.Test
import kotlin.coroutines.CoroutineContext
import kotlin.test.assertFailsWith
import kotlin.time.ExperimentalTime
import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout

@ExperimentalCoroutinesApi
@ExperimentalTime
Expand Down Expand Up @@ -102,31 +108,33 @@ class MoleculeTest {
override val key get() = CoroutineExceptionHandler
}

@Test fun errorDelayed() {
val dispatcher = TestCoroutineDispatcher()
@Test fun errorDelayed() = runBlocking {
val clock = BroadcastFrameClock()
val exceptionHandler = RecordingExceptionHandler()
val scope = CoroutineScope(dispatcher + clock + exceptionHandler)
var value: Int? = null
val values = Channel<Int>(UNLIMITED)

// Use a custom subtype to prevent coroutines from breaking referential equality.
val runtimeException = object : RuntimeException() {}
var count by mutableStateOf(0)
scope.launchMolecule(emitter = { value = it }) {
if (count == 1) {
throw runtimeException
}
count
val exception = async {
runCatching {
withContext(clock) {
launchMolecule(emitter = values::trySend) {
if (count == 1) {
throw runtimeException
}
count
}
}
}.exceptionOrNull()
}

assertEquals(0, value)
assertEquals(0, withTimeout(1000) { values.receive() })

count++
Snapshot.sendApplyNotifications() // Ensure external state mutation is observed.
clock.sendFrame(0)
assertSame(runtimeException, exceptionHandler.exceptions.single())

scope.cancel()
assertSame(runtimeException, withTimeout(1000) { exception.await() })
}

@Test fun errorInEffect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ import androidx.compose.runtime.snapshots.Snapshot
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.job
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -89,7 +93,12 @@ fun <T> CoroutineScope.launchMolecule(
body: @Composable () -> T,
) {
val recomposer = Recomposer(coroutineContext)
val composition = Composition(UnitApplier, recomposer)
Composition(UnitApplier, recomposer).apply {
setContent {
emitter(body())
}
}

launch(start = UNDISPATCHED) {
recomposer.runRecomposeAndApplyChanges()
}
Expand All @@ -107,10 +116,6 @@ fun <T> CoroutineScope.launchMolecule(
coroutineContext.job.invokeOnCompletion {
snapshotHandle.dispose()
}

composition.setContent {
emitter(body())
}
}

private object UnitApplier : AbstractApplier<Unit>(Unit) {
Expand Down