summaryrefslogtreecommitdiff
path: root/qa/qa/support/retrier.rb
blob: 230cec8f8d222ccd27c09a447f86a3682d139b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

module QA
  module Support
    module Retrier
      module_function

      def retry_on_exception(max_attempts: 3, reload_page: nil, sleep_interval: 0.5)
        QA::Runtime::Logger.debug("with retry_on_exception: max_attempts #{max_attempts}; sleep_interval #{sleep_interval}")

        attempts = 0

        begin
          QA::Runtime::Logger.debug("Attempt number #{attempts + 1}")
          yield
        rescue StandardError, RSpec::Expectations::ExpectationNotMetError
          sleep sleep_interval
          reload_page.refresh if reload_page
          attempts += 1

          retry if attempts < max_attempts
          QA::Runtime::Logger.debug("Raising exception after #{max_attempts} attempts")
          raise
        end
      end

      def retry_until(max_attempts: 3, reload: false, sleep_interval: 0)
        QA::Runtime::Logger.debug("with retry_until: max_attempts #{max_attempts}; sleep_interval #{sleep_interval}; reload:#{reload}")
        attempts = 0

        while attempts < max_attempts
          QA::Runtime::Logger.debug("Attempt number #{attempts + 1}")
          result = yield
          return result if result

          sleep sleep_interval

          refresh if reload

          attempts += 1
        end

        false
      end
    end
  end
end