Skip to content

Commit ab2a697

Browse files
committed
[API] Test Runner: Retry actions when server returns timeout or is unavailable
1 parent 2217ba3 commit ab2a697

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

elasticsearch-api/api-spec-testing/test_file.rb

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,7 @@ def setup
118118
return unless @setup
119119

120120
actions = @setup['setup'].select { |action| action['do'] }.map { |action| Action.new(action['do']) }
121-
count = 0
122-
loop do
123-
actions.delete_if do |action|
124-
begin
125-
action.execute(client)
126-
true
127-
rescue Elastic::Transport::Transport::Errors::ServiceUnavailable => e
128-
# The action sometimes gets the cluster in a recovering state, so we
129-
# retry a few times and then raise an exception if it's still
130-
# happening
131-
count += 1
132-
raise e if count > 9
133-
134-
false
135-
end
136-
end
137-
break if actions.empty?
138-
end
139-
121+
run_actions_and_retry(actions)
140122
self
141123
end
142124

@@ -154,9 +136,39 @@ def teardown
154136
return unless @teardown
155137

156138
actions = @teardown['teardown'].select { |action| action['do'] }.map { |action| Action.new(action['do']) }
157-
actions.each { |action| action.execute(client) }
139+
run_actions_and_retry(actions)
158140
self
159141
end
142+
143+
# Helper function to run actions. If the server returns an error, give it some time and retry
144+
# for a few times.
145+
def run_actions_and_retry(actions)
146+
count = 0
147+
loop do
148+
actions.delete_if do |action|
149+
begin
150+
action.execute(client)
151+
true
152+
rescue Elastic::Transport::Transport::Errors::RequestTimeout,
153+
Elastic::Transport::Transport::Errors::ServiceUnavailable => e
154+
# The action sometimes gets the cluster in a recovering state, so we
155+
# retry a few times and then raise an exception if it's still
156+
# happening
157+
count += 1
158+
sleep 10
159+
Elasticsearch::RestAPIYAMLTests::Logging.logger.debug(
160+
"The server responded with an #{e.class} error. Retrying action - (#{count})"
161+
)
162+
raise e if count > 11
163+
164+
false
165+
end
166+
end
167+
break if actions.empty?
168+
end
169+
170+
end
171+
160172
end
161173
end
162174
end

elasticsearch-api/spec/elasticsearch/api/rest_api_yaml_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
require 'rest_api_tests_helper'
2020

2121
describe 'Rest API YAML tests' do
22+
LOGGER = Logger.new($stdout)
2223
if REST_API_YAML_FILES.empty?
23-
logger = Logger.new($stdout)
24-
logger.error 'No test files found!'
25-
logger.info 'Use rake rake elasticsearch:download_artifacts in the root directory of the project to download the test artifacts.'
24+
LOGGER.error 'No test files found!'
25+
LOGGER.info 'Use rake rake elasticsearch:download_artifacts in the root directory of the project to download the test artifacts.'
2626
exit 1
2727
end
2828

@@ -33,8 +33,7 @@
3333
rescue SkipTestsException => _e
3434
# If the test file has a `skip` at the top level that applies to this
3535
# version of Elasticsearch, continue with the next text.
36-
logger = Logger.new($stdout)
37-
logger.info "Skipping #{file} due to 'skip all'."
36+
LOGGER.info "Skipping #{file} due to 'skip all'."
3837
next
3938
end
4039

@@ -153,4 +152,3 @@
153152
end
154153
end
155154
end
156-

0 commit comments

Comments
 (0)