Packages

  • package root
    Definition Classes
    root
  • package zio
    Definition Classes
    root
  • package test

    _ZIO Test_ is a featherweight testing library for effectful programs.

    _ZIO Test_ is a featherweight testing library for effectful programs.

    The library imagines every spec as an ordinary immutable value, providing tremendous potential for composition. Thanks to tight integration with ZIO, specs can use resources (including those requiring disposal), have well- defined linear and parallel semantics, and can benefit from a host of ZIO combinators.

    import zio.test._
    import zio.clock.nanoTime
    import Assertion.isGreaterThan
    
    object MyTest extends DefaultRunnableSpec {
      def spec = suite("clock")(
        testM("time is non-zero") {
          assertM(nanoTime)(isGreaterThan(0))
        }
      )
    }
    Definition Classes
    zio
  • package environment

    The environment package contains testable versions of all the standard ZIO environment types through the TestClock, TestConsole, TestSystem, and TestRandom modules.

    The environment package contains testable versions of all the standard ZIO environment types through the TestClock, TestConsole, TestSystem, and TestRandom modules. See the documentation on the individual modules for more detail about using each of them.

    If you are using ZIO Test and extending RunnableSpec a TestEnvironment containing all of them will be automatically provided to each of your tests. Otherwise, the easiest way to use the test implementations in ZIO Test is by providing the TestEnvironment to your program.

    import zio.test.environment._
    
    myProgram.provideLayer(testEnvironment)

    Then all environmental effects, such as printing to the console or generating random numbers, will be implemented by the TestEnvironment and will be fully testable. When you do need to access the "live" environment, for example to print debugging information to the console, just use the live combinator along with the effect as your normally would.

    If you are only interested in one of the test implementations for your application, you can also access them a la carte through the make method on each module. Each test module requires some data on initialization. Default data is included for each as DefaultData.

    import zio.test.environment._
    
    myProgram.provideM(TestConsole.make(TestConsole.DefaultData))

    Finally, you can create a Test object that implements the test interface directly using the makeTest method. This can be useful when you want to access some testing functionality without using the environment type.

    import zio.test.environment._
    
    for {
      testRandom <- TestRandom.makeTest(TestRandom.DefaultData)
      n          <- testRandom.nextInt
    } yield n

    This can also be useful when you are creating a more complex environment to provide the implementation for test services that you mix in.

    Definition Classes
    test
  • Live
  • Restorable
  • TestClock
  • TestConsole
  • TestEnvironment
  • TestRandom
  • TestSystem

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 wall clock time. Users can adjust the wall clock time using the adjust and setTime methods, 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#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 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 using TestClock is 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 FiberRef and is incremented for the duration each fiber is sleeping. Child fibers inherit the fiber time of their parent so methods that rely on repeated sleep calls work as you would expect.

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

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TestClock
  2. Serializable
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Type Members

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

    The state of the TestClock.

  2. final case class FiberData(duration: Duration, timeZone: ZoneId) extends Product with Serializable
  3. trait Service extends Restorable
  4. final case class Test(clockState: Ref[Data], fiberState: FiberRef[FiberData], live: Live.Service, warningState: RefM[WarningData]) extends clock.Clock.Service with Service with Product with Serializable
  5. sealed trait WarningData extends AnyRef

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. def adjust(duration: => Duration): ZIO[TestClock, Nothing, Unit]

    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.

  5. val any: ZLayer[Clock with TestClock, Nothing, Clock with TestClock]
  6. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  7. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  8. val default: ZLayer[Live, Nothing, Clock with TestClock]
  9. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  10. def equals(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef → Any
  11. val fiberTime: ZIO[TestClock, Nothing, Duration]

    Accesses a TestClock instance in the environment and returns the current fiber time for this fiber.

  12. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  13. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  14. def hashCode(): Int
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  15. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  16. def live(data: Data): ZLayer[Live, Nothing, Clock with TestClock]

    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.

  17. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  18. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  19. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  20. val runAll: ZIO[TestClock, Nothing, Unit]

    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.

  21. val save: ZIO[TestClock, Nothing, UIO[Unit]]

    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): ZIO[TestClock, Nothing, Unit]

    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.

  23. def setTime(duration: => Duration): ZIO[TestClock, Nothing, Unit]

    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.

  24. def setTimeZone(zone: => ZoneId): ZIO[TestClock, Nothing, Unit]

    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.

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

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

  26. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  27. val timeZone: ZIO[TestClock, Nothing, ZoneId]

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

  28. def toString(): String
    Definition Classes
    AnyRef → Any
  29. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  30. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  31. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()
  32. object FiberData extends Serializable
  33. object WarningData

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped