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