2013-04-10 20:38:36 -04:00
|
|
|
require 'helper'
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|