Turbine
Turbine is a small testing library for kotlinx.coroutines Flow
.
flowOf("one", "two").test {
assertEquals("one", expectItem())
assertEquals("two", expectItem())
expectComplete()
}
A turbine is a rotary mechanical device that extracts energy from a fluid flow and converts it into useful work.
Download
repositories {
mavenCentral()
}
dependencies {
testImplementation 'app.cash.turbine:turbine:0.5.1'
}
Snapshots of the development version are available in Sonatype's snapshots repository.
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
testImplementation 'app.cash.turbine:turbine:0.6.0-SNAPSHOT'
}
Usage
The entrypoint for the library is the test
extension for Flow<T>
which accepts a validation block. Like collect
, test
is a suspending function that will not return until the flow is complete or canceled.
someFlow.test {
// Validation code here!
}
Consuming Events
Inside the test
block you must consume all received events from the flow. Failing to consume all events will fail your test.
flowOf("one", "two").test {
assertEquals("one", expectItem())
}
Exception in thread "main" AssertionError:
Unconsumed events found:
- Item(two)
- Complete
As the exception indicates, consuming the "two"
item is not enough. The complete event must also be consumed.
flowOf("one", "two").test {
assertEquals("one", expectItem())
assertEquals("two", expectItem())
expectComplete()
}
Received events can be explicitly ignored, however.
flowOf("one", "two").test {
assertEquals("one", expectItem())
cancelAndIgnoreRemainingEvents()
}
Consuming Errors
Unlike collect
, a flow which causes an exception will still be exposed as an event that you must consume.
flow { throw RuntimeException("broken!") }.test {
assertEquals("broken!", expectError().message)
}
Failure to consume an error will result in the same unconsumed event exception as above, but with the exception added as the cause so that the full stacktrace is available.
flow { throw RuntimeException("broken!") }.test { }
java.lang.AssertionError: Unconsumed events found:
- Error(RuntimeException)
at app.cash.turbine.ChannelBasedFlowTurbine.ensureAllEventsConsumed(FlowTurbine.kt:240)
... 53 more
Caused by: java.lang.RuntimeException: broken!
at example.MainKt$main$1.invokeSuspend(Main.kt:7)
... 32 more
Asynchronous Flows
Calls to expectItem()
, expectComplete()
, and expectError()
are suspending and will wait for events from asynchronous flows.
channelFlow {
withContext(IO) {
Thread.sleep(100)
send("item")
}
}.test {
assertEquals("item", expectItem())
expectComplete()
}
By default, when one of the "expect" methods suspends waiting for an event it will timeout after one second.
channelFlow {
withContext(IO) {
Thread.sleep(2_000)
send("item")
}
}.test {
assertEquals("item", expectItem())
expectComplete()
}
Exception in thread "main" TimeoutCancellationException: Timed out waiting for 1000 ms
A longer timeout can be specified as an argument to test
.
channelFlow {
withContext(IO) {
Thread.sleep(2_000)
send("item")
}
}.test(timeout = 3.seconds) {
assertEquals("item", expectItem())
expectComplete()
}
Asynchronous flows can be canceled at any time and will not require consuming a complete or error event.
channelFlow {
withContext(IO) {
repeat(10) {
Thread.sleep(200)
send("item $it")
}
}
}.test {
assertEquals("item 0", expectItem())
assertEquals("item 1", expectItem())
assertEquals("item 2", expectItem())
cancel()
}
Hot Flows
Emissions to hot flows that don't have active consumers are dropped. It's important to call test
(and therefore have an active collector) on a flow before emissions to a flow are made. For example:
val mutableStateFlow = MutableStateFlow<Int>(replay = 0)
mutableStateFlow.emit(1)
mutableStateFlow.test {
assertEquals(expectItem(), 1)
cancelAndConsumeRemainingEvents()
}
will fail with a timeout exception.
kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 1000 ms
(Coroutine boundary)
at app.cash.turbine.ChannelBasedFlowTurbine$expectEvent$2.invokeSuspend(FlowTurbine.kt:238)
at app.cash.turbine.ChannelBasedFlowTurbine$withTimeout$2.invokeSuspend(FlowTurbine.kt:206)
at app.cash.turbine.ChannelBasedFlowTurbine.expectItem(FlowTurbine.kt:243)
Proper usage of Turbine with hot flows looks like the following.
val mutableStateFlow = MutableStateFlow<Int>(replay = 0)
mutableStateFlow.test {
mutableStateFlow.emit(1)
assertEquals(expectItem(), 1)
cancelAndConsumeRemainingEvents()
}
The hot flow types Kotlin currently provide are:
MutableStateFlow
StateFlow
MutableSharedFlow
SharedFlow
- Channels converted to flow with
Channel.consumeAsFlow
Experimental API Usage
Turbine uses Kotlin experimental APIs:
Duration
is used to declare the event timeout.
Since the library targets test code, the impact and risk of any breaking changes to these APIs are minimal and would likely only require a version bump.
Instead of sprinkling the experimental annotations or @OptIn
all over your tests, opt-in at the compiler level.
Groovy DSL
compileTestKotlin {
kotlinOptions {
freeCompilerArgs += [
'-Xopt-in=kotlin.time.ExperimentalTime',
]
}
}
Kotlin DSL
tasks.compileTestKotlin {
kotlinOptions {
freeCompilerArgs += listOf(
"-Xopt-in=kotlin.time.ExperimentalTime",
)
}
}
For multiplatform projects:
Groovy DSL
kotlin {
sourceSets.matching { it.name.endsWith("Test") }.all {
it.languageSettings {
useExperimentalAnnotation('kotlin.time.ExperimentalTime')
}
}
}
Kotlin DSL
kotlin.sourceSets.matching {
it.name.endsWith("Test")
}.configureEach {
languageSettings.useExperimentalAnnotation("kotlin.time.ExperimentalTime")
}
License
Copyright 2018 Square, Inc.
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.