Sha256: 17a4f02af1f3ecd191f4a324e0ec5a0d25f1ee116c692e829a85383de5889ca0

Contents?: true

Size: 1.49 KB

Versions: 15

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true
require_relative 'helper'
require 'sidekiq/exception_handler'
require 'stringio'
require 'logger'

ExceptionHandlerTestException = Class.new(StandardError)
TEST_EXCEPTION = ExceptionHandlerTestException.new("Something didn't work!")

class Component
  include Sidekiq::ExceptionHandler

  def invoke_exception(args)
    raise TEST_EXCEPTION
  rescue ExceptionHandlerTestException => e
    handle_exception(e,args)
  end
end

class TestExceptionHandler < Sidekiq::Test
  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
      Component.new.invoke_exception(:a => 1)
      @str_logger.rewind
      log = @str_logger.readlines
      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")
    end

    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
        rescue StandardError
          flunk "failed handling a nil backtrace"
        end
      end
    end
  end

end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
sidekiq-5.0.0.beta1 test/test_exception_handler.rb
sidekiq-4.2.9 test/test_exception_handler.rb
sidekiq-4.2.8 test/test_exception_handler.rb
sidekiq-4.2.7 test/test_exception_handler.rb
sidekiq-4.2.6 test/test_exception_handler.rb
sidekiq-4.2.5 test/test_exception_handler.rb
sidekiq-4.2.4 test/test_exception_handler.rb
sidekiq-4.2.3 test/test_exception_handler.rb
sidekiq-4.2.2 test/test_exception_handler.rb
sidekiq-4.2.1 test/test_exception_handler.rb
sidekiq-4.2.0 test/test_exception_handler.rb
sidekiq-4.1.4 test/test_exception_handler.rb
sidekiq-4.1.3 test/test_exception_handler.rb
sr-sidekiq-4.1.6 test/test_exception_handler.rb
sidekiq-4.1.2 test/test_exception_handler.rb