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

Add Retriable #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/cool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

# The list of classes in Cool module
module Cool
autoload :Retriable, 'cool/retriable'
end
21 changes: 21 additions & 0 deletions lib/cool/retriable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Cool
# Retriable
module Retriable
def retry_(new_method, on: StandardError, times: 3) # rubocop:disable Metrics/MethodLength, Metrics/LineLength
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 block_given? && yield(retval)
end
return method(orig_method).call(*args, &block)
end
end
end
end
220 changes: 220 additions & 0 deletions spec/lib/cool/retriable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
RSpec.describe Cool::Retriable do
describe '#retry_' do
specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [:no, :no, :yes].to_enum
end

def do_it
@enum.next
end
retry_(:do_it) { |retval| retval == :no }
end

expect(klass.new.do_it).to eq(:yes)
end

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [:no, :no, :no].to_enum
end

def do_it
@enum.next
end
retry_(:do_it) { |retval| retval == :no }
end

expect(klass.new.do_it).to eq(:no)
end

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [nil, nil, :yes].to_enum
end

def do_it
@enum.next
end
retry_(:do_it, &:nil?)
end

expect(klass.new.do_it).to eq(:yes)
end

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [[], {}, :yes].to_enum
end

def do_it
@enum.next
end
retry_(:do_it, &:empty?)
end

expect(klass.new.do_it).to eq(:yes)
end

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [:no, :no, :yes].to_enum
end

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

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [:no, :no, :no].to_enum
end

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

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [:no].to_enum
end

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

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [:error, nil, :yes].to_enum
end

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

specify do
retriable = described_class
klass = Class.new do
extend retriable

def initialize
@enum = [:yes, :error].to_enum
end

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

specify do
retriable = described_class
error1 = Class.new(StandardError)
error2 = Class.new(StandardError)
klass = Class.new do
extend retriable

def initialize(error1, error2)
@error1 = error1
@error2 = error2
@enum = [:error1, :error2, :yes].to_enum
end

def do_it
@enum.next.tap do |value|
case value
when :error1 then raise @error1
when :error2 then raise @error2
end
end
end
retry_(:do_it, on: [error1, error2])
end

expect(klass.new(error1, error2).do_it).to eq(:yes)
end

specify do
retriable = described_class
error1 = Class.new(StandardError)
error2 = Class.new(StandardError)
klass = Class.new do
extend retriable

def initialize(error1, error2)
@error1 = error1
@error2 = error2
@enum = [:error1, :error2, :error, :yes].to_enum
end

def do_it
@enum.next.tap do |value|
case value
when :error1 then raise @error1
when :error2 then raise @error2
when :error then raise StandardError
end
end
end
retry_(:do_it, times: 4, on: [error1, error2])
end

expect { klass.new(error1, error2).do_it }.to raise_error(StandardError)
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@
# as the one that triggered the failure.
Kernel.srand config.seed
end

require 'cool'