Sha256: 8f25703fed5deb562751618b73ffa4232e8f746fba35feaf76a8904aab1be8c9

Contents?: true

Size: 1.57 KB

Versions: 1

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, RuntimeError => 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, RuntimeError => 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

1 entries across 1 versions & 1 rubygems

Version Path
ops_manager_ui_drivers-2.4.0 lib/ops_manager_ui_drivers/wait_helper.rb