The state of the TestClock.
Accesses a TestClock instance in the environment and increments the
wall clock time by the specified duration, running any actions scheduled
for on or before the new time.
Accesses a TestClock instance in the environment and returns the current
fiber time for this fiber.
Constructs a new Test object that implements the TestClock interface.
Constructs a new Test object that implements the TestClock interface.
This can be useful for mixing in with implementations of other interfaces.
Accesses a TestClock instance in the environment and runs all
scheduled effects.
Accesses a TestClock instance in the environment and runs all
scheduled effects. After this any scheduled effects will be run
immediately.
Accesses a TestClock instance in the environment and saves the clock
state in an effect which, when run, will restore the TestClock to the
saved state
Accesses a TestClock instance in the environment and sets the wall
clock time to the specified OffsetDateTime, running any actions
scheduled for on or before the new time.
Accesses a TestClock instance in the environment and sets the wall
clock time to the specified time in terms of duration since the epoch,
running any actions scheduled for on or before the new time.
Accesses a TestClock instance in the environment, setting the time zone
to the specified time zone.
Accesses a TestClock instance in the environment, setting the time zone
to the specified time zone. The wall clock time in terms of nanoseconds
since the epoch will not be altered and no scheduled actions will be run
as a result of this effect.
Accesses a TestClock instance in the environment and returns a list of
wall clock times that effects are scheduled to run.
Accesses a TestClock instance in the environment and returns the current
time zone.
TestClockmakes it easy to deterministically and efficiently test effects involving the passage of time.Instead of waiting for actual time to pass,
sleepand methods implemented in terms of it schedule effects to take place at a given wall clock time. Users can adjust the wall clock time using theadjustandsetTimemethods, and all effects scheduled to take place on or before that wall clock time will automically be run.For example, here is how we can test
ZIO#timeoutusingTestClock:Note how we forked the fiber that
sleepwas invoked on. Calls tosleepand methods derived from it will semantically block until the wall clock time is set to on or after the time they are scheduled to run. If we didn't fork the fiber on which we called sleep we would never get to set the the wall clock time on the line below. Thus, a useful pattern when usingTestClockis to fork the effect being tested, then adjust the wall clock time, and finally verify that the expected effects have been performed.Sleep and related combinators schedule events to occur at a specified duration in the future relative to the current fiber time (e.g. 10 seconds from the current fiber time). The fiber time is backed by a
FiberRefand is incremented for the duration each fiber is sleeping. Child fibers inherit the fiber time of their parent so methods that rely on repeatedsleepcalls work as you would expect.For example, here is how we can test an effect that recurs with a fixed delay:
Here we verify that no effect is performed before the recurrence period, that an effect is performed after the recurrence period, and that the effect is performed exactly once. The key thing to note here is that after each recurrence the next recurrence is scheduled to occur at the appropriate time in the future, so when we adjust the wall clock time by 60 minutes exactly one value is placed in the queue, and when we adjust the wall clock time by another 60 minutes exactly one more value is placed in the queue.