How to step through suspend function calls when debugging Kotlin coroutines

This page shows some general techniques. In short, run with enabled assertions (-ea JVM flag). kotlinx-coroutines-debug module is specifically designed for what its name says. This is how I use it in unit tests; runBlocking { DebugProbes.install() val deferred = async { methodUnderTest() } delay(3000) DebugProbes.dumpCoroutines() println(“\nDumping only deferred”) DebugProbes.printJob(deferred) DebugProbes.uninstall() cleanup() } There’s a … Read more

MutableStateFlow is not emitting values after 1st emit kotlin coroutine

Pankaj’s answer is correct, StateFlow won’t emit the same value twice. As the documentation suggests: Values in state flow are conflated using Any.equals comparison in a similar way to distinctUntilChanged operator. It is used to conflate incoming updates to value in MutableStateFlow and to suppress emission of the values to collectors when new value is … Read more

Kotlin coroutine unit test fails with “Module with the Main dispatcher had failed to initialize”

UPDATED 2023 Add the following dependency on your gradle. Documentation here testImplementation “org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version” Add the following Custom TestRule as a utility for your tests // Reusable JUnit4 TestRule to override the Main dispatcher @OptIn(ExperimentalCoroutinesApi::class) class MainDispatcherRule( private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher() ) : TestWatcher() { override fun starting(description: Description) { Dispatchers.setMain(testDispatcher) } override fun … Read more

How to implement timer with Kotlin coroutines

Edit: note that the API suggested in the original answer is now marked @ObsoleteCoroutineApi: Ticker channels are not currently integrated with structured concurrency and their api will change in the future. You can now use the Flow API to create your own ticker flow: import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds import kotlinx.coroutines.* import kotlinx.coroutines.flow.* fun tickerFlow(period: Duration, … Read more

Unit testing a Kotlin coroutine with delay

If you don’t want any delay, why don’t you simply resume the continuation in the schedule call?: class TestUiContext : CoroutineDispatcher(), Delay { override fun scheduleResumeAfterDelay(time: Long, unit: TimeUnit, continuation: CancellableContinuation<Unit>) { continuation.resume(Unit) } override fun dispatch(context: CoroutineContext, block: Runnable) { //CommonPool.dispatch(context, block) // dispatch on CommonPool block.run() // dispatch on calling thread } } … Read more

How to use code that relies on ThreadLocal with Kotlin coroutines

Coroutine’s analog to ThreadLocal is CoroutineContext. To interoperate with ThreadLocal-using libraries you need to implement a custom ContinuationInterceptor that supports framework-specific thread-locals. Here is an example. Let us assume that we use some framework that relies on a specific ThreadLocal to store some application-specific data (MyData in this example): val myThreadLocal = ThreadLocal<MyData>() To use … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)