Sha256: 29eeb0036ff76f8356157d7b4e90e95813d5d84dd13fbf67b5b4f755932558b8

Contents?: true

Size: 1.4 KB

Versions: 20

Compression:

Stored size: 1.4 KB

Contents

module Soaspec
  class TimeOutError < StandardError; end
  # Class to enable waiting for an expected condition to return true
  class Wait
    DEFAULT_TIMEOUT  = 5
    DEFAULT_INTERVAL = 0.2

    #
    # Wait until the given block returns a true value.
    #
    # @param [Hash] opts Options for this instance
    # @option opts [Numeric] :timeout (5) Seconds to wait before timing out.
    # @option opts [Numeric] :interval (0.2) Seconds to sleep between polls.
    # @option opts [String] :message Exception mesage if timed out.
    # @option opts [Array, Exception] :ignore Exceptions to ignore while polling (default: Error::NoSuchElementError)
    # @raise [Error::TimeOutError]
    # @return [Object] the result of the block
    #
    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 => last_error
          # 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
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
soaspec-0.2.29 lib/soaspec/wait.rb
soaspec-0.2.28 lib/soaspec/wait.rb
soaspec-0.2.27 lib/soaspec/wait.rb
soaspec-0.2.26 lib/soaspec/wait.rb
soaspec-0.2.25 lib/soaspec/wait.rb
soaspec-0.2.23 lib/soaspec/wait.rb
soaspec-0.2.22 lib/soaspec/wait.rb
soaspec-0.2.21 lib/soaspec/wait.rb
soaspec-0.2.20 lib/soaspec/wait.rb
soaspec-0.2.19 lib/soaspec/wait.rb
soaspec-0.2.18 lib/soaspec/wait.rb
soaspec-0.2.17 lib/soaspec/wait.rb
soaspec-0.2.16 lib/soaspec/wait.rb
soaspec-0.2.15 lib/soaspec/wait.rb
soaspec-0.2.14 lib/soaspec/wait.rb
soaspec-0.2.13 lib/soaspec/wait.rb
soaspec-0.2.12 lib/soaspec/wait.rb
soaspec-0.2.11 lib/soaspec/wait.rb
soaspec-0.2.10 lib/soaspec/wait.rb
soaspec-0.2.9 lib/soaspec/wait.rb