TestCoroutineContext

class TestCoroutineContext : CoroutineContext (source)

Deprecated: This API has been deprecated to integrate with Structured Concurrency.

Platform and version requirements: JVM

This CoroutineContext dispatcher can be used to simulate virtual time to speed up code, especially tests, that deal with delays and timeouts in Coroutines.

Provide an instance of this TestCoroutineContext when calling the non-blocking launch or async and then advance time or trigger the actions to make the co-routines execute as soon as possible.

This works much like the TestScheduler in RxJava2, which allows to speed up tests that deal with non-blocking Rx chains that contain delays, timeouts, intervals and such.

This dispatcher can also handle blocking coroutines that are started by runBlocking. This dispatcher’s virtual time will be automatically advanced based on the delayed actions within the Coroutine(s).

Note: This API will become obsolete in future updates due to integration with structured concurrency. See issue #541.

Parameters

name - A user-readable name for debugging purposes.

Constructors

<init>

TestCoroutineContext(name: String? = null)

This CoroutineContext dispatcher can be used to simulate virtual time to speed up code, especially tests, that deal with delays and timeouts in Coroutines.

Properties

exceptions

val exceptions: List<Throwable>

Exceptions that were caught during a launch or a async + Deferred.await.

Functions

advanceTimeBy

fun advanceTimeBy(
    delayTime: Long,
    unit: TimeUnit = TimeUnit.MILLISECONDS
): Long

Moves the CoroutineContext’s virtual clock forward by a specified amount of time.

advanceTimeTo

fun advanceTimeTo(
    targetTime: Long,
    unit: TimeUnit = TimeUnit.MILLISECONDS
): Unit

Moves the CoroutineContext’s clock-time to a particular moment in time.

assertAllUnhandledExceptions

fun assertAllUnhandledExceptions(
    message: String = "",
    predicate: (Throwable) -> Boolean
): Unit

This method does nothing if there are no unhandled exceptions or all of them satisfy the given predicate. Otherwise it throws an AssertionError with the given message.

assertAnyUnhandledException

fun assertAnyUnhandledException(
    message: String = "",
    predicate: (Throwable) -> Boolean
): Unit

This method does nothing if one or more unhandled exceptions satisfy the given predicate. Otherwise it throws an AssertionError with the given message.

assertExceptions

fun assertExceptions(
    message: String = "",
    predicate: (List<Throwable>) -> Boolean
): Unit

This method does nothing if the list of unhandled exceptions satisfy the given predicate. Otherwise it throws an AssertionError with the given message.

assertUnhandledException

fun assertUnhandledException(
    message: String = "",
    predicate: (Throwable) -> Boolean
): Unit

This method does nothing if there is one unhandled exception that satisfies the given predicate. Otherwise it throws an AssertionError with the given message.

cancelAllActions

fun cancelAllActions(): Unit

Cancels all not yet triggered actions. Be careful calling this, since it can seriously mess with your coroutines work. This method should usually be called on tear-down of a unit test.

fold

fun <R> fold(initial: R, operation: (R, Element) -> R): R

get

fun <E : Element> get(key: Key<E>): E?

minusKey

fun minusKey(key: Key<*>): CoroutineContext

now

fun now(unit: TimeUnit = TimeUnit.MILLISECONDS): Long

Returns the current virtual clock-time as it is known to this CoroutineContext.

toString

fun toString(): String

triggerActions

fun triggerActions(): Unit

Triggers any actions that have not yet been triggered and that are scheduled to be triggered at or before this CoroutineContext’s present virtual clock-time.