Patrick Ramsey
09/02/2021, 6:40 AMsuspendCancellableCoroutine {}
and withTimeout {}
, and I’m running that test using a TestCoroutineDispatcher. In particular, I’m testing that the timeout fires if cont.resume()
is never called within suspendCancellableCoroutine {}
.
My test runs the code in question inside a <test coroutine dispatcher>.runBlockingTest {} block.
What I’m finding is that sometimes, the test passes as expected. But other times (it really feels nondeterministic and timing-related --- it changes depending on which tests are called before it, and whether or not it’s called in a debugger), instead of hitting the timeout, the internal call to dispatcher.advanceUntilIdle()
leaves the test dispatcher idle but the coroutine still running, resulting in an IllegalStateException.while(!queue.isEmpty) {
in advanceUntilIdle(), queue.isEmpty tends to return true. If instead I set the same breakpoint, then step forward a little bit into my method before continuing to the breakpoint in TestCoroutineDispatcher, queue.isEmpty then returns false, and the remaining event in the queue is the timeout.