Commit 3d256250 authored by Max Woolf's avatar Max Woolf
Browse files

style(rate_limit): fix rubocop offenses from cost-aware-incr branch

The 12 offenses split into three groups:

- Idiomatic spec cleanups (Style/RedundantException, Style/RedundantConstantBase,
  Style/Semicolon, RSpec/ReceiveMessages) — pure refactors, no behavior change.
- INCR_SCRIPT heredoc .freeze was redundant under frozen_string_literal: true
  (Style/RedundantFreeze).
- INCR_SCRIPT_SHA disables Fips/SHA1 inline: SHA1 is mandated by Redis EVALSHA,
  not a discretionary hash choice. A targeted disable is more local than adding
  the file to .rubocop_todo.yml.
- test_redis.rb Time.now -> Time.now.utc satisfies Rails/TimeZone without
  reintroducing the ActiveSupport::TimeWithZone dependency that a51a33d4 removed.

Co-Authored-By: default avatarClaude Opus 4.7 (1M context) <noreply@anthropic.com>
parent 99204787
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ module Labkit
      # - ttl_before < 0 covers TTL=-2 (key missing) and TTL=-1 (no expiry).
      #   The -1 case shouldn't arise with the atomic script, but
      #   self-healing recovers keys left without TTL by any prior bug.
      INCR_SCRIPT = <<~LUA.freeze
      INCR_SCRIPT = <<~LUA
        local cost = tonumber(ARGV[2])
        local ttl_before = redis.call('TTL', KEYS[1])
        local count
@@ -39,7 +39,8 @@ module Labkit
        end
        return {count, redis.call('TTL', KEYS[1])}
      LUA
      INCR_SCRIPT_SHA = OpenSSL::Digest::SHA1.hexdigest(INCR_SCRIPT).freeze
      # SHA1 is mandated by the Redis EVALSHA wire protocol, not a discretionary hash choice.
      INCR_SCRIPT_SHA = OpenSSL::Digest::SHA1.hexdigest(INCR_SCRIPT).freeze # rubocop:disable Fips/SHA1

      def initialize(name:, rules:, redis:, logger:)
        @name   = name
+10 −6
Original line number Diff line number Diff line
@@ -230,7 +230,7 @@ RSpec.describe Labkit::RateLimit::Evaluator do
        end

        def evalsha(*)
          raise ::Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value"
          raise Redis::CommandError, "WRONGTYPE Operation against a key holding the wrong kind of value"
        end

        def get(*)
@@ -323,7 +323,7 @@ RSpec.describe Labkit::RateLimit::Evaluator do
        end

        def pipelined
          raise RuntimeError, "connection refused"
          raise "connection refused"
        end
      end.new

@@ -342,7 +342,11 @@ RSpec.describe Labkit::RateLimit::Evaluator do

    it "resolves callable limit/period at peek time" do
      call_count = 0
      callable_rule = make_rule(name: "callable", limit: -> { call_count += 1; 5 }, period: -> { 60 })
      limit_fn = lambda do
        call_count += 1
        5
      end
      callable_rule = make_rule(name: "callable", limit: limit_fn, period: -> { 60 })

      evaluator(rules: [callable_rule]).peek(identifier)

@@ -403,7 +407,7 @@ RSpec.describe Labkit::RateLimit::Evaluator do
        end

        def evalsha(*)
          raise RuntimeError, "down"
          raise "down"
        end
      end.new

@@ -422,7 +426,7 @@ RSpec.describe Labkit::RateLimit::Evaluator do
        end

        def evalsha(*)
          raise RuntimeError, "connection refused"
          raise "connection refused"
        end
      end.new

@@ -500,7 +504,7 @@ RSpec.describe Labkit::RateLimit::Evaluator do
        end

        def evalsha(*)
          raise RuntimeError, "down"
          raise "down"
        end
      end.new

+1 −2
Original line number Diff line number Diff line
@@ -23,8 +23,7 @@ RSpec.describe Labkit::RateLimit::Limiter do

  before do
    stub_env("RAILS_ENV", "test")
    allow(raw_redis).to receive(:evalsha).and_return(["1", 55])
    allow(raw_redis).to receive(:pipelined).and_return(["1", 55])
    allow(raw_redis).to receive_messages(evalsha: ["1", 55], pipelined: ["1", 55])
  end

  describe "Scenario U: name validation" do
+2 −2
Original line number Diff line number Diff line
@@ -92,9 +92,9 @@ module TestRedis
  end

  def self.wait_until_reachable!
    deadline = Time.now + AUTOSTART_TIMEOUT
    deadline = Time.now.utc + AUTOSTART_TIMEOUT
    until reachable?
      raise "TestRedis: Redis at #{URL} not reachable within #{AUTOSTART_TIMEOUT}s of `docker compose up`" if Time.now > deadline
      raise "TestRedis: Redis at #{URL} not reachable within #{AUTOSTART_TIMEOUT}s of `docker compose up`" if Time.now.utc > deadline

      sleep 0.2
    end