Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CAT-1713) Add retry to flaky tests #1313

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions spec/acceptance/remove_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
it 'saves the setting' do
# Force the command to run if not already
subject.exit_status
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
retry_on_error_matching(60, 5) do
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
end

actual_content = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read)
expect(actual_content).to eq(new_content)
Expand All @@ -19,7 +21,9 @@
it 'saves the setting' do
# Force the command to run if not already
subject.exit_status
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
retry_on_error_matching(60, 5) do
expect(File).to exist(ENV.fetch('PDK_ANSWER_FILE', nil))
end

actual_content_raw = File.open(ENV.fetch('PDK_ANSWER_FILE', nil), 'rb:utf-8', &:read)
actual_json_content = JSON.parse(actual_content_raw)
Expand Down
24 changes: 24 additions & 0 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ def default_installed_bin_dir
end
end

# This method allows a block to be passed in and if an exception is raised
# that matches the 'error_matcher' matcher, the block will wait a set number
# of seconds before retrying.
# Params:
# - max_retry_count - Max number of retries
# - retry_wait_interval_secs - Number of seconds to wait before retry
# - error_matcher - Matcher which the exception raised must match to allow retry
# Example Usage:
# retry_on_error_matching(3, 5, /OpenGPG Error/) do
# apply_manifest(pp, :catch_failures => true)
# end
def retry_on_error_matching(max_retry_count = 3, retry_wait_interval_secs = 5, error_matcher = nil)
try = 0
begin
try += 1
yield
rescue StandardError => e
raise unless try < max_retry_count && (error_matcher.nil? || e.message =~ error_matcher)
jordanbreen28 marked this conversation as resolved.
Show resolved Hide resolved

sleep retry_wait_interval_secs
retry
end
end

module Specinfra
module Backend
class Cmd
Expand Down
Loading