2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2015-09-22 15:48:43 -04:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
require 'active_record'
|
|
|
|
require 'action_mailer'
|
|
|
|
require 'sidekiq/rails'
|
|
|
|
require 'sidekiq/extensions/action_mailer'
|
|
|
|
require 'sidekiq/extensions/active_record'
|
|
|
|
|
|
|
|
Sidekiq.hook_rails!
|
|
|
|
|
|
|
|
class TestTesting < Sidekiq::Test
|
|
|
|
describe 'sidekiq testing' do
|
|
|
|
class PerformError < RuntimeError; end
|
|
|
|
|
|
|
|
class DirectWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(a, b)
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class EnqueuedWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(a, b)
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class StoredWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(error)
|
|
|
|
raise PerformError if error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FooMailer < ActionMailer::Base
|
|
|
|
def bar(str)
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FooModel < ActiveRecord::Base
|
|
|
|
def bar(str)
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2015-09-22 15:48:43 -04:00
|
|
|
require 'sidekiq/testing'
|
2013-09-20 17:39:11 -04:00
|
|
|
Sidekiq::Testing.fake!
|
|
|
|
EnqueuedWorker.jobs.clear
|
|
|
|
DirectWorker.jobs.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq::Testing.disable!
|
2015-11-17 13:07:30 -05:00
|
|
|
Sidekiq::Queues.clear_all
|
2013-09-20 17:39:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the async call' do
|
|
|
|
assert_equal 0, DirectWorker.jobs.size
|
|
|
|
assert DirectWorker.perform_async(1, 2)
|
|
|
|
assert_equal 1, DirectWorker.jobs.size
|
|
|
|
assert DirectWorker.perform_in(10, 1, 2)
|
|
|
|
assert_equal 2, DirectWorker.jobs.size
|
|
|
|
assert DirectWorker.perform_at(10, 1, 2)
|
|
|
|
assert_equal 3, DirectWorker.jobs.size
|
|
|
|
assert_in_delta 10.seconds.from_now.to_f, DirectWorker.jobs.last['at'], 0.01
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the delay call on mailers' do
|
|
|
|
assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
FooMailer.delay.bar('hello!')
|
|
|
|
assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
end
|
|
|
|
|
|
|
|
class Something
|
|
|
|
def self.foo(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the delay call on models' do
|
|
|
|
assert_equal 0, Sidekiq::Extensions::DelayedClass.jobs.size
|
|
|
|
Something.delay.foo(Date.today)
|
|
|
|
assert_equal 1, Sidekiq::Extensions::DelayedClass.jobs.size
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the enqueue call' do
|
|
|
|
assert_equal 0, EnqueuedWorker.jobs.size
|
|
|
|
assert Sidekiq::Client.enqueue(EnqueuedWorker, 1, 2)
|
|
|
|
assert_equal 1, EnqueuedWorker.jobs.size
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stubs the enqueue_to call' do
|
|
|
|
assert_equal 0, EnqueuedWorker.jobs.size
|
|
|
|
assert Sidekiq::Client.enqueue_to('someq', EnqueuedWorker, 1, 2)
|
2015-11-17 13:07:30 -05:00
|
|
|
assert_equal 1, Sidekiq::Queues['someq'].size
|
2013-09-20 17:39:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'executes all stored jobs' do
|
|
|
|
assert StoredWorker.perform_async(false)
|
|
|
|
assert StoredWorker.perform_async(true)
|
|
|
|
|
|
|
|
assert_equal 2, StoredWorker.jobs.size
|
|
|
|
assert_raises PerformError do
|
|
|
|
StoredWorker.drain
|
|
|
|
end
|
|
|
|
assert_equal 0, StoredWorker.jobs.size
|
|
|
|
end
|
|
|
|
|
|
|
|
class SpecificJidWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
class_attribute :count
|
|
|
|
self.count = 0
|
|
|
|
def perform(worker_jid)
|
|
|
|
return unless worker_jid == self.jid
|
|
|
|
self.class.count += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'execute only jobs with assigned JID' do
|
|
|
|
4.times do |i|
|
|
|
|
jid = SpecificJidWorker.perform_async(nil)
|
|
|
|
if i % 2 == 0
|
|
|
|
SpecificJidWorker.jobs[-1]["args"] = ["wrong_jid"]
|
|
|
|
else
|
|
|
|
SpecificJidWorker.jobs[-1]["args"] = [jid]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
SpecificJidWorker.perform_one
|
|
|
|
assert_equal 0, SpecificJidWorker.count
|
|
|
|
|
|
|
|
SpecificJidWorker.perform_one
|
|
|
|
assert_equal 1, SpecificJidWorker.count
|
|
|
|
|
|
|
|
SpecificJidWorker.drain
|
|
|
|
assert_equal 2, SpecificJidWorker.count
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'round trip serializes the job arguments' do
|
|
|
|
assert StoredWorker.perform_async(:mike)
|
|
|
|
job = StoredWorker.jobs.first
|
|
|
|
assert_equal "mike", job['args'].first
|
|
|
|
StoredWorker.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'perform_one runs only one job' do
|
|
|
|
DirectWorker.perform_async(1, 2)
|
|
|
|
DirectWorker.perform_async(3, 4)
|
|
|
|
assert_equal 2, DirectWorker.jobs.size
|
|
|
|
|
|
|
|
DirectWorker.perform_one
|
|
|
|
assert_equal 1, DirectWorker.jobs.size
|
|
|
|
|
|
|
|
DirectWorker.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'perform_one raise error upon empty queue' do
|
|
|
|
DirectWorker.clear
|
|
|
|
assert_raises Sidekiq::EmptyQueueError do
|
|
|
|
DirectWorker.perform_one
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FirstWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
class_attribute :count
|
|
|
|
self.count = 0
|
|
|
|
def perform
|
|
|
|
self.class.count += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SecondWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
class_attribute :count
|
|
|
|
self.count = 0
|
|
|
|
def perform
|
|
|
|
self.class.count += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ThirdWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
class_attribute :count
|
|
|
|
def perform
|
|
|
|
FirstWorker.perform_async
|
|
|
|
SecondWorker.perform_async
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'clears jobs across all workers' do
|
|
|
|
Sidekiq::Worker.jobs.clear
|
|
|
|
FirstWorker.count = 0
|
|
|
|
SecondWorker.count = 0
|
|
|
|
|
|
|
|
assert_equal 0, FirstWorker.jobs.size
|
|
|
|
assert_equal 0, SecondWorker.jobs.size
|
|
|
|
|
|
|
|
FirstWorker.perform_async
|
|
|
|
SecondWorker.perform_async
|
|
|
|
|
|
|
|
assert_equal 1, FirstWorker.jobs.size
|
|
|
|
assert_equal 1, SecondWorker.jobs.size
|
|
|
|
|
|
|
|
Sidekiq::Worker.clear_all
|
|
|
|
|
|
|
|
assert_equal 0, FirstWorker.jobs.size
|
|
|
|
assert_equal 0, SecondWorker.jobs.size
|
|
|
|
|
|
|
|
assert_equal 0, FirstWorker.count
|
|
|
|
assert_equal 0, SecondWorker.count
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'drains jobs across all workers' do
|
|
|
|
Sidekiq::Worker.jobs.clear
|
|
|
|
FirstWorker.count = 0
|
|
|
|
SecondWorker.count = 0
|
|
|
|
|
|
|
|
assert_equal 0, FirstWorker.jobs.size
|
|
|
|
assert_equal 0, SecondWorker.jobs.size
|
|
|
|
|
|
|
|
assert_equal 0, FirstWorker.count
|
|
|
|
assert_equal 0, SecondWorker.count
|
|
|
|
|
|
|
|
FirstWorker.perform_async
|
|
|
|
SecondWorker.perform_async
|
|
|
|
|
|
|
|
assert_equal 1, FirstWorker.jobs.size
|
|
|
|
assert_equal 1, SecondWorker.jobs.size
|
|
|
|
|
|
|
|
Sidekiq::Worker.drain_all
|
|
|
|
|
|
|
|
assert_equal 0, FirstWorker.jobs.size
|
|
|
|
assert_equal 0, SecondWorker.jobs.size
|
|
|
|
|
|
|
|
assert_equal 1, FirstWorker.count
|
|
|
|
assert_equal 1, SecondWorker.count
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'drains jobs across all workers even when workers create new jobs' do
|
|
|
|
Sidekiq::Worker.jobs.clear
|
|
|
|
FirstWorker.count = 0
|
|
|
|
SecondWorker.count = 0
|
|
|
|
|
|
|
|
assert_equal 0, ThirdWorker.jobs.size
|
|
|
|
|
|
|
|
assert_equal 0, FirstWorker.count
|
|
|
|
assert_equal 0, SecondWorker.count
|
|
|
|
|
|
|
|
ThirdWorker.perform_async
|
|
|
|
|
|
|
|
assert_equal 1, ThirdWorker.jobs.size
|
|
|
|
|
|
|
|
Sidekiq::Worker.drain_all
|
|
|
|
|
|
|
|
assert_equal 0, ThirdWorker.jobs.size
|
|
|
|
|
|
|
|
assert_equal 1, FirstWorker.count
|
|
|
|
assert_equal 1, SecondWorker.count
|
|
|
|
end
|
2014-09-09 21:57:39 -04:00
|
|
|
|
2016-01-14 09:25:03 -05:00
|
|
|
it 'drains jobs of workers with symbolized queue names' do
|
|
|
|
Sidekiq::Worker.jobs.clear
|
|
|
|
|
|
|
|
AltQueueWorker.perform_async(5,6)
|
|
|
|
assert_equal 1, AltQueueWorker.jobs.size
|
|
|
|
|
|
|
|
Sidekiq::Worker.drain_all
|
|
|
|
assert_equal 0, AltQueueWorker.jobs.size
|
|
|
|
end
|
|
|
|
|
2014-09-09 21:57:39 -04:00
|
|
|
it 'can execute a job' do
|
2015-09-22 15:48:43 -04:00
|
|
|
DirectWorker.execute_job(DirectWorker.new, [2, 3])
|
2014-09-09 21:57:39 -04:00
|
|
|
end
|
2015-11-17 13:07:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'queue testing' do
|
|
|
|
before do
|
|
|
|
require 'sidekiq/testing'
|
|
|
|
Sidekiq::Testing.fake!
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq::Testing.disable!
|
|
|
|
Sidekiq::Queues.clear_all
|
|
|
|
end
|
2015-11-13 15:25:10 -05:00
|
|
|
|
2015-11-17 13:07:30 -05:00
|
|
|
class QueueWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(a, b)
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AltQueueWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
sidekiq_options queue: :alt
|
|
|
|
def perform(a, b)
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds enqueued jobs' do
|
|
|
|
assert_equal 0, Sidekiq::Queues["default"].size
|
|
|
|
|
|
|
|
QueueWorker.perform_async(1, 2)
|
|
|
|
QueueWorker.perform_async(1, 2)
|
|
|
|
AltQueueWorker.perform_async(1, 2)
|
|
|
|
|
|
|
|
assert_equal 2, Sidekiq::Queues["default"].size
|
|
|
|
assert_equal [1, 2], Sidekiq::Queues["default"].first["args"]
|
|
|
|
|
|
|
|
assert_equal 1, Sidekiq::Queues["alt"].size
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'clears out all queues' do
|
|
|
|
assert_equal 0, Sidekiq::Queues["default"].size
|
|
|
|
|
|
|
|
QueueWorker.perform_async(1, 2)
|
|
|
|
QueueWorker.perform_async(1, 2)
|
|
|
|
AltQueueWorker.perform_async(1, 2)
|
|
|
|
|
|
|
|
Sidekiq::Queues.clear_all
|
|
|
|
|
|
|
|
assert_equal 0, Sidekiq::Queues["default"].size
|
2015-11-25 09:52:22 -05:00
|
|
|
assert_equal 0, QueueWorker.jobs.size
|
2015-11-17 13:07:30 -05:00
|
|
|
assert_equal 0, Sidekiq::Queues["alt"].size
|
2015-11-25 09:52:22 -05:00
|
|
|
assert_equal 0, AltQueueWorker.jobs.size
|
2015-11-17 13:07:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds jobs enqueued by client' do
|
|
|
|
Sidekiq::Client.push(
|
|
|
|
'class' => 'NonExistentWorker',
|
|
|
|
'queue' => 'missing',
|
|
|
|
'args' => [1]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert_equal 1, Sidekiq::Queues["missing"].size
|
|
|
|
end
|
2015-11-25 09:52:22 -05:00
|
|
|
|
|
|
|
it 'respects underlying array changes' do
|
|
|
|
# Rspec expect change() syntax saves a reference to
|
|
|
|
# an underlying array. When the array containing jobs is
|
|
|
|
# derived, Rspec test using `change(QueueWorker.jobs, :size).by(1)`
|
2015-12-08 15:39:32 -05:00
|
|
|
# won't pass. This attempts to recreate that scenario
|
2015-11-25 09:52:22 -05:00
|
|
|
# by saving a reference to the jobs array and ensuring
|
|
|
|
# it changes properly on enqueueing
|
|
|
|
jobs = QueueWorker.jobs
|
|
|
|
assert_equal 0, jobs.size
|
|
|
|
QueueWorker.perform_async(1, 2)
|
|
|
|
assert_equal 1, jobs.size
|
|
|
|
end
|
2013-09-20 17:39:11 -04:00
|
|
|
end
|
|
|
|
end
|