2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-12-30 15:54:58 -05:00
|
|
|
require_relative 'helper'
|
2015-10-06 15:43:01 -04:00
|
|
|
require 'sidekiq/fetch'
|
|
|
|
require 'sidekiq/cli'
|
2012-01-26 15:45:04 -05:00
|
|
|
require 'sidekiq/processor'
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe Sidekiq::Processor do
|
|
|
|
TestProcessorException = Class.new(StandardError)
|
|
|
|
TEST_PROC_EXCEPTION = TestProcessorException.new("kerboom!")
|
|
|
|
|
|
|
|
before do
|
|
|
|
$invokes = 0
|
|
|
|
@mgr = Minitest::Mock.new
|
2020-06-19 11:39:18 -04:00
|
|
|
opts = {:queues => ['default']}
|
|
|
|
opts[:fetch] = Sidekiq::BasicFetch.new(opts)
|
|
|
|
@processor = ::Sidekiq::Processor.new(@mgr, opts)
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2012-07-31 11:43:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
class MockWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
def perform(args)
|
|
|
|
raise TEST_PROC_EXCEPTION if args.to_s == 'boom'
|
|
|
|
args.pop if args.is_a? Array
|
|
|
|
$invokes += 1
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2012-01-26 15:45:04 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
def work(msg, queue='queue:default')
|
|
|
|
Sidekiq::BasicFetch::UnitOfWork.new(queue, msg)
|
|
|
|
end
|
2012-01-26 15:45:04 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'processes as expected' do
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
|
|
|
@processor.process(work(msg))
|
|
|
|
assert_equal 1, $invokes
|
|
|
|
end
|
2013-01-06 00:17:08 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'executes a worker as expected' do
|
|
|
|
worker = Minitest::Mock.new
|
|
|
|
worker.expect(:perform, nil, [1, 2, 3])
|
|
|
|
@processor.execute_job(worker, [1, 2, 3])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 're-raises exceptions after handling' do
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['boom'] })
|
|
|
|
re_raise = false
|
|
|
|
|
|
|
|
begin
|
2013-01-06 00:17:08 -05:00
|
|
|
@processor.process(work(msg))
|
2019-03-01 16:25:56 -05:00
|
|
|
flunk "Expected exception"
|
|
|
|
rescue TestProcessorException
|
|
|
|
re_raise = true
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
assert_equal 0, $invokes
|
|
|
|
assert re_raise, "does not re-raise exceptions after handling"
|
|
|
|
end
|
2014-09-09 21:57:39 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'does not modify original arguments' do
|
|
|
|
msg = { 'class' => MockWorker.to_s, 'args' => [['myarg']] }
|
|
|
|
msgstr = Sidekiq.dump_json(msg)
|
|
|
|
@mgr.expect(:processor_done, nil, [@processor])
|
|
|
|
@processor.process(work(msgstr))
|
|
|
|
assert_equal [['myarg']], msg['args']
|
|
|
|
end
|
2012-07-31 11:43:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'exception handling' do
|
|
|
|
let(:errors) { [] }
|
|
|
|
let(:error_handler) do
|
|
|
|
proc do |exception, context|
|
|
|
|
errors << { exception: exception, context: context }
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2012-07-31 11:43:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
before do
|
|
|
|
Sidekiq.error_handlers << error_handler
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|
2012-08-04 15:11:46 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
after do
|
|
|
|
Sidekiq.error_handlers.pop
|
2012-08-04 15:11:46 -04:00
|
|
|
end
|
2012-12-05 20:35:49 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'handles invalid JSON' do
|
|
|
|
ds = Sidekiq::DeadSet.new
|
|
|
|
ds.clear
|
|
|
|
job_hash = { 'class' => MockWorker.to_s, 'args' => ['boom'] }
|
|
|
|
msg = Sidekiq.dump_json(job_hash)
|
|
|
|
job = work(msg[0...-2])
|
|
|
|
ds = Sidekiq::DeadSet.new
|
|
|
|
assert_equal 0, ds.size
|
|
|
|
begin
|
|
|
|
@processor.instance_variable_set(:'@job', job)
|
|
|
|
@processor.process(job)
|
|
|
|
rescue JSON::ParserError
|
2016-11-06 16:11:07 -05:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
assert_equal 1, ds.size
|
|
|
|
end
|
2016-10-28 23:41:06 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'handles exceptions raised by the job' do
|
|
|
|
job_hash = { 'class' => MockWorker.to_s, 'args' => ['boom'], 'jid' => '123987123' }
|
|
|
|
msg = Sidekiq.dump_json(job_hash)
|
|
|
|
job = work(msg)
|
|
|
|
begin
|
|
|
|
@processor.instance_variable_set(:'@job', job)
|
|
|
|
@processor.process(job)
|
|
|
|
rescue TestProcessorException
|
2016-10-28 23:41:06 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
assert_equal 1, errors.count
|
|
|
|
assert_instance_of TestProcessorException, errors.first[:exception]
|
|
|
|
assert_equal msg, errors.first[:context][:jobstr]
|
|
|
|
assert_equal job_hash['jid'], errors.first[:context][:job]['jid']
|
|
|
|
end
|
2016-10-28 23:41:06 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'handles exceptions raised by the reloader' do
|
|
|
|
job_hash = { 'class' => MockWorker.to_s, 'args' => ['boom'] }
|
|
|
|
msg = Sidekiq.dump_json(job_hash)
|
|
|
|
@processor.instance_variable_set(:'@reloader', proc { raise TEST_PROC_EXCEPTION })
|
|
|
|
job = work(msg)
|
|
|
|
begin
|
|
|
|
@processor.instance_variable_set(:'@job', job)
|
|
|
|
@processor.process(job)
|
|
|
|
rescue TestProcessorException
|
2016-10-28 23:41:06 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
assert_equal 1, errors.count
|
|
|
|
assert_instance_of TestProcessorException, errors.first[:exception]
|
|
|
|
assert_equal msg, errors.first[:context][:jobstr]
|
|
|
|
assert_equal job_hash, errors.first[:context][:job]
|
|
|
|
end
|
2016-10-28 23:41:06 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'handles exceptions raised during fetch' do
|
|
|
|
fetch_stub = lambda { raise StandardError, "fetch exception" }
|
|
|
|
# swallow logging because actually care about the added exception handler
|
|
|
|
capture_logging do
|
|
|
|
@processor.instance_variable_get('@strategy').stub(:retrieve_work, fetch_stub) do
|
|
|
|
@processor.process_one
|
2017-01-17 17:58:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
assert_instance_of StandardError, errors.last[:exception]
|
|
|
|
end
|
|
|
|
end
|
2016-10-28 23:41:06 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'acknowledgement' do
|
|
|
|
class ExceptionRaisingMiddleware
|
|
|
|
def initialize(raise_before_yield, raise_after_yield, skip)
|
|
|
|
@raise_before_yield = raise_before_yield
|
|
|
|
@raise_after_yield = raise_after_yield
|
|
|
|
@skip = skip
|
2016-10-28 23:41:06 -04:00
|
|
|
end
|
2017-11-22 16:42:39 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
def call(worker, item, queue)
|
|
|
|
raise TEST_PROC_EXCEPTION if @raise_before_yield
|
|
|
|
yield unless @skip
|
|
|
|
raise TEST_PROC_EXCEPTION if @raise_after_yield
|
2017-11-28 17:11:07 -05:00
|
|
|
end
|
2016-10-28 23:41:06 -04:00
|
|
|
end
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
let(:raise_before_yield) { false }
|
|
|
|
let(:raise_after_yield) { false }
|
|
|
|
let(:skip_job) { false }
|
|
|
|
let(:worker_args) { ['myarg'] }
|
|
|
|
let(:work) { MiniTest::Mock.new }
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
before do
|
|
|
|
work.expect(:queue_name, 'queue:default')
|
|
|
|
work.expect(:job, Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => worker_args }))
|
|
|
|
Sidekiq.server_middleware do |chain|
|
|
|
|
chain.prepend ExceptionRaisingMiddleware, raise_before_yield, raise_after_yield, skip_job
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
after do
|
|
|
|
Sidekiq.server_middleware do |chain|
|
|
|
|
chain.remove ExceptionRaisingMiddleware
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
work.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'middleware throws an exception before processing the work' do
|
|
|
|
let(:raise_before_yield) { true }
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'acks the job' do
|
|
|
|
work.expect(:acknowledge, nil)
|
|
|
|
begin
|
|
|
|
@processor.process(work)
|
|
|
|
flunk "Expected #process to raise exception"
|
|
|
|
rescue TestProcessorException
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'middleware throws an exception after processing the work' do
|
|
|
|
let(:raise_after_yield) { true }
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'acks the job' do
|
|
|
|
work.expect(:acknowledge, nil)
|
|
|
|
begin
|
|
|
|
@processor.process(work)
|
|
|
|
flunk "Expected #process to raise exception"
|
|
|
|
rescue TestProcessorException
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'middleware decides to skip work' do
|
|
|
|
let(:skip_job) { true }
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'acks the job' do
|
|
|
|
work.expect(:acknowledge, nil)
|
|
|
|
@mgr.expect(:processor_done, nil, [@processor])
|
|
|
|
@processor.process(work)
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'worker raises an exception' do
|
|
|
|
let(:worker_args) { ['boom'] }
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
it 'acks the job' do
|
|
|
|
work.expect(:acknowledge, nil)
|
|
|
|
begin
|
2015-09-04 20:13:09 -04:00
|
|
|
@processor.process(work)
|
2019-03-01 16:25:56 -05:00
|
|
|
flunk "Expected #process to raise exception"
|
|
|
|
rescue TestProcessorException
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'everything goes well' do
|
|
|
|
it 'acks the job' do
|
|
|
|
work.expect(:acknowledge, nil)
|
|
|
|
@mgr.expect(:processor_done, nil, [@processor])
|
|
|
|
@processor.process(work)
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
end
|
2015-09-04 20:13:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'retry' do
|
|
|
|
class ArgsMutatingServerMiddleware
|
|
|
|
def call(worker, item, queue)
|
|
|
|
item['args'] = item['args'].map do |arg|
|
|
|
|
arg.to_sym if arg.is_a?(String)
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
yield
|
2015-09-04 20:13:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
class ArgsMutatingClientMiddleware
|
|
|
|
def call(worker, item, queue, redis_pool)
|
|
|
|
item['args'] = item['args'].map do |arg|
|
|
|
|
arg.to_s if arg.is_a?(Symbol)
|
2017-08-11 16:33:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
yield
|
2017-08-11 16:33:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2017-08-11 16:33:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
before do
|
|
|
|
Sidekiq.server_middleware do |chain|
|
|
|
|
chain.prepend ArgsMutatingServerMiddleware
|
2017-08-11 16:33:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
Sidekiq.client_middleware do |chain|
|
|
|
|
chain.prepend ArgsMutatingClientMiddleware
|
2017-08-11 16:33:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
2017-08-11 16:33:09 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
after do
|
|
|
|
Sidekiq.server_middleware do |chain|
|
|
|
|
chain.remove ArgsMutatingServerMiddleware
|
2017-08-11 16:33:09 -04:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
Sidekiq.client_middleware do |chain|
|
|
|
|
chain.remove ArgsMutatingClientMiddleware
|
2017-08-11 16:33:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'middleware mutates the job args and then fails' do
|
|
|
|
it 'requeues with original arguments' do
|
|
|
|
job_data = { 'class' => MockWorker.to_s, 'args' => ['boom'] }
|
2012-12-05 20:35:49 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
retry_stub_called = false
|
|
|
|
retry_stub = lambda { |worker, msg, queue, exception|
|
|
|
|
retry_stub_called = true
|
|
|
|
assert_equal 'boom', msg['args'].first
|
|
|
|
}
|
2013-05-31 12:02:27 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
@processor.instance_variable_get('@retrier').stub(:attempt_retry, retry_stub) do
|
|
|
|
msg = Sidekiq.dump_json(job_data)
|
|
|
|
begin
|
|
|
|
@processor.process(work(msg))
|
|
|
|
flunk "Expected exception"
|
|
|
|
rescue TestProcessorException
|
|
|
|
end
|
2012-12-05 20:35:49 -05:00
|
|
|
end
|
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
assert retry_stub_called
|
2012-12-05 20:35:49 -05:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
end
|
2012-12-05 20:35:49 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'stats' do
|
|
|
|
before do
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
2013-05-31 12:02:27 -04:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
describe 'when successful' do
|
|
|
|
let(:processed_today_key) { "stat:processed:#{Time.now.utc.strftime("%Y-%m-%d")}" }
|
2012-12-05 20:35:49 -05:00
|
|
|
|
2019-03-01 16:25:56 -05:00
|
|
|
def successful_job
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
|
|
|
@mgr.expect(:processor_done, nil, [@processor])
|
|
|
|
@processor.process(work(msg))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments processed stat' do
|
|
|
|
Sidekiq::Processor::PROCESSED.reset
|
|
|
|
successful_job
|
|
|
|
assert_equal 1, Sidekiq::Processor::PROCESSED.reset
|
2013-05-31 12:02:27 -04:00
|
|
|
end
|
2012-12-05 20:35:49 -05:00
|
|
|
end
|
2017-06-07 12:57:55 -04:00
|
|
|
|
|
|
|
describe 'custom job logger class' do
|
2018-12-28 18:05:51 -05:00
|
|
|
class CustomJobLogger < Sidekiq::JobLogger
|
2017-06-07 12:57:55 -04:00
|
|
|
def call(item, queue)
|
|
|
|
yield
|
|
|
|
rescue Exception
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2020-06-19 11:39:18 -04:00
|
|
|
opts = {:queues => ['default'], job_logger: CustomJobLogger}
|
2017-06-07 12:57:55 -04:00
|
|
|
@mgr = Minitest::Mock.new
|
2020-06-19 11:39:18 -04:00
|
|
|
@processor = ::Sidekiq::Processor.new(@mgr, opts)
|
2017-06-07 12:57:55 -04:00
|
|
|
end
|
2019-02-28 15:43:50 -05:00
|
|
|
end
|
|
|
|
end
|
2017-06-07 12:57:55 -04:00
|
|
|
|
2019-03-01 16:37:04 -05:00
|
|
|
describe 'stats' do
|
|
|
|
before do
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
|
2019-03-01 16:37:04 -05:00
|
|
|
def successful_job
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
|
|
|
@mgr.expect(:processor_done, nil, [@processor])
|
|
|
|
@processor.process(work(msg))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments processed stat' do
|
|
|
|
Sidekiq::Processor::PROCESSED.reset
|
|
|
|
successful_job
|
|
|
|
assert_equal 1, Sidekiq::Processor::PROCESSED.reset
|
|
|
|
end
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|
2019-03-01 16:25:56 -05:00
|
|
|
|
|
|
|
describe 'custom job logger class' do
|
|
|
|
before do
|
2020-06-19 11:39:18 -04:00
|
|
|
opts = {:queues => ['default'], :job_logger => CustomJobLogger}
|
|
|
|
opts[:fetch] = Sidekiq::BasicFetch.new(opts)
|
|
|
|
@processor = ::Sidekiq::Processor.new(nil, opts)
|
2019-03-01 16:25:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'is called instead default Sidekiq::JobLogger' do
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
|
|
|
@processor.process(work(msg))
|
|
|
|
assert_equal 1, $invokes
|
|
|
|
end
|
|
|
|
end
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|