2012-07-31 11:43:09 -04:00
|
|
|
require 'helper'
|
|
|
|
require 'sidekiq'
|
|
|
|
require 'sidekiq/exception_handler'
|
|
|
|
require 'stringio'
|
|
|
|
require 'logger'
|
|
|
|
|
|
|
|
ExceptionHandlerTestException = Class.new(StandardError)
|
|
|
|
TEST_EXCEPTION = ExceptionHandlerTestException.new("Something didn't work!")
|
|
|
|
|
2012-08-02 23:46:06 -04:00
|
|
|
class Component
|
2012-11-03 23:47:45 -04:00
|
|
|
include Sidekiq::ExceptionHandler
|
2012-08-02 23:46:06 -04:00
|
|
|
|
|
|
|
def invoke_exception(args)
|
|
|
|
raise TEST_EXCEPTION
|
|
|
|
rescue ExceptionHandlerTestException => e
|
|
|
|
handle_exception(e,args)
|
|
|
|
end
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
2013-05-12 17:25:30 -04:00
|
|
|
class TestExceptionHandler < Minitest::Test
|
2012-07-31 11:43:09 -04:00
|
|
|
describe "with mock logger" do
|
|
|
|
before do
|
|
|
|
@old_logger = Sidekiq.logger
|
|
|
|
@str_logger = StringIO.new
|
|
|
|
Sidekiq.logger = Logger.new(@str_logger)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Sidekiq.logger = @old_logger
|
|
|
|
end
|
|
|
|
|
|
|
|
it "logs the exception to Sidekiq.logger" do
|
2012-08-02 23:46:06 -04:00
|
|
|
Component.new.invoke_exception(:a => 1)
|
2012-07-31 11:43:09 -04:00
|
|
|
@str_logger.rewind
|
|
|
|
log = @str_logger.readlines
|
2012-08-02 23:46:06 -04:00
|
|
|
assert_match /a=>1/, log[0], "didn't include the context"
|
|
|
|
assert_match /Something didn't work!/, log[1], "didn't include the exception message"
|
|
|
|
assert_match /test\/test_exception_handler.rb/, log[2], "didn't include the backtrace"
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with fake Airbrake" do
|
|
|
|
before do
|
2013-05-12 17:25:30 -04:00
|
|
|
::Airbrake = Minitest::Mock.new
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.send(:remove_const, "Airbrake") # HACK should probably inject Airbrake etc into this class in the future
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies Airbrake" do
|
2012-09-06 13:49:58 -04:00
|
|
|
::Airbrake.expect(:notify_or_ignore,nil,[TEST_EXCEPTION,:parameters => { :a => 1 }])
|
2012-08-02 23:46:06 -04:00
|
|
|
Component.new.invoke_exception(:a => 1)
|
2012-07-31 11:43:09 -04:00
|
|
|
::Airbrake.verify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-26 23:33:54 -04:00
|
|
|
describe "with fake Honeybadger" do
|
|
|
|
before do
|
2013-05-12 17:25:30 -04:00
|
|
|
::Honeybadger = Minitest::Mock.new
|
2012-10-26 23:33:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.send(:remove_const, "Honeybadger") # HACK should probably inject Honeybadger etc into this class in the future
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies Honeybadger" do
|
|
|
|
::Honeybadger.expect(:notify_or_ignore,nil,[TEST_EXCEPTION,:parameters => { :a => 1 }])
|
|
|
|
Component.new.invoke_exception(:a => 1)
|
|
|
|
::Honeybadger.verify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-31 11:43:09 -04:00
|
|
|
describe "with fake ExceptionNotifier" do
|
|
|
|
before do
|
2013-07-15 19:09:58 -04:00
|
|
|
::ExceptionNotifier = MiniTest::Mock.new
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.send(:remove_const, "ExceptionNotifier")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies ExceptionNotifier" do
|
2013-07-15 19:09:58 -04:00
|
|
|
::ExceptionNotifier.expect(:notify_exception,true,[TEST_EXCEPTION, :data => { :message => { :b => 2 } }])
|
2012-08-02 23:46:06 -04:00
|
|
|
Component.new.invoke_exception(:b => 2)
|
2013-07-15 19:09:58 -04:00
|
|
|
::ExceptionNotifier.verify
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with fake Exceptional" do
|
|
|
|
before do
|
|
|
|
::Exceptional = Class.new do
|
|
|
|
|
|
|
|
def self.context(msg)
|
|
|
|
@msg = msg
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.check_context
|
|
|
|
@msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-12 17:25:30 -04:00
|
|
|
::Exceptional::Config = Minitest::Mock.new
|
|
|
|
::Exceptional::Remote = Minitest::Mock.new
|
|
|
|
::Exceptional::ExceptionData = Minitest::Mock.new
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Object.send(:remove_const, "Exceptional")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "notifies Exceptional" do
|
|
|
|
::Exceptional::Config.expect(:should_send_to_api?,true)
|
2012-11-05 17:04:28 -05:00
|
|
|
exception_data = Object.new
|
2012-07-31 11:43:09 -04:00
|
|
|
::Exceptional::Remote.expect(:error,nil,[exception_data])
|
|
|
|
::Exceptional::ExceptionData.expect(:new,exception_data,[TEST_EXCEPTION])
|
2012-08-02 23:46:06 -04:00
|
|
|
Component.new.invoke_exception(:c => 3)
|
2012-07-31 11:43:09 -04:00
|
|
|
assert_equal({:c => 3},::Exceptional.check_context,"did not record arguments properly")
|
|
|
|
::Exceptional::Config.verify
|
|
|
|
::Exceptional::Remote.verify
|
|
|
|
::Exceptional::ExceptionData.verify
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|