Hi guys, how can I add debug information to a time...
# coroutines
j
Hi guys, how can I add debug information to a timeout exception? I’m writing tests with a global timeout, and I would like to know where the execution got stuck when the timeout happens. I have a couple places in my testing utilities which are the usual bad boys, so I tried the following:
Copy code
try {
   doTheThingThatUsuallyGetsStuck()
} catch(e: TimeoutCancellationException) {
   fail("some debug information about why the test may have timed out")
}
The problem with this is that sometimes the code under test uses timeouts as well, so this catch block may actually fail the test even in some valid timeout situations. Is there a way for me to transparently add information about the timeout somehow? If I rethrow a
CancellationException
with a custom error message, then how can I catch it upstream and access the message?
t
If your intent is to set a global timeout for testing coroutine-based code, then you probably should have a look at
CoroutinesTimeout
from
kotlinx-coroutines-debug
: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-debug/kotlinx.coroutines.debug.junit4/-coroutines-timeout/index.html
j
This is exaclty the kind of facilities I’m looking for, but the library I’m testing is a multiplatform library, so I can’t use JVM-specific stuff (most of my tests are testing common code). I would love to be able to use kotlinx-coroutines-test/debug but I can’t 😞