Packages

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.

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TestClock
  2. Restorable
  3. Clock
  4. Serializable
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def adjust(duration: zio.Duration)(implicit trace: ZTraceElement): UIO[Unit]
  2. abstract def currentDateTime(implicit trace: ZTraceElement): UIO[OffsetDateTime]
    Definition Classes
    Clock
  3. abstract def currentTime(unit: => TimeUnit)(implicit trace: ZTraceElement): UIO[Long]
    Definition Classes
    Clock
  4. abstract def instant(implicit trace: ZTraceElement): UIO[Instant]
    Definition Classes
    Clock
  5. abstract def localDateTime(implicit trace: ZTraceElement): UIO[LocalDateTime]
    Definition Classes
    Clock
  6. abstract def nanoTime(implicit trace: ZTraceElement): UIO[Long]
    Definition Classes
    Clock
  7. abstract def save(implicit trace: ZTraceElement): UIO[UIO[Unit]]
    Definition Classes
    Restorable
  8. abstract def scheduler(implicit trace: ZTraceElement): UIO[Scheduler]
    Definition Classes
    Clock
  9. abstract def setDateTime(dateTime: OffsetDateTime)(implicit trace: ZTraceElement): UIO[Unit]
  10. abstract def setTime(duration: zio.Duration)(implicit trace: ZTraceElement): UIO[Unit]
  11. abstract def setTimeZone(zone: ZoneId)(implicit trace: ZTraceElement): UIO[Unit]
  12. abstract def sleep(duration: => zio.Duration)(implicit trace: ZTraceElement): UIO[Unit]
    Definition Classes
    Clock
  13. abstract def sleeps(implicit trace: ZTraceElement): UIO[List[zio.Duration]]
  14. abstract def timeZone(implicit trace: ZTraceElement): UIO[ZoneId]

Concrete Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  6. final def driver[Env, In, Out](schedule: Schedule[Env, In, Out])(implicit trace: ZTraceElement): UIO[Driver[State, Env, In, Out]]
    Definition Classes
    Clock
  7. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  8. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  9. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  10. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  11. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  12. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  13. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  14. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  15. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  16. final def repeat[R, R1 <: R, E, A, B](zio: => ZIO[R, E, A])(schedule: => Schedule[R1, A, B])(implicit trace: ZTraceElement): ZIO[R1, E, B]
    Definition Classes
    Clock
  17. final def repeatOrElse[R, R1 <: R, E, E2, A, B](zio: => ZIO[R, E, A])(schedule: => Schedule[R1, A, B], orElse: (E, Option[B]) => ZIO[R1, E2, B])(implicit trace: ZTraceElement): ZIO[R1, E2, B]
    Definition Classes
    Clock
  18. final def repeatOrElseEither[R, R1 <: R, E, E2, A, B, C](zio0: => ZIO[R, E, A])(schedule0: => Schedule[R1, A, B], orElse: (E, Option[B]) => ZIO[R1, E2, C])(implicit trace: ZTraceElement): ZIO[R1, E2, Either[C, B]]
    Definition Classes
    Clock
  19. final def retry[R, R1 <: R, E, A, S](zio: => ZIO[R, E, A])(policy: Schedule[R1, E, S])(implicit ev: CanFail[E], trace: ZTraceElement): ZIO[R1, E, A]
    Definition Classes
    Clock
  20. final def retryOrElse[R, R1 <: R, E, E1, A, A1 >: A, S](zio: => ZIO[R, E, A])(policy: => Schedule[R1, E, S], orElse: (E, S) => ZIO[R1, E1, A1])(implicit ev: CanFail[E], trace: ZTraceElement): ZIO[R1, E1, A1]
    Definition Classes
    Clock
  21. final def retryOrElseEither[R, R1 <: R, E, E1, A, B, Out](zio0: => ZIO[R, E, A])(schedule0: => Schedule[R1, E, Out], orElse: (E, Out) => ZIO[R1, E1, B])(implicit ev: CanFail[E], trace: ZTraceElement): ZIO[R1, E1, Either[B, A]]
    Definition Classes
    Clock
  22. final def schedule[R, R1 <: R, E, A, B](zio: => ZIO[R, E, A])(schedule: => Schedule[R1, Any, B])(implicit trace: ZTraceElement): ZIO[R1, E, B]
    Definition Classes
    Clock
  23. final def scheduleFrom[R, R1 <: R, E, A, A1 >: A, B](zio0: => ZIO[R, E, A])(a: => A1)(schedule0: => Schedule[R1, A1, B])(implicit trace: ZTraceElement): ZIO[R1, E, B]
    Definition Classes
    Clock
  24. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  25. def toString(): String
    Definition Classes
    AnyRef → Any
  26. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  27. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  28. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Inherited from Restorable

Inherited from Clock

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped