Skip to content

Commit

Permalink
More internal cleanups (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Apr 12, 2024
1 parent 0547b86 commit f3527db
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions src/commonMain/kotlin/app/cash/turbine/flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package app.cash.turbine

import app.cash.turbine.testIn as testInExtension
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.time.Duration
Expand Down Expand Up @@ -61,8 +62,7 @@ internal class TurbineContextImpl(
timeout: Duration?,
name: String?,
): ReceiveTurbine<R> =
testInInternal(
this@testIn,
testInExtension(
timeout = timeout,
name = name,
scope = scope + turbineElements,
Expand Down Expand Up @@ -138,16 +138,9 @@ public suspend fun <T> Flow<T>.test(
name: String? = null,
validate: suspend TurbineTestContext<T>.() -> Unit,
) {
turbineScope {
turbineScope(timeout) {
collectTurbineIn(this, null, name).apply {
val testContext = TurbineTestContextImpl(this@apply, currentCoroutineContext())
if (timeout != null) {
withTurbineTimeout(timeout) {
testContext.validate()
}
} else {
testContext.validate()
}
TurbineTestContextImpl(this, currentCoroutineContext()).validate()
cancel()
ensureAllEventsConsumed()
}
Expand Down Expand Up @@ -177,10 +170,6 @@ public fun <T> Flow<T>.testIn(
timeout: Duration? = null,
name: String? = null,
): ReceiveTurbine<T> {
return testInInternal(this, timeout, scope, name)
}

private fun <T> testInInternal(flow: Flow<T>, timeout: Duration?, scope: CoroutineScope, name: String?): ReceiveTurbine<T> {
if (timeout != null) {
// Eager check to throw early rather than in a subsequent 'await' call.
checkTimeout(timeout)
Expand All @@ -189,7 +178,7 @@ private fun <T> testInInternal(flow: Flow<T>, timeout: Duration?, scope: Corouti
throw AssertionError("Turbine can only collect flows within a TurbineContext. Wrap with turbineScope { .. }")
}

val turbine = flow.collectTurbineIn(scope, timeout, name)
val turbine = collectTurbineIn(scope, timeout, name)

scope.coroutineContext.job.invokeOnCompletion { exception ->
if (debug) println("Scope ending ${exception ?: ""}")
Expand Down

0 comments on commit f3527db

Please sign in to comment.