Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
modosc committed Jan 11, 2025
1 parent 6d0cc35 commit daee3d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
9 changes: 5 additions & 4 deletions sentry-rails/lib/sentry/rails/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def already_supported_by_sentry_integration?
class SentryReporter
OP_NAME = "queue.active_job"
SPAN_ORIGIN = "auto.queue.active_job"

NOTIFICATION_NAME = "retry_stopped.active_job"
class << self
def record(job, &block)
Sentry.with_scope do |scope|
Expand Down Expand Up @@ -67,17 +67,18 @@ def capture_exception(job, e)
end

def register_retry_stopped_subscriber
ActiveSupport::Notifications.subscribe("retry_stopped.active_job") do |*args|
ActiveSupport::Notifications.subscribe(NOTIFICATION_NAME) do |*args|
retry_stopped_handler(*args)

Check warning on line 71 in sentry-rails/lib/sentry/rails/active_job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/active_job.rb#L71

Added line #L71 was not covered by tests
end
end

def retry_stopped_handler(*args)
return if !Sentry.initialized? || already_supported_by_sentry_integration?
return unless Sentry.configuration.rails.active_job_report_after_job_retries
event = ActiveSupport::Notifications::Event.new(*args)
job = event.payload[:job]
error = event.payload[:error]

Check warning on line 78 in sentry-rails/lib/sentry/rails/active_job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/active_job.rb#L76-L78

Added lines #L76 - L78 were not covered by tests

return if !Sentry.initialized? || job.already_supported_by_sentry_integration?
return unless Sentry.configuration.rails.active_job_report_after_job_retries
capture_exception(job, error)

Check warning on line 82 in sentry-rails/lib/sentry/rails/active_job.rb

View check run for this annotation

Codecov / codecov/patch

sentry-rails/lib/sentry/rails/active_job.rb#L80-L82

Added lines #L80 - L82 were not covered by tests
end

Expand Down
42 changes: 41 additions & 1 deletion sentry-rails/spec/sentry/rails/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class FailedJobWithCron < FailedJob
sentry_monitor_check_ins slug: "failed_job", monitor_config: Sentry::Cron::MonitorConfig.from_crontab("5 * * * *")
end

class FailedJobWithRetryOn < FailedJob
retry_on StandardError, attempts: 3, wait: 0
end

RSpec.describe "without Sentry initialized", type: :job do
it "runs job" do
Expand Down Expand Up @@ -318,7 +321,6 @@ def perform(event, hint)

it "does not trigger sentry and re-raises" do
expect { FailedJob.perform_now }.to raise_error(FailedJob::TestError)

expect(transport.events.size).to eq(0)
end
end
Expand Down Expand Up @@ -386,4 +388,42 @@ def perform(event, hint)
end
end
end

describe "active_job_report_after_job_retries" do
before do
allow(Sentry::Rails::ActiveJobExtensions::SentryReporter)
.to receive(:capture_exception)
.and_call_original
end
context "when active_job_report_after_job_retries is false" do
it "reports 3 exceptions" do
assert_performed_jobs 3 do
FailedJobWithRetryOn.perform_later rescue nil
end

expect(Sentry::Rails::ActiveJobExtensions::SentryReporter)
.to have_received(:capture_exception)
.exactly(3).times
end
end
context "when active_job_report_after_job_retries is true" do
before do
Sentry.configuration.rails.active_job_report_after_job_retries = true
end

after do
Sentry.configuration.rails.active_job_report_after_job_retries = false
end

it "reports 1 exception" do
assert_performed_jobs 3 do
FailedJobWithRetryOn.perform_later rescue nil
end

expect(Sentry::Rails::ActiveJobExtensions::SentryReporter)
.to have_received(:capture_exception)
.exactly(1).times
end
end
end
end

0 comments on commit daee3d8

Please sign in to comment.