2012-01-26 15:45:04 -05:00
|
|
|
require 'helper'
|
|
|
|
require 'sidekiq/processor'
|
|
|
|
|
|
|
|
class TestProcessor < MiniTest::Unit::TestCase
|
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
|
|
|
|
@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
|
|
|
|
|
|
|
|
it 'processes as expected' do
|
2012-06-21 23:51:47 -04:00
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['myarg'] })
|
2012-07-31 11:43:09 -04:00
|
|
|
@boss.expect(:processor_done!, nil, [@processor])
|
|
|
|
@processor.process(msg, 'default')
|
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
|
|
|
|
msg = Sidekiq.dump_json({ 'class' => MockWorker.to_s, 'args' => ['boom'] })
|
2012-08-02 23:46:06 -04:00
|
|
|
begin
|
|
|
|
@processor.process(msg, 'default')
|
|
|
|
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
|
|
|
|
|
|
|
|
begin
|
|
|
|
@processor.process(msg, 'default')
|
|
|
|
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)
|
|
|
|
@boss.expect(:processor_done!, nil, [processor])
|
|
|
|
processor.process(msgstr, 'default')
|
|
|
|
assert_equal [['myarg']], msg['args']
|
|
|
|
end
|
2012-01-26 15:45:04 -05:00
|
|
|
end
|
|
|
|
end
|