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

72 lines
2.1 KiB
Ruby
Raw Normal View History

require_relative 'helper'
require 'sidekiq/scheduled'
class TestScheduling < Sidekiq::Test
describe 'middleware' do
before do
Sidekiq::Client.instance_variable_set(:@default, nil)
2013-05-12 17:25:30 -04:00
@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)
2014-05-13 17:00:20 -04:00
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
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')
@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
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')
@redis.verify
end
2012-11-20 00:31:14 -05:00
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
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
it 'removes the enqueued_at field when scheduling' do
@redis.expect :zadd, true do |key, args|
job = Sidekiq.load_json(args.first.last)
job.key?('created_at') && !job.key?('enqueued_at')
end
assert ScheduledWorker.perform_in(1.month, 'mike')
@redis.verify
end
end
end