# Hardening Coroutine Tests - Kevin Schildhorn

DevFeed: [Hardening Coroutine Tests - Kevin Schildhorn](<https://devfeed.tech/articles/hardening-coroutine-tests-kevin-schildhorn-98452.md>)

Original publisher: [Read original article](<https://touchlab.co/flake-free-flow-tests-pt1-coroutines>)

Published: 2026-10-23T18:00:00Z

Content type: article

Language: en

Sources: [Touchlab](<https://devfeed.tech/sources/touchlab-enterprise-mobile-innovation-development.md>)

Topics: [coroutine-testing](<https://devfeed.tech/topics/coroutine-testing.md>), [coroutines-flow](<https://devfeed.tech/topics/coroutines-flow.md>), [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [scheduling](<https://devfeed.tech/topics/scheduling.md>)

Tags: [background-work](<https://devfeed.tech/tags/background-work.md>), [best-practices](<https://devfeed.tech/tags/best-practices.md>), [cache](<https://devfeed.tech/tags/cache.md>), [channel](<https://devfeed.tech/tags/channel.md>), [ci](<https://devfeed.tech/tags/ci.md>), [cleanup](<https://devfeed.tech/tags/cleanup.md>), [clock](<https://devfeed.tech/tags/clock.md>), [clocks](<https://devfeed.tech/tags/clocks.md>), [collector](<https://devfeed.tech/tags/collector.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutine-testing](<https://devfeed.tech/tags/coroutine-testing.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [coroutinescope](<https://devfeed.tech/tags/coroutinescope.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [delay](<https://devfeed.tech/tags/delay.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [dispatcher](<https://devfeed.tech/tags/dispatcher.md>), [emissions](<https://devfeed.tech/tags/emissions.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [scheduler](<https://devfeed.tech/tags/scheduler.md>), [tests](<https://devfeed.tech/tags/tests.md>), [turbine](<https://devfeed.tech/tags/turbine.md>)

## AI overview

Use StandardTestDispatcher with the runTest testScheduler, and inject dispatchers, scopes, and clocks so test work uses controlled virtual time. Drive scheduled work with runCurrent, advanceTimeBy, or advanceUntilIdle; inject TimeSource when code reads elapsed time or timestamps. Use backgroundScope for endless observers and settle it with runCurrent or advanceTimeBy; use a cancellable SupervisorJob scope for finite fire-and-forget work. Avoid parallel tests that set global Dispatchers.Main, and reset it after use. For necessary real-thread tests, keep most work virtual, synchronize on signals such as join or awaitItem rather than sleeps, use generous timeouts, cancel created scopes, and assert final values or invariant patterns instead of exact emission sequences. The supplied article excerpt reports substantial ordering variation and stale results in its repeated experiments, and warns that detached failures or shared state can cause false passes and cross-test flakiness.

## Source excerpt

Why coroutine tests flake, and the dispatcher and scheduler habits that stop it.