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

Fix negative DelaySeconds being passed to parameter. #107

Merged
merged 2 commits into from
Dec 13, 2023
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
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
2 changes: 1 addition & 1 deletion lib/generators/aws_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def parse_table_config!
[idx.name, parse_rw_units(idx.name)]
end.to_h

options['table_config'].each do |config, _rw_units|
options['table_config'].each_key do |config|
next if config == 'primary'

gsi = @gsis.select { |idx| idx.name == config }
Expand Down
64 changes: 42 additions & 22 deletions test/active_job/queue_adapters/amazon_sqs_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,49 @@ 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
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
Expand Down
Loading