Verified Commit efcb7bdd authored by Hercules Merscher's avatar Hercules Merscher
Browse files

feat: Checkpoint matcher param to assert it incr by at least N

parent 04eea2c6
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -81,7 +81,7 @@ end
#
# Parameters:
# - covered_experience_id: Required. The ID of the covered experience (e.g., 'rails_request')
RSpec::Matchers.define :checkpoint_covered_experience do |covered_experience_id|
RSpec::Matchers.define :checkpoint_covered_experience do |covered_experience_id, by: 1|
  include Labkit::RSpec::Matchers::CoveredExperience

  description { "checkpoint covered experience '#{covered_experience_id}'" }
@@ -97,12 +97,15 @@ RSpec::Matchers.define :checkpoint_covered_experience do |covered_experience_id|
    checkpoint_after = checkpoint_counter&.get(labels.merge(checkpoint: "intermediate")).to_i
    @checkpoint_change = checkpoint_after - checkpoint_before

    @checkpoint_change == 1
    # Automatic checkpoints can be created in-between depending on the context,
    # such as pushing experiences to background jobs. For this reason, we check
    # that the value increases by at least "by".
    @checkpoint_change >= by
  end

  failure_message do
    "Failed to checkpoint covered experience '#{covered_experience_id}':\n" \
      "expected checkpoint='intermediate' counter to increase by 1, but increased by #{@checkpoint_change}"
      "expected checkpoint='intermediate' counter to increase by at least #{by}, but increased by #{@checkpoint_change}"
  end

  match_when_negated do |actual|
+6 −32
Original line number Diff line number Diff line
@@ -346,7 +346,6 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
      let(:started_experience) { described_class.new(definition) }

      before do
        # Start an experience in the same process, which will store a proc in context
        started_experience.start
      end

@@ -356,33 +355,22 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
        end

        it 'resumes and automatically completes the experience' do
          # When resuming from same process, we expect 2 checkpoint increments:
          # 1. From calling the proc (push_to_context)
          # 2. From the resume itself
          expect do |block|
            experience.resume(&block)
          end.to yield_with_args(experience)
          # When resuming from same process, we expect 2 checkpoint increments:
          # 1. From calling the proc (load_from_context)
          # 2. From the resume itself
          .and checkpoint_covered_experience(:testing_sample, by: 2)
          .and complete_covered_experience(:testing_sample)

          # Verify that 2 checkpoint increments occurred
          labels = definition.to_h.slice(:covered_experience, :feature_category, :urgency)
          checkpoint_counter = Labkit::Metrics::Client.get(:gitlab_covered_experience_checkpoint_total)
          expect(checkpoint_counter.get(labels.merge(checkpoint: "intermediate"))).to be >= 2
        end

        it 'captures exceptions and marks as error' do
          # When resuming from same process, we expect 2 checkpoint increments:
          # 1. From calling the proc (push_to_context)
          # 2. From the resume itself
          expect do
            experience.resume { raise 'Something went wrong' }
          end.to raise_error(RuntimeError, 'Something went wrong')
          .and checkpoint_covered_experience(:testing_sample, by: 2)
          .and complete_covered_experience(:testing_sample, error: true)

          # Verify that 2 checkpoint increments occurred
          labels = definition.to_h.slice(:covered_experience, :feature_category, :urgency)
          checkpoint_counter = Labkit::Metrics::Client.get(:gitlab_covered_experience_checkpoint_total)
          expect(checkpoint_counter.get(labels.merge(checkpoint: "intermediate"))).to be >= 2
        end

        it 'logs resume and end events' do
@@ -442,21 +430,7 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
        subject(:resume) { experience.resume }

        it { is_expected.to be(experience) }

        it 'increments checkpoint counter twice (proc call + resume)' do
          # When resuming from same process, we expect 2 checkpoint increments:
          # 1. From calling the proc (push_to_context)
          # 2. From the resume itself
          labels = definition.to_h.slice(:covered_experience, :feature_category, :urgency)
          checkpoint_counter = Labkit::Metrics::Client.get(:gitlab_covered_experience_checkpoint_total)

          before_count = checkpoint_counter.get(labels.merge(checkpoint: "intermediate")).to_i
          resume
          after_count = checkpoint_counter.get(labels.merge(checkpoint: "intermediate")).to_i

          expect(after_count - before_count).to eq(2)
        end

        it { expect { resume }.to checkpoint_covered_experience(:testing_sample, by: 2) }
        it { expect { resume }.not_to complete_covered_experience(:testing_sample) }

        it 'logs resume event after calling the proc' do