Skip to content

Commit

Permalink
Fix negative delay.
Browse files Browse the repository at this point in the history
Issue: aws#106
  • Loading branch information
Keito Kira committed Dec 12, 2023
1 parent 6f89100 commit 3fffc4a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
2 changes: 2 additions & 0 deletions lib/active_job/queue_adapters/amazon_sqs_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def enqueue(job)

def enqueue_at(job, timestamp)
delay = (timestamp - Time.now.to_f).floor

delay = 0 if delay.negative?
raise ArgumentError, 'Unable to queue a job with a delay great than 15 minutes' if delay > 15.minutes

_enqueue(job, nil, delay_seconds: delay)
Expand Down
66 changes: 43 additions & 23 deletions test/active_job/queue_adapters/amazon_sqs_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,30 +144,50 @@ module QueueAdapters
end
end

it 'enqueues delayed jobs' do
t1 = Time.now
allow(Time).to receive(:now).and_return t1

expect(client).to receive(:send_message).with(
{
queue_url: 'https://queue-url',
delay_seconds: 60,
message_body: instance_of(String),
message_attributes: instance_of(Hash)
}
)

TestJob.set(wait: 1.minute).perform_later('test')
sleep(0.2)
end
describe 'with queue delay' do
it 'enqueues jobs with proper delay' do
t1 = Time.now
allow(Time).to receive(:now).and_return t1

expect(client).to receive(:send_message).with(
{
queue_url: 'https://queue-url',
delay_seconds: 60,
message_body: instance_of(String),
message_attributes: instance_of(Hash)
}
)

TestJob.set(wait: 1.minute).perform_later('test')
sleep(0.2)
end

it 'raises an error when job delay is great than SQS support' do
t1 = Time.now
allow(Time).to receive(:now).and_return t1
expect do
TestJob.set(wait: 1.day).perform_later('test')
end.to raise_error ArgumentError
end
it 'enqueues jobs with zero or negative delay' do
t1 = Time.now
allow(Time).to receive(:now).and_return t1

expect(client).to receive(:send_message).with(
{
queue_url: 'https://queue-url',
delay_seconds: 0,
message_body: instance_of(String),
message_attributes: instance_of(Hash)
}
).twice

TestJob.set(wait: 0).perform_later('test')
TestJob.set(wait: -1).perform_later('test')
sleep(0.2)
end

it 'raises an error when job delay is great than SQS support' do
t1 = Time.now
allow(Time).to receive(:now).and_return t1
expect do
TestJob.set(wait: 1.day).perform_later('test')
end.to raise_error ArgumentError
end
end
end
end
end

0 comments on commit 3fffc4a

Please sign in to comment.