2012-07-31 11:43:09 -04:00
|
|
|
require 'helper'
|
|
|
|
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-09-22 17:38:33 -04:00
|
|
|
class TestExceptionHandler < Sidekiq::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
|
2014-03-19 20:41:11 -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
|
2013-10-07 09:44:31 -04:00
|
|
|
|
|
|
|
describe "when the exception does not have a backtrace" do
|
|
|
|
it "does not fail" do
|
|
|
|
exception = ExceptionHandlerTestException.new
|
|
|
|
assert_nil exception.backtrace
|
|
|
|
|
|
|
|
begin
|
|
|
|
Component.new.handle_exception exception
|
|
|
|
pass
|
2014-03-19 20:41:11 -04:00
|
|
|
rescue StandardError
|
2013-10-07 09:44:31 -04:00
|
|
|
flunk "failed handling a nil backtrace"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|