2014-01-30 12:22:07 -05:00
|
|
|
require 'securerandom'
|
2014-04-09 18:31:09 -04:00
|
|
|
require 'sidekiq'
|
2014-01-30 12:22:07 -05:00
|
|
|
|
2012-02-10 00:46:44 -05:00
|
|
|
module Sidekiq
|
2013-03-28 00:24:47 -04:00
|
|
|
|
2013-09-20 17:39:11 -04:00
|
|
|
class Testing
|
|
|
|
class << self
|
|
|
|
attr_accessor :__test_mode
|
|
|
|
|
|
|
|
def __set_test_mode(mode, &block)
|
|
|
|
if block
|
|
|
|
current_mode = self.__test_mode
|
|
|
|
begin
|
|
|
|
self.__test_mode = mode
|
|
|
|
block.call
|
|
|
|
ensure
|
|
|
|
self.__test_mode = current_mode
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self.__test_mode = mode
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable!(&block)
|
|
|
|
__set_test_mode(:disable, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fake!(&block)
|
|
|
|
__set_test_mode(:fake, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def inline!(&block)
|
|
|
|
__set_test_mode(:inline, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
self.__test_mode != :disable
|
|
|
|
end
|
|
|
|
|
|
|
|
def disabled?
|
|
|
|
self.__test_mode == :disable
|
|
|
|
end
|
|
|
|
|
|
|
|
def fake?
|
|
|
|
self.__test_mode == :fake
|
|
|
|
end
|
|
|
|
|
|
|
|
def inline?
|
|
|
|
self.__test_mode == :inline
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Default to fake testing to keep old behavior
|
|
|
|
Sidekiq::Testing.fake!
|
|
|
|
|
2013-05-30 14:39:01 -04:00
|
|
|
class EmptyQueueError < RuntimeError; end
|
|
|
|
|
2013-03-28 00:24:47 -04:00
|
|
|
class Client
|
2013-10-24 00:47:57 -04:00
|
|
|
alias_method :raw_push_real, :raw_push
|
2013-03-28 00:24:47 -04:00
|
|
|
|
2013-10-24 00:47:57 -04:00
|
|
|
def raw_push(payloads)
|
|
|
|
if Sidekiq::Testing.fake?
|
|
|
|
payloads.each do |job|
|
|
|
|
job['class'].constantize.jobs << Sidekiq.load_json(Sidekiq.dump_json(job))
|
|
|
|
end
|
|
|
|
true
|
|
|
|
elsif Sidekiq::Testing.inline?
|
|
|
|
payloads.each do |item|
|
2014-01-30 12:22:07 -05:00
|
|
|
jid = item['jid'] || SecureRandom.hex(12)
|
2013-10-24 00:47:57 -04:00
|
|
|
marshalled = Sidekiq.load_json(Sidekiq.dump_json(item))
|
2014-01-30 12:22:07 -05:00
|
|
|
worker = marshalled['class'].constantize.new
|
|
|
|
worker.jid = jid
|
|
|
|
worker.perform(*marshalled['args'])
|
2013-03-28 00:24:47 -04:00
|
|
|
end
|
2013-10-24 00:47:57 -04:00
|
|
|
true
|
|
|
|
else
|
|
|
|
raw_push_real(payloads)
|
2013-03-28 00:24:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-10 00:46:44 -05:00
|
|
|
module Worker
|
2012-02-10 23:30:14 -05:00
|
|
|
##
|
2012-02-14 12:00:26 -05:00
|
|
|
# The Sidekiq testing infrastructure overrides perform_async
|
2012-02-10 23:30:14 -05:00
|
|
|
# so that it does not actually touch the network. Instead it
|
2012-02-14 12:00:26 -05:00
|
|
|
# stores the asynchronous jobs in a per-class array so that
|
2012-02-10 23:30:14 -05:00
|
|
|
# their presence/absence can be asserted by your tests.
|
|
|
|
#
|
|
|
|
# This is similar to ActionMailer's :test delivery_method and its
|
|
|
|
# ActionMailer::Base.deliveries array.
|
2012-02-14 12:00:26 -05:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# require 'sidekiq/testing'
|
|
|
|
#
|
|
|
|
# assert_equal 0, HardWorker.jobs.size
|
|
|
|
# HardWorker.perform_async(:something)
|
|
|
|
# assert_equal 1, HardWorker.jobs.size
|
|
|
|
# assert_equal :something, HardWorker.jobs[0]['args'][0]
|
|
|
|
#
|
2012-04-05 23:06:47 -04:00
|
|
|
# assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
2012-10-19 00:16:28 -04:00
|
|
|
# MyMailer.delay.send_welcome_email('foo@example.com')
|
|
|
|
# assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
#
|
|
|
|
# You can also clear and drain all workers' jobs:
|
|
|
|
#
|
|
|
|
# assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
# assert_equal 0, Sidekiq::Extensions::DelayedModel.jobs.size
|
|
|
|
#
|
|
|
|
# MyMailer.delay.send_welcome_email('foo@example.com')
|
|
|
|
# MyModel.delay.do_something_hard
|
|
|
|
#
|
2012-04-05 23:06:47 -04:00
|
|
|
# assert_equal 1, Sidekiq::Extensions::DelayedMailer.jobs.size
|
2012-10-19 00:16:28 -04:00
|
|
|
# assert_equal 1, Sidekiq::Extensions::DelayedModel.jobs.size
|
|
|
|
#
|
|
|
|
# Sidekiq::Worker.clear_all # or .drain_all
|
|
|
|
#
|
|
|
|
# assert_equal 0, Sidekiq::Extensions::DelayedMailer.jobs.size
|
|
|
|
# assert_equal 0, Sidekiq::Extensions::DelayedModel.jobs.size
|
|
|
|
#
|
|
|
|
# This can be useful to make sure jobs don't linger between tests:
|
|
|
|
#
|
|
|
|
# RSpec.configure do |config|
|
|
|
|
# config.before(:each) do
|
|
|
|
# Sidekiq::Worker.clear_all
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# or for acceptance testing, i.e. with cucumber:
|
|
|
|
#
|
|
|
|
# AfterStep do
|
|
|
|
# Sidekiq::Worker.drain_all
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# When I sign up as "foo@example.com"
|
|
|
|
# Then I should receive a welcome email to "foo@example.com"
|
2012-04-05 23:06:47 -04:00
|
|
|
#
|
2012-02-10 00:46:44 -05:00
|
|
|
module ClassMethods
|
2012-02-10 23:30:14 -05:00
|
|
|
|
2012-10-19 00:16:28 -04:00
|
|
|
# Jobs queued for this worker
|
2012-02-10 23:30:14 -05:00
|
|
|
def jobs
|
2012-10-19 00:16:28 -04:00
|
|
|
Worker.jobs[self]
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
2012-05-02 13:45:05 -04:00
|
|
|
|
2012-10-19 00:16:28 -04:00
|
|
|
# Clear all jobs for this worker
|
|
|
|
def clear
|
|
|
|
jobs.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
# Drain and run all jobs for this worker
|
2012-05-02 13:59:31 -04:00
|
|
|
def drain
|
2012-05-02 13:45:05 -04:00
|
|
|
while job = jobs.shift do
|
2013-07-01 17:31:20 -04:00
|
|
|
worker = new
|
|
|
|
worker.jid = job['jid']
|
|
|
|
worker.perform(*job['args'])
|
2012-05-02 13:45:05 -04:00
|
|
|
end
|
|
|
|
end
|
2013-05-30 01:06:08 -04:00
|
|
|
|
|
|
|
# Pop out a single job and perform it
|
2013-05-30 12:28:47 -04:00
|
|
|
def perform_one
|
2013-05-31 00:16:56 -04:00
|
|
|
raise(EmptyQueueError, "perform_one called with empty job queue") if jobs.empty?
|
2013-05-30 01:06:08 -04:00
|
|
|
job = jobs.shift
|
2013-07-01 17:31:20 -04:00
|
|
|
worker = new
|
|
|
|
worker.jid = job['jid']
|
|
|
|
worker.perform(*job['args'])
|
2013-05-30 01:06:08 -04:00
|
|
|
end
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
2012-10-19 00:16:28 -04:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def jobs # :nodoc:
|
|
|
|
@jobs ||= Hash.new { |hash, key| hash[key] = [] }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Clear all queued jobs across all workers
|
|
|
|
def clear_all
|
|
|
|
jobs.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
# Drain all queued jobs across all workers
|
|
|
|
def drain_all
|
2012-11-01 10:49:35 -04:00
|
|
|
until jobs.values.all?(&:empty?) do
|
|
|
|
jobs.keys.each(&:drain)
|
|
|
|
end
|
2012-10-19 00:16:28 -04:00
|
|
|
end
|
|
|
|
end
|
2012-02-10 00:46:44 -05:00
|
|
|
end
|
|
|
|
end
|