diff --git a/sentry-rails/spec/active_job/continuable_spec.rb b/sentry-rails/spec/active_job/continuable_spec.rb index f52cd37f2..0e4a6e669 100644 --- a/sentry-rails/spec/active_job/continuable_spec.rb +++ b/sentry-rails/spec/active_job/continuable_spec.rb @@ -5,7 +5,7 @@ if RAILS_VERSION >= 8.1 require "active_job/continuation/test_helper" - RSpec.describe "Sentry + ActiveJob::Continuable", type: :job do + RSpec.describe "Sentry + ActiveJob::Continuable", type: :job, isolate: :group do include_context "active_job backend harness", adapter: :test include_context "test adapter" include ActiveJob::Continuation::TestHelper diff --git a/sentry-rails/spec/active_job/delayed_job_adapter_spec.rb b/sentry-rails/spec/active_job/delayed_job_adapter_spec.rb index a807c48ea..9d3b685ee 100644 --- a/sentry-rails/spec/active_job/delayed_job_adapter_spec.rb +++ b/sentry-rails/spec/active_job/delayed_job_adapter_spec.rb @@ -20,7 +20,7 @@ return end -RSpec.describe "Sentry + ActiveJob on the delayed_job adapter", type: :job do +RSpec.describe "Sentry + ActiveJob on the delayed_job adapter", type: :job, isolate: :group do include ActiveSupport::Testing::TimeHelpers include_context "active_job backend harness", adapter: :delayed_job include_context "delayed_job adapter" diff --git a/sentry-rails/spec/active_job/good_job_cron_spec.rb b/sentry-rails/spec/active_job/good_job_cron_spec.rb index 746e4729b..4f0d997b6 100644 --- a/sentry-rails/spec/active_job/good_job_cron_spec.rb +++ b/sentry-rails/spec/active_job/good_job_cron_spec.rb @@ -8,7 +8,7 @@ end if defined?(::GoodJob) - RSpec.describe Sentry::Rails::GoodJobCron, type: :job do + RSpec.describe Sentry::Rails::GoodJobCron, type: :job, isolate: :group do before(:all) do skip "GoodJob cron specs run the real GoodJob adapter, which ActiveJob::TestHelper supports from Rails 7.1" if RAILS_VERSION < 7.1 end @@ -458,7 +458,7 @@ def perform; end end if RAILS_VERSION >= 7.1 - RSpec.describe "Sentry::Rails::GoodJobCron on a GoodJob database", type: :job do + RSpec.describe "Sentry::Rails::GoodJobCron on a GoodJob database", type: :job, isolate: :group do include_context "active_job backend harness", adapter: :good_job include_context "good_job database" diff --git a/sentry-rails/spec/active_job/good_job_cron_subscriber_spec.rb b/sentry-rails/spec/active_job/good_job_cron_subscriber_spec.rb index 99a643d78..3df36ae25 100644 --- a/sentry-rails/spec/active_job/good_job_cron_subscriber_spec.rb +++ b/sentry-rails/spec/active_job/good_job_cron_subscriber_spec.rb @@ -8,7 +8,7 @@ end if defined?(::GoodJob) - RSpec.describe "GoodJob cron notification subscriber", type: :job do + RSpec.describe "GoodJob cron notification subscriber", type: :job, isolate: :group do include_context "active_job backend harness", adapter: :test include_context "test adapter" diff --git a/sentry-rails/spec/active_job/good_job_spec.rb b/sentry-rails/spec/active_job/good_job_spec.rb index e4c8889f2..b8eebc807 100644 --- a/sentry-rails/spec/active_job/good_job_spec.rb +++ b/sentry-rails/spec/active_job/good_job_spec.rb @@ -12,7 +12,7 @@ GOOD_JOB_ADAPTER_FOR_TEST = ::ActiveJob::QueueAdapters::GoodJobAdapter.new(execution_mode: :external) -RSpec.describe "Sentry + ActiveJob on GoodJob", type: :job do +RSpec.describe "Sentry + ActiveJob on GoodJob", type: :job, isolate: :group do include ActiveSupport::Testing::TimeHelpers include_context "active_job backend harness", adapter: :good_job include_context "good_job database" diff --git a/sentry-rails/spec/active_job/resque_adapter_spec.rb b/sentry-rails/spec/active_job/resque_adapter_spec.rb index cc9b8ffe0..d203ad603 100644 --- a/sentry-rails/spec/active_job/resque_adapter_spec.rb +++ b/sentry-rails/spec/active_job/resque_adapter_spec.rb @@ -21,7 +21,7 @@ return end -RSpec.describe "Sentry + ActiveJob on the resque adapter", type: :job do +RSpec.describe "Sentry + ActiveJob on the resque adapter", type: :job, isolate: :group do include ActiveSupport::Testing::TimeHelpers include_context "active_job backend harness", adapter: :resque include_context "resque adapter" diff --git a/sentry-rails/spec/active_job/sidekiq_adapter_spec.rb b/sentry-rails/spec/active_job/sidekiq_adapter_spec.rb index be7b15fd7..964ab554e 100644 --- a/sentry-rails/spec/active_job/sidekiq_adapter_spec.rb +++ b/sentry-rails/spec/active_job/sidekiq_adapter_spec.rb @@ -23,7 +23,7 @@ return end -RSpec.describe "Sentry + ActiveJob on the sidekiq adapter", type: :job do +RSpec.describe "Sentry + ActiveJob on the sidekiq adapter", type: :job, isolate: :group do include_context "active_job backend harness", adapter: :sidekiq include_context "sidekiq adapter" diff --git a/sentry-rails/spec/active_job/solid_queue_recurring_spec.rb b/sentry-rails/spec/active_job/solid_queue_recurring_spec.rb index 30aedb04c..9bc98790a 100644 --- a/sentry-rails/spec/active_job/solid_queue_recurring_spec.rb +++ b/sentry-rails/spec/active_job/solid_queue_recurring_spec.rb @@ -7,7 +7,7 @@ SOLID_QUEUE_RECURRING_ADAPTER = ::ActiveJob::QueueAdapters::SolidQueueAdapter.new - RSpec.describe Sentry::Rails::SolidQueueRecurring, type: :job do + RSpec.describe Sentry::Rails::SolidQueueRecurring, type: :job, isolate: :group do include_context "active_job backend harness", adapter: :solid_queue let(:excluded_tasks) { [] } diff --git a/sentry-rails/spec/active_job/solid_queue_spec.rb b/sentry-rails/spec/active_job/solid_queue_spec.rb index 6d511b367..91210fc3c 100644 --- a/sentry-rails/spec/active_job/solid_queue_spec.rb +++ b/sentry-rails/spec/active_job/solid_queue_spec.rb @@ -5,7 +5,7 @@ if RAILS_VERSION >= 7.1 && RUBY_VERSION >= "3.1" require "solid_queue" - RSpec.describe "Sentry + ActiveJob on SolidQueue", type: :job do + RSpec.describe "Sentry + ActiveJob on SolidQueue", type: :job, isolate: :group do include ActiveSupport::Testing::TimeHelpers include_context "active_job backend harness", adapter: :solid_queue diff --git a/sentry-rails/spec/active_job/test_adapter_spec.rb b/sentry-rails/spec/active_job/test_adapter_spec.rb index d968699d6..c2cbe7add 100644 --- a/sentry-rails/spec/active_job/test_adapter_spec.rb +++ b/sentry-rails/spec/active_job/test_adapter_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe "Sentry + ActiveJob on the test adapter", type: :job do +RSpec.describe "Sentry + ActiveJob on the test adapter", type: :job, isolate: :group do include_context "active_job backend harness", adapter: :test include_context "test adapter" diff --git a/sentry-rails/spec/sentry/rails/action_cable_spec.rb b/sentry-rails/spec/sentry/rails/action_cable_spec.rb index e47ad2efa..b13a4317f 100644 --- a/sentry-rails/spec/sentry/rails/action_cable_spec.rb +++ b/sentry-rails/spec/sentry/rails/action_cable_spec.rb @@ -40,7 +40,7 @@ def disconnect end end - RSpec.describe "without Sentry initialized" do + RSpec.describe "without Sentry initialized", isolate: true do before do allow(Sentry).to receive(:get_main_hub).and_return(nil) make_basic_app @@ -66,7 +66,7 @@ def disconnect end end - RSpec.describe "Sentry::Rails::ActionCableExtensions", type: :channel do + RSpec.describe "Sentry::Rails::ActionCableExtensions", type: :channel, isolate: true do let(:transport) { Sentry.get_current_client.transport } after do diff --git a/sentry-rails/spec/sentry/rails/active_job_hub_isolation_spec.rb b/sentry-rails/spec/sentry/rails/active_job_hub_isolation_spec.rb index 0f1bcf7e8..6c1651d8b 100644 --- a/sentry-rails/spec/sentry/rails/active_job_hub_isolation_spec.rb +++ b/sentry-rails/spec/sentry/rails/active_job_hub_isolation_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe "ActiveJob hub isolation through the Rails request stack", type: :request do +RSpec.describe "ActiveJob hub isolation through the Rails request stack", type: :request, isolate: true do let(:transport) { Sentry.get_current_client.transport } before do diff --git a/sentry-rails/spec/sentry/rails/active_job_job_extensions_spec.rb b/sentry-rails/spec/sentry/rails/active_job_job_extensions_spec.rb index 3354eeca1..39c6a1ff6 100644 --- a/sentry-rails/spec/sentry/rails/active_job_job_extensions_spec.rb +++ b/sentry-rails/spec/sentry/rails/active_job_job_extensions_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe "Sentry + ActiveJob with a registered job extension", type: :job do +RSpec.describe "Sentry + ActiveJob with a registered job extension", type: :job, isolate: :group do include_context "active_job backend harness", adapter: :test include_context "test adapter" diff --git a/sentry-rails/spec/sentry/rails/active_job_ownership_spec.rb b/sentry-rails/spec/sentry/rails/active_job_ownership_spec.rb index 3cc14c575..56bdf708c 100644 --- a/sentry-rails/spec/sentry/rails/active_job_ownership_spec.rb +++ b/sentry-rails/spec/sentry/rails/active_job_ownership_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe "Sentry::Rails.active_job_owns_adapter?" do +RSpec.describe "Sentry::Rails.active_job_owns_adapter?", isolate: true do let(:adapter_class_name) { "ActiveJob::QueueAdapters::SidekiqAdapter" } it "is true when Sentry is initialized and the adapter is not skipped" do diff --git a/sentry-rails/spec/sentry/rails/breadcrumbs/active_support_logger_spec.rb b/sentry-rails/spec/sentry/rails/breadcrumbs/active_support_logger_spec.rb index c7ad9eddc..6d41e1818 100644 --- a/sentry-rails/spec/sentry/rails/breadcrumbs/active_support_logger_spec.rb +++ b/sentry-rails/spec/sentry/rails/breadcrumbs/active_support_logger_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe "Sentry::Breadcrumbs::ActiveSupportLogger", type: :request do +RSpec.describe "Sentry::Breadcrumbs::ActiveSupportLogger", type: :request, isolate: true do after do Sentry::Rails::Breadcrumb::ActiveSupportLogger.detach # even though we cleanup breadcrumbs in the rack middleware diff --git a/sentry-rails/spec/sentry/rails/client_spec.rb b/sentry-rails/spec/sentry/rails/client_spec.rb index aff8a1a62..b14d853d5 100644 --- a/sentry-rails/spec/sentry/rails/client_spec.rb +++ b/sentry-rails/spec/sentry/rails/client_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Client, type: :request, retry: 3, skip: Gem::Version.new(Rails.version) < Gem::Version.new('5.1.0') do +RSpec.describe Sentry::Client, type: :request, retry: 3, skip: Gem::Version.new(Rails.version) < Gem::Version.new('5.1.0'), isolate: true do let(:transport) do Sentry.get_current_client.transport end diff --git a/sentry-rails/spec/sentry/rails/configuration_spec.rb b/sentry-rails/spec/sentry/rails/configuration_spec.rb index f1d58a656..8a60e50fd 100644 --- a/sentry-rails/spec/sentry/rails/configuration_spec.rb +++ b/sentry-rails/spec/sentry/rails/configuration_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::Configuration do +RSpec.describe Sentry::Rails::Configuration, isolate: true do it "adds #rails option to Sentry::Configuration" do config = Sentry::Configuration.new diff --git a/sentry-rails/spec/sentry/rails/controller_methods_spec.rb b/sentry-rails/spec/sentry/rails/controller_methods_spec.rb index 0f9c82af6..2009e1cee 100644 --- a/sentry-rails/spec/sentry/rails/controller_methods_spec.rb +++ b/sentry-rails/spec/sentry/rails/controller_methods_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require "sentry/rails/controller_methods" -RSpec.describe Sentry::Rails::ControllerMethods do +RSpec.describe Sentry::Rails::ControllerMethods, isolate: true do include described_class def request diff --git a/sentry-rails/spec/sentry/rails/event_spec.rb b/sentry-rails/spec/sentry/rails/event_spec.rb index 92db6f0c3..2c6647d29 100644 --- a/sentry-rails/spec/sentry/rails/event_spec.rb +++ b/sentry-rails/spec/sentry/rails/event_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' -RSpec.describe Sentry::Event do +RSpec.describe Sentry::Event, isolate: true do before do make_basic_app end diff --git a/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb index 3f8ab3af1..6e70116e1 100644 --- a/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/log_subscriber_spec.rb @@ -4,7 +4,7 @@ require "sentry/rails/log_subscriber" -RSpec.describe Sentry::Rails::LogSubscriber, type: :request do +RSpec.describe Sentry::Rails::LogSubscriber, type: :request, isolate: true do let!(:test_subscriber) { test_subscriber_class.new } after do diff --git a/sentry-rails/spec/sentry/rails/log_subscribers/action_controller_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/log_subscribers/action_controller_subscriber_spec.rb index 029a5c09f..b86d36d02 100644 --- a/sentry-rails/spec/sentry/rails/log_subscribers/action_controller_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/log_subscribers/action_controller_subscriber_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::LogSubscribers::ActionControllerSubscriber, type: :request do +RSpec.describe Sentry::Rails::LogSubscribers::ActionControllerSubscriber, type: :request, isolate: true do context "when logging is enabled" do let(:send_default_pii) { false } diff --git a/sentry-rails/spec/sentry/rails/log_subscribers/action_mailer_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/log_subscribers/action_mailer_subscriber_spec.rb index 6cfc8ea34..d198ece92 100644 --- a/sentry-rails/spec/sentry/rails/log_subscribers/action_mailer_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/log_subscribers/action_mailer_subscriber_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::LogSubscribers::ActionMailerSubscriber do +RSpec.describe Sentry::Rails::LogSubscribers::ActionMailerSubscriber, isolate: true do context "when logging is enabled" do let(:send_default_pii) { false } diff --git a/sentry-rails/spec/sentry/rails/log_subscribers/active_job_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/log_subscribers/active_job_subscriber_spec.rb index 58dda3d4e..94a26c48b 100644 --- a/sentry-rails/spec/sentry/rails/log_subscribers/active_job_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/log_subscribers/active_job_subscriber_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::LogSubscribers::ActiveJobSubscriber, skip: Rails.version.to_f < 5.1 do +RSpec.describe Sentry::Rails::LogSubscribers::ActiveJobSubscriber, skip: Rails.version.to_f < 5.1, isolate: true do context "when logging is enabled" do let(:send_default_pii) { false } diff --git a/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb index 3e9f108db..ebea16f3f 100644 --- a/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/log_subscribers/active_record_subscriber_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::LogSubscribers::ActiveRecordSubscriber do +RSpec.describe Sentry::Rails::LogSubscribers::ActiveRecordSubscriber, isolate: true do context "when logging is enabled" do let(:send_default_pii) { false } diff --git a/sentry-rails/spec/sentry/rails/structured_logging_spec.rb b/sentry-rails/spec/sentry/rails/structured_logging_spec.rb index 4d9ad9894..11626be32 100644 --- a/sentry-rails/spec/sentry/rails/structured_logging_spec.rb +++ b/sentry-rails/spec/sentry/rails/structured_logging_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::StructuredLogging, type: :request do +RSpec.describe Sentry::Rails::StructuredLogging, type: :request, isolate: true do context "when rails structured logging is disabled" do before do make_basic_app do |config| diff --git a/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb index b847089b6..cc88c8fae 100644 --- a/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/action_view_subscriber_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::Tracing::ActionViewSubscriber, :subscriber, type: :request do +RSpec.describe Sentry::Rails::Tracing::ActionViewSubscriber, :subscriber, type: :request, isolate: true do let(:transport) do Sentry.get_current_client.transport end diff --git a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb index 45b80d321..71496f43e 100644 --- a/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/active_record_subscriber_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::Tracing::ActiveRecordSubscriber, :subscriber do +RSpec.describe Sentry::Rails::Tracing::ActiveRecordSubscriber, :subscriber, isolate: true do let(:transport) do Sentry.get_current_client.transport end diff --git a/sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb index e77d041fa..428b6b7dd 100644 --- a/sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/active_storage_subscriber_spec.rb @@ -4,7 +4,7 @@ # Skipped on JRuby because it's very flaky there due to shelling out to use imagemagick CLI RSpec.describe Sentry::Rails::Tracing::ActiveStorageSubscriber, :subscriber, type: :request, - skip: Rails.version.to_f <= 5.2 || RUBY_PLATFORM == "java" do + skip: Rails.version.to_f <= 5.2 || RUBY_PLATFORM == "java", isolate: true do let(:transport) do Sentry.get_current_client.transport end diff --git a/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb b/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb index 0efaa33d5..3487bbfa8 100644 --- a/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing/active_support_subscriber_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::Tracing::ActiveSupportSubscriber, :subscriber, type: :request do +RSpec.describe Sentry::Rails::Tracing::ActiveSupportSubscriber, :subscriber, type: :request, isolate: true do let(:transport) do Sentry.get_current_client.transport end diff --git a/sentry-rails/spec/sentry/rails/tracing_spec.rb b/sentry-rails/spec/sentry/rails/tracing_spec.rb index 55028ea0d..cd3102916 100644 --- a/sentry-rails/spec/sentry/rails/tracing_spec.rb +++ b/sentry-rails/spec/sentry/rails/tracing_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails::Tracing, type: :request do +RSpec.describe Sentry::Rails::Tracing, type: :request, isolate: true do let(:transport) do Sentry.get_current_client.transport end diff --git a/sentry-rails/spec/sentry/rails_spec.rb b/sentry-rails/spec/sentry/rails_spec.rb index a3ca2dcae..35e9aebef 100644 --- a/sentry-rails/spec/sentry/rails_spec.rb +++ b/sentry-rails/spec/sentry/rails_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -RSpec.describe Sentry::Rails, type: :request do +RSpec.describe Sentry::Rails, type: :request, isolate: true do let(:transport) do Sentry.get_current_client.transport end diff --git a/sentry-rails/spec/support/isolation.rb b/sentry-rails/spec/support/isolation.rb new file mode 100644 index 000000000..95ddda68a --- /dev/null +++ b/sentry-rails/spec/support/isolation.rb @@ -0,0 +1,250 @@ +# frozen_string_literal: true + +require "json" + +# Runs specs in a forked child process, so the process-global state a Rails app +# boot leaves behind (load hooks, callbacks, singletons) dies with the child +# instead of leaking into later examples. +# +# it "boots its own app", isolate: true do # one process per example +# make_basic_app { |config| config.traces_sample_rate = 1.0 } +# end +# +# RSpec.describe "boots once", isolate: :group do # one process per group +# before(:all) { make_basic_app } +# end +# +# Both forms also work on a context or describe block. The whole lifecycle +# (hooks, `let`s, bodies) runs in the child, so the parent never touches the +# app. The child sends back plain data and the parent records it on the real +# examples, so formatters, retries and the exit status behave as usual. +# +# Without `fork` (JRuby, Windows) tagged specs run in-process instead. +# +# This hooks into RSpec::Core::Example#run, RSpec::Core::ExampleGroup.run and +# the private Example#start/finish/set_exception, because an `around` hook +# can't skip the example body in the parent. +module Test + module Isolation + TIMEOUT = 120 + + ChildCrashed = Class.new(StandardError) + + # Stands in for RSpec's reporter inside a group-isolated child and keeps + # what the parent needs to replay. + class Recorder + attr_reader :examples, :non_example_errors + + def initialize + @examples = [] + @non_example_errors = [] + end + + def example_finished(example) + @examples << [example.id, Isolation.summarize(example)] + end + + def notify_non_example_exception(error, context) + @non_example_errors << { error: Isolation.serialize_error(error), context: context } + end + + def fail_fast_limit_met? + false + end + + def method_missing(*) + nil + end + + def respond_to_missing?(*) + true + end + end + + module ExampleExtension + def run(example_group_instance, reporter) + return super unless metadata[:isolate] == true && Isolation.forking? + + summary = Isolation.run_in_child do + super(example_group_instance, RSpec::Core::NullReporter) + Isolation.summarize(self) + end + Isolation.replay(self, summary, reporter) + end + end + + module GroupExtension + def run(reporter = RSpec::Core::NullReporter) + return super unless Isolation.isolate_group?(self) + + result = Isolation.run_in_child do + recorder = Recorder.new + super(recorder) + { examples: recorder.examples, errors: recorder.non_example_errors } + end + + Isolation.replay_group(self, result, reporter) + end + end + + class << self + def forking? + Process.respond_to?(:fork) && !@in_child + end + + def isolate_group?(group) + return false unless forking? && group.metadata[:isolate] == :group + + parent = group.superclass + !(parent.respond_to?(:metadata) && parent.metadata && parent.metadata[:isolate] == :group) + end + + def run_in_child + reader, writer = IO.pipe + reader.binmode + writer.binmode + + pid = fork do + @in_child = true + reader.close + Coverage.start_in_child + result = yield + writer.write(Marshal.dump(result: result, coverage: Coverage.result_from_child)) + writer.close + $stdout.flush + $stderr.flush + exit!(0) + end + + writer.close + wait_for_result(reader, pid) + ensure + reader&.close + writer&.close + end + + def summarize(example) + result = example.execution_result + summary = { status: result.status, message: result.pending_message } + summary[:error] = serialize_error(example.exception) if example.exception + summary + end + + def serialize_error(error) + serialized = { class: error.class.name, message: error.message, backtrace: error.backtrace } + serialized[:all] = error.all_exceptions.map { |e| serialize_error(e) } if error.respond_to?(:all_exceptions) + serialized + end + + def replay(example, summary, reporter) + RSpec.current_example = example + example.send(:start, reporter) + + case summary[:status] + when :failed + example.send(:set_exception, rebuild_error(summary[:error])) + when :pending + RSpec::Core::Pending.mark_pending!(example, summary[:message]) + end + + example.send(:finish, reporter) + ensure + example.execution_result.ensure_timing_set(RSpec::Core::Time) + RSpec.current_example = nil + end + + def replay_group(group, result, reporter) + reporter.example_group_started(group) + + by_id = group.descendants.flat_map(&:examples).to_h { |example| [example.id, example] } + recorded = result.fetch(:examples, []).to_h + crash = result[:crash] + + by_id.each do |id, example| + summary = recorded[id] || (crash && { status: :failed, error: crash }) + replay(example, summary, reporter) if summary + end + + result.fetch(:errors, []).each do |entry| + reporter.notify_non_example_exception(rebuild_error(entry[:error]), entry[:context]) + end + + recorded.values.none? { |s| s[:status] == :failed } && !crash + ensure + reporter.example_group_finished(group) + end + + def wait_for_result(reader, pid) + if IO.select([reader], nil, nil, TIMEOUT) + data = reader.read + status = Process.wait2(pid).last + unless data.empty? + payload = Marshal.load(data) + Coverage.absorb(payload[:coverage]) + return payload[:result] + end + + crashed("isolated spec exited without reporting (#{status.inspect})") + else + Process.kill("KILL", pid) + Process.wait(pid) + crashed("isolated spec timed out after #{TIMEOUT}s") + end + end + + def crashed(message) + error = { class: ChildCrashed.name, message: message, backtrace: [] } + { status: :failed, error: error, crash: error, examples: [] } + end + + def rebuild_error(error) + return RSpec::Core::MultipleExceptionError.new(*error[:all].map { |e| rebuild_error(e) }) if error[:all] + + klass = Object.const_get(error[:class]) if error[:class] + exception = klass.is_a?(Class) && klass <= Exception ? klass.new(error[:message]) : ChildCrashed.new("#{error[:class]}: #{error[:message]}") + exception.set_backtrace(error[:backtrace] || []) + exception + rescue NameError, ArgumentError, TypeError + ChildCrashed.new("#{error[:class]}: #{error[:message]}").tap { |e| e.set_backtrace(error[:backtrace] || []) } + end + end + + module Coverage + module_function + + def start_in_child + ::Coverage.result(stop: false, clear: true) if simplecov_running? + end + + def result_from_child + return unless simplecov_running? + + root = SimpleCov.root + project_files = ::Coverage.result(stop: false, clear: true).select { |file, _| file.start_with?(root) } + JSON.parse(JSON.dump(SimpleCov::ResultAdapter.call(project_files))) + end + + def absorb(coverage) + return unless coverage + + @children = SimpleCov::Combine::ResultsCombiner.combine(@children || {}, coverage) + end + + def store_children + return unless @children && simplecov_running? + + result = SimpleCov::Result.new(@children, command_name: "#{SimpleCov.command_name} (isolated)") + SimpleCov::ResultMerger.store_result(result) + end + + def simplecov_running? + defined?(::SimpleCov) && ::SimpleCov.running + end + end + end +end + +RSpec::Core::Example.prepend(Test::Isolation::ExampleExtension) +RSpec::Core::ExampleGroup.singleton_class.prepend(Test::Isolation::GroupExtension) + +at_exit { Test::Isolation::Coverage.store_children } diff --git a/sentry-rails/spec/versioned/2.7/activejob_spec.rb b/sentry-rails/spec/versioned/2.7/activejob_spec.rb index ab905a5fb..13016ef20 100644 --- a/sentry-rails/spec/versioned/2.7/activejob_spec.rb +++ b/sentry-rails/spec/versioned/2.7/activejob_spec.rb @@ -5,7 +5,7 @@ # isolated tests need a SimpleCov name otherwise they will overwrite coverage SimpleCov.command_name "RSpecVersioned_2.7_ActiveJob" -RSpec.describe "ActiveJob integration", type: :job do +RSpec.describe "ActiveJob integration", type: :job, isolate: true do before do make_basic_app do |config| config.data_collection.queues = true