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
|
|
|
|
before do
|
2014-03-24 13:20:16 -04:00
|
|
|
Sidekiq::Client.instance_variable_set(:@default, nil)
|
2013-05-12 17:25:30 -04:00
|
|
|
@redis = Minitest::Mock.new
|
2012-05-25 23:21:42 -04:00
|
|
|
# Ugh, this is terrible.
|
|
|
|
Sidekiq.instance_variable_set(:@redis, @redis)
|
2013-09-29 03:58:16 -04:00
|
|
|
def @redis.multi; [yield] * 2 if block_given?; end
|
2012-05-25 23:21:42 -04:00
|
|
|
def @redis.with; yield self; end
|
|
|
|
end
|
|
|
|
|
2014-03-24 13:20:16 -04:00
|
|
|
after do
|
|
|
|
Sidekiq::Client.instance_variable_set(:@default, nil)
|
2014-05-13 17:00:20 -04:00
|
|
|
Sidekiq.instance_variable_set(:@redis, REDIS)
|
2014-03-24 13:20:16 -04:00
|
|
|
end
|
|
|
|
|
2012-05-25 23:21:42 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
it 'schedules a job via interval' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :zadd, true, ['schedule', Array]
|
2012-08-11 14:47:25 -04:00
|
|
|
assert ScheduledWorker.perform_in(600, 'mike')
|
2012-05-25 23:21:42 -04:00
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'schedules a job via timestamp' do
|
2013-03-24 20:42:43 -04:00
|
|
|
@redis.expect :zadd, true, ['schedule', Array]
|
2012-08-11 14:47:25 -04:00
|
|
|
assert ScheduledWorker.perform_in(5.days.from_now, 'mike')
|
2012-05-25 23:21:42 -04:00
|
|
|
@redis.verify
|
|
|
|
end
|
2012-11-20 00:31:14 -05:00
|
|
|
|
2013-09-29 03:58:16 -04:00
|
|
|
it 'schedules job right away on negative timestamp/interval' do
|
2013-11-14 22:12:27 -05:00
|
|
|
@redis.expect :sadd, true, ['queues', 'custom_queue']
|
2013-09-29 03:58:16 -04:00
|
|
|
@redis.expect :lpush, true, ['queue:custom_queue', Array]
|
|
|
|
assert ScheduledWorker.perform_in(-300, 'mike')
|
|
|
|
@redis.verify
|
|
|
|
end
|
|
|
|
|
2012-11-20 00:31:14 -05:00
|
|
|
it 'schedules multiple jobs at once' do
|
|
|
|
@redis.expect :zadd, true, ['schedule', Array]
|
2013-03-24 20:42:43 -04:00
|
|
|
assert Sidekiq::Client.push_bulk('class' => ScheduledWorker, 'args' => [['mike'], ['mike']], 'at' => 600)
|
2012-11-20 00:31:14 -05:00
|
|
|
@redis.verify
|
|
|
|
end
|
2012-05-25 23:21:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|