2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2013-04-10 20:38:36 -04:00
|
|
|
require 'sidekiq/scheduled'
|
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
describe Sidekiq::Scheduled do
|
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
|
2015-09-22 15:48:43 -04:00
|
|
|
Sidekiq.redis{|c| c.flushdb}
|
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
|
|
|
|
|
2019-02-28 15:43:50 -05:00
|
|
|
class MyStopper
|
2015-09-22 15:48:43 -04:00
|
|
|
def call(worker_class, job, queue, r)
|
|
|
|
yield if job['args'].first.odd?
|
2013-05-24 08:22:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes client middleware' do
|
2019-02-28 15:43:50 -05:00
|
|
|
Sidekiq.client_middleware.add MyStopper
|
2013-05-24 08:22:24 -04:00
|
|
|
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
|
|
|
|
|
2015-10-03 00:07:09 -04:00
|
|
|
@poller.enqueue
|
2013-05-24 08:22:24 -04:00
|
|
|
|
2015-09-22 15:48:43 -04:00
|
|
|
assert_equal 0, Sidekiq::Queue.new("queue_1").size
|
|
|
|
assert_equal 1, Sidekiq::Queue.new("queue_2").size
|
|
|
|
assert_equal 0, Sidekiq::Queue.new("queue_5").size
|
|
|
|
assert_equal 1, Sidekiq::Queue.new("queue_6").size
|
2013-05-24 08:22:24 -04:00
|
|
|
ensure
|
2019-02-28 15:43:50 -05:00
|
|
|
Sidekiq.client_middleware.remove MyStopper
|
2013-05-24 08:22:24 -04:00
|
|
|
end
|
2013-04-10 20:38:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should empty the retry and scheduled queues up to the current time' do
|
2015-06-03 06:45:35 -04:00
|
|
|
created_time = Time.new(2013, 2, 3)
|
2013-05-23 23:32:49 -04:00
|
|
|
enqueued_time = Time.new(2013, 2, 4)
|
|
|
|
|
2015-06-03 06:45:35 -04:00
|
|
|
Time.stub(:now, created_time) do
|
|
|
|
@retry.schedule (enqueued_time - 60).to_f, @error_1.merge!('created_at' => created_time.to_f)
|
|
|
|
@retry.schedule (enqueued_time - 50).to_f, @error_2.merge!('created_at' => created_time.to_f)
|
|
|
|
@retry.schedule (enqueued_time + 60).to_f, @error_3.merge!('created_at' => created_time.to_f)
|
|
|
|
@scheduled.schedule (enqueued_time - 60).to_f, @future_1.merge!('created_at' => created_time.to_f)
|
|
|
|
@scheduled.schedule (enqueued_time - 50).to_f, @future_2.merge!('created_at' => created_time.to_f)
|
|
|
|
@scheduled.schedule (enqueued_time + 60).to_f, @future_3.merge!('created_at' => created_time.to_f)
|
|
|
|
end
|
2013-05-24 08:22:24 -04:00
|
|
|
|
2015-06-03 06:45:35 -04:00
|
|
|
Time.stub(:now, enqueued_time) do
|
2015-10-03 00:07:09 -04:00
|
|
|
@poller.enqueue
|
2013-05-24 08:22:24 -04:00
|
|
|
|
2013-05-23 23:32:49 -04:00
|
|
|
Sidekiq.redis do |conn|
|
2015-06-03 06:45:35 -04:00
|
|
|
%w(queue:queue_1 queue:queue_2 queue:queue_4 queue:queue_5).each do |queue_name|
|
|
|
|
assert_equal 1, conn.llen(queue_name)
|
|
|
|
job = Sidekiq.load_json(conn.lrange(queue_name, 0, -1)[0])
|
|
|
|
assert_equal enqueued_time.to_f, job['enqueued_at']
|
|
|
|
assert_equal created_time.to_f, job['created_at']
|
|
|
|
end
|
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
|
2016-01-08 20:27:49 -05:00
|
|
|
intervals = Array.new(i){ @poller.send(:random_poll_interval) }
|
2015-04-24 11:35:47 -04:00
|
|
|
|
2015-06-22 12:14:55 -04:00
|
|
|
assert intervals.all?{|x| x >= 5}
|
|
|
|
assert intervals.all?{|x| x <= 15}
|
2019-10-08 17:50:26 -04:00
|
|
|
assert_in_delta 10, intervals.sum.to_f / i, 0.5
|
2015-04-24 11:35:47 -04:00
|
|
|
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
|