Verified Commit 694cbc6d authored by Hercules Merscher's avatar Hercules Merscher 🌴
Browse files

fix: Renaming fields and checking rspec matchers

parent 846969df
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -121,13 +121,13 @@ module Labkit
        URGENCY_THRESHOLDS_IN_SECONDS[@definition.urgency.to_sym]
      end

      def elapsed
      def elapsed_time
        last_time = @end_time || @checkpoint_time || @start_time
        last_time - @start_time
      end

      def success?
        elapsed <= urgency_threshold
        elapsed_time <= urgency_threshold
      end

      def checkpoint_counter
@@ -168,8 +168,8 @@ module Labkit
          start_time: @start_time,
          checkpoint_time: @checkpoint_time,
          end_time: @end_time,
          elapsed: elapsed,
          urgency_threshold_in_seconds: urgency_threshold
          elapsed_time_s: elapsed_time,
          urgency_threshold_s: urgency_threshold
        ).compact

        if has_error?
+17 −6
Original line number Diff line number Diff line
@@ -46,6 +46,9 @@ module Labkit

          expected_attrs = {
            checkpoint: event_type,
            start_time: be_a(String),
            elapsed_time_s: be_a(Numeric),
            urgency_threshold_s: be_a(Numeric),
            **attributes(covered_experience_id),
            **extra_args
          }
@@ -58,13 +61,17 @@ module Labkit
            begin
              log_entry = JSON.parse(line, symbolize_names: true)
              expected_attrs.all? do |key, value|
                actual_value = log_entry[key]

                case value
                when Class
                  log_entry[key].is_a?(value)
                  actual_value.is_a?(value)
                when Regexp
                  log_entry[key]&.match?(value)
                  actual_value&.match?(value)
                when ::RSpec::Matchers::BuiltIn::BaseMatcher
                  value.matches?(actual_value)
                else
                  log_entry[key] == value
                  actual_value == value
                end
              end
            rescue JSON::ParserError
@@ -141,7 +148,7 @@ 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 && expect_log_for(covered_experience_id, "intermediate")
    @checkpoint_change == 1 && expect_log_for(covered_experience_id, "intermediate", checkpoint_time: be_a(String))
  end

  failure_message do
@@ -205,9 +212,13 @@ RSpec::Matchers.define :complete_covered_experience do |covered_experience_id, e
    @total_change = total_after - total_before
    @apdex_change = apdex_after - apdex_before

    expected_log_attrs = error ? { error: true } : {}
    expected_log_attrs = { end_time: be_a(String) }
    expected_log_attrs[:error] = true if error

    @checkpoint_change == 1 && @total_change == 1 && @apdex_change == (error ? 0 : 1) && expect_log_for(covered_experience_id, "end", expected_log_attrs)
    @checkpoint_change == 1 &&
      @total_change == 1 &&
      @apdex_change == (error ? 0 : 1) &&
      expect_log_for(covered_experience_id, "end", expected_log_attrs)
  end

  failure_message do