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

fix: Experience methods should return itself when state is invalid

parent 7f8ed740
Loading
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -92,7 +92,7 @@ module Labkit
      # @param extra [Hash] Additional data to include in the log event
      # @return [self]
      def checkpoint(**extra)
        return unless ensure_started!
        return self unless ensure_started!

        @checkpoint_time = Time.now.utc
        checkpoint_counter.increment(checkpoint: "intermediate", **base_labels)
@@ -106,7 +106,7 @@ module Labkit
      # @yield [self] When a block is provided, the experience will be completed automatically.
      # @param extra [Hash] Additional data to include in the log
      def resume(**extra, &)
        return unless ensure_started!
        return self unless ensure_started!

        checkpoint(checkpoint_action: 'resume', **extra)

@@ -120,8 +120,7 @@ module Labkit
      # @param extra [Hash] Additional data to include in the log event
      # @return [self]
      def complete(**extra)
        return unless ensure_started!
        return unless ensure_incomplete!
        return self unless ensure_started! && ensure_incomplete!

        begin
          @end_time = Time.now.utc
+7 −0
Original line number Diff line number Diff line
@@ -275,6 +275,10 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
        expect { experience.resume }.not_to raise_error
        expect { experience.resume }.not_to resume_covered_experience(:testing_sample)
      end

      it 'returns itself' do
        expect(experience.resume).to be(experience)
      end
    end
  end

@@ -305,6 +309,7 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
    context 'when not started' do
      it { expect { checkpoint }.not_to raise_error }
      it { expect { checkpoint }.not_to checkpoint_covered_experience(:testing_sample) }
      it { is_expected.to be(experience) }
    end
  end

@@ -356,6 +361,7 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do
    context 'when not started' do
      it { expect { complete }.not_to raise_error }
      it { expect { complete }.not_to complete_covered_experience(:testing_sample) }
      it { is_expected.to be(experience) }
    end

    context 'when already completed' do
@@ -365,6 +371,7 @@ RSpec.describe Labkit::CoveredExperience::Experience, :with_metrics_config do

      it { expect { complete }.not_to raise_error }
      it { expect { complete }.not_to complete_covered_experience(:testing_sample) }
      it { is_expected.to be(experience) }
    end
  end