summaryrefslogtreecommitdiff
path: root/qa/qa/support/waiter.rb
blob: fdcf2d7e157b26cc894eb036db5708ee6a93c9d7 (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
# frozen_string_literal: true

module QA
  module Support
    module Waiter
      DEFAULT_MAX_WAIT_TIME = 60

      module_function

      def wait(max: DEFAULT_MAX_WAIT_TIME, interval: 0.1)
        QA::Runtime::Logger.debug("with wait: max #{max}; interval #{interval}")
        start = Time.now

        while Time.now - start < max
          result = yield
          if result
            log_end(Time.now - start)
            return result
          end

          sleep(interval)
        end
        log_end(Time.now - start)

        false
      end

      def self.log_end(duration)
        QA::Runtime::Logger.debug("ended wait after #{duration} seconds")
      end
    end
  end
end