Skip to content

UnconfinedTestDispatcher documentation is unclear about delay and virtual time #4592

@GabrielBrasileiro

Description

@GabrielBrasileiro

Context:

The current documentation is unclear about how delay behaves when using UnconfinedTestDispatcher.

The phrase “it skips delays” can reasonably be interpreted as delayed coroutines resuming immediately when delay() is called inside the coroutine block.

The actual execution semantics are not clearly described, and this part of the documentation could be improved.

Page:

https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-unconfined-test-dispatcher.html

Section:

This dispatcher is similar to Dispatchers.Unconfined: the tasks that it executes are not confined to any particular thread and form an event loop; it's different in that it skips delays, as all TestDispatchers do.

Test:

@Test
fun unconfinedTestDispatcherDelay() = runTest {
    val dispatcher = UnconfinedTestDispatcher(testScheduler)

    var resumed = false

    launch(dispatcher) {
        delay(100)
        resumed = true
    }

    // At this point, resumed is false
    assertFalse(resumed)

    testScheduler.advanceUntilIdle()

    assertTrue(resumed)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsKDoc and API reference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions