Skip to content

Commit

Permalink
Validate UberTask::RetryTask's wait value (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzaakiirr authored Jul 10, 2024
1 parent d8819fe commit 1c3efc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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

0 comments on commit 1c3efc4

Please sign in to comment.