Object

zio.test.environment

TestClock

Related Doc: package environment

Permalink

object TestClock extends Serializable

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.duration._
import zio.test.environment.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.duration._
import zio.test.environment.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
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TestClock
  2. Serializable
  3. Serializable
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. final case class Data(duration: Duration, sleeps: List[(Duration, Promise[Nothing, Unit])], timeZone: ZoneId) extends Product with Serializable

    Permalink

    Data represents the state of the TestClock, including the clock time and time zone.

  2. trait Service extends Restorable

    Permalink
  3. final case class Sleep(duration: Duration, promise: Promise[Nothing, Unit], fiberId: Id) extends Product with Serializable

    Permalink

    Sleep represents the state of a scheduled effect, including the time the effect is scheduled to run, a promise that can be completed to resume execution of the effect, and the fiber executing the effect.

  4. sealed abstract class SuspendedWarningData extends AnyRef

    Permalink
  5. final case class Test(clockState: Ref[Data], live: Live.Service, annotations: Annotations.Service, warningState: RefM[WarningData], suspendedWarningState: RefM[SuspendedWarningData]) extends clock.Clock.Service with Service with Product with Serializable

    Permalink
  6. sealed abstract class WarningData extends AnyRef

    Permalink

    WarningData describes the state of the warning message that is displayed if a test is using time by is not advancing the TestClock.

    WarningData describes the state of the warning message that is displayed if a test is using time by is not advancing the TestClock. The possible states are Start if a test has not used time, Pending if a test has used time but has not adjusted the TestClock, and Done if a test has adjusted the TestClock or the warning message has already been displayed.

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. object SuspendedWarningData

    Permalink
  5. object WarningData

    Permalink
  6. def adjust(duration: ⇒ Duration): URIO[TestClock, Unit]

    Permalink

    Accesses a TestClock instance in the environment and increments the time by the specified duration, running any actions scheduled for on or before the new time in order.

  7. val any: ZLayer[Clock with TestClock, Nothing, Clock with TestClock]

    Permalink
  8. final def asInstanceOf[T0]: T0

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

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  10. val default: ZLayer[Live with Annotations, Nothing, Clock with TestClock]

    Permalink
  11. final def eq(arg0: AnyRef): Boolean

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

    Permalink
    Definition Classes
    AnyRef → Any
  13. def finalize(): Unit

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

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

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

    Permalink
    Definition Classes
    Any
  17. def live(data: Data): ZLayer[Live with Annotations, Nothing, Clock with TestClock]

    Permalink

    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.

  18. final def ne(arg0: AnyRef): Boolean

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

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

    Permalink
    Definition Classes
    AnyRef
  21. val save: ZIO[TestClock, Nothing, UIO[Unit]]

    Permalink

    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.

  22. def setDateTime(dateTime: ⇒ OffsetDateTime): URIO[TestClock, Unit]

    Permalink

    Accesses a TestClock instance in the environment and sets the clock time to the specified OffsetDateTime, running any actions scheduled for on or before the new time in order.

  23. def setTime(duration: ⇒ Duration): URIO[TestClock, Unit]

    Permalink

    Accesses a TestClock instance in the environment and sets the clock time to the specified time in terms of duration since the epoch, running any actions scheduled for on or before the new time in order.

  24. def setTimeZone(zone: ⇒ ZoneId): URIO[TestClock, Unit]

    Permalink

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

  25. val sleeps: ZIO[TestClock, Nothing, List[Duration]]

    Permalink

    Accesses a TestClock instance in the environment and returns a list of times that effects are scheduled to run.

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

    Permalink
    Definition Classes
    AnyRef
  27. val timeZone: URIO[TestClock, ZoneId]

    Permalink

    Accesses a TestClock instance in the environment and returns the current time zone.

  28. def toString(): String

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

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

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

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

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped