waitUntil

suspend fun <T> waitUntil(timeout: Duration, pollInterval: Duration = 10.milliseconds, observe: suspend () -> T, matching: (T) -> Boolean): T(source)

Observes state until it matches matching, polling every pollInterval.

The deadline is measured with the monotonic clock in real time. Inside runTest, delay uses virtual time, so call this from withContext(Dispatchers.Default) when the observed work runs on another thread; prefer advanceUntilIdle() for work on the test scheduler. The observe block must return promptly and cooperate with cancellation: a deadline cannot interrupt arbitrary code. One observation is made even for a zero timeout, and errors from observe propagate unchanged.

val count = waitUntil(2.seconds, observe = { model.count }) { it >= 3 }

Return

The first matching observation.

Parameters

timeout

The total observation budget, at least zero.

pollInterval

The positive delay between observations.

observe

Reads the state without modifying it.

matching

Returns whether an observation satisfies the condition.

Throws

with the last observation when the deadline passes.