Commit fea1ae37 authored by Andrejs Cunskis's avatar Andrejs Cunskis

Merge branch 'acunskis-repeater-custom-error' into 'master'

E2E: Add ability to pass custom message to repeater

See merge request gitlab-org/gitlab!74371
parents 915bfa4f d4f65056
...@@ -57,16 +57,25 @@ module QA ...@@ -57,16 +57,25 @@ module QA
end end
end end
def retry_until(max_attempts: 3, reload: false, sleep_interval: 0, raise_on_failure: true) def retry_until(max_attempts: 3, reload: false, sleep_interval: 0, raise_on_failure: true, message: nil, &block)
Support::Retrier.retry_until(max_attempts: max_attempts, reload_page: (reload && self), sleep_interval: sleep_interval, raise_on_failure: raise_on_failure) do Support::Retrier.retry_until(
yield max_attempts: max_attempts,
end reload_page: (reload && self),
end sleep_interval: sleep_interval,
raise_on_failure: raise_on_failure,
def retry_on_exception(max_attempts: 3, reload: false, sleep_interval: 0.5) message: message,
Support::Retrier.retry_on_exception(max_attempts: max_attempts, reload_page: (reload && self), sleep_interval: sleep_interval) do &block
yield )
end end
def retry_on_exception(max_attempts: 3, reload: false, sleep_interval: 0.5, message: nil, &block)
Support::Retrier.retry_on_exception(
max_attempts: max_attempts,
reload_page: (reload && self),
sleep_interval: sleep_interval,
message: message,
&block
)
end end
def scroll_to(selector, text: nil) def scroll_to(selector, text: nil)
......
...@@ -34,7 +34,7 @@ module QA ...@@ -34,7 +34,7 @@ module QA
click_element(:target_namespace_selector_dropdown) click_element(:target_namespace_selector_dropdown)
click_element(:target_group_dropdown_item, group_name: target_group_name) click_element(:target_group_dropdown_item, group_name: target_group_name)
retry_until do retry_until(message: "Triggering import") do
click_element(:import_group_button) click_element(:import_group_button)
# Make sure import started before waiting for completion # Make sure import started before waiting for completion
has_no_element?(:import_status_indicator, text: "Not started", wait: 1) has_no_element?(:import_status_indicator, text: "Not started", wait: 1)
......
...@@ -18,7 +18,8 @@ module QA ...@@ -18,7 +18,8 @@ module QA
sleep_interval: 0, sleep_interval: 0,
raise_on_failure: true, raise_on_failure: true,
retry_on_exception: false, retry_on_exception: false,
log: true log: true,
message: nil
) )
attempts = 0 attempts = 0
start = Time.now start = Time.now
...@@ -63,11 +64,14 @@ module QA ...@@ -63,11 +64,14 @@ module QA
unless remaining_attempts?(attempts, max_attempts) unless remaining_attempts?(attempts, max_attempts)
raise( raise(
RetriesExceededError, RetriesExceededError,
"Retry condition not met after #{max_attempts} #{'attempt'.pluralize(max_attempts)}" "#{message || 'Retry'} failed after #{max_attempts} #{'attempt'.pluralize(max_attempts)}"
) )
end end
raise WaitExceededError, "Wait condition not met after #{max_duration} #{'second'.pluralize(max_duration)}" raise(
WaitExceededError,
"#{message || 'Wait'} failed after #{max_duration} #{'second'.pluralize(max_duration)}"
)
end end
log_completion(log, attempts) log_completion(log, attempts)
......
...@@ -7,14 +7,15 @@ module QA ...@@ -7,14 +7,15 @@ module QA
module_function module_function
def retry_on_exception(max_attempts: 3, reload_page: nil, sleep_interval: 0.5, log: true) def retry_on_exception(max_attempts: 3, reload_page: nil, sleep_interval: 0.5, log: true, message: nil)
result = nil result = nil
repeat_until( repeat_until(
max_attempts: max_attempts, max_attempts: max_attempts,
reload_page: reload_page, reload_page: reload_page,
sleep_interval: sleep_interval, sleep_interval: sleep_interval,
retry_on_exception: true, retry_on_exception: true,
log: log log: log,
message: message
) do ) do
result = yield result = yield
...@@ -33,7 +34,8 @@ module QA ...@@ -33,7 +34,8 @@ module QA
sleep_interval: 0, sleep_interval: 0,
raise_on_failure: true, raise_on_failure: true,
retry_on_exception: false, retry_on_exception: false,
log: true log: true,
message: nil
) )
# For backwards-compatibility # For backwards-compatibility
max_attempts = 3 if max_attempts.nil? && max_duration.nil? max_attempts = 3 if max_attempts.nil? && max_duration.nil?
...@@ -46,7 +48,8 @@ module QA ...@@ -46,7 +48,8 @@ module QA
sleep_interval: sleep_interval, sleep_interval: sleep_interval,
raise_on_failure: raise_on_failure, raise_on_failure: raise_on_failure,
retry_on_exception: retry_on_exception, retry_on_exception: retry_on_exception,
log: log log: log,
message: message
) do ) do
result = yield result = yield
end end
......
...@@ -23,7 +23,7 @@ RSpec.describe QA::Support::Repeater do ...@@ -23,7 +23,7 @@ RSpec.describe QA::Support::Repeater do
context 'when retry_on_exception is not provided (default: false)' do context 'when retry_on_exception is not provided (default: false)' do
context 'when max_duration is provided' do context 'when max_duration is provided' do
context 'when max duration is reached' do context 'when max duration is reached' do
it 'raises an exception' do it 'raises an exception with default message' do
expect do expect do
Timecop.freeze do Timecop.freeze do
subject.repeat_until(max_duration: 1) do subject.repeat_until(max_duration: 1) do
...@@ -31,7 +31,20 @@ RSpec.describe QA::Support::Repeater do ...@@ -31,7 +31,20 @@ RSpec.describe QA::Support::Repeater do
false false
end end
end end
end.to raise_error(QA::Support::Repeater::WaitExceededError, "Wait condition not met after 1 second") end.to raise_error(QA::Support::Repeater::WaitExceededError, "Wait failed after 1 second")
end
it 'raises an exception with custom message' do
message = 'Some custom action'
expect do
Timecop.freeze do
subject.repeat_until(max_duration: 1, message: message) do
Timecop.travel(2)
false
end
end
end.to raise_error(QA::Support::Repeater::WaitExceededError, "#{message} failed after 1 second")
end end
it 'ignores attempts' do it 'ignores attempts' do
...@@ -70,14 +83,26 @@ RSpec.describe QA::Support::Repeater do ...@@ -70,14 +83,26 @@ RSpec.describe QA::Support::Repeater do
context 'when max_attempts is provided' do context 'when max_attempts is provided' do
context 'when max_attempts is reached' do context 'when max_attempts is reached' do
it 'raises an exception' do it 'raises an exception with default message' do
expect do expect do
Timecop.freeze do Timecop.freeze do
subject.repeat_until(max_attempts: 1) do subject.repeat_until(max_attempts: 1) do
false false
end end
end end
end.to raise_error(QA::Support::Repeater::RetriesExceededError, "Retry condition not met after 1 attempt") end.to raise_error(QA::Support::Repeater::RetriesExceededError, "Retry failed after 1 attempt")
end
it 'raises an exception with custom message' do
message = 'Some custom action'
expect do
Timecop.freeze do
subject.repeat_until(max_attempts: 1, message: message) do
false
end
end
end.to raise_error(QA::Support::Repeater::RetriesExceededError, "#{message} failed after 1 attempt")
end end
it 'ignores duration' do it 'ignores duration' do
...@@ -126,7 +151,7 @@ RSpec.describe QA::Support::Repeater do ...@@ -126,7 +151,7 @@ RSpec.describe QA::Support::Repeater do
false false
end end
end end
end.to raise_error(QA::Support::Repeater::RetriesExceededError, "Retry condition not met after 1 attempt") end.to raise_error(QA::Support::Repeater::RetriesExceededError, "Retry failed after 1 attempt")
end end
end end
...@@ -141,7 +166,7 @@ RSpec.describe QA::Support::Repeater do ...@@ -141,7 +166,7 @@ RSpec.describe QA::Support::Repeater do
false false
end end
end end
end.to raise_error(QA::Support::Repeater::WaitExceededError, "Wait condition not met after 1 second") end.to raise_error(QA::Support::Repeater::WaitExceededError, "Wait failed after 1 second")
end end
end end
end end
...@@ -210,7 +235,7 @@ RSpec.describe QA::Support::Repeater do ...@@ -210,7 +235,7 @@ RSpec.describe QA::Support::Repeater do
false false
end end
end end
end.to raise_error(QA::Support::Repeater::RetriesExceededError, "Retry condition not met after 1 attempt") end.to raise_error(QA::Support::Repeater::RetriesExceededError, "Retry failed after 1 attempt")
end end
end end
...@@ -225,7 +250,7 @@ RSpec.describe QA::Support::Repeater do ...@@ -225,7 +250,7 @@ RSpec.describe QA::Support::Repeater do
false false
end end
end end
end.to raise_error(QA::Support::Repeater::WaitExceededError, "Wait condition not met after 1 second") end.to raise_error(QA::Support::Repeater::WaitExceededError, "Wait failed after 1 second")
end end
end end
end end
......
...@@ -33,6 +33,12 @@ RSpec.describe QA::Support::Retrier do ...@@ -33,6 +33,12 @@ RSpec.describe QA::Support::Retrier do
subject.retry_until(log: false) subject.retry_until(log: false)
end end
it 'sets custom error message' do
expect(subject).to receive(:repeat_until).with(hash_including(message: 'Custom message'))
subject.retry_until(message: 'Custom message')
end
end end
describe '.retry_on_exception' do describe '.retry_on_exception' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment