2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2012-05-25 23:21:42 -04:00
|
|
|
require 'sidekiq/scheduled'
|
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestScheduling < Sidekiq::Test
|
2012-05-25 23:21:42 -04:00
|
|
|
describe 'middleware' do
|
|
|
|
class ScheduledWorker
|
|
|
|
include Sidekiq::Worker
|
2013-09-29 03:58:16 -04:00
|
|
|
sidekiq_options :queue => :custom_queue
|
2012-05-25 23:21:42 -04:00
|
|
|
def perform(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-30 05:31:08 -04:00
|
|
|
# Assume we can pass any class as time to perform_in
|
|
|
|
class TimeDuck
|
|
|
|
def to_f; 42.0 end
|
|
|
|
end
|
|
|
|
|
2015-09-22 15:48:43 -04:00
|
|
|
it 'schedules jobs' do
|
|
|
|
ss = Sidekiq::ScheduledSet.new
|
|
|
|
ss.clear
|
|
|
|
|
|
|
|
assert_equal 0, ss.size
|
|
|
|
|
2012-08-11 14:47:25 -04:00
|
|
|
assert ScheduledWorker.perform_in(600, 'mike')
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_equal 1, ss.size
|
2012-05-25 23:21:42 -04:00
|
|
|
|
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-19 19:25:46 -05:00
|
|
|
assert ScheduledWorker.perform_in(1.month, 'mike')
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_equal 2, ss.size
|
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-19 19:25:46 -05:00
|
|
|
|
2012-08-11 14:47:25 -04:00
|
|
|
assert ScheduledWorker.perform_in(5.days.from_now, 'mike')
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_equal 3, ss.size
|
2012-11-20 00:31:14 -05:00
|
|
|
|
2015-09-23 19:21:25 -04:00
|
|
|
q = Sidekiq::Queue.new("custom_queue")
|
|
|
|
qs = q.size
|
2013-09-29 03:58:16 -04:00
|
|
|
assert ScheduledWorker.perform_in(-300, 'mike')
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_equal 3, ss.size
|
2015-09-23 19:21:25 -04:00
|
|
|
assert_equal qs+1, q.size
|
2013-09-29 03:58:16 -04:00
|
|
|
|
2013-03-24 20:42:43 -04:00
|
|
|
assert Sidekiq::Client.push_bulk('class' => ScheduledWorker, 'args' => [['mike'], ['mike']], 'at' => 600)
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_equal 5, ss.size
|
2016-09-30 05:31:08 -04:00
|
|
|
|
|
|
|
assert ScheduledWorker.perform_in(TimeDuck.new, 'samwise')
|
|
|
|
assert_equal 6, ss.size
|
2012-11-20 00:31:14 -05:00
|
|
|
end
|
2015-06-03 06:45:35 -04:00
|
|
|
|
|
|
|
it 'removes the enqueued_at field when scheduling' do
|
2015-09-22 15:48:43 -04:00
|
|
|
ss = Sidekiq::ScheduledSet.new
|
|
|
|
ss.clear
|
|
|
|
|
2015-06-03 06:45:35 -04:00
|
|
|
assert ScheduledWorker.perform_in(1.month, 'mike')
|
2015-09-22 15:48:43 -04:00
|
|
|
job = ss.first
|
|
|
|
assert job['created_at']
|
|
|
|
refute job['enqueued_at']
|
2015-06-03 06:45:35 -04:00
|
|
|
end
|
2012-05-25 23:21:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|