Sha256: f15156d8d3f90b94b32a82bdc66cec323c960f1f4a72c0c872d82a3f2a55ed63

Contents?: true

Size: 1.96 KB

Versions: 41

Compression:

Stored size: 1.96 KB

Contents

require 'time'
require 'active_support'
require 'active_support/core_ext'

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, sleep_interval = SLEEP_INTERVAL, &blk)
      title = "Polling for #{minutes} mins"

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

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

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

    private

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

    def retry_times(retries, sleep_interval, &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, sleep_interval, &blk)
      blk.call
    rescue EarlyFailException
      raise
    rescue RSpec::Expectations::ExpectationNotMetError, StandardError => e
      seconds_left = (end_time - Time.now).round
      last_logged_at ||= 1.seconds.ago

      if seconds_left % 10 == 0 && (Time.now - last_logged_at).round != 0
        Logger.debug "------- seconds_left=#{seconds_left}"
      end

      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

41 entries across 41 versions & 1 rubygems

Version Path
ops_manager_ui_drivers-2.42.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.41.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.40.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.40.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.39.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.38.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.37.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.36.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.35.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.35.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.35.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.34.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.34.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.33.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.33.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.32.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.32.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.31.3 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.31.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.31.1 lib/ops_manager_ui_drivers/wait_helper.rb