Sha256: 8b6c6a671d037f36251c788bc9bee5aa662fe07d09f505e792a0eca166fc6176

Contents?: true

Size: 1.65 KB

Versions: 31

Compression:

Stored size: 1.65 KB

Contents

require 'time'

module OpsManagerUiDrivers
  module WaitHelper
    SLEEP_INTERVAL = 0.5

    EarlyFailException = Class.new(Exception)

    def fail_early(msg)
      fail(EarlyFailException.new(msg))
    end

    def poll_up_to_mins(minutes, &blk)
      title = "Polling for #{minutes} mins"

      Logger.debug "--- BEGIN #{title}"
      retry_until(minutes_to_time(minutes), &blk)
    ensure
      Logger.debug "--- END   #{title}"
    end

    def poll_up_to_times(times, &blk)
      title = "Polling for #{times} times"

      Logger.debug "--- BEGIN #{title}"
      retry_times(times, &blk)
    ensure
      Logger.debug "--- END   #{title}"
    end

    private

    # We're rescuing StandardError because Capybara throws random stuff at us.

    def retry_times(retries, &blk)
      blk.call
    rescue EarlyFailException
      raise
    rescue RSpec::Expectations::ExpectationNotMetError, StandardError => e
      retries -= 1
      Logger.debug "------- retries_left=#{retries}"
      if retries > 0
        sleep(SLEEP_INTERVAL)
        retry
      else
        Logger.debug "------- propagate error=#{e}"
        raise
      end
    end

    def retry_until(end_time, &blk)
      blk.call
    rescue EarlyFailException
      raise
    rescue RSpec::Expectations::ExpectationNotMetError, StandardError => e
      seconds_left = (end_time - Time.now).round
      Logger.debug "------- seconds_left=#{seconds_left}"
      if seconds_left > SLEEP_INTERVAL
        sleep(SLEEP_INTERVAL)
        retry
      else
        Logger.debug "------- propagate error=#{e}"
        raise
      end
    end

    def minutes_to_time(minutes)
      Time.now + (minutes * 60)
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ops_manager_ui_drivers-2.10.8 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.7 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.6 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.5 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.4 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.3 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.10.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.9.6 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.9.5 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.9.4 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.9.3 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.9.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.9.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.9.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.8.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.7.3 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.7.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.7.1 lib/ops_manager_ui_drivers/wait_helper.rb