public class TestCoroutineContext
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
BuildersKt.launch or BuildersKt.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 BuildersKt.runBlocking.
This dispatcher's virtual time will be automatically advanced based 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 https://github.com/Kotlin/kotlinx.coroutines/issues/541.
| Constructor and Description |
|---|
TestCoroutineContext(java.lang.String name)
This CoroutineContext dispatcher can be used to simulate virtual time to speed up
code, especially tests, that deal with delays and timeouts in Coroutines.
|
TestCoroutineContext()
This CoroutineContext dispatcher can be used to simulate virtual time to speed up
code, especially tests, that deal with delays and timeouts in Coroutines.
|
| Modifier and Type | Method and Description |
|---|---|
long |
advanceTimeBy(long delayTime,
java.util.concurrent.TimeUnit unit)
Moves the CoroutineContext's virtual clock forward by a specified amount of time.
|
void |
advanceTimeTo(long targetTime,
java.util.concurrent.TimeUnit unit)
Moves the CoroutineContext's clock-time to a particular moment in time.
|
void |
assertAllUnhandledExceptions(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.lang.Throwable,java.lang.Boolean> predicate)
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.
|
void |
assertAnyUnhandledException(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.lang.Throwable,java.lang.Boolean> predicate)
This method does nothing if one or more unhandled exceptions satisfy the given predicate.
Otherwise it throws an AssertionError with the given message.
|
void |
assertExceptions(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.util.List<? extends java.lang.Throwable>,java.lang.Boolean> predicate)
This method does nothing if the list of unhandled exceptions satisfy the given predicate.
Otherwise it throws an AssertionError with the given message.
|
void |
assertUnhandledException(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.lang.Throwable,java.lang.Boolean> predicate)
This method does nothing if there is one unhandled exception that satisfies the given predicate.
Otherwise it throws an AssertionError with the given message.
|
void |
cancelAllActions()
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.
|
<R> R |
fold(R initial,
kotlin.jvm.functions.Function2<? super R,? super error.NonExistentClass,? extends R> operation) |
<E> E |
get(NonExistentClass key) |
java.util.List<java.lang.Throwable> |
getExceptions()
|
NonExistentClass |
minusKey(NonExistentClass key) |
long |
now(java.util.concurrent.TimeUnit unit)
Returns the current virtual clock-time as it is known to this CoroutineContext.
|
java.lang.String |
toString() |
void |
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.
|
public TestCoroutineContext(java.lang.String name)
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
BuildersKt.launch or BuildersKt.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 BuildersKt.runBlocking.
This dispatcher's virtual time will be automatically advanced based 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 https://github.com/Kotlin/kotlinx.coroutines/issues/541.
name - A user-readable name for debugging purposes.name - A user-readable name for debugging purposes.BuildersKt.launch,
BuildersKt.async,
BuildersKt.runBlocking,
https://github.com/Kotlin/kotlinx.coroutines/issues/541public TestCoroutineContext()
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
BuildersKt.launch or BuildersKt.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 BuildersKt.runBlocking.
This dispatcher's virtual time will be automatically advanced based 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 https://github.com/Kotlin/kotlinx.coroutines/issues/541.
public java.util.List<java.lang.Throwable> getExceptions()
Exceptions that were caught during a BuildersKt.launch or a BuildersKt.async + Deferred.await.
BuildersKt.launch,
BuildersKt.async,
Deferred.awaitpublic <R> R fold(R initial,
kotlin.jvm.functions.Function2<? super R,? super error.NonExistentClass,? extends R> operation)
public <E> E get(NonExistentClass key)
public NonExistentClass minusKey(NonExistentClass key)
public long now(java.util.concurrent.TimeUnit unit)
Returns the current virtual clock-time as it is known to this CoroutineContext.
unit - The TimeUnit in which the clock-time must be returned.public long advanceTimeBy(long delayTime,
java.util.concurrent.TimeUnit unit)
Moves the CoroutineContext's virtual clock forward by a specified amount of time.
The returned delay-time can be larger than the specified delay-time if the code under test contains blocking Coroutines.
delayTime - The amount of time to move the CoroutineContext's clock forward.unit - The TimeUnit in which delayTime and the return value is expressed.public void advanceTimeTo(long targetTime,
java.util.concurrent.TimeUnit unit)
Moves the CoroutineContext's clock-time to a particular moment in time.
targetTime - The point in time to which to move the CoroutineContext's clock.unit - The TimeUnit in which targetTime is expressed.public void 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.
public void cancelAllActions()
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.
public void assertUnhandledException(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.lang.Throwable,java.lang.Boolean> predicate)
This method does nothing if there is one unhandled exception that satisfies the given predicate. Otherwise it throws an AssertionError with the given message.
(this method will clear the list of unhandled exceptions)
message - Message of the AssertionError. Defaults to an empty String.predicate - The predicate that must be satisfied.public void assertAllUnhandledExceptions(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.lang.Throwable,java.lang.Boolean> predicate)
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.
(this method will clear the list of unhandled exceptions)
message - Message of the AssertionError. Defaults to an empty String.predicate - The predicate that must be satisfied.public void assertAnyUnhandledException(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.lang.Throwable,java.lang.Boolean> predicate)
This method does nothing if one or more unhandled exceptions satisfy the given predicate. Otherwise it throws an AssertionError with the given message.
(this method will clear the list of unhandled exceptions)
message - Message of the AssertionError. Defaults to an empty String.predicate - The predicate that must be satisfied.public void assertExceptions(java.lang.String message,
kotlin.jvm.functions.Function1<? super java.util.List<? extends java.lang.Throwable>,java.lang.Boolean> predicate)
This method does nothing if the list of unhandled exceptions satisfy the given predicate. Otherwise it throws an AssertionError with the given message.
(this method will clear the list of unhandled exceptions)
message - Message of the AssertionError. Defaults to an empty String.predicate - The predicate that must be satisfied.public java.lang.String toString()