Class: Soaspec::Wait

Inherits:
Object
  • Object
show all
Defined in:
lib/soaspec/wait.rb

Overview

Class to enable waiting for an expected condition to return true

Constant Summary collapse

DEFAULT_TIMEOUT =
5
DEFAULT_INTERVAL =
0.2

Class Method Summary collapse

Class Method Details

.until(opts = {}) ⇒ Object

Wait until the given block returns a true value.

Parameters:

  • opts (Hash) (defaults to: {})

    Options for this instance

Options Hash (opts):

  • :timeout (Numeric) — default: 5

    Seconds to wait before timing out.

  • :interval (Numeric) — default: 0.2

    Seconds to sleep between polls.

  • :message (String)

    Exception mesage if timed out.

  • :ignore (Array, Exception)

    Exceptions to ignore while polling (default: Error::NoSuchElementError)

Returns:

  • (Object)

    the result of the block

Raises:

  • (Error::TimeOutError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/soaspec/wait.rb', line 21

def self.until(opts = {})
  timeout = opts.fetch(:timeout, DEFAULT_TIMEOUT)
  ignored = Array(opts[:ignore] || NoElementAtPath)
  interval = opts.fetch(:interval, DEFAULT_INTERVAL)
  end_time = Time.now + timeout
  last_error = nil

  until Time.now > end_time
    begin
      result = yield
      return result if result
    rescue *ignored => e
      # swallowed
    end
    sleep interval
  end

  msg = opts[:message] ? opts[:message].dup : "timed out after #{timeout} seconds with interval of #{interval}"
  msg << " (#{last_error.message})" if last_error
  raise Soaspec::TimeOutError, msg
end