-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add Retriable #4
Open
ermaker
wants to merge
8
commits into
master
Choose a base branch
from
retriable
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
979b901
Add Retriable
ermaker 02dfd2a
Fix rubocop
ermaker 012cdee
Fix rubocop
ermaker 97dfce2
Fix hound
ermaker 033143f
Use specify
ermaker f531e47
Fix Constant warnings
ermaker 809987f
Add multiple errors support
ermaker 8b87830
Check errors case
ermaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
# The list of classes in Cool module | ||
module Cool | ||
autoload :Retriable, 'cool/retriable' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Cool | ||
# Retriable | ||
module Retriable | ||
def retry_(new_method, on: StandardError, times: 3, &condition) # rubocop:disable Metrics/MethodLength, Metrics/LineLength | ||
condition ||= ->(*) { false } | ||
orig_method = "__retry_orig_#{new_method}".to_sym | ||
alias_method orig_method, new_method | ||
define_method(new_method) do |*args, &block| | ||
(times - 1).times do | ||
retval = | ||
begin | ||
method(orig_method).call(*args, &block) | ||
rescue on | ||
next | ||
end | ||
return retval unless condition.call(retval) | ||
end | ||
return method(orig_method).call(*args, &block) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
RSpec.describe Cool::Retriable do | ||
describe '#retry_' do | ||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [:no, :no, :yes].to_enum | ||
def do_it | ||
ENUM.next | ||
end | ||
retry_(:do_it) { |retval| retval == :no } | ||
end | ||
|
||
expect(klass.new.do_it).to eq(:yes) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [:no, :no, :no].to_enum | ||
def do_it | ||
ENUM.next | ||
end | ||
retry_(:do_it) { |retval| retval == :no } | ||
end | ||
|
||
expect(klass.new.do_it).to eq(:no) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [nil, nil, :yes].to_enum | ||
def do_it | ||
ENUM.next | ||
end | ||
retry_(:do_it, &:nil?) | ||
end | ||
|
||
expect(klass.new.do_it).to eq(:yes) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [[], {}, :yes].to_enum | ||
def do_it | ||
ENUM.next | ||
end | ||
retry_(:do_it, &:empty?) | ||
end | ||
|
||
expect(klass.new.do_it).to eq(:yes) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [:no, :no, :yes].to_enum | ||
def do_it | ||
ENUM.next.tap { |value| raise if value == :no } | ||
end | ||
retry_(:do_it) | ||
end | ||
|
||
expect(klass.new.do_it).to eq(:yes) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [:no, :no, :no].to_enum | ||
def do_it | ||
ENUM.next.tap { |value| raise if value == :no } | ||
end | ||
retry_(:do_it) | ||
end | ||
|
||
expect { klass.new.do_it }.to raise_error(StandardError) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [:no].to_enum | ||
def do_it | ||
ENUM.next.tap { |value| raise if value == :no } | ||
end | ||
retry_(:do_it, times: 1) | ||
end | ||
|
||
expect { klass.new.do_it }.to raise_error(StandardError) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [:error, nil, :yes].to_enum | ||
def do_it | ||
ENUM.next.tap { |value| raise if value == :error } | ||
end | ||
retry_(:do_it, &:nil?) | ||
end | ||
|
||
expect(klass.new.do_it).to eq(:yes) | ||
end | ||
|
||
it 'works' do | ||
retriable = described_class | ||
klass = Class.new do | ||
extend retriable | ||
|
||
ENUM = [:yes, :error].to_enum | ||
def do_it | ||
ENUM.next.tap { |value| raise if value == :error } | ||
end | ||
retry_(:do_it) | ||
end | ||
|
||
expect(klass.new.do_it).to eq(:yes) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,3 +88,5 @@ | |
# as the one that triggered the failure. | ||
Kernel.srand config.seed | ||
end | ||
|
||
require 'cool' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use
yield
instead ofcondition.call
.