From 1c3efc422d436078a0361a3e4b0352e4f8a9b960 Mon Sep 17 00:00:00 2001 From: Zakir Date: Wed, 10 Jul 2024 17:11:55 +0300 Subject: [PATCH] Validate UberTask::RetryTask's wait value (#17) --- lib/uber_task/retry_task.rb | 11 ++++++++++- spec/uber_task/retry_task_spec.rb | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/uber_task/retry_task.rb b/lib/uber_task/retry_task.rb index d80efa2..29d927e 100644 --- a/lib/uber_task/retry_task.rb +++ b/lib/uber_task/retry_task.rb @@ -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 diff --git a/spec/uber_task/retry_task_spec.rb b/spec/uber_task/retry_task_spec.rb index 6feb91f..4e2a945 100644 --- a/spec/uber_task/retry_task_spec.rb +++ b/spec/uber_task/retry_task_spec.rb @@ -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