CoroutinesTimeout

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

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 enabled if the creation stack traces are necessary.

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

Link copied to clipboard
constructor(testTimeoutMs: Long, cancelOnTimeout: Boolean = false)
constructor(testTimeoutMs: Long, cancelOnTimeout: Boolean = false, enableCoroutineCreationStackTraces: Boolean = false)

Types

Link copied to clipboard
object Companion