Commit d4f65056 authored by Andrejs Cunskis's avatar Andrejs Cunskis

Add ability to pass custom message to repeater

parent ea2d0e11
......@@ -57,16 +57,25 @@ module QA
end
end
def retry_until(max_attempts: 3, reload: false, sleep_interval: 0, raise_on_failure: true)
Support::Retrier.retry_until(max_attempts: max_attempts, reload_page: (reload && self), sleep_interval: sleep_interval, raise_on_failure: raise_on_failure) do
yield
end
end
def retry_on_exception(max_attempts: 3, reload: false, sleep_interval: 0.5)
Support::Retrier.retry_on_exception(max_attempts: max_attempts, reload_page: (reload && self), sleep_interval: sleep_interval) do
yield
end
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,
message: message,
&block
)
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
def scroll_to(selector, text: nil)
......
......@@ -34,7 +34,7 @@ module QA
click_element(:target_namespace_selector_dropdown)
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)
# Make sure import started before waiting for completion
has_no_element?(:import_status_indicator, text: "Not started", wait: 1)
......
......@@ -18,7 +18,8 @@ module QA
sleep_interval: 0,
raise_on_failure: true,
retry_on_exception: false,
log: true
log: true,
message: nil
)
attempts = 0
start = Time.now
......@@ -63,11 +64,14 @@ module QA
unless remaining_attempts?(attempts, max_attempts)
raise(
RetriesExceededError,
"Retry condition not met after #{max_attempts} #{'attempt'.pluralize(max_attempts)}"
"#{message || 'Retry'} failed after #{max_attempts} #{'attempt'.pluralize(max_attempts)}"
)
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
log_completion(log, attempts)
......
......@@ -7,14 +7,15 @@ module QA
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
repeat_until(
max_attempts: max_attempts,
reload_page: reload_page,
sleep_interval: sleep_interval,
retry_on_exception: true,
log: log
log: log,
message: message
) do
result = yield
......@@ -33,7 +34,8 @@ module QA
sleep_interval: 0,
raise_on_failure: true,
retry_on_exception: false,
log: true
log: true,
message: nil
)
# For backwards-compatibility
max_attempts = 3 if max_attempts.nil? && max_duration.nil?
......@@ -46,7 +48,8 @@ module QA
sleep_interval: sleep_interval,
raise_on_failure: raise_on_failure,
retry_on_exception: retry_on_exception,
log: log
log: log,
message: message
) do
result = yield
end
......
......@@ -23,7 +23,7 @@ RSpec.describe QA::Support::Repeater do
context 'when retry_on_exception is not provided (default: false)' do
context 'when max_duration is provided' do
context 'when max duration is reached' do
it 'raises an exception' do
it 'raises an exception with default message' do
expect do
Timecop.freeze do
subject.repeat_until(max_duration: 1) do
......@@ -31,7 +31,20 @@ RSpec.describe QA::Support::Repeater do
false
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
it 'ignores attempts' do
......@@ -70,14 +83,26 @@ RSpec.describe QA::Support::Repeater do
context 'when max_attempts is provided' do
context 'when max_attempts is reached' do
it 'raises an exception' do
it 'raises an exception with default message' do
expect do
Timecop.freeze do
subject.repeat_until(max_attempts: 1) do
false
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
it 'ignores duration' do
......@@ -126,7 +151,7 @@ RSpec.describe QA::Support::Repeater do
false
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
......@@ -141,7 +166,7 @@ RSpec.describe QA::Support::Repeater do
false
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
......@@ -210,7 +235,7 @@ RSpec.describe QA::Support::Repeater do
false
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
......@@ -225,7 +250,7 @@ RSpec.describe QA::Support::Repeater do
false
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
......
......@@ -33,6 +33,12 @@ RSpec.describe QA::Support::Retrier do
subject.retry_until(log: false)
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
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