trait TestClock extends Clock with Restorable
TestClock
makes it easy to deterministically and efficiently test effects
involving the passage of time.
Instead of waiting for actual time to pass, sleep
and methods implemented
in terms of it schedule effects to take place at a given clock time. Users
can adjust the clock time using the adjust
and setTime
methods, and all
effects scheduled to take place on or before that time will automatically be
run in order.
For example, here is how we can test ZIO#timeout
using TestClock
:
import zio.ZIO import zio.test.TestClock for { fiber <- ZIO.sleep(5.minutes).timeout(1.minute).fork _ <- TestClock.adjust(1.minute) result <- fiber.join } yield result == None
Note how we forked the fiber that sleep
was invoked on. Calls to sleep
and methods derived from it will semantically block until the 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 time on the line
below. Thus, a useful pattern when using TestClock
is to fork the effect
being tested, then adjust the clock time, and finally verify that the
expected effects have been performed.
For example, here is how we can test an effect that recurs with a fixed delay:
import zio.Queue import zio.test.TestClock for { q <- Queue.unbounded[Unit] _ <- q.offer(()).delay(60.minutes).forever.fork a <- q.poll.map(_.isEmpty) _ <- TestClock.adjust(60.minutes) b <- q.take.as(true) c <- q.poll.map(_.isEmpty) _ <- TestClock.adjust(60.minutes) d <- q.take.as(true) e <- q.poll.map(_.isEmpty) } yield a && b && c && d && e
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 clock by 60 minutes exactly one value is placed in the queue, and when we adjust the clock by another 60 minutes exactly one more value is placed in the queue.
- Alphabetic
- By Inheritance
- TestClock
- Restorable
- Clock
- Serializable
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Abstract Value Members
- abstract def adjust(duration: zio.Duration)(implicit trace: Trace): UIO[Unit]
- abstract def adjustWith[R, E, A](duration: zio.Duration)(zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]
- abstract def currentDateTime(implicit trace: Trace): UIO[OffsetDateTime]
- Definition Classes
- Clock
- abstract def currentTime(unit: => ChronoUnit)(implicit trace: Trace, d: DummyImplicit): UIO[Long]
- Definition Classes
- Clock
- abstract def currentTime(unit: => TimeUnit)(implicit trace: Trace): UIO[Long]
- Definition Classes
- Clock
- abstract def instant(implicit trace: Trace): UIO[Instant]
- Definition Classes
- Clock
- abstract def javaClock(implicit trace: Trace): UIO[java.time.Clock]
- Definition Classes
- Clock
- abstract def localDateTime(implicit trace: Trace): UIO[LocalDateTime]
- Definition Classes
- Clock
- abstract def nanoTime(implicit trace: Trace): UIO[Long]
- Definition Classes
- Clock
- abstract def save(implicit trace: Trace): UIO[UIO[Unit]]
- Definition Classes
- Restorable
- abstract def scheduler(implicit trace: Trace): UIO[Scheduler]
- Definition Classes
- Clock
- abstract def setTime(instant: Instant)(implicit trace: Trace): UIO[Unit]
- abstract def setTimeZone(zone: ZoneId)(implicit trace: Trace): UIO[Unit]
- abstract def sleep(duration: => zio.Duration)(implicit trace: Trace): UIO[Unit]
- Definition Classes
- Clock
- abstract def sleeps(implicit trace: Trace): UIO[List[Instant]]
- abstract def timeZone(implicit trace: Trace): UIO[ZoneId]
Concrete Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- def unsafe: UnsafeAPI
- Definition Classes
- Clock
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()