Skip to content

Commit ddd8624

Browse files
authored
Km/fix timeout error (#82)
* revert timeout loop logic * update changelog * add a timer test
1 parent 34bfa9b commit ddd8624

File tree

4 files changed

+56
-44
lines changed

4 files changed

+56
-44
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
### Enhancements
66

77
### Bug fixes
8+
- Revert timeout logic in `1.4.1`
89

910
### Deprecations
1011

lib/appium_lib_core/common/wait.rb

+19-24
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ def until(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: nil, ig
3232
ignored = Array(ignored || ::Exception)
3333

3434
last_error = nil
35-
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
35+
timer = Wait::Timer.new(timeout)
36+
37+
until timer.timeout?
38+
begin
39+
return yield(object)
40+
rescue ::Errno::ECONNREFUSED => e
41+
raise e
42+
rescue *ignored => last_error # rubocop:disable Lint/HandleExceptions
43+
# swallowed
44+
end
45+
sleep interval
4246
end
4347

4448
msg = message_for timeout, message
@@ -72,16 +76,18 @@ def until_true(timeout: DEFAULT_TIMEOUT, interval: DEFAULT_INTERVAL, message: ni
7276
ignored = Array(ignored || ::Exception)
7377

7478
last_error = nil
79+
timer = Wait::Timer.new(timeout)
7580

76-
begin
77-
run_with_timer(timeout, interval) do
81+
until timer.timeout?
82+
begin
7883
result = yield(object)
7984
return result if result
85+
rescue ::Errno::ECONNREFUSED => e
86+
raise e
87+
rescue *ignored => last_error # rubocop:disable Lint/HandleExceptions
88+
# swallowed
8089
end
81-
rescue ::Errno::ECONNREFUSED => e
82-
raise e
83-
rescue *ignored => last_error # rubocop:disable Lint/HandleExceptions
84-
# swallowed
90+
sleep interval
8591
end
8692

8793
msg = message_for timeout, message
@@ -97,17 +103,6 @@ def message_for(timeout, message)
97103
msg << ", #{message}" if message
98104
msg
99105
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
111106
end # self
112107
end # module Wait
113108

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
module Appium
22
module Core
33
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
4+
class Timer
5+
def initialize(timeout)
6+
@end_time = current_time + timeout
7+
end
148

15-
private
9+
def timeout?
10+
current_time > @end_time
11+
end
1612

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
13+
if defined?(Process::CLOCK_MONOTONIC)
14+
def current_time
15+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
16+
end
17+
else
18+
def current_time
19+
::Time.now.to_f
2520
end
2621
end
27-
end # module Timer
22+
end # class Timer
2823
end # module Wait
2924
end # module Core
3025
end # module Appium

test/unit/common/timer_test.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'test_helper'
2+
3+
class AppiumLibCoreTest
4+
class TimterTest < Minitest::Test
5+
def test_timer
6+
timeout = 0.3
7+
8+
timer = ::Appium::Core::Wait::Timer.new(timeout)
9+
start = timer.current_time
10+
11+
count = 0
12+
until timer.timeout?
13+
count += 1
14+
sleep 0.1
15+
end
16+
17+
assert_equal 3, count
18+
assert timeout <= timer.current_time - start
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)