org.scalatest.concurrent

Eventually

trait Eventually extends TimeoutConfiguration

Trait that provides the eventually construct, which periodically retries executing a passed by-name parameter, until it either succeeds or the configured timeout has been surpassed.

The by-name parameter "succeeds" if it returns a result. It "fails" if it throws any exception that would normally cause a test to fail. (These are any exceptions except TestPendingException and Errors listed in the Treatment of java.lang.Errors section of the documentation of trait Suite.)

For example, the following invocation of eventually would succeed (not throw an exception):

val xs = 1 to 125
val it = xs.iterator
eventually { it.next should be (3) }

However, because the default timeout is one second, the following invocation of eventually would ultimately produce a TestFailedException:

val xs = 1 to 125
val it = xs.iterator
eventually { Thread.sleep(999); it.next should be (110) }

Assuming the default configuration parameters, a timeout of 1 second and an interval of 10 milliseconds, were passed implicitly to eventually, the detail message of the thrown TestFailedException would look like:

The code passed to eventually never returned normally. Attempted 2 times, sleeping 10 milliseconds between each attempt.

Configuration of eventually

The eventually methods of this trait can be flexibly configured. The two configuration parameters for eventually along with their default values and meanings are described in the following table:

Configuration Parameter Default Value Meaning
timeout 1 second the maximum amount of time to allow unsuccessful attempts before giving up and throwing TestFailedException
interval 10 milliseconds the amount of time to sleep between each attempt

The eventually methods of trait Eventually each take an TimeoutConfig object as an implicit parameter. This object provides values for the two configuration parameters. Trait Eventually provides an implicit val named retryConfig with each configuration parameter set to its default value. If you want to set one or more configuration parameters to a different value for all invocations of eventually in a suite you can override this val (or hide it, for example, if you are importing the members of the Eventually companion object rather than mixing in the trait). For example, if you always want the default timeout to be 2 seconds and the default interval to be 5 milliseconds, you can override retryConfig, like this:

implicit override val retryConfig =
  TimeoutConfig(timeout = Span(2, Seconds), interval = Span(5, Millis))

Or, hide it by declaring a variable of the same name in whatever scope you want the changed values to be in effect:

implicit val retryConfig =
  TimeoutConfig(timeout = Span(2, Seconds), interval = Span(5, Millis))

In addition to taking a TimeoutConfig object as an implicit parameter, the eventually methods of trait Eventually include overloaded forms that take one or two TimeoutConfigParam objects that you can use to override the values provided by the implicit TimeoutConfig for a single eventually invocation. For example, if you want to set timeout to 5000 for just one particular eventually invocation, you can do so like this:

eventually (timeout(Span(5, Seconds))) { Thread.sleep(10); it.next should be (110) }

This invocation of eventually will use 5 seconds for the timeout and whatever value is specified by the implicitly passed TimeoutConfig object for the interval configuration parameter. If you want to set both configuration parameters in this way, just list them separated by commas:

eventually (timeout(Span(5, Seconds)), interval(Span(5, Millis)) { it.next should be (110) }

You can also import or mix in the members of SpanSugar if you want a more concise DSL for expressing time spans:

eventually (timeout(5 seconds), interval(5 millis) { it.next should be (110) }

Linear Supertypes
Known Subclasses
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Hide All
  2. Show all
  1. Eventually
  2. TimeoutConfiguration
  3. AnyRef
  4. Any
Visibility
  1. Public
  2. All

Type Members

  1. case class Interval (value: Span) extends TimeoutConfigParam with Product with Serializable

    A TimeoutConfigParam that specifies the amount of time to sleep after each retry: each unsuccessful invocation of the by-name parameter passed to eventually or each query of a future passed to whenReady.

  2. case class Timeout (value: Span) extends TimeoutConfigParam with Product with Serializable

    A TimeoutConfigParam that specifies the maximum amount of time to allow retries: either invocations of the by-name parameter passed to eventually that give an unsuccessful result, or futures passed to whenReady that are canceled, or expired, or not ready.

  3. case class TimeoutConfig (timeout: Span, interval: Span) extends Product with Serializable

    Configuration object for traits Eventually and Futures.

  4. class TimeoutConfigParam extends AnyRef

    Abstract class defining a family of configuration parameters for traits Eventually and Futures.

Value Members

  1. def != (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  2. def != (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  3. def ## (): Int

    Attributes
    final
    Definition Classes
    AnyRef → Any
  4. def == (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  5. def == (arg0: Any): Boolean

    Attributes
    final
    Definition Classes
    Any
  6. def asInstanceOf [T0] : T0

    Attributes
    final
    Definition Classes
    Any
  7. def clone (): AnyRef

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  8. def eq (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  9. def equals (arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def eventually [T] (fun: ⇒ T)(implicit config: TimeoutConfig): T

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    The by-name parameter "succeeds" if it returns a result. It "fails" if it throws any exception that would normally cause a test to fail. (These are any exceptions except TestPendingException and Errors listed in the Treatment of java.lang.Errors section of the documentation of trait Suite.)

    The maximum amount of time in milliseconds to tolerate unsuccessful attempts before giving up is configured by the timeout field of the TimeoutConfig passed implicitly as the last parameter. The interval to sleep between attempts is configured by the interval field of the TimeoutConfig passed implicitly as the last parameter.

    fun

    the by-name parameter to repeatedly invoke

    config

    the TimeoutConfig object containing the timeout and interval parameters

    returns

    the result of invoking the fun by-name parameter, the first time it succeeds

  11. def eventually [T] (interval: Interval)(fun: ⇒ T)(implicit config: TimeoutConfig): T

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    The by-name parameter "succeeds" if it returns a result. It "fails" if it throws any exception that would normally cause a test to fail. (These are any exceptions except TestPendingException and Errors listed in the Treatment of java.lang.Errors section of the documentation of trait Suite.)

    The maximum amount of time in milliseconds to tolerate unsuccessful attempts before giving up is configured by the timeout field of the TimeoutConfig passed implicitly as the last parameter. The interval to sleep between attempts is configured by the value contained in the passed interval parameter.

    interval

    the Interval configuration parameter

    fun

    the by-name parameter to repeatedly invoke

    config

    the TimeoutConfig object containing the (used) timeout and (unused) interval parameters

    returns

    the result of invoking the fun by-name parameter, the first time it succeeds

  12. def eventually [T] (timeout: Timeout)(fun: ⇒ T)(implicit config: TimeoutConfig): T

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    The by-name parameter "succeeds" if it returns a result. It "fails" if it throws any exception that would normally cause a test to fail. (These are any exceptions except TestPendingException and Errors listed in the Treatment of java.lang.Errors section of the documentation of trait Suite.)

    The maximum amount of time in milliseconds to tolerate unsuccessful attempts before giving up and throwing TestFailedException is configured by the value contained in the passed timeout parameter. The interval to sleep between attempts is configured by the interval field of the TimeoutConfig passed implicitly as the last parameter.

    timeout

    the Timeout configuration parameter

    fun

    the by-name parameter to repeatedly invoke

    config

    the TimeoutConfig object containing the (unused) timeout and (used) interval parameters

    returns

    the result of invoking the fun by-name parameter, the first time it succeeds

  13. def eventually [T] (timeout: Timeout, interval: Interval)(fun: ⇒ T): T

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    Invokes the passed by-name parameter repeatedly until it either succeeds, or a configured maximum amount of time has passed, sleeping a configured interval between attempts.

    The by-name parameter "succeeds" if it returns a result. It "fails" if it throws any exception that would normally cause a test to fail. (These are any exceptions except TestPendingException and Errors listed in the Treatment of java.lang.Errors section of the documentation of trait Suite.)

    The maximum amount of time in milliseconds to tolerate unsuccessful attempts before giving up and throwing TestFailedException is configured by the value contained in the passed timeout parameter. The interval to sleep between attempts is configured by the value contained in the passed interval parameter.

    timeout

    the Timeout configuration parameter

    interval

    the Interval configuration parameter

    fun

    the by-name parameter to repeatedly invoke

    returns

    the result of invoking the fun by-name parameter, the first time it succeeds

  14. def finalize (): Unit

    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  15. def getClass (): java.lang.Class[_]

    Attributes
    final
    Definition Classes
    AnyRef
  16. def hashCode (): Int

    Definition Classes
    AnyRef → Any
  17. def interval (value: Span): Interval

    Returns an Interval configuration parameter containing the passed value, which specifies the amount of time to sleep after each retry: each unsuccessful invocation of the by-name parameter passed to eventually or each query of a non-ready, canceled, or expired future passed to whenReady.

    Returns an Interval configuration parameter containing the passed value, which specifies the amount of time to sleep after each retry: each unsuccessful invocation of the by-name parameter passed to eventually or each query of a non-ready, canceled, or expired future passed to whenReady.

    Definition Classes
    TimeoutConfiguration
  18. def isInstanceOf [T0] : Boolean

    Attributes
    final
    Definition Classes
    Any
  19. def ne (arg0: AnyRef): Boolean

    Attributes
    final
    Definition Classes
    AnyRef
  20. def notify (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  21. def notifyAll (): Unit

    Attributes
    final
    Definition Classes
    AnyRef
  22. def synchronized [T0] (arg0: ⇒ T0): T0

    Attributes
    final
    Definition Classes
    AnyRef
  23. def timeout (value: Span): Timeout

    Returns a Timeout configuration parameter containing the passed value, which specifies the maximum amount of time to retry: to allow invocations of the by-name parameter passed to eventually to give an unsuccessful result, or to allow a future passed to whenReady to be canceled, or expired, or not ready.

    Returns a Timeout configuration parameter containing the passed value, which specifies the maximum amount of time to retry: to allow invocations of the by-name parameter passed to eventually to give an unsuccessful result, or to allow a future passed to whenReady to be canceled, or expired, or not ready.

    Definition Classes
    TimeoutConfiguration
  24. implicit val timeoutConfig : TimeoutConfig

    Implicit TimeoutConfig value providing default configuration values.

    Implicit TimeoutConfig value providing default configuration values.

    To change the default configuration, override or hide this val with another implicit TimeoutConfig containing your desired default configuration values.

    Attributes
    implicit
    Definition Classes
    TimeoutConfiguration
  25. def toString (): String

    Definition Classes
    AnyRef → Any
  26. def wait (): Unit

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

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

    Attributes
    final
    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from TimeoutConfiguration

Inherited from AnyRef

Inherited from Any