2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2013-04-10 20:38:36 -04:00
|
|
|
require 'sidekiq/scheduled'
|
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestScheduled < Sidekiq::Test
|
2013-04-10 20:38:36 -04:00
|
|
|
class ScheduledWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(x)
|
|
|
|
end
|
|
|
|
end
|
2013-05-10 23:43:53 -04:00
|
|
|
|
2013-04-10 20:38:36 -04:00
|
|
|
describe 'poller' do
|
|
|
|
before do
|
|
|
|
Sidekiq.redis = REDIS
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.flushdb
|
|
|
|
end
|
2013-05-24 08:22:24 -04:00
|
|
|
|
|
|
|
@error_1 = { 'class' => ScheduledWorker.name, 'args' => [0], 'queue' => 'queue_1' }
|
|
|
|
@error_2 = { 'class' => ScheduledWorker.name, 'args' => [1], 'queue' => 'queue_2' }
|
|
|
|
@error_3 = { 'class' => ScheduledWorker.name, 'args' => [2], 'queue' => 'queue_3' }
|
|
|
|
@future_1 = { 'class' => ScheduledWorker.name, 'args' => [3], 'queue' => 'queue_4' }
|
|
|
|
@future_2 = { 'class' => ScheduledWorker.name, 'args' => [4], 'queue' => 'queue_5' }
|
|
|
|
@future_3 = { 'class' => ScheduledWorker.name, 'args' => [5], 'queue' => 'queue_6' }
|
|
|
|
|
|
|
|
@retry = Sidekiq::RetrySet.new
|
|
|
|
@scheduled = Sidekiq::ScheduledSet.new
|
|
|
|
@poller = Sidekiq::Scheduled::Poller.new
|
|
|
|
end
|
|
|
|
|
|
|
|
class Stopper
|
2014-03-26 00:38:17 -04:00
|
|
|
def call(worker_class, message, queue, r)
|
2013-05-24 08:22:24 -04:00
|
|
|
yield if message['args'].first.odd?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes client middleware' do
|
|
|
|
Sidekiq.client_middleware.add Stopper
|
|
|
|
begin
|
|
|
|
@retry.schedule (Time.now - 60).to_f, @error_1
|
|
|
|
@retry.schedule (Time.now - 60).to_f, @error_2
|
|
|
|
@scheduled.schedule (Time.now - 60).to_f, @future_2
|
|
|
|
@scheduled.schedule (Time.now - 60).to_f, @future_3
|
|
|
|
|
|
|
|
@poller.poll
|
|
|
|
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
assert_equal 0, conn.llen("queue:queue_1")
|
|
|
|
assert_equal 1, conn.llen("queue:queue_2")
|
|
|
|
assert_equal 0, conn.llen("queue:queue_5")
|
|
|
|
assert_equal 1, conn.llen("queue:queue_6")
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Sidekiq.client_middleware.remove Stopper
|
|
|
|
end
|
2013-04-10 20:38:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should empty the retry and scheduled queues up to the current time' do
|
2013-05-23 23:32:49 -04:00
|
|
|
enqueued_time = Time.new(2013, 2, 4)
|
|
|
|
|
|
|
|
Time.stub(:now, enqueued_time) do
|
2013-05-24 08:22:24 -04:00
|
|
|
@retry.schedule (Time.now - 60).to_f, @error_1
|
|
|
|
@retry.schedule (Time.now - 50).to_f, @error_2
|
|
|
|
@retry.schedule (Time.now + 60).to_f, @error_3
|
|
|
|
@scheduled.schedule (Time.now - 60).to_f, @future_1
|
|
|
|
@scheduled.schedule (Time.now - 50).to_f, @future_2
|
|
|
|
@scheduled.schedule (Time.now + 60).to_f, @future_3
|
|
|
|
|
|
|
|
@poller.poll
|
|
|
|
|
2013-05-23 23:32:49 -04:00
|
|
|
Sidekiq.redis do |conn|
|
2013-05-24 08:22:24 -04:00
|
|
|
assert_equal 1, conn.llen("queue:queue_1")
|
|
|
|
assert_equal enqueued_time.to_f, Sidekiq.load_json(conn.lrange("queue:queue_1", 0, -1)[0])['enqueued_at']
|
|
|
|
assert_equal 1, conn.llen("queue:queue_2")
|
|
|
|
assert_equal enqueued_time.to_f, Sidekiq.load_json(conn.lrange("queue:queue_2", 0, -1)[0])['enqueued_at']
|
|
|
|
assert_equal 1, conn.llen("queue:queue_4")
|
|
|
|
assert_equal enqueued_time.to_f, Sidekiq.load_json(conn.lrange("queue:queue_4", 0, -1)[0])['enqueued_at']
|
|
|
|
assert_equal 1, conn.llen("queue:queue_5")
|
|
|
|
assert_equal enqueued_time.to_f, Sidekiq.load_json(conn.lrange("queue:queue_5", 0, -1)[0])['enqueued_at']
|
2013-05-23 23:32:49 -04:00
|
|
|
end
|
2013-05-24 08:22:24 -04:00
|
|
|
|
|
|
|
assert_equal 1, @retry.size
|
|
|
|
assert_equal 1, @scheduled.size
|
2013-04-10 20:38:36 -04:00
|
|
|
end
|
|
|
|
end
|
2015-04-24 11:35:47 -04:00
|
|
|
|
|
|
|
def with_sidekiq_option(name, value)
|
|
|
|
_original, Sidekiq.options[name] = Sidekiq.options[name], value
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Sidekiq.options[name] = _original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates random intervals that target a configured average' do
|
|
|
|
with_sidekiq_option(:poll_interval_average, 10) do
|
|
|
|
i = 500
|
|
|
|
intervals = i.times.map{ @poller.send(:random_poll_interval) }
|
|
|
|
|
|
|
|
assert intervals.all?{|i| i >= 5}
|
|
|
|
assert intervals.all?{|i| i <= 15}
|
|
|
|
assert_in_delta 10, intervals.reduce(&:+).to_f / i, 0.5
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'calculates an average poll interval based on the number of known Sidekiq processes' do
|
2015-05-04 13:38:10 -04:00
|
|
|
with_sidekiq_option(:average_scheduled_poll_interval, 10) do
|
2015-04-25 10:53:04 -04:00
|
|
|
3.times do |i|
|
|
|
|
Sidekiq.redis do |conn|
|
|
|
|
conn.sadd("processes", "process-#{i}")
|
|
|
|
conn.hset("process-#{i}", "info", nil)
|
2015-04-24 11:35:47 -04:00
|
|
|
end
|
|
|
|
end
|
2015-04-25 10:53:04 -04:00
|
|
|
|
|
|
|
assert_equal 30, @poller.send(:scaled_poll_interval)
|
2015-04-24 11:35:47 -04:00
|
|
|
end
|
|
|
|
end
|
2013-04-10 20:38:36 -04:00
|
|
|
end
|
|
|
|
end
|