Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sentry-rails/spec/active_job/shared_examples/error_capture.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.shared_examples "an ActiveJob backend that captures errors" do
let(:failing_job) do
job_fixture do
def perform
raise "boom from failing_job spec"
end
end
end

it "captures an error event when a job fails" do
expect do
failing_job.perform_later
drain
end.to raise_error(RuntimeError, /boom from failing_job spec/)

expect(sentry_events.size).to eq(1)

exception = extract_sentry_exceptions(sentry_events.last).first
expect(exception.value).to match(/boom from failing_job spec/)
end
end
25 changes: 25 additions & 0 deletions sentry-rails/spec/active_job/shared_examples/retry_semantics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

RSpec.shared_examples "an ActiveJob backend that respects retry semantics" do
let(:retryable_job) do
job_fixture do
retry_on StandardError, attempts: 3, wait: 0

def perform
raise "boom from retryable_job spec"
end
end
end

it "captures one error event after retries are exhausted" do
expect do
retryable_job.perform_later
3.times { drain }
end.to raise_error(RuntimeError, /boom from retryable_job spec/)

expect(sentry_events.size).to eq(1)

exception = extract_sentry_exceptions(sentry_events.last).first
expect(exception.value).to match(/boom from retryable_job spec/)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

RSpec.shared_examples "a Sentry-instrumented ActiveJob backend" do
it_behaves_like "an ActiveJob backend that captures errors"
it_behaves_like "an ActiveJob backend that respects retry semantics"
end
46 changes: 46 additions & 0 deletions sentry-rails/spec/active_job/support/harness.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

RSpec.shared_context "active_job backend harness" do |adapter:|
let(:adapter) { adapter }

around do |example|
make_basic_app
setup_sentry_test

::ActiveJob::Base.queue_adapter = adapter

boot_adapter(adapter)

example.run
ensure
reset_adapter(adapter)

teardown_sentry_test
end

def boot_adapter(_adapter)
# Per-adapter setup hook. Backends extend this when they need to load
# schemas, start supervisors, or otherwise prepare the environment.
end

def reset_adapter(_adapter)
# Per-adapter teardown hook. Backends extend this to truncate tables
# or otherwise clean up state between examples.
end

def drain
case adapter
when :test
perform_enqueued_jobs
else
raise NotImplementedError, "active_job backend harness has no drain strategy for adapter: #{adapter.inspect}"
end
end

def job_fixture(name = nil, &block)
name ||= "JobFixture_#{SecureRandom.hex(4)}"
klass = Class.new(::ActiveJob::Base, &block)
stub_const(name, klass)
klass
end
end
9 changes: 9 additions & 0 deletions sentry-rails/spec/active_job/test_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe "Sentry + ActiveJob on the test adapter", type: :job do
include_context "active_job backend harness", adapter: :test

it_behaves_like "a Sentry-instrumented ActiveJob backend"
end
2 changes: 2 additions & 0 deletions sentry-rails/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
end

Dir["#{__dir__}/support/**/*.rb"].each { |file| require file }
Dir["#{__dir__}/active_job/support/**/*.rb"].each { |file| require file }
Dir["#{__dir__}/active_job/shared_examples/**/*.rb"].each { |file| require file }

RAILS_VERSION = Rails.version.to_f

Expand Down
Loading