1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Add Sidekiq::Processor testing, with bug fixes

This commit is contained in:
Mike Perham 2012-01-26 12:45:04 -08:00
parent 4043f11f78
commit 922d5f8dbe
6 changed files with 76 additions and 11 deletions

View file

@ -2,4 +2,4 @@
- resque-ui-esque web ui?
- graceful shutdown
- monit/god example scripts
- nice homepage
- prettier homepage

View file

@ -1,8 +1,10 @@
require 'active_support/inflector'
require 'sidekiq/util'
require 'celluloid'
module Sidekiq
class Processor
include Celluloid
include Util
include Celluloid unless $TESTING
def initialize(boss)
@boss = boss
@ -10,9 +12,9 @@ module Sidekiq
def process(msg)
begin
klass = msg['class'].constantize
klass = constantize(msg['class'])
klass.new.perform(*msg['args'])
@boss.processor_done!(current_actor)
@boss.processor_done!(self)
rescue => ex
send_to_airbrake(msg, ex) if defined?(::Airbrake)
raise ex
@ -21,8 +23,8 @@ module Sidekiq
def send_to_airbrake(msg, ex)
::Airbrake.notify(:error_class => ex.class.name,
:error_message => "#{ex.class.name}: #{e.message}",
:parameters => json)
:error_message => "#{ex.class.name}: #{ex.message}",
:parameters => msg)
end
end
end

View file

@ -1,6 +1,17 @@
module Sidekiq
module Util
def constantize(camel_cased_word)
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?
constant = Object
names.each do |name|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
def watchdog(last_words)
yield
rescue => ex

View file

@ -4,12 +4,12 @@ require File.expand_path('../lib/sidekiq/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Mike Perham"]
gem.email = ["mperham@gmail.com"]
gem.description = gem.summary = ""
gem.homepage = ""
gem.description = gem.summary = "Simple, efficient message processing for Ruby"
gem.homepage = "http://mperham.github.com/sidekiq"
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.executables = ['bin/sidekiq']
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.test_files = `git ls-files -- test/*`.split("\n")
gem.name = "sidekiq"
gem.require_paths = ["lib"]
gem.version = Sidekiq::VERSION

View file

@ -1,3 +1,4 @@
$TESTING = true
if false
require 'simplecov'
SimpleCov.start

51
test/test_processor.rb Normal file
View file

@ -0,0 +1,51 @@
require 'helper'
require 'sidekiq/processor'
class TestProcessor < MiniTest::Unit::TestCase
describe 'with mock setup' do
before do
$invokes = 0
$errors = []
@boss = MiniTest::Mock.new
end
class MockWorker
def perform(args)
raise "kerboom!" if args == 'boom'
$invokes += 1
end
end
it 'processes as expected' do
msg = { 'class' => MockWorker.to_s, 'args' => ['myarg'] }
processor = ::Sidekiq::Processor.new(@boss)
@boss.expect(:processor_done!, nil, [processor])
processor.process(msg)
@boss.verify
assert_equal 1, $invokes
assert_equal 0, $errors.size
end
it 'handles exceptions' do
msg = { 'class' => MockWorker.to_s, 'args' => ['boom'] }
processor = ::Sidekiq::Processor.new(@boss)
assert_raises RuntimeError do
processor.process(msg)
end
@boss.verify
assert_equal 0, $invokes
assert_equal 1, $errors.size
assert_equal "RuntimeError", $errors[0][:error_class]
assert_equal msg, $errors[0][:parameters]
end
end
end
class FakeAirbrake
def self.notify(hash)
$errors << hash
end
end
Airbrake = FakeAirbrake