TestCoroutineContext

class TestCoroutineContext(name: String?) : CoroutineContext

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-blockinglaunch 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

TestCoroutineContext
Link copied to clipboard
fun TestCoroutineContext(name: String? = null)
A user-readable name for debugging purposes.

Functions

advanceTimeBy
Link copied to clipboard
fun advanceTimeBy(delayTime: Long, unit: TimeUnit = TimeUnit.MILLISECONDS): Long
Moves the CoroutineContext's virtual clock forward by a specified amount of time.
advanceTimeTo
Link copied to clipboard
fun advanceTimeTo(targetTime: Long, unit: TimeUnit = TimeUnit.MILLISECONDS)
Moves the CoroutineContext's clock-time to a particular moment in time.
assertAllUnhandledExceptions
Link copied to clipboard
fun assertAllUnhandledExceptions(message: String = "", predicate: (Throwable) -> Boolean)
This method does nothing if there are no unhandled exceptions or all of them satisfy the given predicate.
assertAnyUnhandledException
Link copied to clipboard
fun assertAnyUnhandledException(message: String = "", predicate: (Throwable) -> Boolean)
This method does nothing if one or more unhandled exceptions satisfy the given predicate.
assertExceptions
Link copied to clipboard
fun assertExceptions(message: String = "", predicate: (List<Throwable>) -> Boolean)
This method does nothing if the list of unhandled exceptions satisfy the given predicate.
assertUnhandledException
Link copied to clipboard
fun assertUnhandledException(message: String = "", predicate: (Throwable) -> Boolean)
This method does nothing if there is one unhandled exception that satisfies the given predicate.
cancelAllActions
Link copied to clipboard
fun cancelAllActions()
Cancels all not yet triggered actions.
fold
Link copied to clipboard
open override fun <R> fold(initial: R, operation: (R, CoroutineContext.Element) -> R): R
get
Link copied to clipboard
open operator override fun <E : CoroutineContext.Element> get(key: CoroutineContext.Key<E>): E?
minusKey
Link copied to clipboard
open override fun minusKey(key: CoroutineContext.Key<*>): CoroutineContext
now
Link copied to clipboard
fun now(unit: TimeUnit = TimeUnit.MILLISECONDS): Long
Returns the current virtual clock-time as it is known to this CoroutineContext.
toString
Link copied to clipboard
open override fun toString(): String
triggerActions
Link copied to clipboard
fun triggerActions()
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.

Properties

exceptions
Link copied to clipboard
val exceptions: List<Throwable>
Exceptions that were caught during a launch or a async + Deferred.await.

Sources

jvm source
Link copied to clipboard