CoroutinesTimeout

class CoroutinesTimeout : 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.

Example of usage:

class HangingTest {

  @Rule
  @JvmField
  val timeout = CoroutinesTimeout.seconds(5)

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

Constructors

<init>

CoroutinesTimeout(
    testTimeoutMs: Long,
    cancelOnTimeout: Boolean = false)

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.

Companion Object Functions

seconds

fun seconds(
    seconds: Int,
    cancelOnTimeout: Boolean = false
): CoroutinesTimeout
fun seconds(
    seconds: Long,
    cancelOnTimeout: Boolean = false
): CoroutinesTimeout

Creates CoroutinesTimeout rule with the given timeout in seconds.