-
Notifications
You must be signed in to change notification settings - Fork 48
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
Accept Proc in the :on
argument
#55
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,17 +60,33 @@ def retriable(opts = {}) | |
return Timeout.timeout(timeout) { return yield(try) } if timeout | ||
return yield(try) | ||
rescue *[*exception_list] => exception | ||
if on.is_a?(Hash) | ||
raise unless exception_list.any? do |e| | ||
exception.is_a?(e) && ([*on[e]].empty? || [*on[e]].any? { |pattern| exception.message =~ pattern }) | ||
end | ||
end | ||
|
||
interval = intervals[index] | ||
raise unless matched_exception?(on, exception, try, elapsed_time.call, interval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [88/80] |
||
|
||
on_retry.call(exception, try, elapsed_time.call, interval) if on_retry | ||
raise if try >= tries || (elapsed_time.call + interval) > max_elapsed_time | ||
sleep interval if sleep_disabled != true | ||
end | ||
end | ||
end | ||
|
||
def matched_exception?(on, exception, *proc_args) | ||
return true unless on.is_a?(Hash) | ||
|
||
on.any? do |expected_exception, matchers| | ||
next false unless exception.is_a?(expected_exception) | ||
next true if matchers.nil? | ||
|
||
Array(matchers).any? do |matcher| | ||
if matcher.is_a?(Regexp) | ||
exception.message =~ matcher | ||
elsif matcher.is_a?(Proc) | ||
matcher.call(exception, *proc_args) | ||
else | ||
raise ArgumentError, "Exception hash values must be Proc or Regexp" | ||
end | ||
end | ||
end | ||
end | ||
private_class_method :matched_exception? | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,6 +207,34 @@ def increment_tries_with_exception(exception_class = nil) | |
end | ||
end | ||
|
||
it "#retriable retries with a hash exception where the value is a proc that returns true" do | ||
matcher = lambda do |e, _try, _elapsed_time, _next_interval| | ||
e.message == "something went wrong" | ||
end | ||
tries = 0 | ||
expect { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/BlockDelimiters: Avoid using {...} for multi-line blocks. |
||
subject.retriable on: { NonStandardError => matcher }, tries: 2 do | ||
tries += 1 | ||
raise NonStandardError, "something went wrong" | ||
end | ||
}.to raise_error NonStandardError | ||
|
||
expect(tries).to eq 2 | ||
end | ||
|
||
it "#retriable does not retry with a hash exception where the value is a proc that returns false" do | ||
matcher = ->(e, *_args) { e.message == "something went wrong" } | ||
tries = 0 | ||
expect { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style/BlockDelimiters: Avoid using {...} for multi-line blocks. |
||
subject.retriable on: { NonStandardError => matcher }, tries: 2 do | ||
tries += 1 | ||
raise NonStandardError, "not a match" | ||
end | ||
}.to raise_error NonStandardError | ||
|
||
expect(tries).to eq 1 | ||
end | ||
|
||
it "runs for a max elapsed time of 2 seconds" do | ||
described_class.configure { |c| c.sleep_disabled = false } | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is all sort of duplicative of the docs above... it's probably better to just link back there than to duplicate the explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just staying consistent with the style of this documentation. Above there are other expanded versions of what's on those bullet points as well.