Skip to content

Commit

Permalink
Add isolated tests to sentry-rails (#1497)
Browse files Browse the repository at this point in the history
* Add isolated test case for ActiveJob extension

* Add a rake task for isolated specs
  • Loading branch information
st0012 committed Jul 4, 2021
1 parent 083a0d6 commit 7b9f8fc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sentry-rails/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ RSpec::Core::RakeTask.new(:spec).tap do |task|
task.rspec_opts = "--order rand"
end

task :default => :spec
task :isolated_specs do
Dir["spec/isolated/*"].each do |file|
sh "bundle exec ruby #{file}"
end
end

task :default => [:spec, :isolated_specs]
59 changes: 59 additions & 0 deletions sentry-rails/spec/isolated/active_job_activation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true
# for https://github.com/getsentry/sentry-ruby/issues/1249
require "active_job"
require "active_support/all"
require "sentry/rails"
require "minitest/autorun"

class TestApp < Rails::Application
end

IO_STUB = StringIO.new

app = TestApp

# to simulate jobs being load during the eager_load initializer
app.initializer :eager_load! do
Object.class_eval <<~CODE
class ApplicationJob < ActiveJob::Base
self.logger = Logger.new(IO_STUB)
self.queue_adapter = :inline
rescue_from Exception do |exception|
logger.info(">>> RESCUED")
end
end
class ErrorJob < ApplicationJob
around_perform do |job, block|
result = block.call
logger.info(">>> I SHOULD NEVER BE EXECUTED!")
end
def perform
1/0
end
end
CODE
end

app.config.eager_load = true
app.initializer :sentry do
Sentry.init do |config|
config.logger = Logger.new(nil)
config.dsn = 'https://2fb45f003d054a7ea47feb45898f7649@o447951.ingest.sentry.io/5434472'
config.background_worker_threads = 0
end
end

app.initialize!

class ActiveJobExtensionsTest < ActiveSupport::TestCase
def test_the_extension_is_loaded_before_eager_load_is_called
ErrorJob.perform_later

log_result = IO_STUB.string
assert_match(/RESCUED/, log_result, "ApplicationJob's rescue_from should be called")
refute_match(/I SHOULD NEVER BE EXECUTED/, log_result, "ErrorJob's around_perform should not be triggered")
end
end

0 comments on commit 7b9f8fc

Please sign in to comment.