TestContext

cats.effect.kernel.testkit.TestContext
See theTestContext companion object
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)

Attributes

Companion:
object
Source:
TestContext.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Concise view

Value members

Concrete methods

Attributes

Source:
TestContext.scala

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.

Attributes

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

Attributes

Source:
TestContext.scala

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

Attributes

Source:
TestContext.scala

Attributes

Source:
TestContext.scala

Attributes

Source:
TestContext.scala
def schedule(delay: FiniteDuration, runnable: Runnable): () => Unit

Attributes

Source:
TestContext.scala

Attributes

Source:
TestContext.scala

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.

Attributes

Source:
TestContext.scala
def tick(): Unit

Attributes

Source:
TestContext.scala
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".

Attributes

Source:
TestContext.scala

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.

Attributes

Returns:

true if a task was available in the internal queue, and was executed, or false otherwise

Source:
TestContext.scala

Deprecated and Inherited methods

Attributes

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