2012-01-26 15:45:04 -05:00
|
|
|
require 'helper'
|
|
|
|
require 'sidekiq/processor'
|
|
|
|
|
2013-09-22 17:38:33 -04:00
|
|
|
class TestProcessor < Sidekiq::Test
|
2012-07-31 11:43:09 -04:00
|
|
|
TestException = Class.new(StandardError)
|
|
|
|
TEST_EXCEPTION = TestException.new("kerboom!")
|
|
|
|
|
2012-01-26 15:45:04 -05:00
|
|
|
describe 'with mock setup' do
|
|
|
|
before do
|
|
|
|
$invokes = 0
|
2013-05-12 17:25:30 -04:00
|
|
|
@boss = Minitest::Mock.new
|
2012-07-31 11:43:09 -04:00
|
|
|
@processor = ::Sidekiq::Processor.new(@boss)
|
2012-01-29 17:35:16 -05:00
|
|
|
Celluloid.logger = nil
|
2012-03-17 16:32:52 -04:00
|
|
|
Sidekiq.redis = REDIS
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class MockWorker
|
2012-02-11 02:16:12 -05:00
|
|
|
include Sidekiq::Worker
|
2012-01-26 15:45:04 -05:00
|
|
|
def perform(args)
|
2012-07-31 11:43:09 -04:00
|
|
|
raise TEST_EXCEPTION if args == 'boom'
|
2012-08-04 15:11:46 -04:00
|
|
|
args.pop if args.is_a? Array
|
2012-01-26 15:45:04 -05:00
|
|
|
$invokes += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-06 00:17:08 -05:00
|
|
|
def work(msg, queue='queue:default')
|
|
|
|
Sidekiq::BasicFetch::UnitOfWork.new(queue, msg)
|
|
|
|
end
|
|
|
|
|
2012-01-26 15:45:04 -05:00
|
|
|
it 'processes as expected' do
|
2012-06-21 23:51:47 -04:00
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
2013-05-12 17:25:30 -04:00
|
|
|
actor = Minitest::Mock.new
|
2012-11-03 22:56:06 -04:00
|
|
|
actor.expect(:processor_done, nil, [@processor])
|
2013-06-11 01:20:15 -04:00
|
|
|
actor.expect(:real_thread, nil, [nil, Celluloid::Thread])
|
|
|
|
@boss.expect(:async, actor, [])
|
2012-11-03 22:56:06 -04:00
|
|
|
@boss.expect(:async, actor, [])
|
2013-01-06 00:17:08 -05:00
|
|
|
@processor.process(work(msg))
|
2012-01-26 15:45:04 -05:00
|
|
|
@boss.verify
|
|
|
|
assert_equal 1, $invokes
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes exceptions to ExceptionHandler' do
|
2013-06-11 01:20:15 -04:00
|
|
|
actor = Minitest::Mock.new
|
|
|
|
actor.expect(:real_thread, nil, [nil, Celluloid::Thread])
|
|
|
|
@boss.expect(:async, actor, [])
|
2012-07-31 11:43:09 -04:00
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['boom'] })
|
2012-08-02 23:46:06 -04:00
|
|
|
begin
|
2013-01-06 00:17:08 -05:00
|
|
|
@processor.process(work(msg))
|
2012-08-02 23:46:06 -04:00
|
|
|
flunk "Expected #process to raise exception"
|
|
|
|
rescue TestException
|
|
|
|
end
|
|
|
|
|
2012-07-31 11:43:09 -04:00
|
|
|
assert_equal 0, $invokes
|
|
|
|
end
|
|
|
|
|
|
|
|
it 're-raises exceptions after handling' do
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['boom'] })
|
|
|
|
re_raise = false
|
2013-06-11 01:20:15 -04:00
|
|
|
actor = Minitest::Mock.new
|
|
|
|
actor.expect(:real_thread, nil, [nil, Celluloid::Thread])
|
|
|
|
@boss.expect(:async, actor, [])
|
2012-07-31 11:43:09 -04:00
|
|
|
|
|
|
|
begin
|
2013-01-06 00:17:08 -05:00
|
|
|
@processor.process(work(msg))
|
2012-07-31 11:43:09 -04:00
|
|
|
rescue TestException
|
|
|
|
re_raise = true
|
|
|
|
end
|
|
|
|
|
|
|
|
assert re_raise, "does not re-raise exceptions after handling"
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|
2012-08-04 15:11:46 -04:00
|
|
|
|
|
|
|
it 'does not modify original arguments' do
|
|
|
|
msg = { 'class' => MockWorker.to_s, 'args' => [['myarg']] }
|
|
|
|
msgstr = Sidekiq.dump_json(msg)
|
|
|
|
processor = ::Sidekiq::Processor.new(@boss)
|
2013-05-12 17:25:30 -04:00
|
|
|
actor = Minitest::Mock.new
|
2012-11-03 22:56:06 -04:00
|
|
|
actor.expect(:processor_done, nil, [processor])
|
2013-06-11 01:20:15 -04:00
|
|
|
actor.expect(:real_thread, nil, [nil, Celluloid::Thread])
|
|
|
|
@boss.expect(:async, actor, [])
|
2012-11-03 22:56:06 -04:00
|
|
|
@boss.expect(:async, actor, [])
|
2013-01-06 00:17:08 -05:00
|
|
|
processor.process(work(msgstr))
|
2012-08-04 15:11:46 -04:00
|
|
|
assert_equal [['myarg']], msg['args']
|
|
|
|
end
|
2012-12-05 20:35:49 -05:00
|
|
|
|
|
|
|
describe 'stats' do
|
|
|
|
before do
|
|
|
|
Sidekiq.redis {|c| c.flushdb }
|
|
|
|
end
|
|
|
|
|
2013-05-31 12:02:27 -04:00
|
|
|
def with_expire(time)
|
|
|
|
begin
|
|
|
|
old = Sidekiq::Processor::STATS_TIMEOUT
|
|
|
|
silence_warnings { Sidekiq::Processor.const_set(:STATS_TIMEOUT, time) }
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
silence_warnings { Sidekiq::Processor.const_set(:STATS_TIMEOUT, old) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-05 20:35:49 -05:00
|
|
|
describe 'when successful' do
|
2013-05-31 12:02:27 -04:00
|
|
|
let(:processed_today_key) { "stat:processed:#{Time.now.utc.to_date}" }
|
|
|
|
|
2012-12-05 20:35:49 -05:00
|
|
|
def successful_job
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
2013-05-12 17:25:30 -04:00
|
|
|
actor = Minitest::Mock.new
|
2013-06-11 01:20:15 -04:00
|
|
|
actor.expect(:real_thread, nil, [nil, Celluloid::Thread])
|
2012-12-05 20:35:49 -05:00
|
|
|
actor.expect(:processor_done, nil, [@processor])
|
|
|
|
@boss.expect(:async, actor, [])
|
2013-06-11 01:20:15 -04:00
|
|
|
@boss.expect(:async, actor, [])
|
2013-01-06 00:17:08 -05:00
|
|
|
@processor.process(work(msg))
|
2012-12-05 20:35:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments processed stat' do
|
|
|
|
successful_job
|
|
|
|
assert_equal 1, Sidekiq::Stats.new.processed
|
|
|
|
end
|
|
|
|
|
2013-05-31 12:02:27 -04:00
|
|
|
it 'expires processed stat' do
|
|
|
|
successful_job
|
|
|
|
assert_equal Sidekiq::Processor::STATS_TIMEOUT, Sidekiq.redis { |conn| conn.ttl(processed_today_key) }
|
|
|
|
end
|
|
|
|
|
2012-12-05 20:35:49 -05:00
|
|
|
it 'increments date processed stat' do
|
2013-05-31 12:02:27 -04:00
|
|
|
successful_job
|
|
|
|
assert_equal 1, Sidekiq.redis { |conn| conn.get(processed_today_key) }.to_i
|
2012-12-05 20:35:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when failed' do
|
2013-05-31 12:02:27 -04:00
|
|
|
let(:failed_today_key) { "stat:failed:#{Time.now.utc.to_date}" }
|
|
|
|
|
2012-12-05 20:35:49 -05:00
|
|
|
def failed_job
|
2013-06-11 01:20:15 -04:00
|
|
|
actor = Minitest::Mock.new
|
|
|
|
actor.expect(:real_thread, nil, [nil, Celluloid::Thread])
|
|
|
|
@boss.expect(:async, actor, [])
|
2012-12-05 20:35:49 -05:00
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['boom'] })
|
|
|
|
begin
|
2013-01-06 00:17:08 -05:00
|
|
|
@processor.process(work(msg))
|
2012-12-05 20:35:49 -05:00
|
|
|
rescue TestException
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments failed stat' do
|
|
|
|
failed_job
|
|
|
|
assert_equal 1, Sidekiq::Stats.new.failed
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments date failed stat' do
|
2013-05-31 12:02:27 -04:00
|
|
|
failed_job
|
|
|
|
assert_equal 1, Sidekiq.redis { |conn| conn.get(failed_today_key) }.to_i
|
2012-12-05 20:35:49 -05:00
|
|
|
end
|
|
|
|
|
2013-05-31 12:02:27 -04:00
|
|
|
it 'expires failed stat' do
|
|
|
|
failed_job
|
|
|
|
assert_equal Sidekiq::Processor::STATS_TIMEOUT, Sidekiq.redis { |conn| conn.ttl(failed_today_key) }
|
|
|
|
end
|
|
|
|
end
|
2012-12-05 20:35:49 -05:00
|
|
|
end
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|
|
|
|
end
|