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..a4348b49f --- /dev/null +++ b/sentry-rails/spec/support/isolation.rb @@ -0,0 +1,199 @@ +# frozen_string_literal: true + +module Test + module Isolation + TIMEOUT = 120 + + ChildCrashed = Class.new(StandardError) + + class Recorder + attr_reader :examples, :errors + + def initialize + @examples = {} + @errors = [] + end + + def example_finished(example) + result = example.execution_result + summary = { status: result.status, message: result.pending_message } + summary[:error] = Isolation.pack(example.exception) if example.exception + @examples[example.id] = summary + end + + def notify_non_example_exception(error, context) + @errors << { error: Isolation.pack(error), context: context } + end + + def fail_fast_limit_met? + false + end + + def example_group_started(_group); end + def example_group_finished(_group); end + def example_started(_example); end + def example_passed(_example); end + def example_failed(_example); end + def example_pending(_example); end + end + + module GroupExtension + def run(reporter = RSpec::Core::NullReporter) + return super unless Isolation.isolated_root?(self) + return if RSpec.world.wants_to_quit + + Isolation.run_group(self, reporter) { |recorder| super(recorder) } + end + end + + class << self + def isolated_root?(group) + return false unless Process.respond_to?(:fork) && group.metadata[:isolate] + + parent = group.superclass + !(parent.respond_to?(:metadata) && parent.metadata&.dig(:isolate)) + end + + def run_group(group, reporter, &run) + examples = group.metadata[:isolate] == :group ? [nil] : ordered_examples(group) + + reporter.example_group_started(group) + results = examples.map do |only| + next if RSpec.world.wants_to_quit + + result = run_in_child do + restrict_to(group, only) if only + recorder = Recorder.new + route_hook_errors_to(recorder) + run.call(recorder) + { examples: recorder.examples, errors: recorder.errors } + end + succeeded = replay(group, only, result, reporter) + RSpec.world.wants_to_quit = true if !succeeded && reporter.fail_fast_limit_met? + succeeded + end + results.all? + ensure + reporter.example_group_finished(group) + end + + def pack(error) + dump = + begin + Marshal.dump(error) + rescue TypeError + nil + end + { dump: dump, class: error.class.name, message: error.message, backtrace: error.backtrace } + end + + def unpack(packed) + Marshal.load(packed[:dump]) + rescue TypeError, ArgumentError, NameError + ChildCrashed.new("#{packed[:class]}: #{packed[:message]}").tap { |e| e.set_backtrace(packed[:backtrace] || []) } + end + + private + + def ordered_examples(group) + group.ordering_strategy.order(group.filtered_examples) + + group.ordering_strategy.order(group.children).flat_map { |child| ordered_examples(child) } + end + + def route_hook_errors_to(recorder) + RSpec.configuration.reporter.define_singleton_method(:notify_non_example_exception) do |error, context| + recorder.notify_non_example_exception(error, context) + end + end + + def restrict_to(group, example) + group.descendants.each do |g| + RSpec.world.filtered_examples[g] = g.filtered_examples.select { |e| e.equal?(example) } + g.reset_memoized + end + end + + def replay(group, only, result, reporter) + expected = only ? [only] : group.descendant_filtered_examples + recorded = result[:examples] || {} + + expected.each do |example| + summary = recorded[example.id] || (result[:crash] && { status: :failed, error: result[:crash] }) + replay_example(example, summary, reporter) if summary + end + + (result[:errors] || []).each do |entry| + reporter.notify_non_example_exception(unpack(entry[:error]), entry[:context]) + end + + !result[:crash] && recorded.values.none? { |s| s[:status] == :failed } + end + + def replay_example(example, summary, reporter) + RSpec.current_example = example + + case summary[:status] + when :failed + example.fail_with_exception(reporter, unpack(summary[:error])) + when :pending + example.skip_with_exception(reporter, RSpec::Core::Pending::SkipDeclaredInExample.new(summary[:message])) + else + example.send(:start, reporter) + example.send(:finish, reporter) + end + ensure + example.execution_result.ensure_timing_set(RSpec::Core::Time) + RSpec.current_example = nil + end + + def run_in_child + reader, writer = IO.pipe + reader.binmode + writer.binmode + + pid = fork do + reader.close + writer.write(Marshal.dump(in_child { yield })) + writer.close + $stdout.flush + $stderr.flush + exit!(0) + end + + writer.close + wait_for_result(reader, pid) + ensure + reader&.close + writer&.close + end + + def wait_for_result(reader, pid) + if IO.select([reader], nil, nil, TIMEOUT) + data = reader.read + status = Process.wait2(pid).last + return from_child(Marshal.load(data)) unless data.empty? + + 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 in_child + yield + end + + def from_child(payload) + payload + end + + def crashed(message) + { crash: pack(ChildCrashed.new(message).tap { |e| e.set_backtrace([]) }) } + end + end + end +end + +RSpec::Core::ExampleGroup.singleton_class.prepend(Test::Isolation::GroupExtension) diff --git a/sentry-rails/spec/support/isolation/coverage.rb b/sentry-rails/spec/support/isolation/coverage.rb new file mode 100644 index 000000000..9dc03b841 --- /dev/null +++ b/sentry-rails/spec/support/isolation/coverage.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require "json" +require_relative "../isolation" + +module Test + module Isolation + module Coverage + private + + def in_child + ::Coverage.result(stop: false, clear: true) + result = super + { result: result, coverage: Coverage.measured_in_child } + end + + def from_child(payload) + Coverage.absorb(payload[:coverage]) + super(payload[:result]) + end + + class << self + def measured_in_child + 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) + @children = SimpleCov::Combine::ResultsCombiner.combine(@children || {}, coverage) + end + + def store + return unless @children + + result = SimpleCov::Result.new(@children, command_name: "#{SimpleCov.command_name} (isolated)") + SimpleCov::ResultMerger.store_result(result) + end + end + end + end +end + +if defined?(SimpleCov) && SimpleCov.running + Test::Isolation.singleton_class.prepend(Test::Isolation::Coverage) + at_exit { Test::Isolation::Coverage.store } +end 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