### Testing a Single Flow with Extension Function Source: https://github.com/cashapp/turbine/blob/trunk/README.md Explains how to use the `test` extension function on a `Flow` to automatically create and manage a `Turbine` for testing. It simplifies the setup and teardown process. ```kotlin someFlow.test { // Validation code here! } ``` -------------------------------- ### Await Item and Complete Flow with Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Tests an asynchronous flow by awaiting a single item and then its completion. This is a basic example of how Turbine's 'pull' mechanism works to control asynchronous operations. ```kotlin channelFlow { withContext(IO) { Thread.sleep(100) send("item") } }.test { assertEquals("item", awaitItem()) awaitComplete() } ``` -------------------------------- ### Emit Shared Flow Value After Test Starts Source: https://github.com/cashapp/turbine/blob/trunk/README.md Shows the correct way to handle shared flows with Turbine by ensuring the test starts collecting before the emission occurs, preventing dropped values. ```kotlin val mutableSharedFlow = MutableSharedFlow(replay = 0) mutableSharedFlow.test { mutableSharedFlow.emit(1) assertEquals(awaitItem(), 1) } ``` -------------------------------- ### Handle Shared Flow Emission Before Collection Source: https://github.com/cashapp/turbine/blob/trunk/README.md Illustrates a common issue with shared flows where emitting before collecting can lead to dropped values. This example shows the problem and the resulting error. ```kotlin val mutableSharedFlow = MutableSharedFlow(replay = 0) mutableSharedFlow.emit(1) mutableSharedFlow.test { assertEquals(awaitItem(), 1) } ``` -------------------------------- ### Basic Flow Testing with Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Demonstrates the core usage of Turbine for testing a simple Flow. It shows how to await items and the completion signal from a Flow. ```kotlin flowOf("one", "two").test { assertEquals("one", awaitItem()) assertEquals("two", awaitItem()) awaitComplete() } ``` -------------------------------- ### Testing Multiple Flows Concurrently Source: https://github.com/cashapp/turbine/blob/trunk/README.md Demonstrates how to test multiple Flows simultaneously using `testIn` within a `turbineScope`. It shows how to manage individual `Turbine` instances for each Flow. ```kotlin runTest { turbineScope { val turbine1 = flowOf(1).testIn(backgroundScope) val turbine2 = flowOf(2).testIn(backgroundScope) assertEquals(1, turbine1.awaitItem()) assertEquals(2, turbine2.awaitItem()) turbine1.awaitComplete() turbine2.awaitComplete() } } ``` -------------------------------- ### Using Development Version of Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Shows how to configure your Gradle build to use snapshot versions of the Turbine library from the Central Portal Snapshots repository. This is useful for testing unreleased features. ```kotlin repositories { maven { url = uri("https://central.sonatype.com/repository/maven-snapshots/") } } dependencies { testImplementation("app.cash.turbine:turbine:1.3.0-SNAPSHOT") } ``` -------------------------------- ### Tag Release - Git Source: https://github.com/cashapp/turbine/blob/trunk/RELEASING.md Creates an annotated Git tag for a specific release version. Annotated tags are recommended for releases as they contain metadata like the tagger name, email, and date. ```shell git tag -am "Version X.Y.Z" X.Y.Z ``` -------------------------------- ### Use Standalone Turbine for Communication Source: https://github.com/cashapp/turbine/blob/trunk/README.md Illustrates creating standalone Turbine instances to communicate with test code outside of flows, useful for fakes and presenters. ```kotlin class FakeNavigator : Navigator { val goTos = Turbine() override fun goTo(screen: Screen) { goTos.add(screen) } } ``` ```kotlin runTest { val navigator = FakeNavigator() val events: Flow = MutableSharedFlow(extraBufferCapacity = 50) val models: Flow = makePresenter(navigator).present(events) models.test { assertEquals(UiModel(title = "Hi there"), awaitItem()) events.emit(UiEvent.Close) assertEquals(Screens.Back, navigator.goTos.awaitItem()) } } ``` -------------------------------- ### Await Multiple Items from Flow with Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Tests an asynchronous flow by awaiting multiple items sequentially. This demonstrates how Turbine can be used to verify the order and content of emitted items over time. ```kotlin channelFlow { withContext(IO) { repeat(10) { Thread.sleep(200) send("item $it") } } }.test { assertEquals("item 0", awaitItem()) assertEquals("item 1", awaitItem()) assertEquals("item 2", awaitItem()) } ``` -------------------------------- ### Awaiting Flow Completion with Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Demonstrates the use of `awaitComplete()` to suspend until the Turbine signals that the associated Flow has completed successfully without any errors. ```kotlin turbine.awaitComplete() ``` -------------------------------- ### Name Turbine Instances for Error Reporting Source: https://github.com/cashapp/turbine/blob/trunk/README.md Demonstrates naming Turbine instances to provide more context in error messages. This helps in debugging when multiple flows are being tested concurrently. ```kotlin runTest { turbineScope { val turbine1 = flowOf(1).testIn(backgroundScope, name = "turbine 1") val turbine2 = flowOf(2).testIn(backgroundScope, name = "turbine 2") turbine1.awaitComplete() turbine2.awaitComplete() } } ``` -------------------------------- ### Awaiting a Single Item from a Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Illustrates how to use the `awaitItem()` function to suspend execution until an item is emitted by the Turbine. This is a fundamental operation for verifying Flow emissions. ```kotlin assertEquals("one", turbine.awaitItem()) ``` -------------------------------- ### Standalone Turbine Compat APIs (Non-Suspending) Source: https://github.com/cashapp/turbine/blob/trunk/README.md Provides non-suspending compat APIs (takeItem, etc.) for Turbine, mimicking a simple queue, suitable for mixed coroutine and non-coroutine codebases. These will throw if used in a suspending context on JVM. ```kotlin val navigator = FakeNavigator() val events: PublishRelay = PublishRelay.create() val models: Observable = makePresenter(navigator).present(events) val testObserver = models.test() testObserver.assertValue(UiModel(title = "Hi there")) events.accept(UiEvent.Close) assertEquals(Screens.Back, navigator.goTos.takeItem()) ``` -------------------------------- ### Push to Remote - Git Source: https://github.com/cashapp/turbine/blob/trunk/RELEASING.md Pushes local commits and tags to the remote repository. This action often triggers automated workflows like CI/CD pipelines for building and deploying releases. ```shell git push && git push --tags ``` -------------------------------- ### Adding Turbine Dependency to Gradle Source: https://github.com/cashapp/turbine/blob/trunk/README.md Provides the necessary Gradle configuration to include the Turbine testing library in your project's dependencies. This is essential for using Turbine's features in your tests. ```kotlin repositories { mavenCentral() } dependencies { testImplementation("app.cash.turbine:turbine:1.2.1") } ``` -------------------------------- ### Awaiting a Flow Error with Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Shows how to use `awaitError()` to suspend and retrieve a `Throwable` when the associated Flow completes with an error. This is crucial for testing error handling. ```kotlin assertEquals("broken!", turbine.awaitError().message) ``` -------------------------------- ### Handle Flow Termination Events Source: https://github.com/cashapp/turbine/blob/trunk/README.md Demonstrates how to consume flow termination events like exceptions. Exceptions are exposed as Turbine error events that must be explicitly handled. ```kotlin flow { throw RuntimeException("broken!") }.test { assertEquals("broken!", awaitError().message) } ``` ```kotlin flow { throw RuntimeException("broken!") }.test {} ``` -------------------------------- ### Commit Changes - Git Source: https://github.com/cashapp/turbine/blob/trunk/RELEASING.md Commits all staged changes with a specific message. This is typically used to prepare for a release or a new development cycle. ```shell git commit -am "Prepare version X.Y.Z" ``` -------------------------------- ### Override Turbine Timeout for Standalone Instances Source: https://github.com/cashapp/turbine/blob/trunk/README.md Shows how to set a custom timeout for standalone Turbine instances created with `testIn` or `Turbine()`. ```kotlin val standalone = Turbine(timeout = 10.milliseconds) val flow = flowOf("one").testIn( scope = backgroundScope, timeout = 10.milliseconds, ) ``` -------------------------------- ### Set Block-Level Timeout with withTurbineTimeout Source: https://github.com/cashapp/turbine/blob/trunk/README.md Illustrates how to apply a specific timeout to a block of Turbine operations using the `withTurbineTimeout` function. ```kotlin withTurbineTimeout(10.milliseconds) { ... } ``` -------------------------------- ### Cancel Flow and Await Items with Turbine Source: https://github.com/cashapp/turbine/blob/trunk/README.md Tests an asynchronous flow, cancels it after a delay, and then awaits specific items. This showcases Turbine's ability to manage flow cancellation and verify behavior up to the point of cancellation. ```kotlin channelFlow { withContext(IO) { repeat(10) { Thread.sleep(200) send("item $it") } } }.test { Thread.sleep(700) cancel() assertEquals("item 0", awaitItem()) assertEquals("item 1", awaitItem()) assertEquals("item 2", awaitItem()) } ``` -------------------------------- ### Override Turbine Timeout for Specific Tests Source: https://github.com/cashapp/turbine/blob/trunk/README.md Demonstrates how to set a custom timeout for a flow test using the 'timeout' parameter in the test function. ```kotlin flowOf("one", "two").test(timeout = 10.milliseconds) { ... } ``` -------------------------------- ### Consume All Events in Flow Test Source: https://github.com/cashapp/turbine/blob/trunk/README.md Ensures all events from a flow are consumed within a test block. Failure to do so results in an AssertionError indicating unconsumed events. ```kotlin flowOf("one", "two").test { assertEquals("one", awaitItem()) } ``` ```kotlin runTest { turbineScope { val turbine = flowOf("one", "two").testIn(backgroundScope) turbine.assertEquals("one", awaitItem()) } } ``` ```kotlin flowOf("one", "two").test { assertEquals("one", awaitItem()) cancelAndIgnoreRemainingEvents() } ``` ```kotlin flowOf("one", "two", "three") .map { delay(100) it } .test { delay(250) assertEquals("two", expectMostRecentItem()) cancelAndIgnoreRemainingEvents() } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.