CoroutinesTimeout

class CoroutinesTimeout(testTimeoutMs: Long, cancelOnTimeout: Boolean, enableCoroutineCreationStackTraces: Boolean) : TestRule

Coroutines timeout rule for JUnit4 that is applied to all methods in the class. This rule is very similar to Timeout rule: it runs tests in a separate thread, fails tests after the given timeout and interrupts test thread.

Additionally, this rule installs DebugProbes and dumps all coroutines at the moment of the timeout. It may cancel coroutines on timeout if cancelOnTimeout set to true. enableCoroutineCreationStackTraces controls the corresponding DebugProbes.enableCreationStackTraces property and can be optionally disabled to speed-up tests if creation stack traces are not needed.

Example of usage:

class HangingTest {
@get:Rule
val timeout = CoroutinesTimeout.seconds(5)

@Test
fun testThatHangs() = runBlocking {
...
delay(Long.MAX_VALUE) // somewhere deep in the stack
...
}
}

Constructors

CoroutinesTimeout
Link copied to clipboard
fun CoroutinesTimeout(testTimeoutMs: Long, cancelOnTimeout: Boolean = false)
CoroutinesTimeout
Link copied to clipboard
fun CoroutinesTimeout(testTimeoutMs: Long, cancelOnTimeout: Boolean = false, enableCoroutineCreationStackTraces: Boolean = true)

Types

Companion
Link copied to clipboard
object Companion