1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/test/test_scheduling.rb
David Stosik a2dd36c6d1 MyWorker.perform_in(1.month) does not always schedule job in one month.
At the moment, `MyWorker.perform_in(1.month)` always schedules a job in 30
days. When added to a date, `1.month` will add 1 to the date's month count,
which means that it will add 28, 29, 30, or 31 days depending on the month and
year.
When I call `MyWorker.perform_in(1.month)`, I would expect the job to be
scheduled next month, same day of the month, all the time. At the moment, it is
true only four months in the year.
My pull request tries to fix this by converting the interval object to a Float
at the last possible moment.
Plaese note that the test I wrote will fail only during months that do not have
30 days. Ideally, I would add a dependency to Timecop and freeze time to any
day in a month of 28, 29 or 31 days. This could also avoid using
`#assert_in_delta`, in favour of `#assert_equal`.

Feel free to read my blog post [Rails' `1.month` has a variable
length](http://dstosik.github.io/rails/2015/02/19/rails-1month-variable-length/)
for more details.
2015-02-20 09:36:32 +09:00

62 lines
1.8 KiB
Ruby

require_relative 'helper'
require 'sidekiq/scheduled'
class TestScheduling < Sidekiq::Test
describe 'middleware' do
before do
Sidekiq::Client.instance_variable_set(:@default, nil)
@redis = Minitest::Mock.new
# Ugh, this is terrible.
Sidekiq.instance_variable_set(:@redis, @redis)
def @redis.multi; [yield] * 2 if block_given?; end
def @redis.with; yield self; end
end
after do
Sidekiq::Client.instance_variable_set(:@default, nil)
Sidekiq.instance_variable_set(:@redis, REDIS)
end
class ScheduledWorker
include Sidekiq::Worker
sidekiq_options :queue => :custom_queue
def perform(x)
end
end
it 'schedules a job via interval' do
@redis.expect :zadd, true, ['schedule', Array]
assert ScheduledWorker.perform_in(600, 'mike')
@redis.verify
end
it 'schedules a job in one month' do
@redis.expect :zadd, true do |key, args|
assert_equal 'schedule', key
assert_in_delta 1.month.since.to_f, args[0][0].to_f, 1
end
assert ScheduledWorker.perform_in(1.month, 'mike')
@redis.verify
end
it 'schedules a job via timestamp' do
@redis.expect :zadd, true, ['schedule', Array]
assert ScheduledWorker.perform_in(5.days.from_now, 'mike')
@redis.verify
end
it 'schedules job right away on negative timestamp/interval' do
@redis.expect :sadd, true, ['queues', 'custom_queue']
@redis.expect :lpush, true, ['queue:custom_queue', Array]
assert ScheduledWorker.perform_in(-300, 'mike')
@redis.verify
end
it 'schedules multiple jobs at once' do
@redis.expect :zadd, true, ['schedule', Array]
assert Sidekiq::Client.push_bulk('class' => ScheduledWorker, 'args' => [['mike'], ['mike']], 'at' => 600)
@redis.verify
end
end
end