TestContext

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)
Companion:
object
Source:
TestContext.scala
trait ExecutionContext
class Object
trait Matchable
class Any

Value members

Concrete methods

def advance(time: FiniteDuration): Unit
def advanceAndTick(time: FiniteDuration): Unit
def derive(): ExecutionContext
def deriveBlocking(): ExecutionContext

Derives a new ExecutionContext which delegates to this, but wrapping all tasks in scala.concurrent.blocking.

Derives a new ExecutionContext which delegates to this, but wrapping all tasks in scala.concurrent.blocking.

Source:
TestContext.scala
def execute(runnable: Runnable): Unit
def nextInterval(): FiniteDuration

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())).

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())).

Source:
TestContext.scala
def now(): FiniteDuration
def reportFailure(cause: Throwable): Unit
def schedule(delay: FiniteDuration, runnable: Runnable): () => Unit
def seed: String

Returns the internal state of the TestContext, useful for testing that certain execution conditions have been met.

Returns the internal state of the TestContext, useful for testing that certain execution conditions have been met.

Source:
TestContext.scala
def tick(): Unit
def tickAll(): Unit

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.

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".

Source:
TestContext.scala
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, or false otherwise

Source:
TestContext.scala

Deprecated and Inherited methods

def prepare(): ExecutionContext
Deprecated
[Since version 2.12.0] preparation of ExecutionContexts will be removed
Inherited from:
ExecutionContext