-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
201 additions
and
14 deletions.
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
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,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 |
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
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,6 @@ | ||
# frozen_string_literal: true | ||
|
||
TestError = Class.new(StandardError) | ||
ParentError = Class.new(TestError) | ||
ChildError = Class.new(ParentError) | ||
UnexpectedError = Class.new(StandardError) |
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,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 |