Skip to content

Commit

Permalink
adds more specs and enrich dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
moofkit committed Jan 16, 2024
1 parent baef87e commit e241ac1
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 14 deletions.
46 changes: 33 additions & 13 deletions lib/sidekiq/rescue/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,46 @@ module ClassMethods
# @param limit [Integer] The maximum number of retries.
# @return [void]
# @raise [ArgumentError] if error is not a StandardError
# @raise [ArgumentError] if error is not an array of StandardError
# @raise [ArgumentError] if delay is not an Integer or Float
# @raise [ArgumentError] if limit is not an Integer
# @example
# sidekiq_rescue NetworkError, delay: 60, limit: 10
def sidekiq_rescue(error, **options)
def sidekiq_rescue(error, delay: nil, limit: nil)
validate_error_argument(error)
validate_delay_argument(delay)
validate_limit_argument(limit)

self.sidekiq_rescue_options = {
error: error,
delay: delay || Sidekiq::Rescue::DEFAULT_DELAY,
limit: limit || Sidekiq::Rescue::DEFAULT_LIMIT
}
end

private

def validate_error_argument(error)
error_arg_valid = if error.is_a?(Array)
error.all? { _1 < StandardError }
error.all? { |e| e < StandardError }
else
error < StandardError
end
unless error_arg_valid
raise ArgumentError,
"error must be an ancestor of StandardError or an array of ancestors of StandardError"
end
raise ArgumentError, "delay must be integer" if options[:delay] && !options[:delay].is_a?(Integer)
raise ArgumentError, "limit must be integer" if options[:limit] && !options[:limit].is_a?(Integer)
return if error_arg_valid

self.sidekiq_rescue_options = {
error: error,
delay: Sidekiq::Rescue::DEFAULT_DELAY,
limit: Sidekiq::Rescue::DEFAULT_LIMIT
}.merge(options)
raise ArgumentError,
"error must be an ancestor of StandardError or an array of ancestors of StandardError"
end

def validate_delay_argument(delay)
return unless delay && !delay.is_a?(Integer) && !delay.is_a?(Float)

raise ArgumentError,
"delay must be integer or float"
end

def validate_limit_argument(limit)
raise ArgumentError, "limit must be integer" if limit && !limit.is_a?(Integer)
end
end
end
Expand Down
88 changes: 88 additions & 0 deletions spec/sidekiq/rescue/dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

RSpec.describe Sidekiq::Rescue::DSL do
let(:job_class) do
Class.new do
include Sidekiq::Job
include Sidekiq::Rescue::DSL
end
end

def define_dsl(&block)
job_class.instance_eval(&block)
end

describe "#sidekiq_rescue" do
it "sets error and default options" do
define_dsl { sidekiq_rescue TestError }

expect(job_class.sidekiq_rescue_options).to eq(error: TestError, delay: 60, limit: 10)
end

it "sets the error classes" do
define_dsl { sidekiq_rescue [TestError, ParentError, ChildError] }

expect(job_class.sidekiq_rescue_options[:error]).to eq([TestError, ParentError, ChildError])
end

it "sets the delay" do
define_dsl { sidekiq_rescue TestError, delay: 10 }

expect(job_class.sidekiq_rescue_options[:delay]).to eq(10)
end

it "sets the limit" do
define_dsl { sidekiq_rescue TestError, limit: 5 }

expect(job_class.sidekiq_rescue_options[:limit]).to eq(5)
end

it "raises ArgumentError if there are no arguments" do
expect do
define_dsl do
sidekiq_rescue
end
end.to raise_error(ArgumentError, "wrong number of arguments (given 0, expected 1)")
end

it "raises ArgumentError if there are unknown options" do
expect do
define_dsl do
sidekiq_rescue TestError, unknown: "option"
end
end.to raise_error(ArgumentError, "unknown keyword: :unknown")
end

it "raises ArgumentError if error is not a StandardError child" do
exception_class = Class.new(Exception) # rubocop:disable Lint/InheritException

expect { define_dsl { sidekiq_rescue exception_class } }.to raise_error(
ArgumentError,
"error must be an ancestor of StandardError or an array of ancestors of StandardError"
)
end

it "raises ArgumentError if error is not an array of StandardError children" do
klass = Class.new

expect { define_dsl { sidekiq_rescue [TestError, klass] } }.to raise_error(
ArgumentError,
"error must be an ancestor of StandardError or an array of ancestors of StandardError"
)
end

it "raises ArgumentError if delay is not an integer or float" do
expect { define_dsl { sidekiq_rescue TestError, delay: "60" } }.to raise_error(
ArgumentError,
"delay must be integer or float"
)
end

it "raises ArgumentError if limit is not an integer" do
expect { define_dsl { sidekiq_rescue TestError, limit: "10" } }.to raise_error(
ArgumentError,
"limit must be integer"
)
end
end
end
8 changes: 7 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@
c.syntax = :expect
end

Sidekiq.default_configuration.logger = nil unless ENV["LOG"]
if ENV["LOG"].nil?
if defined?(Sidekiq::MAJOR) && Sidekiq::MAJOR >= 7
Sidekiq.default_configuration.logger = nil
else
Sidekiq.logger = nil
end
end
end
6 changes: 6 additions & 0 deletions spec/support/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

TestError = Class.new(StandardError)
ParentError = Class.new(TestError)
ChildError = Class.new(ParentError)
UnexpectedError = Class.new(StandardError)
67 changes: 67 additions & 0 deletions spec/support/jobs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

class WithTestErrorJob
include Sidekiq::Job
include Sidekiq::Rescue::DSL

sidekiq_rescue TestError

def perform(*)
raise TestError
end
end

class WithTestErrorWithoutResqueJob
include Sidekiq::Job
include Sidekiq::Rescue::DSL

sidekiq_rescue TestError

def perform(*)
raise TestError
end
end

class WithParentErrorJob
include Sidekiq::Job
include Sidekiq::Rescue::DSL

sidekiq_rescue ParentError

def perform(*)
raise ParentError
end
end

class WithChildErrorJob
include Sidekiq::Job
include Sidekiq::Rescue::DSL

sidekiq_rescue ChildError

def perform(*)
raise ChildError
end
end

class WithAllErrorJob
include Sidekiq::Job
include Sidekiq::Rescue::DSL

sidekiq_rescue [TestError, ParentError, ChildError]

def perform(*)
raise [TestError, ParentError, ChildError].sample
end
end

class WithUnexpectedErrorJob
include Sidekiq::Job
include Sidekiq::Rescue::DSL

sidekiq_rescue TestError

def perform(*)
raise UnexpectedError
end
end

0 comments on commit e241ac1

Please sign in to comment.