2015-12-31 15:33:35 -08:00
|
|
|
# frozen_string_literal: true
|
2022-03-03 12:50:03 -08:00
|
|
|
|
|
|
|
require_relative "helper"
|
2022-05-31 13:37:31 -07:00
|
|
|
require "sidekiq/component"
|
2022-03-03 12:50:03 -08:00
|
|
|
require "stringio"
|
|
|
|
require "logger"
|
2012-07-31 11:43:09 -04:00
|
|
|
|
|
|
|
ExceptionHandlerTestException = Class.new(StandardError)
|
|
|
|
TEST_EXCEPTION = ExceptionHandlerTestException.new("Something didn't work!")
|
|
|
|
|
2022-05-31 13:37:31 -07:00
|
|
|
class Thing
|
|
|
|
include Sidekiq::Component
|
|
|
|
attr_reader :config
|
|
|
|
|
|
|
|
def initialize(config)
|
|
|
|
@config = config
|
|
|
|
end
|
2012-08-02 20:46:06 -07:00
|
|
|
|
|
|
|
def invoke_exception(args)
|
|
|
|
raise TEST_EXCEPTION
|
|
|
|
rescue ExceptionHandlerTestException => e
|
2022-03-03 12:50:03 -08:00
|
|
|
handle_exception(e, args)
|
2012-08-02 20:46:06 -07:00
|
|
|
end
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
2022-05-31 13:37:31 -07:00
|
|
|
describe Sidekiq::Component do
|
2012-07-31 11:43:09 -04:00
|
|
|
describe "with mock logger" do
|
|
|
|
before do
|
2022-05-31 13:37:31 -07:00
|
|
|
@config = Sidekiq
|
|
|
|
@config[:error_handlers] << Sidekiq.method(:default_error_handler)
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
after do
|
2022-05-31 13:37:31 -07:00
|
|
|
@config[:error_handlers].clear
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "logs the exception to Sidekiq.logger" do
|
2022-05-31 13:37:31 -07:00
|
|
|
output = capture_logging do
|
|
|
|
Thing.new(@config).invoke_exception(a: 1)
|
|
|
|
end
|
|
|
|
assert_match(/"a":1/, output, "didn't include the context")
|
|
|
|
assert_match(/Something didn't work!/, output, "didn't include the exception message")
|
|
|
|
assert_match(/test\/test_exception_handler.rb/, output, "didn't include the backtrace")
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
2013-10-07 14:44:31 +01:00
|
|
|
|
|
|
|
describe "when the exception does not have a backtrace" do
|
|
|
|
it "does not fail" do
|
|
|
|
exception = ExceptionHandlerTestException.new
|
|
|
|
assert_nil exception.backtrace
|
|
|
|
|
2022-05-31 13:37:31 -07:00
|
|
|
Thing.new(@config).handle_exception exception
|
2013-10-07 14:44:31 +01:00
|
|
|
end
|
|
|
|
end
|
2012-07-31 11:43:09 -04:00
|
|
|
end
|
|
|
|
end
|