flushWithin

suspend fun flushWithin(timeout: Duration, drain: suspend () -> FlushOutcome): FlushOutcome

Run drain under timeout, with the two things every flush implementation has to get right.

  • The timeout becomes FlushOutcome.TimedOut rather than a cancellation. A bare withTimeout would throw TimeoutCancellationException at the caller without cancelling it, and a caller's guard would then rethrow that and cancel itself in silence (the obligation on Seam.close).

  • Anything drain throws becomes FlushOutcome.Failed, and only the caller's own cancellation propagates. A cancellation minted by a callee is a failure like any other.

kotlin.time.Duration.INFINITE runs drain with no timer at all, rather than a timer for the end of time: under runTest that timer fires as soon as the test goes idle, which would turn an unbounded wait on a real socket into an instant FlushOutcome.TimedOut. A timeout of zero or less reports FlushOutcome.TimedOut without running drain.

Public because it is the shape a third-party fabric needs too: SeamStateGate stayed internal and four fabrics outside its module then hand-rolled it, three of them wrongly (#1803).

Samples

flushWithin(timeout) {
        queueDrained.await()
        FlushOutcome.Drained
    }