Class/Object

cats.effect.laws.util

TestContext

Related Docs: object TestContext | package util

Permalink

final class TestContext extends ExecutionContext

A scala.concurrent.ExecutionContext implementation and a provider of cats.effect.Timer instances, that can simulate async boundaries and time passage, useful for testing purposes.

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)

Our TestContext can also simulate time passage, as we are able to builds a cats.effect.Timer instance for any data type that has a LiftIO instance:

val ctx = TestContext()

val timer: Timer[IO] = ctx.timer[IO]

We can now simulate actual time:

val io = timer.sleep(10.seconds) *> IO(1 + 1)
val f = io.unsafeToFuture()

// This invariant holds true, because our IO is async
assert(f.value == None)

// Not yet completed, because this does not simulate time passing:
ctx.tick()
assert(f.value == None)

// Simulating time passing:
ctx.tick(10.seconds)
assert(f.value == Some(Success(2))

Simulating time makes this pretty useful for testing race conditions:

val never = IO.async[Int](_ => {})
val timeoutError = new TimeoutException
val timeout = timer.sleep(10.seconds) *> IO.raiseError[Int](timeoutError)

val pair = (never, timeout).parMapN(_ + _)

// Not yet
ctx.tick()
assert(f.value == None)
// Not yet
ctx.tick(5.seconds)
assert(f.value == None)

// Good to go:
ctx.tick(5.seconds)
assert(f.value, Some(Failure(timeoutError)))
Self Type
TestContext
Source
TestContext.scala
Linear Supertypes
ExecutionContext, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TestContext
  2. ExecutionContext
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. def contextShift[F[_]](implicit F: Async[F]): ContextShift[F]

    Permalink

    Derives a cats.effect.ContextShift from this TestContext, for any data type that has a LiftIO and MonadError instance.

    Derives a cats.effect.ContextShift from this TestContext, for any data type that has a LiftIO and MonadError instance.

    Example:

    val ctx = TestContext()
    // Building a Timer[IO] from this:
    implicit val timer: Timer[IO] = ctx.timer[IO]
    
    // Can now simulate time
    val io = timer.sleep(10.seconds) *> IO(1 + 1)
    val f = io.unsafeToFuture()
    
    // This invariant holds true, because our IO is async
    assert(f.value == None)
    
    // Not yet completed, because this does not simulate time passing:
    ctx.tick()
    assert(f.value == None)
    
    // Simulating time passing:
    ctx.tick(10.seconds)
    assert(f.value == Some(Success(2))
  7. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  8. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  9. def execute(r: Runnable): Unit

    Permalink

    Inherited from ExecutionContext, schedules a runnable for execution.

    Inherited from ExecutionContext, schedules a runnable for execution.

    Definition Classes
    TestContext → ExecutionContext
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. def prepare(): ExecutionContext

    Permalink
    Definition Classes
    ExecutionContext
  18. def reportFailure(cause: Throwable): Unit

    Permalink

    Inherited from ExecutionContext, reports uncaught errors.

    Inherited from ExecutionContext, reports uncaught errors.

    Definition Classes
    TestContext → ExecutionContext
  19. def state: State

    Permalink

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

  20. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  21. def tick(time: FiniteDuration = Duration.Zero): Unit

    Permalink

    Triggers execution by going through the queue of scheduled tasks and executing them all, until no tasks remain in the queue to execute.

    Triggers execution by going through the queue of scheduled tasks and executing them all, until no tasks remain in the queue to execute.

    Order of execution isn't guaranteed, the queued Runnables are being shuffled in order to simulate the needed non-determinism that happens with multi-threading.

    implicit val ec = TestContext()
    
    val f = Future(1 + 1).flatMap(_ + 1)
    // Execution is momentarily suspended in TestContext
    assert(f.value == None)
    
    // Simulating async execution:
    ec.tick()
    assert(f.value, Some(Success(2)))

    The optional parameter can be used for simulating time, to be used in combination with cats.effect.Timer. See the timer method.

    Example:

    val ctx = TestContext()
    // Building a Timer[IO] from this:
    implicit val timer: Timer[IO] = ctx.timer[IO]
    
    // Can now simulate time
    val io = timer.sleep(10.seconds) *> IO(1 + 1)
    val f = io.unsafeToFuture()
    
    // This invariant holds true, because our IO is async
    assert(f.value == None)
    
    // Not yet completed, because this does not simulate time passing:
    ctx.tick()
    assert(f.value == None)
    
    // Simulating time passing:
    ctx.tick(10.seconds)
    assert(f.value == Some(Success(2))
    time

    is an optional parameter for simulating time passing;

  22. def tickOne(): Boolean

    Permalink

    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 non-determinism. 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

  23. def timer[F[_]](implicit F: LiftIO[F]): Timer[F]

    Permalink

    Derives a cats.effect.Timer from this TestContext, for any data type that has a LiftIO instance.

    Derives a cats.effect.Timer from this TestContext, for any data type that has a LiftIO instance.

    Example:

    val ctx = TestContext()
    // Building a Timer[IO] from this:
    implicit val timer: Timer[IO] = ctx.timer[IO]
    
    // Can now simulate time
    val io = timer.sleep(10.seconds) *> IO(1 + 1)
    val f = io.unsafeToFuture()
    
    // This invariant holds true, because our IO is async
    assert(f.value == None)
    
    // Not yet completed, because this does not simulate time passing:
    ctx.tick()
    assert(f.value == None)
    
    // Simulating time passing:
    ctx.tick(10.seconds)
    assert(f.value == Some(Success(2))
  24. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  25. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  26. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  27. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from ExecutionContext

Inherited from AnyRef

Inherited from Any

Ungrouped