Permalink
Browse files

Concerted effort to increase test coverage: 86 -> 88%

  • Loading branch information...
1 parent 9aef296 commit 9867b728c4ed06cb1eb6723c2e057315c6c9acdc @mperham committed Feb 1, 2014
Showing with 109 additions and 4 deletions.
  1. +2 −0 Gemfile
  2. +29 −1 lib/sidekiq/actor.rb
  3. +13 −1 lib/sidekiq/fetch.rb
  4. +1 −2 sidekiq.gemspec
  5. +2 −0 test/helper.rb
  6. +6 −0 test/test_cli.rb
  7. +56 −0 test/test_fetch.rb
View
2 Gemfile
@@ -7,3 +7,5 @@ platforms :rbx do
gem 'minitest' # if using minitest
gem 'rubinius-developer_tools' # if using any of coverage, debugger, profiler
end
+
+gem 'sqlite3'
View
30 lib/sidekiq/actor.rb
@@ -1,7 +1,35 @@
module Sidekiq
module Actor
+
+ module ClassMethods
+ def trap_exit(*args)
+ end
+ def new_link(*args)
+ new(*args)
+ end
+ end
+
+ module InstanceMethods
+ def current_actor
+ self
+ end
+ def after(interval)
+ end
+ def alive?
+ !@dead
+ end
+ def terminate
+ @dead = true
+ end
+ end
+
def self.included(klass)
- klass.send(:include, Celluloid)
+ if $TESTING
+ klass.send(:include, InstanceMethods)
+ klass.send(:extend, ClassMethods)
+ else
+ klass.send(:include, Celluloid)
+ end
end
end
end
View
14 lib/sidekiq/fetch.rb
@@ -12,6 +12,8 @@ class Fetcher
TIMEOUT = 1
+ attr_reader :down
+
def initialize(mgr, options)
@down = nil
@mgr = mgr
@@ -47,6 +49,12 @@ def fetch
end
end
+ private
+
+ def pause
+ sleep(TIMEOUT)
+ end
+
def handle_fetch_exception(ex)
if !@down
logger.error("Error fetching message: #{ex}")
@@ -55,7 +63,7 @@ def handle_fetch_exception(ex)
end
end
@down ||= Time.now
- sleep(TIMEOUT)
+ pause
after(0) { fetch }
rescue Task::TerminatedError
# If redis is down when we try to shut down, all the fetch backlog
@@ -69,6 +77,10 @@ def self.done!
@done = true
end
+ def self.reset # testing only
+ @done = nil
+ end
+
def self.done?
@done
end
View
3 sidekiq.gemspec
@@ -22,7 +22,6 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'sinatra'
gem.add_development_dependency 'minitest', '~> 4.2'
gem.add_development_dependency 'rake'
- gem.add_development_dependency 'actionmailer', '>= 4.0.0'
- gem.add_development_dependency 'activerecord', '>= 4.0.0'
+ gem.add_development_dependency 'rails', '>= 4.0.0'
gem.add_development_dependency 'coveralls'
end
View
2 test/helper.rb
@@ -2,13 +2,15 @@
require 'coveralls'
Coveralls.wear! do
add_filter "/test/"
+ add_filter "/myapp/"
end
ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
if ENV.has_key?("SIMPLECOV")
require 'simplecov'
SimpleCov.start do
add_filter "/test/"
+ add_filter "/myapp/"
end
end
View
6 test/test_cli.rb
@@ -30,6 +30,12 @@ class TestCli < Sidekiq::Test
assert @cli.valid?
end
+ it 'boots rails' do
+ refute defined?(::Rails)
+ @cli.parse(['sidekiq', '-r', './myapp'])
+ assert defined?(::Rails)
+ end
+
it 'changes concurrency' do
@cli.parse(['sidekiq', '-c', '60', '-r', './test/fake_env.rb'])
assert_equal 60, Sidekiq.options[:concurrency]
View
56 test/test_fetch.rb
@@ -40,5 +40,61 @@ class TestFetcher < Sidekiq::Test
assert_equal 2, q1.size
assert_equal 1, q2.size
end
+
+ describe 'fetching' do
+ before do
+ Sidekiq::Fetcher.reset
+ end
+
+ it 'instantiates' do
+ begin
+ Sidekiq.options[:fetch] = NullFetch
+ mgr = Minitest::Mock.new
+ fetch = Sidekiq::Fetcher.new(mgr, {})
+ fetch.fetch
+ Sidekiq::Fetcher.done!
+ ensure
+ Sidekiq.options[:fetch] = Sidekiq::BasicFetch
+ end
+ end
+
+ class NullFetch
+ def initialize(opts)
+ end
+ def retrieve_work
+ end
+ def self.bulk_requeue(*args)
+ end
+ end
+
+ it 'handles redis network errors' do
+ begin
+ Sidekiq.logger.level = Logger::FATAL
+ Sidekiq.options[:fetch] = ErrorFetch
+ mgr = Minitest::Mock.new
+ fetch = Sidekiq::Fetcher.new(mgr, {})
+ def fetch.pause
+ end
+ refute fetch.down
+ fetch.fetch
+ Sidekiq::Fetcher.done!
+ assert fetch.down
+ ensure
+ Sidekiq.options[:fetch] = Sidekiq::BasicFetch
+ Sidekiq.logger.level = Logger::ERROR
+ end
+ end
+
+ class ErrorFetch
+ def initialize(opts)
+ end
+ def retrieve_work
+ raise IOError, "ker-BOOM"
+ end
+ def self.bulk_requeue(*args)
+ end
+ end
+ end
+
end
end

0 comments on commit 9867b72

Please sign in to comment.