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

Validate UberTask::RetryTask's wait value #17

Merged
merged 1 commit into from
Jul 10, 2024
Merged
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
11 changes: 10 additions & 1 deletion lib/uber_task/retry_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ class RetryTask < Exception # rubocop:disable Lint/InheritException
attr_accessor :reason,
:wait

def initialize(reason: nil, wait: nil)
def initialize(reason: nil, wait: 0)
validate_wait_value!(wait)

super('Requested to retry the task.')
@reason = reason
@wait = wait
end

private

def validate_wait_value!(value)
raise ArgumentError, '`wait` is not numberic' unless value.is_a?(Numeric)
raise ArgumentError, '`wait` cannot be negative' if value.negative?
end
end
end
18 changes: 18 additions & 0 deletions spec/uber_task/retry_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,25 @@
expect(exception.wait).to eq(42)
end

it 'has 0 as a default wait attribute value' do
expect(described_class.new.wait).to eq(0)
end

it 'has a message' do
expect(exception.message).to eq('Requested to retry the task.')
end

describe 'wait attribute validations' do
it 'does not allow non-numeric value' do
expect do
described_class.new(wait: 'string')
end.to raise_error(ArgumentError, '`wait` is not numberic')
end

it 'does not allow negative value' do
expect do
described_class.new(wait: -1)
end.to raise_error(ArgumentError, '`wait` cannot be negative')
end
end
end