2012-02-10 00:46:44 -05:00
|
|
|
require 'helper'
|
2012-03-10 16:07:19 -05:00
|
|
|
require 'sidekiq'
|
2012-02-10 00:46:44 -05:00
|
|
|
require 'sidekiq/worker'
|
2012-03-10 14:56:34 -05:00
|
|
|
require 'active_record'
|
|
|
|
require 'action_mailer'
|
2012-04-28 00:21:23 -04:00
|
|
|
require 'sidekiq/rails'
|
2012-03-10 14:56:34 -05:00
|
|
|
require 'sidekiq/extensions/action_mailer'
|
|
|
|
require 'sidekiq/extensions/active_record'
|
2012-02-10 00:46:44 -05:00
|
|
|
|
2012-03-10 16:07:19 -05:00
|
|
|
Sidekiq.hook_rails!
|
|
|
|
|
2012-02-10 00:46:44 -05:00
|
|
|
class TestTesting < MiniTest::Unit::TestCase
|
|
|
|
describe 'sidekiq testing' do
|
2012-05-02 13:45:05 -04:00
|
|
|
class PerformError < RuntimeError; end
|
|
|
|
|
2012-02-10 00:46:44 -05:00
|
|
|
class DirectWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(a, b)
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-01 17:20:35 -04:00
|
|
|
class EnqueuedWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(a, b)
|
|
|
|
a + b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-02 13:45:05 -04:00
|
|
|
class StoredWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(error)
|
|
|
|
raise PerformError if error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-10 14:56:34 -05:00
|
|
|
class FooMailer < ActionMailer::Base
|
|
|
|
def bar(str)
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class FooModel < ActiveRecord::Base
|
|
|
|
def bar(str)
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2012-05-01 17:20:35 -04:00
|
|
|
load 'sidekiq/testing.rb'
|
2012-09-07 12:02:23 -04:00
|
|
|
EnqueuedWorker.jobs.clear
|
2012-03-10 14:56:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
# Undo override
|
|
|
|
Sidekiq::Worker::ClassMethods.class_eval do
|
2012-06-28 03:46:18 -04:00
|
|
|
remove_method :client_push
|
|
|
|
alias_method :client_push, :client_push_old
|
|
|
|
remove_method :client_push_old
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-01 17:20:35 -04:00
|
|
|
it 'stubs the async call' do
|
2012-03-10 14:56:34 -05:00
|
|
|
assert_equal 0, DirectWorker.jobs.size
|
|
|
|
assert DirectWorker.perform_async(1, 2)
|
|
|
|
assert_equal 1, DirectWorker.jobs.size
|
2012-06-03 14:03:22 -04:00
|
|
|
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
|
2012-05-01 17:20:35 -04:00
|
|
|
end
|
2012-03-10 14:56:34 -05:00
|
|
|
|
2012-05-01 17:20:35 -04:00
|
|
|
it 'stubs the delay call on mailers' do
|
2012-03-10 14:56:34 -05:00
|
|
|
assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
FooMailer.delay.bar('hello!')
|
|
|
|
assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
|
2012-05-01 17:20:35 -04:00
|
|
|
end
|
2012-03-10 14:56:34 -05:00
|
|
|
|
2012-08-16 19:43:01 -04:00
|
|
|
class Something
|
|
|
|
def self.foo(x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-01 17:20:35 -04:00
|
|
|
it 'stubs the delay call on models' do
|
2012-08-16 19:43:01 -04:00
|
|
|
assert_equal 0, Sidekiq::Extensions::DelayedClass.jobs.size
|
|
|
|
Something.delay.foo(Date.today)
|
|
|
|
assert_equal 1, Sidekiq::Extensions::DelayedClass.jobs.size
|
2012-03-10 14:56:34 -05:00
|
|
|
end
|
|
|
|
|
2012-05-01 17:20:35 -04:00
|
|
|
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
|
2012-05-02 13:45:05 -04:00
|
|
|
|
2012-09-07 12:02:23 -04:00
|
|
|
it 'stubs the enqueue_to call' do
|
|
|
|
assert_equal 0, EnqueuedWorker.jobs.size
|
|
|
|
assert Sidekiq::Client.enqueue_to('someq', EnqueuedWorker, 1, 2)
|
|
|
|
assert_equal 1, EnqueuedWorker.jobs.size
|
|
|
|
end
|
|
|
|
|
2012-05-02 13:45:05 -04:00
|
|
|
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
|
2012-05-02 13:59:31 -04:00
|
|
|
StoredWorker.drain
|
2012-05-02 13:45:05 -04:00
|
|
|
end
|
|
|
|
assert_equal 0, StoredWorker.jobs.size
|
|
|
|
end
|
2012-10-19 00:16:28 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-11-01 10:49:35 -04:00
|
|
|
class ThirdWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
class_attribute :count
|
|
|
|
def perform
|
|
|
|
FirstWorker.perform_async
|
|
|
|
SecondWorker.perform_async
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-19 00:16:28 -04:00
|
|
|
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
|
2012-11-01 10:49:35 -04:00
|
|
|
|
|
|
|
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
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
|
|
|
end
|