Skip to content

Use test-specific unconfined when test scheduler is in use #151

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

Merged
merged 1 commit into from
Sep 15, 2022
Merged
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ kotlin {
commonMain {
dependencies {
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:${versions.coroutines}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.coroutines}"
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:${versions.coroutines}"
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/commonMain/kotlin/app/cash/turbine/flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineScheduler
import kotlinx.coroutines.test.UnconfinedTestDispatcher

/**
* Terminal flow operator that collects events from given flow and allows the [validate] lambda to
Expand Down Expand Up @@ -108,10 +111,16 @@ public fun <T> Flow<T>.testIn(scope: CoroutineScope): ReceiveTurbine<T> {
return turbine
}

@OptIn(ExperimentalCoroutinesApi::class) // New kotlinx.coroutines test APIs are not stable 😬
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to #132 😭

private fun <T> Flow<T>.collectTurbineIn(scope: CoroutineScope): Turbine<T> {
lateinit var channel: Channel<T>

val job = scope.launch(Unconfined, start = UNDISPATCHED) {
// Use test-specific unconfined if test scheduler is in use to inherit its virtual time.
val unconfined = scope.coroutineContext[TestCoroutineScheduler]
?.let(::UnconfinedTestDispatcher)
?: Unconfined

val job = scope.launch(unconfined, start = UNDISPATCHED) {
channel = collectIntoChannel(this)
}

Expand Down
16 changes: 16 additions & 0 deletions src/commonTest/kotlin/app/cash/turbine/FlowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.seconds
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.RENDEZVOUS
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.emitAll
Expand Down Expand Up @@ -520,4 +524,16 @@ class FlowTest {
assertSame(error, actual.cause)
}
}

@OptIn(ExperimentalTime::class)
@Test fun turbineSkipsDelaysInRunTest() = runTest {
val took = measureTime {
flow<Nothing> {
delay(5.seconds)
}.test {
awaitComplete()
}
}
assertTrue(took < 5.seconds, "$took > 5s")
}
}