final class TestContext extends ExecutionContext
A scala.concurrent.ExecutionContext implementation that can simulate async boundaries and
time passage, useful for law testing purposes. This is intended primarily for datatype
implementors. Most end-users will be better served by the cats.effect.testkit.TestControl
utility, rather than using TestContext
directly.
Usage for simulating an ExecutionContext
):
implicit val ec = TestContext() ec.execute(new Runnable { def run() = println("task1") }) ex.execute(new Runnable { def run() = { println("outer") ec.execute(new Runnable { def run() = println("inner") }) } }) // Nothing executes until `tick` gets called ec.tick() // Testing the resulting state assert(ec.state.tasks.isEmpty) assert(ec.state.lastReportedFailure == None)
- Self Type
- TestContext
- Source
- TestContext.scala
- Alphabetic
- By Inheritance
- TestContext
- ExecutionContext
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def advance(time: FiniteDuration): Unit
- def advanceAndTick(time: FiniteDuration): Unit
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def derive(): ExecutionContext
- def deriveBlocking(): ExecutionContext
Derives a new
ExecutionContext
which delegates tothis
, but wrapping all tasks in scala.concurrent.blocking. - final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def execute(runnable: Runnable): Unit
- Definition Classes
- TestContext → ExecutionContext
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def nextInterval(): FiniteDuration
Returns the current interval between "now" and the earliest scheduled task.
Returns the current interval between "now" and the earliest scheduled task. If there are tasks which will run immediately, this will return
Duration.Zero
. Passing this value to tick will guarantee minimum time-oriented progress on the task queue (e.g.tick(nextInterval())
). - final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def now(): FiniteDuration
- def reportFailure(cause: Throwable): Unit
- Definition Classes
- TestContext → ExecutionContext
- def schedule(delay: FiniteDuration, runnable: Runnable): () => Unit
- def seed: String
- def state: State
Returns the internal state of the
TestContext
, useful for testing that certain execution conditions have been met. - final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def tick(): Unit
- Annotations
- @tailrec()
- def tickAll(): Unit
Repeatedly runs
tick(nextInterval())
until all work has completed.Repeatedly runs
tick(nextInterval())
until all work has completed. This is useful for emulating the quantized passage of time. For any discrete tick, the scheduler will randomly pick from all eligible tasks until the only remaining work is delayed. At that point, the scheduler will then advance the minimum delay (to the next time interval) and the process repeats.This is intuitively equivalent to "running to completion".
- Annotations
- @tailrec()
- def tickOne(): Boolean
Executes just one tick, one task, from the internal queue, useful for testing that a some runnable will definitely be executed next.
Executes just one tick, one task, from the internal queue, useful for testing that a some runnable will definitely be executed next.
Returns a boolean indicating that tasks were available and that the head of the queue has been executed, so normally you have this equivalence:
while (ec.tickOne()) {} // ... is equivalent with: ec.tick()
Note that ask extraction has a random factor, the behavior being like tick, in order to simulate nondeterminism. So you can't rely on some ordering of execution if multiple tasks are waiting execution.
- returns
true
if a task was available in the internal queue, and was executed, orfalse
otherwise
- Annotations
- @tailrec()
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
Deprecated Value Members
- def prepare(): ExecutionContext
- Definition Classes
- ExecutionContext
- Annotations
- @deprecated
- Deprecated
(Since version 2.12.0) preparation of ExecutionContexts will be removed