From fa7440ebfa22bc8890a4f4103d5aaaad6f845736 Mon Sep 17 00:00:00 2001 From: Todd Sedano Date: Tue, 30 Apr 2024 13:07:58 -0700 Subject: [PATCH] Request store uses Fiber so our test should too --- spec/paper_trail/request_spec.rb | 38 +++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/spec/paper_trail/request_spec.rb b/spec/paper_trail/request_spec.rb index 5e2dae54..44c42ccf 100644 --- a/spec/paper_trail/request_spec.rb +++ b/spec/paper_trail/request_spec.rb @@ -117,18 +117,34 @@ module PaperTrail expect(described_class.enabled_for_model?(Widget)).to eq true end - it "sets options only for the current thread" do - described_class.whodunnit = "some_whodunnit" - described_class.enabled_for_model(Widget, true) - - described_class.with(whodunnit: "foo", enabled_for_Widget: false) do - expect(described_class.whodunnit).to eq("foo") - expect(described_class.enabled_for_model?(Widget)).to eq false - Thread.new { expect(described_class.whodunnit).to be_nil }.join - Thread.new { expect(described_class.enabled_for_model?(Widget)).to eq true }.join + if RequestStore.scope == Fiber + it "sets options only for the current Fiber" do + described_class.whodunnit = "some_whodunnit" + described_class.enabled_for_model(Widget, true) + + described_class.with(whodunnit: "foo", enabled_for_Widget: false) do + expect(described_class.whodunnit).to eq("foo") + expect(described_class.enabled_for_model?(Widget)).to eq false + Fiber.new(storage: nil) { expect(described_class.whodunnit).to be_nil }.resume + Fiber.new(storage: nil) { expect(described_class.enabled_for_model?(Widget)).to eq true }.resume + end + expect(described_class.whodunnit).to eq "some_whodunnit" + expect(described_class.enabled_for_model?(Widget)).to eq true + end + else + it "sets options only for the current thread" do + described_class.whodunnit = "some_whodunnit" + described_class.enabled_for_model(Widget, true) + + described_class.with(whodunnit: "foo", enabled_for_Widget: false) do + expect(described_class.whodunnit).to eq("foo") + expect(described_class.enabled_for_model?(Widget)).to eq false + Thread.new { expect(described_class.whodunnit).to be_nil }.join + Thread.new { expect(described_class.enabled_for_model?(Widget)).to eq true }.join + end + expect(described_class.whodunnit).to eq "some_whodunnit" + expect(described_class.enabled_for_model?(Widget)).to eq true end - expect(described_class.whodunnit).to eq "some_whodunnit" - expect(described_class.enabled_for_model?(Widget)).to eq true end end