Sha256: 3059a79091b3cf16c719bbf39a955300c42cb94ad578bcad1dd715fc7b2c64bd

Contents?: true

Size: 1.57 KB

Versions: 51

Compression:

Stored size: 1.57 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

    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

51 entries across 51 versions & 1 rubygems

Version Path
ops_manager_ui_drivers-2.3.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.3.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.3.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.2.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.2.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.8 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.7 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.6 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.5 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.4 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.3 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.1.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.0.4 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.0.3 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.0.2 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-2.0.0 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-1.28.1 lib/ops_manager_ui_drivers/wait_helper.rb
ops_manager_ui_drivers-1.28.0 lib/ops_manager_ui_drivers/wait_helper.rb