Skip to content

Commit 0b10980

Browse files
committed
split timer loop from wait
1 parent 009f679 commit 0b10980

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

lib/appium_lib_core/common/wait.rb

+25-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# rubocop:disable Lint/HandleExceptions
1+
require_relative 'wait/timer'
2+
23
module Appium
34
module Core
45
module Wait
@@ -30,19 +31,14 @@ class << self
3031
def until(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil)
3132
ignored = Array(ignored || ::Exception)
3233

33-
end_time = Time.now + timeout
3434
last_error = nil
3535

36-
until Time.now > end_time
37-
begin
38-
return yield(object)
39-
rescue ::Errno::ECONNREFUSED => e
40-
raise e
41-
rescue *ignored => last_error
42-
# swallowed
43-
end
44-
45-
sleep interval
36+
begin
37+
run_with_timer(timeout, interval) { return yield(object) }
38+
rescue ::Errno::ECONNREFUSED => e
39+
raise e
40+
rescue *ignored => last_error # rubocop:disable Lint/HandleExceptions
41+
# swallowed
4642
end
4743

4844
msg = message_for timeout, message
@@ -75,20 +71,17 @@ def until(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ig
7571
def until_true(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ignored: nil, object: nil)
7672
ignored = Array(ignored || ::Exception)
7773

78-
end_time = Time.now + timeout
7974
last_error = nil
8075

81-
until Time.now > end_time
82-
begin
76+
begin
77+
run_with_timer(timeout, interval) do
8378
result = yield(object)
8479
return result if result
85-
rescue ::Errno::ECONNREFUSED => e
86-
raise e
87-
rescue *ignored => last_error
88-
# swallowed
8980
end
90-
91-
sleep interval
81+
rescue ::Errno::ECONNREFUSED => e
82+
raise e
83+
rescue *ignored => last_error # rubocop:disable Lint/HandleExceptions
84+
# swallowed
9285
end
9386

9487
msg = message_for timeout, message
@@ -104,6 +97,17 @@ def message_for(timeout, message)
10497
msg << ", #{message}" if message
10598
msg
10699
end
100+
101+
def run_with_timer(timeout, interval, &block)
102+
if timeout.zero?
103+
block.call # rubocop:disable Performance/RedundantBlockCall
104+
else
105+
Timer.wait timeout do
106+
block.call # rubocop:disable Performance/RedundantBlockCall
107+
sleep interval
108+
end
109+
end
110+
end
107111
end # self
108112
end # module Wait
109113

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Appium
2+
module Core
3+
module Wait
4+
module Timer
5+
class << self
6+
# @private
7+
def wait(timeout, &block)
8+
end_time = current_time + timeout
9+
loop do
10+
yield(block)
11+
break if current_time > end_time
12+
end
13+
end
14+
15+
private
16+
17+
if defined?(Process::CLOCK_MONOTONIC)
18+
def current_time
19+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
20+
end
21+
else
22+
def current_time
23+
::Time.now.to_f
24+
end
25+
end
26+
end
27+
end # module Timer
28+
end # module Wait
29+
end # module Core
30+
end # module Appium

0 commit comments

Comments
 (0)