|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe Sentry::Scope do |
| 6 | + subject(:scope) { described_class.new } |
| 7 | + |
| 8 | + before do |
| 9 | + perform_basic_setup { |config| config.traces_sample_rate = 1.0 } |
| 10 | + end |
| 11 | + |
| 12 | + describe "#add_feature_flag" do |
| 13 | + it "adds flags to event contexts" do |
| 14 | + scope.add_feature_flag("a", true) |
| 15 | + scope.add_feature_flag(:b, false) |
| 16 | + |
| 17 | + event = scope.apply_to_event(Sentry::ErrorEvent.new(configuration: Sentry.configuration)) |
| 18 | + expect(event.contexts[:flags]).to eq(values: [{ flag: "a", result: true }, { flag: "b", result: false }]) |
| 19 | + end |
| 20 | + |
| 21 | + it "ignores non-boolean results" do |
| 22 | + scope.add_feature_flag("a", "yes") |
| 23 | + scope.add_feature_flag("b", nil) |
| 24 | + expect(scope.flags.values).to be_empty |
| 25 | + end |
| 26 | + |
| 27 | + it "moves a re-evaluated flag to the end with its new result" do |
| 28 | + scope.add_feature_flag("a", true) |
| 29 | + scope.add_feature_flag("b", true) |
| 30 | + scope.add_feature_flag("a", false) |
| 31 | + expect(scope.flags.values).to eq("b" => true, "a" => false) |
| 32 | + end |
| 33 | + |
| 34 | + it "evicts the oldest flag beyond the limit" do |
| 35 | + (Sentry::Scope::MAX_FLAGS + 1).times { |i| scope.add_feature_flag("f#{i}", true) } |
| 36 | + expect(scope.flags.values.size).to eq(Sentry::Scope::MAX_FLAGS) |
| 37 | + expect(scope.flags.values.first.first).to eq("f1") |
| 38 | + end |
| 39 | + |
| 40 | + it "does not add flags to transaction events" do |
| 41 | + scope.add_feature_flag("a", true) |
| 42 | + transaction = Sentry.start_transaction(name: "foo", op: "bar") |
| 43 | + event = scope.apply_to_event(Sentry::TransactionEvent.new(configuration: Sentry.configuration, transaction: transaction)) |
| 44 | + expect(event.contexts).not_to have_key(:flags) |
| 45 | + end |
| 46 | + |
| 47 | + it "does not add flags to check-in events" do |
| 48 | + scope.add_feature_flag("a", true) |
| 49 | + event = Sentry::CheckInEvent.new(configuration: Sentry.configuration, slug: "foo", status: :ok) |
| 50 | + scope.apply_to_event(event) |
| 51 | + expect(event.contexts).not_to have_key(:flags) |
| 52 | + end |
| 53 | + |
| 54 | + it "is isolated in duplicated and updated scopes and cleared by #clear" do |
| 55 | + scope.add_feature_flag("a", true) |
| 56 | + copy = scope.dup |
| 57 | + copy.add_feature_flag("b", true) |
| 58 | + copy.add_feature_flag("a", false) |
| 59 | + expect(scope.flags.values).to eq("a" => true) |
| 60 | + |
| 61 | + updated = described_class.new |
| 62 | + updated.update_from_scope(scope) |
| 63 | + updated.add_feature_flag("b", true) |
| 64 | + expect(scope.flags.values).to eq("a" => true) |
| 65 | + |
| 66 | + scope.clear |
| 67 | + expect(scope.flags.values).to be_empty |
| 68 | + end |
| 69 | + |
| 70 | + context "with an active span" do |
| 71 | + let(:span) { Sentry::Span.new(transaction: nil) } |
| 72 | + before { scope.set_span(span) } |
| 73 | + |
| 74 | + it "records flag.evaluation.* span data, capped per span" do |
| 75 | + scope.add_feature_flag("a", true) |
| 76 | + expect(span.to_h[:data]["flag.evaluation.a"]).to eq(true) |
| 77 | + |
| 78 | + 12.times { |i| scope.add_feature_flag("g#{i}", true) } |
| 79 | + keys = span.to_h[:data].keys.select { |k| k.start_with?("flag.evaluation.") } |
| 80 | + expect(keys.size).to eq(Sentry::Span::MAX_FLAGS) |
| 81 | + |
| 82 | + scope.add_feature_flag("a", false) |
| 83 | + expect(span.to_h[:data]["flag.evaluation.a"]).to eq(true) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "Sentry.add_feature_flag" do |
| 89 | + it "adds to the current scope" do |
| 90 | + Sentry.add_feature_flag("x", true) |
| 91 | + expect(Sentry.get_current_scope.flags.values).to eq("x" => true) |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments